-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtotals.go
212 lines (177 loc) · 5.17 KB
/
totals.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"bufio"
// "fmt"
"os"
"sort"
"strings"
"time"
)
type totals struct {
sowIndex int
notesPath string
current string
days []map[string]int
}
func newTotals(notesPath string, days int) *totals {
return &totals{
notesPath: notesPath,
days: make([]map[string]int, days),
}
}
func (t *totals) weekTotalMinutes() int {
total := 0
for i := 0; i <= t.sowIndex; i++ {
for _, v := range t.days[i] {
total += v
}
}
return total
}
func (t *totals) nDayTotalMinutes(n int) int {
total := 0
for i := 0; i < n; i++ {
for _, v := range t.days[i] {
total += v
}
}
return total
}
func (t *totals) nDayThemeTotalMinutes(n int) map[string]float64 {
totals := make(map[string]float64)
for i := 0; i < n; i++ {
for k, _ := range t.days[i] {
theme := strings.Split(k, ", ")[0]
totals[theme] += float64(t.days[i][k])
}
}
return totals
}
func (t *totals) nDayThemePercent(n int, theme string) float64 {
return (t.nDayThemeTotalMinutes(n)[theme] / float64(t.nDayTotalMinutes(n))) * 100
}
func (t *totals) nDayProductiveTotalMinutes(n int) int {
total := 0
for i := 0; i < n; i++ {
for k, _ := range t.days[i] {
theme := strings.Split(k, ", ")[0]
if isProductiveTheme(theme) {
total += t.days[i][k]
}
}
}
return total
}
func (t *totals) nDayProductivePercent(n int) float64 {
return (float64(t.nDayProductiveTotalMinutes(n)) / float64(t.nDayTotalMinutes(n))) * 100
}
func (t *totals) calculate(today time.Time, days int) error {
filenames, err := getLastNFilenames(t.notesPath, days)
if err != nil {
return err
}
var thisCategoryStart time.Time
var thisCategory string
fileIndex := 0
foundSOW := false
// for each file
for _, filename := range filenames {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
t.days[fileIndex] = make(map[string]int)
timestampLineCount := 0
scanner := bufio.NewScanner(f)
// for each line
for scanner.Scan() {
line := scanner.Text()
// if this is a timestamp line
if strings.HasPrefix(line, "## ") {
timestampLineCount++
// get the thisCategory and timestamp
category, date, err := parseLine(line)
if err != nil {
return err
}
// TODO this comment
// set the current day of the week based on the _first_ timestamp
if timestampLineCount == 1 {
// do we need to account for the case where we have not created a file
// yet? probably only is an issue on monday
// - yes!
// cases to think about
// - [ ] today is monday, and I have a notes file
// - [ ] today is monday, and I don't have a notes file
// - [ ] today is tuesday 12:30AM, still working from monday
// - [ ] today is tuesday, and I have a notes file
// - [ ] today is tuesday, and I don't have a notes file
// - [ ] today is sunday, last day of work was friday
// if this is the first file and the first file's day is monday
// - monday index is zero
// - we're done looking back
// if this is not the first file
// - monday index is the first one that we find (with a max search of 7)
//if fileIndex == 0 && fileDOW == "Mon" {
// just found another bug where when there is no monday file, we can't
// reliably look for the file to see when we started the week
// XXX works, ish!
// - problem: when it's monday and I haven't creted a file yet, it
// thinks that it's the last day that a file existed
if fileIndex >= 0 && !foundSOW {
fileDOW := string([]byte(date.Weekday().String())[:3])
if fileDOW == "Mon" {
foundSOW = true
t.sowIndex = fileIndex
}
}
// fmt.Printf("\n\n----------> %+v\n", t.sowIndex)
// if t.sowIndex == 0 && fileIndex < 7 && todayDOW != "Mon" && fileDOW == "Mon" {
// }
// set the thisCategory
thisCategory = category
thisCategoryStart = date
continue
}
// if the thisCategory is not "break," (meaning we're currently working on
// something) add its minutes
if thisCategory != "break" {
minutes := date.Sub(thisCategoryStart).Minutes()
t.days[fileIndex][thisCategory] += int(minutes)
}
thisCategoryStart = date
thisCategory = category
}
}
// if we end with a currently open task, add its minutes
if thisCategory != "break" {
minutes := int(today.Sub(thisCategoryStart).Minutes())
t.days[fileIndex][thisCategory] += minutes
t.current = thisCategory
}
fileIndex++
}
return nil
}
// sort by 15 day theme percentage
func (t *totals) sortedKeysByThemePercentage(in map[string]int) []string {
var sortedThemes []string
for k, _ := range in {
sortedThemes = append(sortedThemes, k)
}
for i := 0; i < len(sortedThemes)-1; i++ {
for j := 0; j < len(sortedThemes)-i-1; j++ {
if t.nDayThemePercent(15, sortedThemes[j]) == t.nDayThemePercent(15, sortedThemes[j+1]) {
alph := []string{sortedThemes[j], sortedThemes[j+1]}
sort.Strings(alph)
sortedThemes[j] = alph[0]
sortedThemes[j+1] = alph[1]
}
if t.nDayThemePercent(15, sortedThemes[j]) < t.nDayThemePercent(15, sortedThemes[j+1]) {
sortedThemes[j], sortedThemes[j+1] = sortedThemes[j+1], sortedThemes[j]
}
}
}
return sortedThemes
}