-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_timelog.go
120 lines (105 loc) · 2.71 KB
/
cmd_timelog.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
package main
import (
"fmt"
"os"
"strings"
"github.com/rodaine/table"
)
func formatAsTime(decimal float64) string {
hours := int(decimal)
minutes := int((decimal - float64(hours)) * 60)
return fmt.Sprintf("%d:%02d", hours, minutes)
}
func doListTimelog() {
if (flagProjectId == 0 && flagIssueId == 0) || (flagProjectId != 0 && flagIssueId != 0) {
fmt.Println("Error: must specify either -project or -issue")
flagSetTimelog.PrintDefaults()
os.Exit(1)
}
params := remoteTimelogParams{}
if flagProjectId > 0 {
params.projectId = flagProjectId
params.criteria = "project"
} else {
params.issueId = uint(flagIssueId)
params.criteria = "issue"
}
if err := params.TimeFilter(flagTimeSince, flagTimeUntil); err != nil {
panic(err)
}
entries, err := context.fetchTimeEntries(¶ms)
if err != nil {
panic(err)
}
switch flagPivot {
case "issue":
doIssuePivotTimelog(entries)
case "":
doRawTimelog(entries)
}
}
func doRawTimelog(entries *remoteTimelogList) {
var (
totalTime float64 = 0
totalTimeString string
)
tbl := table.New("ID", "USER", "ACTIVITY", "ISSUE", "COMMENT", "DATE", "TIME")
for _, entry := range entries.Entries {
totalTime += entry.Hours
var hours string
if flagDecimalTime {
hours = fmt.Sprintf("%.02f", entry.Hours)
} else {
hours = formatAsTime(entry.Hours)
}
tbl.AddRow(
entry.Id, entry.User.Name, entry.Activity.Name,
entry.Issue.Name, entry.Comment, entry.SpentOn, hours,
)
}
if flagDecimalTime {
totalTimeString = fmt.Sprintf("%.02f", totalTime)
} else {
totalTimeString = formatAsTime(totalTime)
}
tbl.AddRow("", "", "", "", "", "----------", strings.Repeat("-", len(totalTimeString)))
tbl.AddRow("", "", "", "", "", "TOTAL TIME", totalTimeString)
tbl.Print()
}
func doIssuePivotTimelog(entries *remoteTimelogList) {
var (
totals = map[int]float64{}
issues = map[int]namedEntry{}
totalTime float64 = 0
)
// pivot by timelog
for _, entry := range entries.Entries {
if _, ok := totals[entry.Issue.Id]; !ok {
totals[entry.Issue.Id] = 0
}
totals[entry.Issue.Id] += entry.Hours
if _, ok := issues[entry.Issue.Id]; !ok {
issues[entry.Issue.Id] = entry.Issue
}
}
tbl := table.New("ISSUE", "SUBJECT", "TIME")
for id, total := range totals {
totalTime += total
var hours string
if flagDecimalTime {
hours = fmt.Sprintf("%.02f", total)
} else {
hours = formatAsTime(total)
}
tbl.AddRow(id, issues[id].Name, hours)
}
var totalHours string
if flagDecimalTime {
totalHours = fmt.Sprintf("%.02f", totalTime)
} else {
totalHours = formatAsTime(totalTime)
}
tbl.AddRow("", "----------", strings.Repeat("-", len(totalHours)))
tbl.AddRow("", "TOTAL TIME", totalHours)
tbl.Print()
}