-
Notifications
You must be signed in to change notification settings - Fork 23
/
report.go
205 lines (173 loc) · 6.06 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
196
197
198
199
200
201
202
203
204
205
// Copyright © 2019 Lucas dos Santos Abreu <lucas.s.abreu@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"io"
"os"
"sort"
"time"
"github.com/lucassabreu/clockify-cli/api"
"github.com/lucassabreu/clockify-cli/api/dto"
"github.com/lucassabreu/clockify-cli/reports"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// reportsCmd represents the reports command
var reportCmd = &cobra.Command{
Use: "report <start> <end>",
Short: "List all time entries in the date ranges and with more data (format date as 2016-01-02)",
Args: cobra.ExactArgs(2),
RunE: withClockifyClient(func(cmd *cobra.Command, args []string, c *api.Client) error {
start, err := time.Parse("2006-01-02", args[0])
if err != nil {
return err
}
end, err := time.Parse("2006-01-02", args[1])
if err != nil {
return err
}
return reportWithRange(c, start, end, cmd)
}),
}
// reportThisMonthCmd represents the reports this-month command
var reportThisMonthCmd = &cobra.Command{
Use: "this-month",
Short: "List all time entries in this month",
RunE: withClockifyClient(func(cmd *cobra.Command, args []string, c *api.Client) error {
first, last := getMonthRange(time.Now())
return reportWithRange(c, first, last, cmd)
}),
}
// reportLastMonthCmd represents the report last-month command
var reportLastMonthCmd = &cobra.Command{
Use: "last-month",
Short: "List all time entries in last month",
RunE: withClockifyClient(func(cmd *cobra.Command, args []string, c *api.Client) error {
first, last := getMonthRange(time.Now().AddDate(0, -1, 0))
return reportWithRange(c, first, last, cmd)
}),
}
// reportThisWeekCmd represents the report last-month command
var reportThisWeekCmd = &cobra.Command{
Use: "this-week",
Short: "List all time entries in this week",
RunE: withClockifyClient(func(cmd *cobra.Command, args []string, c *api.Client) error {
first, last := getWeekRange(time.Now())
return reportWithRange(c, first, last, cmd)
}),
}
// reportLastWeekCmd represents the report last-month command
var reportLastWeekCmd = &cobra.Command{
Use: "last-week",
Short: "List all time entries in last week",
RunE: withClockifyClient(func(cmd *cobra.Command, args []string, c *api.Client) error {
first, last := getWeekRange(time.Now().AddDate(0, 0, -7))
return reportWithRange(c, first, last, cmd)
}),
}
func init() {
rootCmd.AddCommand(reportCmd)
_ = reportCmd.MarkFlagRequired("workspace")
_ = reportCmd.MarkFlagRequired("user-id")
reportFlags(reportCmd)
reportFlags(reportThisMonthCmd)
reportFlags(reportLastMonthCmd)
reportFlags(reportThisWeekCmd)
reportFlags(reportLastWeekCmd)
reportCmd.AddCommand(reportThisMonthCmd)
reportCmd.AddCommand(reportLastMonthCmd)
reportCmd.AddCommand(reportThisWeekCmd)
reportCmd.AddCommand(reportLastWeekCmd)
}
func reportFlags(cmd *cobra.Command) {
cmd.Flags().StringP("format", "f", "", "golang text/template format to be applied on each time entry")
cmd.Flags().BoolP("json", "j", false, "print as JSON")
cmd.Flags().BoolP("csv", "v", false, "print as CSV")
cmd.Flags().BoolP("fill-missing-dates", "e", false, "add empty lines for dates without time entries")
}
func getMonthRange(ref time.Time) (first time.Time, last time.Time) {
first = ref.AddDate(0, 0, ref.Day()*-1+1).Truncate(time.Hour)
last = first.AddDate(0, 1, -1)
return
}
func getWeekRange(ref time.Time) (first time.Time, last time.Time) {
first = ref.AddDate(0, 0, int(ref.Weekday())*-1).Truncate(time.Hour)
last = first.AddDate(0, 0, 5)
return
}
func reportWithRange(c *api.Client, start, end time.Time, cmd *cobra.Command) error {
format, _ := cmd.Flags().GetString("format")
asJSON, _ := cmd.Flags().GetBool("json")
asCSV, _ := cmd.Flags().GetBool("csv")
fillMissingDates, _ := cmd.Flags().GetBool("fill-missing-dates")
start = start.Add(time.Duration(start.Hour()) * time.Hour * -1)
end = end.Add(time.Duration(24-start.Hour()) * time.Hour * 1)
userId, err := getUserId(c)
if err != nil {
return err
}
log, err := c.LogRange(api.LogRangeParam{
Workspace: viper.GetString("workspace"),
UserID: userId,
FirstDate: start,
LastDate: end,
PaginationParam: api.PaginationParam{AllPages: true},
})
if err != nil {
return err
}
sort.Slice(log, func(i, j int) bool {
return log[j].TimeInterval.Start.After(
log[i].TimeInterval.Start,
)
})
if fillMissingDates && len(log) > 0 {
newLog := make([]dto.TimeEntry, 0, len(log))
fillMissing := func(user *dto.User, first, last time.Time) []dto.TimeEntry {
first = time.Date(first.Year(), first.Month(), first.Day(), 0, 0, 0, 0, time.Local)
last = time.Date(last.Year(), last.Month(), last.Day(), 0, 0, 0, 0, time.Local)
d := int(last.Sub(first).Hours() / 24)
if d <= 0 {
return []dto.TimeEntry{}
}
missing := make([]dto.TimeEntry, d)
for i, t := range missing {
ti := first.AddDate(0, 0, i)
t.TimeInterval.Start = ti
t.TimeInterval.End = &ti
missing[i] = t
}
return missing
}
nextDay := start
for _, t := range log {
newLog = append(newLog, fillMissing(t.User, nextDay, t.TimeInterval.Start)...)
newLog = append(newLog, t)
nextDay = t.TimeInterval.Start.Add(time.Duration(24-t.TimeInterval.Start.Hour()) * time.Hour)
}
log = append(newLog, fillMissing(log[0].User, nextDay, end)...)
}
var fn func([]dto.TimeEntry, io.Writer) error
fn = reports.TimeEntriesPrintWithTimeFormat(reports.TIME_FORMAT_FULL)
if asJSON {
fn = reports.TimeEntriesJSONPrint
}
if asCSV {
fn = reports.TimeEntriesCSVPrint
}
if format != "" {
fn = reports.TimeEntriesPrintWithTemplate(format)
}
return fn(log, os.Stdout)
}