Skip to content

Commit a2c9f1a

Browse files
authored
Filtering docs and tests
1 parent 197e848 commit a2c9f1a

File tree

7 files changed

+48
-14
lines changed

7 files changed

+48
-14
lines changed

klog/app/cli/args/filter.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import (
1111

1212
type FilterArgs struct {
1313
// Date-related filters:
14-
Date klog.Date `name:"date" placeholder:"DATE" group:"Filter Flags:" help:"Entries at this date. DATE has to be in format YYYY-MM-DD or YYYY/MM/DD. E.g., '2024-01-31' or '2024/01/31'."`
15-
Since klog.Date `name:"since" placeholder:"DATE" group:"Filter Flags:" help:"Entries since this date (inclusive)."`
16-
Until klog.Date `name:"until" placeholder:"DATE" group:"Filter Flags:" help:"Entries until this date (inclusive)."`
17-
Period period.Period `name:"period" placeholder:"PERIOD" group:"Filter Flags:" help:"Entries within a calendar period. PERIOD has to be in format YYYY, YYYY-MM, YYYY-Www or YYYY-Qq. E.g., '2024', '2024-04', '2022-W21' or '2024-Q1'."`
14+
Date klog.Date `name:"date" placeholder:"DATE" group:"Filter Flags:" help:"Records at this date. DATE has to be in format YYYY-MM-DD or YYYY/MM/DD. E.g., '2024-01-31' or '2024/01/31'."`
15+
Since klog.Date `name:"since" placeholder:"DATE" group:"Filter Flags:" help:"Records since this date (inclusive)."`
16+
Until klog.Date `name:"until" placeholder:"DATE" group:"Filter Flags:" help:"Records until this date (inclusive)."`
17+
Period period.Period `name:"period" placeholder:"PERIOD" group:"Filter Flags:" help:"Records within a calendar period. PERIOD has to be in format YYYY, YYYY-MM, YYYY-Www or YYYY-Qq. E.g., '2024', '2024-04', '2022-W21' or '2024-Q1'."`
1818

1919
// Filter shortcuts:
2020
// The two `XXX` ones are dummy entries just for the help output, they also aren’t available
@@ -34,8 +34,8 @@ type FilterArgs struct {
3434
LastYear bool `hidden:"" name:"last-year" group:"Filter Flags:" completion-enabled:"true"`
3535

3636
// General filters:
37-
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 '#'."`
38-
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."`
37+
Tags []klog.Tag `name:"tag" placeholder:"TAG" group:"Filter Flags:" help:"Records or entries that match these tags (either in the record summary or the entry summary). You can omit the leading '#'."`
38+
Filter string `name:"filter" placeholder:"EXPR" group:"Filter Flags:" help:"Records or entries that match this filter expression. Run 'klog info --filtering' to learn how expressions works."`
3939

4040
hasPartialRecordsWithShouldTotal bool // Field only for internal use
4141
singleShortHandFilter period.Period // Field only for internal use
@@ -140,7 +140,7 @@ func (args *FilterArgs) ApplyFilter(now gotime.Time, rs []klog.Record) ([]klog.R
140140
}
141141

142142
// SinglePeriodRequested returns the corresponding period if a single short-hand
143-
// filter (such as --this-month or --last-week) was used.
143+
// periodic filter (such as --this-month, --period) was used.
144144
func (args *FilterArgs) SinglePeriodRequested() period.Period {
145145
return args.singleShortHandFilter
146146
}

klog/app/cli/args/misc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type DiffArgs struct {
1818
// nonsensical results.
1919
func (args *DiffArgs) GetWarning(filterArgs FilterArgs) service.UsageWarning {
2020
if args.Diff && filterArgs.hasPartialRecordsWithShouldTotal {
21-
return service.DiffEntryFilteringWarning
21+
return service.EntryFilteredDiffWarning
2222
}
2323
return service.UsageWarning{}
2424
}

klog/app/cli/info.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ func (opt *Info) Run(ctx app.Context) app.Error {
2525
2626
klog total --filter='2025-04 && #work' mytimes.klg
2727
28-
This would evaluate all entries in April 2025 that match the tag #work. Wrap the filter expression in single quotes to avoid undesired shell word splitting or substitution. Filter expressions consist of operands for matching the data that shall be included in the filter result. Operands can be combined via logical operators and grouped via parentheses.
28+
This would evaluate all records and entries in April 2025 that match the tag #work. Wrap the filter expression in single quotes to avoid undesired shell word splitting or substitution. Filter expressions consist of operands for matching the data that shall be included in the filter result. Operands can be combined via logical operators and grouped via parentheses.
29+
30+
Filters can match at record-level and/or at entry-level. It only keeps the data that satisfies the filter condition. For entry-level filters, this means that all non-matching entries are stripped from the record.
2931
3032
Examples:
3133
2025-04-20 || 2020-04-21

klog/app/cli/print.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ func (opt *Print) Help() string {
2525
Outputs data on the terminal, by default with syntax-highlighting turned on.
2626
Note that the output doesn’t resemble the file verbatim, but it may apply some minor formatting.
2727
28-
If run with filter flags, it only outputs those entries that match the filter clauses. E.g., when filtering for a tag that only appears particular entries, it will exclude all other entries from that record.
29-
3028
You can optionally also sort the records, or print out the total times for each record and entry.
3129
`
3230
}

klog/service/filter/filter_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ func TestQueryWithNoClauses(t *testing.T) {
7474
}, rs)
7575
}
7676

77+
func TestQueryWithNoMatches(t *testing.T) {
78+
rs, hprws := Filter(IsInDateRange{
79+
From: klog.Ɀ_Date_(2002, 1, 1),
80+
To: klog.Ɀ_Date_(2002, 1, 1),
81+
}, sampleRecordsForQuerying())
82+
assert.False(t, hprws)
83+
assertResult(t, []expect{}, rs)
84+
}
85+
86+
func TestQueryAgainstEmptyInput(t *testing.T) {
87+
rs, hprws := Filter(IsInDateRange{
88+
From: klog.Ɀ_Date_(2002, 1, 1),
89+
To: klog.Ɀ_Date_(2002, 1, 1),
90+
}, nil)
91+
assert.False(t, hprws)
92+
assertResult(t, []expect{}, rs)
93+
}
94+
7795
func TestQueryWithAtDate(t *testing.T) {
7896
rs, hprws := Filter(IsInDateRange{
7997
From: klog.Ɀ_Date_(2000, 1, 2),
@@ -239,4 +257,19 @@ func TestComplexFilterQueries(t *testing.T) {
239257
{klog.Ɀ_Date_(2000, 1, 3), []int{240}},
240258
}, rs)
241259
}
260+
{
261+
rs, hprws := Filter(Not{Or{[]Predicate{
262+
IsInDateRange{From: klog.Ɀ_Date_(1999, 12, 30), To: klog.Ɀ_Date_(2000, 1, 1)},
263+
HasTag{klog.NewTagOrPanic("xyz", "")},
264+
And{[]Predicate{
265+
IsEntryType{ENTRY_TYPE_DURATION_POSITIVE},
266+
HasTag{klog.NewTagOrPanic("bar", "")},
267+
}},
268+
}}}, sampleRecordsForQuerying())
269+
assert.True(t, hprws)
270+
assertResult(t, []expect{
271+
{klog.Ɀ_Date_(1999, 12, 29), []int{}},
272+
{klog.Ɀ_Date_(2000, 1, 3), []int{240, 0}},
273+
}, rs)
274+
}
242275
}

klog/service/filter/predicate.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
"github.com/jotaen/klog/klog"
88
)
99

10-
// Predicate is the generic base type for all predicates.
10+
// Predicate is the generic base type for all predicates. The caller is responsible for
11+
// selecting the applicable match function (it’s meant to be either/or).
1112
type Predicate interface {
1213
// Matches returns true if the record’s entry satisfies the predicate.
1314
Matches(klog.Record, klog.Entry) bool

klog/service/warning.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var (
1818
Name: "POINTLESS_NOW",
1919
Message: "You specified --now, but there was no open-ended time range",
2020
}
21-
DiffEntryFilteringWarning = UsageWarning{
21+
EntryFilteredDiffWarning = UsageWarning{
2222
Name: "ENTRY_FILTERED_DIFFING",
2323
Message: "Combining --diff and filtering at entry-level may yield nonsensical results",
2424
}
@@ -41,7 +41,7 @@ func NewDisabledCheckers() DisabledCheckers {
4141
(&overlappingTimeRangesChecker{}).Name(): false,
4242
(&moreThan24HoursChecker{}).Name(): false,
4343
PointlessNowWarning.Name: false,
44-
DiffEntryFilteringWarning.Name: false,
44+
EntryFilteredDiffWarning.Name: false,
4545
}
4646
}
4747

0 commit comments

Comments
 (0)