forked from lucassabreu/clockify-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.go
195 lines (163 loc) · 4.83 KB
/
report.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
package util
import (
"io"
"sort"
"time"
"github.com/lucassabreu/clockify-cli/api"
"github.com/lucassabreu/clockify-cli/api/dto"
"github.com/lucassabreu/clockify-cli/pkg/cmd/time-entry/util"
"github.com/lucassabreu/clockify-cli/pkg/cmdcompl"
"github.com/lucassabreu/clockify-cli/pkg/cmdcomplutil"
"github.com/lucassabreu/clockify-cli/pkg/cmdutil"
"github.com/lucassabreu/clockify-cli/pkg/search"
"github.com/lucassabreu/clockify-cli/pkg/timehlp"
"github.com/spf13/cobra"
)
const (
HelpNamesForIds = util.HelpNamesForIds
HelpMoreInfoAboutPrinting = util.HelpMoreInfoAboutPrinting
)
// ReportFlags reads the "shared" flags for report commands
type ReportFlags struct {
util.OutputFlags
FillMissingDates bool
Billable bool
NotBillable bool
Description string
Project string
TagIDs []string
}
// Check will assure that there is no conflicting flag values
func (rf ReportFlags) Check() error {
if err := rf.OutputFlags.Check(); err != nil {
return err
}
return cmdutil.XorFlag(map[string]bool{
"billable": rf.Billable,
"not-billable": rf.NotBillable,
})
}
// NewReportFlags helps creating a util.ReportFlags for report commands
func NewReportFlags() ReportFlags {
return ReportFlags{
OutputFlags: util.OutputFlags{TimeFormat: timehlp.FullTimeFormat, TimeZone: "local"},
}
}
// AddReportFlags add flags for print out the time entries
func AddReportFlags(
f cmdutil.Factory, cmd *cobra.Command, rf *ReportFlags,
) {
util.AddPrintTimeEntriesFlags(cmd, &rf.OutputFlags)
util.AddPrintMultipleTimeEntriesFlags(cmd)
cmd.Flags().BoolVarP(&rf.FillMissingDates, "fill-missing-dates", "e", false,
"add empty lines for dates without time entries")
cmd.Flags().StringVarP(&rf.Description, "description", "d", "",
"will filter time entries that contains this on the description field")
cmd.Flags().StringVarP(&rf.Project, "project", "p", "",
"Will filter time entries using this project")
_ = cmdcompl.AddSuggestionsToFlag(cmd, "project",
cmdcomplutil.NewProjectAutoComplete(f))
cmd.Flags().StringSliceVarP(&rf.TagIDs, "tag", "T", []string{},
"Will filter time entries using these tags")
_ = cmdcompl.AddSuggestionsToFlag(cmd, "tag",
cmdcomplutil.NewTagAutoComplete(f))
cmd.Flags().BoolVar(&rf.Billable, "billable", false,
"Will filter time entries that are billable")
cmd.Flags().BoolVar(&rf.NotBillable, "not-billable", false,
"Will filter time entries that are not billable")
}
// ReportWithRange fetches and prints out time entries
func ReportWithRange(
f cmdutil.Factory, start, end time.Time,
out io.Writer, rf ReportFlags,
) error {
userId, err := f.GetUserID()
if err != nil {
return err
}
workspace, err := f.GetWorkspaceID()
if err != nil {
return err
}
c, err := f.Client()
if err != nil {
return err
}
if rf.Project != "" && f.Config().IsAllowNameForID() {
if rf.Project, err = search.GetProjectByName(
c, workspace, rf.Project); err != nil {
return err
}
}
if len(rf.TagIDs) > 0 && f.Config().IsAllowNameForID() {
if rf.TagIDs, err = search.GetTagsByName(
c, workspace, rf.TagIDs); err != nil {
return err
}
}
start = timehlp.TruncateDate(start)
end = timehlp.TruncateDate(end).Add(time.Hour * 24)
log, err := c.LogRange(api.LogRangeParam{
Workspace: workspace,
UserID: userId,
FirstDate: start,
LastDate: end,
Description: rf.Description,
ProjectID: rf.Project,
TagIDs: rf.TagIDs,
PaginationParam: api.AllPages(),
})
if err != nil {
return err
}
if rf.Billable || rf.NotBillable {
log = filterBilling(log, rf.Billable)
}
sort.Slice(log, func(i, j int) bool {
return log[j].TimeInterval.Start.After(
log[i].TimeInterval.Start,
)
})
if rf.FillMissingDates && len(log) > 0 {
l := log
log = make([]dto.TimeEntry, 0, len(l))
log = append(log, fillMissing(start, l[0].TimeInterval.Start)...)
nextDay := start
for i := range l {
log = append(log,
fillMissing(nextDay, l[i].TimeInterval.Start)...)
log = append(log, l[i])
nextDay = l[i].TimeInterval.Start.Add(
time.Duration(24-l[i].TimeInterval.Start.Hour()) * time.Hour)
}
log = append(log, fillMissing(nextDay, end)...)
}
return util.PrintTimeEntries(
log, out, f.Config(), rf.OutputFlags)
}
func filterBilling(l []dto.TimeEntry, billable bool) []dto.TimeEntry {
r := make([]dto.TimeEntry, 0, len(l))
for i := 0; i < len(l); i++ {
if l[i].Billable == billable {
r = append(r, l[i])
}
}
return r
}
func fillMissing(first, last time.Time) []dto.TimeEntry {
first = timehlp.TruncateDate(first)
last = timehlp.TruncateDate(last)
d := int(last.Sub(first).Hours() / 24)
if d <= 0 {
return []dto.TimeEntry{}
}
missing := make([]dto.TimeEntry, d)
for i := 0; i < d; i++ {
t := missing[i]
ti := first.AddDate(0, 0, i)
t.TimeInterval.Start = ti
t.TimeInterval.End = &ti
missing[i] = t
}
return missing
}