-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwork.go
131 lines (100 loc) · 2.3 KB
/
work.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
package work
import (
"database/sql"
"fmt"
"time"
)
const dateLayout = "2006-01-02 15:04:05"
const dateShortLayout = "2006-01-02"
type App struct {
db *sql.DB
}
func NewApp(dbPath string) App {
db, err := connectToDB(dbPath)
if err != nil {
panic(err)
}
return App{db: db}
}
func (a App) Close() error {
return a.db.Close()
}
func (a App) Start() {
now := time.Now()
err := startWork(a.db, now)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Work started at %s\n", now.Format(dateLayout))
status, err := workStatus(a.db, now)
if err != nil {
fmt.Println(err)
return
}
if status.Remaining > 0 {
fmt.Printf("\tExpected finish at: %s\n", now.Add(status.Remaining).Format(dateLayout))
}
}
func (a App) Stop() {
now := time.Now()
result, err := stopWork(a.db, now)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Work stopped at %s (after %s)\n", now.Format(dateLayout), result.SessionWorked)
fmt.Printf(
"\nToday:\n\tWorked: %s\n\tTotal: %s%s\n",
result.DayWorked,
getSignForPositiveDuration(result.DayTotal),
result.DayTotal,
)
}
func (a App) Log(weekOffset int) {
now := time.Now()
from, to := getDateRangeForLog(now, weekOffset)
stats, err := statsWork(a.db, from, to)
if err != nil {
fmt.Println(err)
return
}
total := stats.Total
fmt.Printf("%s - %s\n\n", from.Format(dateShortLayout), to.Format(dateShortLayout))
fmt.Printf(
"Expected: %s\nWorked: %s\nTotal: %s%s\n",
stats.Expected,
stats.Worked,
getSignForPositiveDuration(total),
total,
)
if len(stats.DayStats) > 0 {
fmt.Print("\nBy day:\n")
for _, day := range stats.DayStats {
dayTotal := day.Total
fmt.Printf("\t%s: %s%s\n", day.Date.Format(dateShortLayout), getSignForPositiveDuration(dayTotal), dayTotal)
}
}
}
func (a App) Status() {
now := time.Now()
status, err := workStatus(a.db, now)
if err != nil {
fmt.Println(err)
return
}
if status.IsActive {
fmt.Println("Work: IN PROGRESS")
} else {
fmt.Println("Work: STOPPED")
}
fmt.Printf("\nToday:\n\tWorked: %s\n", status.Worked)
if status.Remaining > 0 {
fmt.Printf("\tRemaining: %s\n", status.Remaining)
if status.IsActive {
fmt.Printf("\tExpected finish at: %s\n", now.Add(status.Remaining).Format(dateLayout))
}
} else {
fmt.Printf("\tOvertime: %s\n", status.Remaining.Abs())
}
}