Skip to content

Commit 197e848

Browse files
authored
Fill entire period when using --fill with periodic filter
When using `klog report --fill` and combining this with a periodic filter such as `--this-week`, `--last-month`, or `--period`, this PR fills the entire requested period from begin to end, rather than just the gaps in between the first and last record. See #411.
1 parent 1654220 commit 197e848

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

klog/app/cli/args/filter.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ type FilterArgs struct {
3737
Tags []klog.Tag `name:"tag" placeholder:"TAG" group:"Filter Flags:" help:"Entries that match these tags (either in the record summary or the entry summary). You can omit the leading '#'."`
3838
Filter string `name:"filter" placeholder:"EXPR" group:"Filter Flags:" help:"Entries that match this filter expression. Run 'klog info --filtering' to learn how expressions works."`
3939

40-
hasPartialRecordsWithShouldTotal bool // Field only for internal use
40+
hasPartialRecordsWithShouldTotal bool // Field only for internal use
41+
singleShortHandFilter period.Period // Field only for internal use
4142
}
4243

4344
func (args *FilterArgs) ApplyFilter(now gotime.Time, rs []klog.Record) ([]klog.Record, app.Error) {
@@ -85,6 +86,9 @@ func (args *FilterArgs) ApplyFilter(now gotime.Time, rs []klog.Record) ([]klog.R
8586
}
8687
return res
8788
}()
89+
if len(dateRanges) == 1 {
90+
args.singleShortHandFilter = dateRanges[0]
91+
}
8892
for _, d := range dateRanges {
8993
predicates = append(predicates, filter.IsInDateRange{
9094
From: d.Since(),
@@ -134,3 +138,9 @@ func (args *FilterArgs) ApplyFilter(now gotime.Time, rs []klog.Record) ([]klog.R
134138
}
135139
return rs, nil
136140
}
141+
142+
// SinglePeriodRequested returns the corresponding period if a single short-hand
143+
// filter (such as --this-month or --last-week) was used.
144+
func (args *FilterArgs) SinglePeriodRequested() period.Period {
145+
return args.singleShortHandFilter
146+
}

klog/app/cli/report.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ func (opt *Report) Run(ctx app.Context) app.Error {
6565
aggregator := opt.aggregator()
6666
recordGroups, dates := groupByDate(aggregator.DateHash, records)
6767
if opt.Fill {
68-
dates = allDatesRange(records[0].Date(), records[len(records)-1].Date())
68+
singlePeriod := opt.FilterArgs.SinglePeriodRequested()
69+
if singlePeriod != nil {
70+
dates = allDatesRange(singlePeriod.Since(), singlePeriod.Until())
71+
} else {
72+
dates = allDatesRange(records[0].Date(), records[len(records)-1].Date())
73+
}
6974
}
7075

7176
// Table setup

klog/app/cli/report_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,29 @@ func TestYearReport(t *testing.T) {
282282
`, state.printBuffer)
283283
}
284284

285+
func TestPeriodicalReportWithFill(t *testing.T) {
286+
state, err := NewTestingContext()._SetNow(2018, 8, 24, 0, 0)._SetRecords(`
287+
2018-04-02 (8h!)
288+
8h
289+
290+
2018-08-10 (5h30m!)
291+
2h
292+
293+
2018-08-23 (2h!)
294+
5h20m
295+
`)._Run((&Report{AggregateBy: "quarter", DiffArgs: args.DiffArgs{Diff: true}, Fill: true, FilterArgs: args.FilterArgs{ThisYear: true}}).Run)
296+
require.Nil(t, err)
297+
assert.Equal(t, `
298+
Total Should Diff
299+
2018 Q1
300+
Q2 8h 8h! 0m
301+
Q3 7h20m 7h30m! -10m
302+
Q4
303+
======== ========= ========
304+
15h20m 15h30m! -10m
305+
`, state.printBuffer)
306+
}
307+
285308
func TestReportWithChart(t *testing.T) {
286309
t.Run("Daily (default) aggregation", func(t *testing.T) {
287310
state, err := NewTestingContext()._SetRecords(`

0 commit comments

Comments
 (0)