-
Notifications
You must be signed in to change notification settings - Fork 0
/
track.go
61 lines (53 loc) · 1.16 KB
/
track.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
package main
import "time"
type TrackStep struct {
Time time.Time
Event string
Place string
Provider string
Important bool
Finish bool
}
type Track struct {
Id string
Steps []TrackStep
}
func (track *Track) IsFinished() bool {
for _, step := range track.Steps {
if step.Finish { return true }
}
return false
}
// GetLastStepsTime returns last Time per provider
func (track *Track) GetLastStepsTime() map[string]time.Time {
lasts := make(map[string]time.Time)
for _, step := range track.Steps {
last, found := lasts[step.Provider]
if !found {
lasts[step.Provider] = step.Time
continue
}
if last.Before(step.Time) { lasts[step.Provider] = step.Time }
}
return lasts
}
// AddSteps returns added (new) steps
func (track *Track) AddSteps(steps []TrackStep) []TrackStep {
lasts := track.GetLastStepsTime()
addedSteps := make([]TrackStep, 0)
for _, step := range steps {
last, _ := lasts[step.Provider]
if step.Time.After(last) {
track.Steps = append(track.Steps, step)
addedSteps = append(addedSteps, step)
}
}
return addedSteps
}
func NewTrack(id string) Track {
track := Track{
Id: id,
Steps: make([]TrackStep, 0),
}
return track
}