forked from porridge/calendar-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspan.go
More file actions
55 lines (48 loc) · 1.48 KB
/
Copy pathspan.go
File metadata and controls
55 lines (48 loc) · 1.48 KB
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
package core
import (
"time"
"cloud.google.com/go/civil"
"google.golang.org/api/calendar/v3"
)
type span struct {
start time.Time
events map[*calendar.Event]CategoryName
categories []*Category
}
func newSpan(categories []*Category) *span {
return &span{categories: categories, events: make(map[*calendar.Event]CategoryName)}
}
func (s *span) checkpoint(dayTotals map[civil.Date]time.Duration, categoryTotals map[CategoryName]time.Duration, dayCategoryTotals map[civil.Date]map[CategoryName]time.Duration, end time.Time) {
timeSpent := end.Sub(s.start)
eventCount := len(s.events)
var timePerEvent int64
if eventCount > 0 {
timePerEvent = int64(timeSpent) / int64(eventCount)
dayTotals[civil.DateOf(s.start)] += timeSpent
}
for _, categoryName := range s.events {
categoryTotals[categoryName] += time.Duration(timePerEvent)
if eventCount > 0 {
day := civil.DateOf(s.start)
if dayCategoryTotals[day] == nil {
dayCategoryTotals[day] = make(map[CategoryName]time.Duration)
}
dayCategoryTotals[day][categoryName] += time.Duration(timePerEvent)
}
}
s.start = end
}
func (s *span) eventEnd(event *calendar.Event) {
delete(s.events, event)
}
// eventStart returns false if the event was not recognized to belong to a category.
func (s *span) eventStart(event *calendar.Event) bool {
for _, aCategory := range s.categories {
if aCategory.recognizes(event) {
s.events[event] = aCategory.Name
return true
}
}
s.events[event] = Uncategorized
return false
}