-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.go
170 lines (140 loc) · 3.51 KB
/
stats.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package work
import (
"context"
"database/sql"
"time"
)
type DayStats struct {
Date time.Time
Total time.Duration
Worked time.Duration
Expected time.Duration
}
type Stats struct {
Total time.Duration
Worked time.Duration
Expected time.Duration
DayStats []DayStats
From time.Time
To time.Time
}
func statsWork(db *sql.DB, from, to time.Time) (Stats, error) {
expected := calculateExpected(from, to)
stats := Stats{Expected: expected, From: from, To: to}
total, err := getTotalDuration(db, from, to)
if err != nil {
return Stats{}, err
}
stats.Worked = total
stats.Total = total - expected
dayStats, err := getDayStats(db, from, to)
if err != nil {
return Stats{}, nil
}
stats.DayStats = dayStats
return stats, nil
}
func calculateExpected(from time.Time, to time.Time) time.Duration {
fromDate := time.Date(from.Year(), from.Month(), from.Day(), 0, 0, 0, 0, time.UTC)
toDate := time.Date(to.Year(), to.Month(), to.Day(), 0, 0, 0, 0, time.UTC)
result := 0 * time.Hour
for d := fromDate; d.Unix() <= toDate.Unix(); d = d.AddDate(0, 0, 1) {
if d.Weekday() != time.Sunday && d.Weekday() != time.Saturday {
result += 8 * time.Hour
}
}
return result
}
func getTotalDuration(db *sql.DB, from, to time.Time) (time.Duration, error) {
var seconds sql.NullInt64
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := db.QueryRowContext(
ctx,
`SELECT
SUM(ended_at - started_at)
FROM work_log
WHERE
started_at >= $1
AND ended_at <= $2
AND started_at IS NOT NULL
AND ended_at IS NOT NULL`,
from.Unix(),
to.Unix(),
).Scan(&seconds)
if err == sql.ErrNoRows {
return 0, nil
}
if err != nil {
return 0, err
}
if seconds.Valid {
return time.Duration(seconds.Int64) * time.Second, nil
}
return 0, nil
}
func getDayStats(db *sql.DB, from, to time.Time) ([]DayStats, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
rows, err := db.QueryContext(
ctx,
`SELECT
STRFTIME('%Y-%m-%d', started_at, 'unixepoch') AS day,
SUM(ended_at - started_at) AS duration
FROM work_log
WHERE
started_at >= $1
AND ended_at <= $2
AND started_at IS NOT NULL
AND ended_at IS NOT NULL
GROUP BY 1`,
from.Unix(),
to.Unix(),
)
if err != nil {
return nil, err
}
var stats []DayStats
for rows.Next() {
var dayStr string
var seconds int64
err := rows.Scan(&dayStr, &seconds)
if err != nil {
return nil, err
}
date, err := time.ParseInLocation("2006-01-02", dayStr, time.Local)
if err != nil {
return nil, err
}
item := DayStats{}
item.Date = date
item.Expected = 8 * time.Hour
item.Worked = time.Duration(seconds) * time.Second
item.Total = item.Worked - item.Expected
stats = append(stats, item)
}
return stats, nil
}
func getDateRangeForLog(now time.Time, weekOffset int) (time.Time, time.Time) {
normWeekday := int(now.Weekday())
if normWeekday == 0 {
normWeekday = 7
}
var fromOffset, toOffset int
if weekOffset == 0 {
fromOffset = normWeekday - 1
if normWeekday >= 1 && normWeekday <= 5 {
toOffset = 0
} else if normWeekday == 6 {
toOffset = 1
} else if normWeekday == 7 {
toOffset = 2
}
} else {
fromOffset = 7 * weekOffset + normWeekday - 1
toOffset = (7 * (weekOffset - 1)) + normWeekday + 2
}
from := time.Date(now.Year(), now.Month(), now.Day()-fromOffset, 0, 0, 0, 0, now.Location())
to := time.Date(now.Year(), now.Month(), now.Day()-toOffset, 23, 59, 59, 0, now.Location())
return from, to
}