-
Notifications
You must be signed in to change notification settings - Fork 0
/
stop.go
89 lines (77 loc) · 1.88 KB
/
stop.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
package work
import (
"context"
"database/sql"
"time"
)
type StopStats struct {
SessionWorked time.Duration
DayWorked time.Duration
DayTotal time.Duration
}
func stopWork(db *sql.DB, when time.Time) (StopStats, error) {
unfinished, err := checkForUnfinished(db)
if err != nil {
return StopStats{}, err
}
if !unfinished {
return StopStats{}, ErrFinishedWork
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err = db.ExecContext(
ctx,
`UPDATE work_log SET ended_at = $1 WHERE ended_at IS NULL`,
when.Unix(),
)
if err != nil {
return StopStats{}, err
}
return getStopStats(db, when)
}
func getStopStats(db *sql.DB, when time.Time) (StopStats, error) {
ctx1, cancel1 := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel1()
var lastSessionSeconds int64
err := db.QueryRowContext(
ctx1,
`SELECT
ended_at - started_at
FROM work_log
WHERE
started_at IS NOT NULL
AND ended_at IS NOT NULL
ORDER BY id DESC
LIMIT 1`,
).Scan(&lastSessionSeconds)
if err != nil {
return StopStats{}, err
}
ctx2, cancel2 := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel2()
from := time.Date(when.Year(), when.Month(), when.Day(), 0, 0, 0, 0, time.Local)
to := time.Date(when.Year(), when.Month(), when.Day(), 23, 59, 59, 0, time.Local)
var daySeconds int64
err = db.QueryRowContext(
ctx2,
`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(&daySeconds)
if err != nil {
return StopStats{}, err
}
return StopStats{
SessionWorked: time.Duration(lastSessionSeconds) * time.Second,
DayWorked: time.Duration(daySeconds) * time.Second,
DayTotal: (time.Duration(daySeconds) * time.Second) - (8 * time.Hour),
}, nil
}