-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.go
72 lines (56 loc) · 1.39 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
package events
import (
"time"
cache "github.com/patrickmn/go-cache"
geonet "github.com/penguinpowernz/go-geonet"
)
// NewProcessor will return a new processor with a cache already instantiated
func NewProcessor() *Processor {
quakes := cache.New(time.Hour*24, time.Hour*24)
return &Processor{quakes}
}
// Processor will process quakes given to it
type Processor struct {
cache *cache.Cache
}
// Process will process new quakes and return events
func (pr *Processor) Process(qks []geonet.Quake) Events {
evts := Events{}
for _, qk := range qks {
qki, found := pr.cache.Get(qk.PublicID)
if !found {
pr.cache.SetDefault(qk.PublicID, qk)
evts.Add("new", qk)
continue
}
xqk := qki.(geonet.Quake)
fields := CompareQuakes(xqk, qk)
if len(fields) == 0 {
continue
}
pr.cache.SetDefault(qk.PublicID, qk)
evts.Add("updated", qk, fields)
}
return evts
}
// CompareQuakes will compare 2 quakes and return a list of fields
// that are different between them
func CompareQuakes(a, b geonet.Quake) []string {
fields := []string{}
if a.Magnitude != b.Magnitude {
fields = append(fields, "magnitude")
}
if a.Depth != b.Depth {
fields = append(fields, "depth")
}
if a.Quality != b.Quality {
fields = append(fields, "quality")
}
if a.Locality != b.Locality {
fields = append(fields, "locality")
}
if a.MMI != b.MMI {
fields = append(fields, "mmi")
}
return fields
}