-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
45 lines (37 loc) · 977 Bytes
/
app.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
package gomet
import (
"time"
)
// application contains current state of all program components
// it is a map of groups, each group is map of workers, each worker is in particular state that starts at some time point
type app map[string]map[int64]worker
func newApp() app {
return make(app)
}
// update app state with new event,
// return previous sate and duration for it
func (a app) update(ev Event) (string, time.Time, time.Duration) {
// get group
g, ok := a[ev.Group]
if !ok {
g = make(map[int64]worker)
a[ev.Group] = g
}
w, ok := g[ev.Worker]
g[ev.Worker] = worker{ev.State, ev.Time} // set new state
if !ok {
// this is worker's start, no further processing is needed
return "", w.Start, 0
}
if ev.State == "" {
// this is wroker's end, remove it form app sate
delete(g, ev.Worker)
}
// calculate duration for event and
dur := ev.Time.Sub(w.Start)
return w.State, w.Start, dur
}
type worker struct {
State string
Start time.Time
}