-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter.go
59 lines (48 loc) · 1.43 KB
/
filter.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
// Package filter provides functions for filtering log nodes
package filter
import (
"time"
shared "github.com/aquilax/hranoprovod-cli/v3"
)
// Config contains node filtering configuration
type Config struct {
BeginningTime *time.Time
EndTime *time.Time
}
func NewDefaultConfig() Config {
return Config{}
}
// LogNodeFilter is a filter callback function that filters nodes based on the filter config
type LogNodeFilter = func(t time.Time, node *shared.ParserNode) (bool, error)
// GetIntervalNodeFilter creates a node filter callback function given filter config
func GetIntervalNodeFilter(fc Config) *LogNodeFilter {
if fc.BeginningTime == nil && fc.EndTime == nil {
// no filter if beginning and end time are nil
return nil
}
inInterval := func(t time.Time) bool {
if (fc.BeginningTime != nil && !isGoodDate(t, *fc.BeginningTime, dateBeginning)) || (fc.EndTime != nil && !isGoodDate(t, *fc.EndTime, dateEnd)) {
return false
}
return true
}
filter := func(t time.Time, node *shared.ParserNode) (bool, error) {
return inInterval(t), nil
}
return &filter
}
// compareType identifies the type of date comparison
type compareType bool
const (
dateBeginning compareType = true
dateEnd compareType = false
)
func isGoodDate(time, compareTime time.Time, ct compareType) bool {
if time.Equal(compareTime) {
return true
}
if ct == dateBeginning {
return time.After(compareTime)
}
return time.Before(compareTime)
}