-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
95 lines (85 loc) · 2.07 KB
/
writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package pipetimer
import (
"encoding/csv"
"fmt"
"io"
"time"
"github.com/vitraum/golang-pipedrive"
)
// PipeWriter is used to write deals in CSV format
type PipeWriter struct {
csv *csv.Writer
stages pipedrive.Stages
}
// DataProvider represents the data to be writtten in CSV format
type DataProvider interface {
ID() string
Age() time.Duration
Added() time.Time
Status() string
Source() string
LastStage() string
DealUpdates() pipedrive.DealFlowUpdates
Value() float64
DecisionTime() time.Time
}
// NewPipeWriter constructs a new PipeWriter object
func NewPipeWriter(out io.Writer, stages pipedrive.Stages) *PipeWriter {
s := PipeWriter{
csv: csv.NewWriter(out),
stages: stages,
}
return &s
}
// WriteHeader generates the CSV header
func (w *PipeWriter) WriteHeader() error {
columnNames := []string{
"Lead ID",
"Status",
"Quelle",
"Letzte Phase",
"Endscheidungsdatum",
"Lead Alter",
"Wert",
}
for _, stage := range w.stages {
columnNames = append(columnNames, stage.Name)
columnNames = append(columnNames, stage.Name+" Dauer")
columnNames = append(columnNames, stage.Name+" Ersteintritt")
}
return w.csv.Write(columnNames)
}
// Write a new line in CSV format
func (w *PipeWriter) Write(d DataProvider) error {
data := []string{
d.ID(),
d.Status(),
d.Source(),
d.LastStage(),
d.DecisionTime().Local().Format("2006-01-02 15:04:05"),
fmt.Sprintf("%v", int(d.Age().Seconds()/86400)),
fmt.Sprintf("%0.2f", d.Value()),
}
for _, stage := range w.stages {
pit := ""
duration := ""
firstContact := ""
for _, update := range d.DealUpdates() {
if update.Phase == stage.Name && update.PiT.Time.Before(d.DecisionTime()) {
pit = update.PiT.String()
duration = fmt.Sprintf("%d", int(update.Duration/86400))
if firstContact == "" {
fCsecs := update.PiT.Sub(d.Added()).Seconds()
firstContact = fmt.Sprintf("%d", int(fCsecs/86400))
}
}
}
data = append(data, pit, duration, firstContact)
}
return w.csv.Write(data)
}
// Flush the csv file
func (w *PipeWriter) Flush() error {
w.csv.Flush()
return nil
}