-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
469 lines (413 loc) · 12.8 KB
/
helpers.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package main
import (
"encoding/json"
"fmt"
"math"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"time"
"gorm.io/gorm"
)
func getSaveDir(environment string) (string, error) {
var dataDir string
switch runtime.GOOS {
case "windows":
dataDir = os.Getenv("APPDATA")
case "darwin":
dataDir = filepath.Join(os.Getenv("HOME"), "Library", "Application Support")
default: // Unix-like system
dataDir = os.Getenv("XDG_DATA_HOME")
if dataDir == "" {
dataDir = filepath.Join(os.Getenv("HOME"), ".local", "share")
}
}
saveDir := filepath.Join(dataDir, "Go-Work-Tracker")
if environment == "development" {
Logger.Println("Running in development mode")
saveDir = filepath.Join(saveDir, "dev")
}
if _, err := os.Stat(saveDir); os.IsNotExist(err) {
os.Mkdir(saveDir, 0755)
}
return saveDir, nil
}
func readVersionConfig(WailsConfigFile []byte) (string, error) {
var wailsConfig WailsConfig
err := json.Unmarshal(WailsConfigFile, &wailsConfig)
if err != nil {
return "", err
}
return wailsConfig.Info.ProductVersion, nil
}
func readEnvConfig(WailsConfigFile []byte) (string, error) {
var wailsConfig WailsConfig
err := json.Unmarshal(WailsConfigFile, &wailsConfig)
if err != nil {
return "", err
}
return wailsConfig.Info.Environment, nil
}
func containsAll(str string, keys []string) bool {
for _, key := range keys {
if !strings.Contains(str, key) {
return false
}
}
return true
}
// fixOutdatedDb checks if the database is outdated and updates it if necessary
// This is necessary because the primary key setup for the work_hours table was changed in version 0.2.0
// and the database is now managed by GORM in version 0.5.0
// normalized database in 0.6.0 so handle transition from old single table to new normalized tables
func fixOutdatedDb(db *gorm.DB) {
if os.Getenv("BUILDING") == "true" {
return
}
// Query the sqlite_master table to get the SQL used to create the work_hours table
row := db.Raw("SELECT sql FROM sqlite_master WHERE type='table' AND name='work_hours'").Row()
var createSQL string
err := row.Scan(&createSQL)
if err != nil {
panic(err)
}
// Check if the primary key setup matches the expected setup
createSQL = strings.ReplaceAll(createSQL, " ", "")
gormKeys := []string{"created_at", "updated_at", "project_id"}
if !containsAll(createSQL, gormKeys) {
Logger.Println("Database is outdated, updating...")
type NewWorkHours struct {
gorm.Model
Date string
Seconds int
ProjectID uint
}
// Create new table
err := db.AutoMigrate(&NewWorkHours{}, &Project{}, &Organization{})
if err != nil {
panic(err)
}
tx := db.Begin()
defer func() {
if r := recover(); r != nil {
// If there is a panic, rollback the transaction
tx.Rollback()
}
}()
if tx.Error != nil {
panic(tx.Error)
}
// Check if the 'deleted_at' column exists in the 'work_hours' table
if !db.Migrator().HasColumn(&WorkHours{}, "deleted_at") {
err := db.Migrator().AddColumn(&WorkHours{}, "deleted_at")
if err != nil {
panic(err)
}
}
// Copy data from old table to new tables
rows, err := tx.Raw("SELECT DISTINCT date, organization, project, seconds FROM work_hours").Rows()
if err != nil {
panic(err)
}
defer rows.Close()
for rows.Next() {
var date, organizationName, projectName string
var seconds int
if err := rows.Scan(&date, &organizationName, &projectName, &seconds); err != nil {
panic(err)
}
if projectName == "" {
projectName = "default"
}
Logger.Printf("date: %s, organization: %s, project: %s, seconds: %d\n", date, organizationName, projectName, seconds)
// Find or create the organization
var organization Organization
tx.Where(Organization{Name: organizationName}).FirstOrCreate(&organization)
// Find or create the project
var project Project
tx.Where(Project{Name: projectName, OrganizationID: organization.ID}).FirstOrCreate(&project)
// Create the work hours entry
workHours := NewWorkHours{
Date: date,
ProjectID: project.ID,
Seconds: seconds,
}
if err := tx.Create(&workHours).Error; err != nil {
panic(err)
}
}
// Delete old table
err = tx.Migrator().DropTable("work_hours")
if err != nil {
panic(err)
}
// Rename new table to old table name
err = tx.Migrator().RenameTable("new_work_hours", "work_hours")
if err != nil {
panic(err)
}
// Commit the transaction
if err := tx.Commit().Error; err != nil {
panic(err)
}
}
}
type ExportType string
type ProjectTotal struct {
Name string
Seconds int
}
const (
CSV ExportType = "csv"
PDF ExportType = "pdf"
)
var monthMap = map[int]string{
1: "January",
2: "February",
3: "March",
4: "April",
5: "May",
6: "June",
7: "July",
8: "August",
9: "September",
10: "October",
11: "November",
12: "December",
}
func formatTime(seconds int) string {
hours := seconds / 3600
minutes := (seconds % 3600) / 60
seconds %= 60
return fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
}
type MonthlyTotals struct {
DailyTotals map[string]map[string]int
WeeklyTotals map[int]map[string]int
ProjectTotals []ProjectTotal
MonthlyTotal int
Dates []string
DateSumTotals map[string]int
WeekSumTotals map[int]int
}
func (a *App) GetWeekOfMonth(year int, month time.Month, day int) int {
t := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
firstOfMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.UTC)
currDay := firstOfMonth
week := 1
for currDay.Before(t) {
if currDay.Weekday() == time.Sunday {
week++
}
currDay = currDay.AddDate(0, 0, 1)
}
return week
}
func secondsToHours(seconds int) float64 {
hours := float64(seconds) / 3600
return math.Round(hours*100) / 100
}
func getWeekRange(year int, month time.Month, week int) (startOfWeek string, endOfWeek string) {
firstOfMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.UTC)
lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
currDay := firstOfMonth
for w := 1; w <= 5; w++ {
startOfWeek = currDay.Format("2006-01-02")
for currDay.Weekday() != time.Sunday && currDay.Before(lastOfMonth) {
currDay = currDay.AddDate(0, 0, 1)
}
if w == week {
endOfWeek = currDay.Format("2006-01-02")
return startOfWeek, endOfWeek
}
currDay = currDay.AddDate(0, 0, 1)
}
return startOfWeek, endOfWeek
}
func getWeekRanges(year int, month time.Month) map[int]string {
weekRanges := make(map[int]string)
firstOfMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.UTC)
lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
currDay := firstOfMonth
for week := 1; week <= 5; week++ {
startOfWeekStr := currDay.Format("01-02")
for currDay.Weekday() != time.Sunday && currDay.Before(lastOfMonth) {
currDay = currDay.AddDate(0, 0, 1)
}
weekRanges[week] = fmt.Sprintf("%s - %s", startOfWeekStr, currDay.Format("01-02"))
currDay = currDay.AddDate(0, 0, 1)
}
return weekRanges
}
func (a *App) getMonthlyTotals(organizationName string, year int, month time.Month) (MonthlyTotals, error) {
// Find the organization
var organization Organization
if err := a.db.Where(&Organization{Name: organizationName}).First(&organization).Error; err != nil {
Logger.Println(err)
return MonthlyTotals{}, err
}
rows, err := a.db.Table("work_hours").
Select("date, projects.name, seconds").
Joins("JOIN projects ON projects.id = work_hours.project_id").
Where("projects.deleted_at IS NULL"). // Ignore deleted projects
Where("strftime('%Y-%m', date) = ? AND projects.organization_id = ?", fmt.Sprintf("%04d-%02d", year, month), organization.ID).
Order("date").
Rows()
if err != nil {
return MonthlyTotals{}, err
}
defer rows.Close()
var dates []string
// Initialize variables to store the daily and weekly totals
dailyTotals := make(map[string]map[string]int) // map[date]map[project]seconds
dateSumTotals := make(map[string]int) // map[date]seconds
weeklyTotals := make(map[int]map[string]int) // map[week]map[project]seconds
weekSumTotals := make(map[int]int) // map[week]seconds
monthlyTotals := make(map[string]int) // map[project]seconds
monthlyTotal := 0
// Iterate over the rows and calculate the daily, weekly, and monthly totals
for rows.Next() {
var date string
var project string
var seconds int
if err := rows.Scan(&date, &project, &seconds); err != nil {
return MonthlyTotals{}, err
}
if _, ok := dailyTotals[date]; !ok {
dates = append(dates, date)
dailyTotals[date] = make(map[string]int)
}
dateSumTotals[date] += seconds
dailyTotals[date][project] += seconds
parsedDate, err := time.Parse("2006-01-02", date)
if err != nil {
return MonthlyTotals{}, err
}
week := a.GetWeekOfMonth(year, month, parsedDate.Day())
if _, ok := weeklyTotals[week]; !ok {
weeklyTotals[week] = make(map[string]int)
}
weekSumTotals[week] += seconds
weeklyTotals[week][project] += seconds
if _, ok := monthlyTotals[project]; !ok {
monthlyTotals[project] = 0
}
monthlyTotals[project] += seconds
monthlyTotal += seconds
}
if err := rows.Err(); err != nil {
return MonthlyTotals{}, err
}
// Convert the map to a slice of ProjectTotal
var projectTotals []ProjectTotal
for project, seconds := range monthlyTotals {
projectTotals = append(projectTotals, ProjectTotal{Name: project, Seconds: seconds})
}
// Sort the slice by seconds in descending order
sort.Slice(projectTotals, func(i, j int) bool {
return projectTotals[i].Seconds > projectTotals[j].Seconds
})
return MonthlyTotals{
DailyTotals: dailyTotals,
WeeklyTotals: weeklyTotals,
ProjectTotals: projectTotals,
MonthlyTotal: monthlyTotal,
Dates: dates,
DateSumTotals: dateSumTotals,
WeekSumTotals: weekSumTotals,
}, nil
}
type YearlyTotals struct {
MonthlyTotals map[string]map[string]int
MonthSumTotals map[string]int
ProjectTotals []ProjectTotal
YearlyTotal int
}
func (a *App) getYearlyTotals(organizationName string, year int) (YearlyTotals, error) {
// Find the organization
var organization Organization
if err := a.db.Where(&Organization{Name: organizationName}).First(&organization).Error; err != nil {
Logger.Println(err)
return YearlyTotals{}, err
}
rows, err := a.db.Table("work_hours").
Select("date, projects.name, seconds").
Joins("JOIN projects ON projects.id = work_hours.project_id").
Where("projects.deleted_at IS NULL"). // Ignore deleted projects
Where("strftime('%Y', date) = ? AND projects.organization_id = ?", fmt.Sprintf("%04d", year), organization.ID).
Order("date").
Rows()
if err != nil {
return YearlyTotals{}, err
}
defer rows.Close()
// Initialize variables to store the monthly totals
monthlyTotals := make(map[string]map[string]int) // map[month]map[project]seconds
monthSumTotals := make(map[string]int) // map[month]seconds
yearlyTotals := make(map[string]int) // map[project]seconds
yearlyTotal := 0
// Iterate over the rows and calculate the monthly and yearly totals
for rows.Next() {
var date string
var project string
var seconds int
if err := rows.Scan(&date, &project, &seconds); err != nil {
return YearlyTotals{}, err
}
parsedDate, err := time.Parse("2006-01-02", date)
if err != nil {
return YearlyTotals{}, err
}
month := int(parsedDate.Month())
if _, ok := monthlyTotals[monthMap[month]]; !ok {
monthlyTotals[monthMap[month]] = make(map[string]int)
}
monthlyTotals[monthMap[month]][project] += seconds
monthSumTotals[monthMap[month]] += seconds
if _, ok := yearlyTotals[project]; !ok {
yearlyTotals[project] = 0
}
yearlyTotals[project] += seconds
yearlyTotal += seconds
}
if err := rows.Err(); err != nil {
return YearlyTotals{}, err
}
// Convert the map to a slice of ProjectTotal
var projectTotals []ProjectTotal
for project, seconds := range yearlyTotals {
projectTotals = append(projectTotals, ProjectTotal{Name: project, Seconds: seconds})
}
// Sort the slice by seconds in descending order
sort.Slice(projectTotals, func(i, j int) bool {
return projectTotals[i].Seconds > projectTotals[j].Seconds
})
return YearlyTotals{
MonthlyTotals: monthlyTotals,
MonthSumTotals: monthSumTotals,
ProjectTotals: projectTotals,
YearlyTotal: yearlyTotal,
}, nil
}
func (a *App) ExportByMonth(exportType ExportType, organization string, year int, month time.Month) (string, error) {
Logger.Println("Exporting by month...", exportType, organization, year, month)
if exportType == CSV {
return a.exportCSVByMonth(organization, year, month)
} else if exportType == PDF {
return a.exportPDFByMonth(organization, year, month)
} else {
return "", fmt.Errorf("invalid export type")
}
}
func (a *App) ExportByYear(exportType ExportType, organization string, year int) (string, error) {
Logger.Println("Exporting by year...", exportType, organization, year)
if exportType == CSV {
return a.exportCSVByYear(organization, year)
} else if exportType == PDF {
return a.exportPDFByYear(organization, year)
} else {
return "", fmt.Errorf("invalid export type")
}
}