Skip to content

Commit

Permalink
(feat): fill missing dates with empty entries (#21)
Browse files Browse the repository at this point in the history
* (feat): fill missing dates with empty entries

* (release): v0.2.0
  • Loading branch information
lucassabreu authored Mar 2, 2020
1 parent 03264c3 commit e34801d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [v0.2.0] - 2020-03-02

## Added

- `clockify-cli report --fill-missing-dates` when this parameters is set, if there
are dates from the range informed, will be created "stub" entries to better show
that are missing entries.

## [v0.1.7] - 2020-02-03

## Added
Expand Down
38 changes: 35 additions & 3 deletions cmd/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ func init() {
}

func reportFlags(cmd *cobra.Command) {
cmd.Flags().StringP("format", "f", "", "golang text/template format to be applyed on each time entry")
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) {
Expand All @@ -99,12 +100,15 @@ func reportWithRange(c *api.Client, start, end time.Time, cmd *cobra.Command) {
format, _ := cmd.Flags().GetString("format")
asJSON, _ := cmd.Flags().GetBool("json")
asCSV, _ := cmd.Flags().GetBool("csv")
fillEmptyDates, _ := cmd.Flags().GetBool("fill-empty-dates")

start = start.Add(time.Duration(start.Hour()) * time.Hour * -1)
end = end.Add(time.Duration(24-start.Hour()) * time.Hour * 1)
log, err := c.LogRange(api.LogRangeParam{
Workspace: viper.GetString("workspace"),
UserID: viper.GetString("user.id"),
FirstDate: start.Add(time.Duration(start.Hour()) * time.Hour * -1),
LastDate: end.Add(time.Duration(24-start.Hour()) * time.Hour * 1),
FirstDate: start,
LastDate: end,
PaginationParam: api.PaginationParam{AllPages: true},
})

Expand All @@ -119,6 +123,34 @@ func reportWithRange(c *api.Client, start, end time.Time, cmd *cobra.Command) {
)
})

if fillEmptyDates && 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 {
t.TimeInterval.Start = first.AddDate(0, 0, i)
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.TimeEntriesPrint
if asJSON {
Expand Down
12 changes: 11 additions & 1 deletion reports/timeEntry.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,24 @@ func TimeEntriesCSVPrint(timeEntries []dto.TimeEntry, out io.Writer) error {
p = *te.Project
}

end := te.TimeInterval.Start
if te.TimeInterval.End != nil {
end = *te.TimeInterval.End
}

if te.User == nil {
u := dto.User{}
te.User = &u
}

arr := []string{
te.ID,
te.Description,
p.ID,
p.Name,
format(&te.TimeInterval.Start),
format(te.TimeInterval.End),
fmt.Sprintf("%-8v", te.TimeInterval.End.Sub(te.TimeInterval.Start)),
fmt.Sprintf("%-8v", end.Sub(te.TimeInterval.Start)),
te.User.ID,
te.User.Email,
te.User.Name,
Expand Down

0 comments on commit e34801d

Please sign in to comment.