-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
65 lines (51 loc) · 1.44 KB
/
session.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
package main
import (
"time"
)
// Session represents a SNOO session
type Session struct {
AsleepDuration int
EndTime time.Time
ID string
SoothingDuration int
StartTime time.Time
levels []Level
}
// NewSession initializes a new `Session`
func NewSession(sessionID string, levels []Level) Session {
// https://stackoverflow.com/questions/25065055/what-is-the-maximum-time-time-in-go
MaxTime := time.Unix(1<<63-62135596801, 999999999)
var asleepDuration, soothingDuration int
minStartTime := MaxTime
maxEndTime := time.Time{} // initialize to min time
for _, level := range levels {
if level.StartTime.Before(minStartTime) {
minStartTime = level.StartTime.Time
}
if level.EndTime().After(maxEndTime) {
maxEndTime = level.EndTime()
}
if level.Type == "asleep" {
asleepDuration += level.StateDuration
}
if level.Type == "soothing" {
soothingDuration += level.StateDuration
}
}
return Session{
AsleepDuration: asleepDuration,
EndTime: maxEndTime,
ID: sessionID,
SoothingDuration: soothingDuration,
StartTime: minStartTime,
levels: levels,
}
}
// EndTime calculates the end time of a level
func (l *Level) EndTime() time.Time {
return l.StartTime.Add(time.Second * time.Duration(l.StateDuration))
}
// TotalDuration calculates the total session duration
func (s *Session) TotalDuration() int {
return s.SoothingDuration + s.AsleepDuration
}