forked from TylerBrock/saw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.go
128 lines (102 loc) · 2.97 KB
/
configuration.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package config
import (
"errors"
"sort"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
)
type Configuration struct {
Group string
Prefix string
Start string
End string
Filter string
Streams []*cloudwatchlogs.LogStream
Descending bool
OrderBy string
}
// Define the order of time formats to attempt to use to parse our input absolute time
var absoluteTimeFormats = []string{
time.RFC3339,
"2006-01-02", // Simple date
"2006-01-02 15:04:05", // Simple date & time
}
// Parse the input string into a time.Time object.
// Provide the currentTime as a parameter to support relative time.
func getTime(timeStr string, currentTime time.Time) (time.Time, error) {
relative, err := time.ParseDuration(timeStr)
if err == nil {
return currentTime.Add(relative), nil
}
// Iterate over available absolute time formats until we find one that works
for _, timeFormat := range absoluteTimeFormats {
absolute, err := time.Parse(timeFormat, timeStr)
if err == nil {
return absolute, err
}
}
return time.Time{}, errors.New("Could not parse relative or absolute time")
}
func (c *Configuration) DescribeLogGroupsInput() *cloudwatchlogs.DescribeLogGroupsInput {
input := cloudwatchlogs.DescribeLogGroupsInput{}
if c.Prefix != "" {
input.SetLogGroupNamePrefix(c.Prefix)
}
return &input
}
func (c *Configuration) DescribeLogStreamsInput() *cloudwatchlogs.DescribeLogStreamsInput {
input := cloudwatchlogs.DescribeLogStreamsInput{}
input.SetLogGroupName(c.Group)
input.SetDescending(c.Descending)
if c.OrderBy != "" {
input.SetOrderBy(c.OrderBy)
}
if c.Prefix != "" {
input.SetLogStreamNamePrefix(c.Prefix)
}
return &input
}
func (c *Configuration) FilterLogEventsInput() *cloudwatchlogs.FilterLogEventsInput {
input := cloudwatchlogs.FilterLogEventsInput{}
input.SetInterleaved(true)
input.SetLogGroupName(c.Group)
if len(c.Streams) != 0 {
input.SetLogStreamNames(c.TopStreamNames())
}
currentTime := time.Now()
absoluteStartTime := currentTime
if c.Start != "" {
st, err := getTime(c.Start, currentTime)
if err == nil {
absoluteStartTime = st
}
}
input.SetStartTime(aws.TimeUnixMilli(absoluteStartTime))
if c.End != "" {
et, err := getTime(c.End, currentTime)
if err == nil {
input.SetEndTime(aws.TimeUnixMilli(et))
}
}
if c.Filter != "" {
input.SetFilterPattern(c.Filter)
}
return &input
}
func (c *Configuration) TopStreamNames() []*string {
// FilerLogEvents can only take 100 streams so lets sort by LastEventTimestamp
// (descending) and take only the names of the most recent 100.
sort.Slice(c.Streams, func(i int, j int) bool {
return *c.Streams[i].LastEventTimestamp > *c.Streams[j].LastEventTimestamp
})
numStreams := 100
if len(c.Streams) < 100 {
numStreams = len(c.Streams)
}
streamNames := make([]*string, 0)
for _, stream := range c.Streams[:numStreams] {
streamNames = append(streamNames, stream.LogStreamName)
}
return streamNames
}