kraken
58 строк · 1.6 Кб
1// Copyright (c) 2016-2019 Uber Technologies, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package networkevent
15
16import (
17"sort"
18"time"
19
20"github.com/uber/kraken/utils/stringset"
21)
22
23type byTime []*Event
24
25func (s byTime) Len() int { return len(s) }
26func (s byTime) Less(i, j int) bool { return s[i].Time.Before(s[j].Time) }
27func (s byTime) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
28
29// Sort sorts events in place by timestamp.
30func Sort(events []*Event) {
31sort.Sort(byTime(events))
32}
33
34// Filter filters events by name.
35func Filter(events []*Event, names ...Name) []*Event {
36s := make(stringset.Set)
37for _, name := range names {
38s.Add(string(name))
39}
40var f []*Event
41for _, e := range events {
42if s.Has(string(e.Name)) {
43f = append(f, e)
44}
45}
46return f
47}
48
49// StripTimestamps overwrites timestamps in events as empty, allowing clients
50// to check equality of events.
51//
52// Mutates events in place and returns events for chaining purposes.
53func StripTimestamps(events []*Event) []*Event {
54for _, e := range events {
55e.Time = time.Time{}
56}
57return events
58}
59