-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.go
77 lines (66 loc) · 2.17 KB
/
processor.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
package pipetimer
import (
"errors"
"sort"
"github.com/vitraum/golang-pipedrive"
)
// FetchDeals uses the pipedrive API to fetch all deals from the pipeline, using an optional filter
// filterID is either 0 or a valid filterID
func (t *Pipetimer) FetchDeals(filterName string, filterID int) (pipedrive.Deals, error) {
if filterName != "" && filterID == 0 {
var err error
filterID, err = t.API.GetFilterIDByName(filterName)
if err != nil {
return nil, err
}
} else if filterName != "" && filterID != 0 {
return nil, errors.New("Don't provide filter name and id simultainously")
}
res, err := t.API.FetchDealsFromPipeline(t.id, filterID)
return res, err
}
// FilterPipelineChanges fetches the updates for the given deals and processes them
func (t *Pipetimer) FilterPipelineChanges(deals []pipedrive.Deal) (pipedrive.PipelineChangeResults, error) {
changes, err := t.API.FetchPipelineChanges(deals, t.Stages)
if err != nil {
return nil, err
}
t.processPipelineChanges(changes)
t.zeroShortPhases(changes, 30)
return changes, err
}
// zeroShortPhases will add the duration of phases < cutoff to the next phase
func (t *Pipetimer) zeroShortPhases(changes pipedrive.PipelineChangeResults, cutoff float64) {
for _, dealFlow := range changes {
if len(dealFlow.Updates) <= 1 {
continue
}
for i := range dealFlow.Updates[0 : len(dealFlow.Updates)-1] {
next := &dealFlow.Updates[i+1]
cur := &dealFlow.Updates[i]
if cur.Duration < cutoff {
next.Duration += cur.Duration
cur.Duration = 0
cur.PiT = next.PiT
}
}
}
}
func (t *Pipetimer) processPipelineChanges(changes pipedrive.PipelineChangeResults) {
for _, dealFlow := range changes {
if len(dealFlow.Updates) > 0 {
sort.Sort(dealFlow.Updates)
if len(dealFlow.Updates) > 1 {
for i, update := range dealFlow.Updates[1:] {
last := dealFlow.Updates[i]
dealFlow.Updates[i].Duration += update.PiT.Time.Sub(last.PiT.Time).Seconds()
dealFlow.Updates[i].PhaseTouchdowns++
}
}
j := len(dealFlow.Updates) - 1
last := dealFlow.Updates[j]
dealFlow.Updates[j].Duration += dealFlow.DecisionTime().Sub(last.PiT.Time).Seconds()
dealFlow.Updates[j].PhaseTouchdowns++
}
}
}