-
Notifications
You must be signed in to change notification settings - Fork 17
/
support.go
401 lines (338 loc) · 8.68 KB
/
support.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package main
import (
"fmt"
"math"
"sort"
"strings"
"time"
"github.com/jason0x43/go-alfred"
"github.com/jason0x43/go-toggl"
)
func checkRefresh() error {
if config.TestMode {
dlog.Printf("Test mode is active; not auto-refreshing")
return nil
}
if time.Now().Sub(cache.Time).Minutes() < 5.0 {
return nil
}
dlog.Println("Refreshing cache...")
err := refresh()
if err != nil {
dlog.Println("Error refreshing cache:", err)
}
return err
}
func refresh() error {
s := toggl.OpenSession(config.APIKey)
account, err := s.GetAccount()
if err != nil {
return err
}
dlog.Printf("got account: %#v", account)
cache.Time = time.Now()
cache.Account = account
cache.Workspace = account.Workspaces[0].ID
return alfred.SaveJSON(cacheFile, &cache)
}
func getRunningTimer() (timer toggl.TimeEntry, found bool) {
for _, entry := range cache.Account.TimeEntries {
if entry.IsRunning() {
return entry, true
}
}
return
}
func getProjectsByID() (projectsByID map[int]toggl.Project) {
projectsByID = map[int]toggl.Project{}
for _, proj := range cache.Account.Projects {
projectsByID[proj.ID] = proj
}
return
}
func getProjectsByName() (projectsByName map[string]toggl.Project) {
projectsByName = map[string]toggl.Project{}
for _, proj := range cache.Account.Projects {
projectsByName[proj.Name] = proj
}
return
}
func findProjectByName(name string) (project toggl.Project, found bool) {
for _, proj := range cache.Account.Projects {
if proj.Name == name {
return proj, true
}
}
return
}
func getProjectByID(id int) (project toggl.Project, index int, found bool) {
for i, proj := range cache.Account.Projects {
if proj.ID == id {
return proj, i, true
}
}
return
}
func getTagByID(id int) (tag toggl.Tag, index int, found bool) {
for i, entry := range cache.Account.Tags[:] {
if entry.ID == id {
return entry, i, true
}
}
return
}
func getTimerByID(id int) (timer toggl.TimeEntry, index int, found bool) {
for i, entry := range cache.Account.TimeEntries[:] {
if entry.ID == id {
return entry, i, true
}
}
return
}
func getClientByID(id int) (client toggl.Client, index int, found bool) {
for i, client := range cache.Account.Clients {
if client.ID == id {
return client, i, true
}
}
return
}
func getWorkspaceByID(id int) (workspace toggl.Workspace, index int, found bool) {
for i, workspace := range cache.Account.Workspaces {
if workspace.ID == id {
return workspace, i, true
}
}
return
}
func findTimersByProjectID(pid int) (entries []toggl.TimeEntry) {
for _, entry := range cache.Account.TimeEntries[:] {
if entry.Pid != nil && *entry.Pid == pid {
entries = append(entries, entry)
}
}
return
}
func findTimersByTag(tag string) (entries []toggl.TimeEntry) {
for _, entry := range cache.Account.TimeEntries[:] {
for _, t := range entry.Tags {
if t == tag {
entries = append(entries, entry)
break
}
}
}
return
}
func findTagByName(name string) (tag toggl.Tag, found bool) {
for _, tag := range cache.Account.Tags {
if tag.Name == name {
return tag, true
}
}
return
}
func findTagNameByID(id int) (name string, found bool) {
for _, tag := range cache.Account.Tags {
if tag.ID == id {
return tag.Name, true
}
}
return
}
func getTimeEntriesForQuery(query string) (matched []toggl.TimeEntry) {
entries := cache.Account.TimeEntries[:]
matchQuery := strings.ToLower(query)
for _, entry := range entries {
if strings.Contains(strings.ToLower(entry.Description), matchQuery) {
matched = append(matched, entry)
}
}
sort.Sort(sort.Reverse(byTime(matched)))
return
}
func getLatestTimeEntriesForProject(pid int) (matchedArr []toggl.TimeEntry) {
entries := cache.Account.TimeEntries[:]
matched := map[string]toggl.TimeEntry{}
for _, entry := range entries {
if entry.Pid != nil && *entry.Pid == pid {
e, ok := matched[entry.Description]
if !ok ||
(!entry.StartTime().IsZero() && !e.StartTime().IsZero() && entry.StartTime().After(e.StartTime())) {
matched[entry.Description] = entry
}
}
}
for _, value := range matched {
matchedArr = append(matchedArr, value)
}
sort.Sort(sort.Reverse(byTime(matchedArr)))
return
}
func isWorkspacePremium(id int) bool {
workspace, _, _ := getWorkspaceByID(id)
return workspace.Premium
}
func projectHasTimeEntries(pid int) bool {
entries := cache.Account.TimeEntries
for i := range entries {
if entries[i].Pid != nil && *entries[i].Pid == pid {
return true
}
}
return false
}
func getLatestTimeEntriesForTag(tag string) (matched []toggl.TimeEntry) {
entries := cache.Account.TimeEntries[:]
for _, entry := range entries {
for _, t := range entry.Tags {
if t == tag {
matched = append(matched, entry)
break
}
}
}
sort.Sort(sort.Reverse(byTime(matched)))
return
}
func tagHasTimeEntries(tag string) bool {
entries := cache.Account.TimeEntries
for i := range entries {
if entries[i].HasTag(tag) {
return true
}
}
return false
}
// is date1's date before date2's date
func isDateBefore(date1 time.Time, date2 time.Time) bool {
return (date1.Year() == date2.Year() && date1.YearDay() < date2.YearDay()) ||
date1.Year() < date1.Year()
}
// is date1's date after date2's date
func isDateAfter(date1 time.Time, date2 time.Time) bool {
return (date1.Year() == date2.Year() && date2.YearDay() < date1.YearDay()) ||
date2.Year() < date1.Year()
}
// do date1 and date2 refer to the same date
func isSameDate(date1 time.Time, date2 time.Time) bool {
return date1.Year() == date2.Year() && date1.Month() == date2.Month() &&
date1.Day() == date2.Day()
}
func isSameWeek(date1 time.Time, date2 time.Time) bool {
y1, w1 := date1.ISOWeek()
y2, w2 := date2.ISOWeek()
return y1 == y2 && w1 == w2
}
func toIsoDateString(date time.Time) string {
return date.Format("2006-01-02")
}
func toHumanDateString(date time.Time) string {
date = date.Local()
today := time.Now()
if isSameDate(date, today) {
return "today"
} else if isSameDate(date, today.AddDate(0, 0, -1)) {
return "yesterday"
} else if isSameWeek(date, today) {
return date.Weekday().String()
} else if isDateAfter(date, today.AddDate(0, 0, -7)) {
return "last " + date.Weekday().String()
} else {
return toIsoDateString(date)
}
}
// roundDuration converts a number of seconds to a quantized fractional hour, as an int
//
// 1.25 hours = 125
// 0.25 hours = 25
//
// If the `floor` argument is true, any fractional part of the pre-quantized
// value is truncated before quantization.
//
// floor == false: 1.05 -> 1.25 -> 125
// floor == true: 1.05 -> 1.00 -> 100
func roundDuration(duration int64, floor bool) int64 {
if config.Rounding != 0 {
// the number of seconds in the rounding increment
incr := config.Rounding * 60
// the number of increments in the duration
var fracHours int64
if floor {
fracHours = int64(math.Floor(float64(duration) / float64(incr)))
} else {
fracHours = int64(math.Ceil(float64(duration) / float64(incr)))
}
// the fraction of hour that is being rounded to
frac := 60.0 / float64(config.Rounding)
return fracHours * int64((100.0 / frac))
}
// not rounding, so just return the duration as a number of hours * 100
hours := float64(duration) / 3600.0
return int64(hours * 100)
}
// formatTime formats a duration in hours*100 (the return value of
// roundDuration) according to the current configured format (fractional time
// or hh:mm)
func formatDuration(hoursTimes100 int64) string {
if config.HoursMinutes {
hours := float64(hoursTimes100) / 100.0
wholeHours := int64(hours)
minutes := round((hours - float64(wholeHours)) * 60.0)
return fmt.Sprintf("%d:%02d", wholeHours, minutes)
}
return fmt.Sprintf("%.2f", float64(hoursTimes100)/100.0)
}
// round rounds a float64, returning an int64
func round(value float64) int64 {
return int64(math.Floor(value + 0.5))
}
type byTime []toggl.TimeEntry
func (b byTime) Len() int {
return len(b)
}
func (b byTime) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}
func (b byTime) Less(i, j int) bool {
if b[i].StartTime().IsZero() {
return true
} else if b[j].StartTime().IsZero() {
return false
} else {
return b[i].StartTime().Before(b[j].StartTime())
}
}
type matchEntry struct {
portion float64
start int
title string
subtitle string
id int
}
type byBestMatch []matchEntry
func (b byBestMatch) Len() int {
return len(b)
}
func (b byBestMatch) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}
func (b byBestMatch) Less(i, j int) bool {
if b[i].portion < b[j].portion {
return true
}
if b[j].portion < b[i].portion {
return false
}
return false
}
type byMatchID []matchEntry
func (b byMatchID) Len() int {
return len(b)
}
func (b byMatchID) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}
func (b byMatchID) Less(i, j int) bool {
return b[i].id < b[j].id
}