-
-
Notifications
You must be signed in to change notification settings - Fork 404
/
filters.go
96 lines (82 loc) · 2.68 KB
/
filters.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
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* 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 ofthe 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 specificlanguage governing permissions and
* limitations under the License.
*
*/
package graph
import (
"github.com/skydive-project/skydive/graffiti/filters"
"github.com/skydive-project/skydive/graffiti/getter"
)
// ElementMatcher defines an interface used to match an element
type ElementMatcher interface {
Match(g getter.Getter) bool
Filter() (*filters.Filter, error)
}
// ElementFilter implements ElementMatcher interface based on filter
type ElementFilter struct {
filter *filters.Filter
}
// Match returns true if the given element matches the filter.
func (mf *ElementFilter) Match(g getter.Getter) bool {
return mf.filter.Eval(g)
}
// Filter returns the filter
func (mf *ElementFilter) Filter() (*filters.Filter, error) {
return mf.filter, nil
}
// NewElementFilter returns a new ElementFilter
func NewElementFilter(f *filters.Filter) *ElementFilter {
return &ElementFilter{filter: f}
}
// NewFilterForEdge creates a filter based on parent or child
func NewFilterForEdge(parent Identifier, child Identifier) *filters.Filter {
return filters.NewOrFilter(
filters.NewTermStringFilter("Parent", string(parent)),
filters.NewTermStringFilter("Child", string(child)),
)
}
// TimeSlice defines a time boudary values
type TimeSlice struct {
Start int64 `json:"Start"`
Last int64 `json:"Last"`
}
// NewTimeSlice creates a new TimeSlice based on Start and Last
func NewTimeSlice(s, l int64) *TimeSlice {
return &TimeSlice{Start: s, Last: l}
}
// filterForTimeSlice creates a filter based on a time slice between
// startName and endName. time.Now() is used as reference if t == nil
func filterForTimeSlice(t *TimeSlice, startName, endName string) *filters.Filter {
if t == nil {
now := TimeNow().UnixMilli()
t = NewTimeSlice(now, now)
}
return filters.NewAndFilter(
filters.NewLteInt64Filter(startName, t.Last),
filters.NewOrFilter(
filters.NewNullFilter(endName),
filters.NewGteInt64Filter(endName, t.Start),
),
)
}
func getTimeFilter(t *TimeSlice) *filters.Filter {
if t == nil {
return filters.NewNullFilter("ArchivedAt")
}
return filters.NewAndFilter(
filterForTimeSlice(t, "CreatedAt", "DeletedAt"),
filterForTimeSlice(t, "UpdatedAt", "ArchivedAt"),
)
}