-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimestampModule.go
119 lines (102 loc) · 2.39 KB
/
TimestampModule.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
package main
import (
"os"
"strconv"
"time"
)
type TimestampModule struct {
}
func NewTimestampModule() *TimestampModule {
return new(TimestampModule)
}
func (t TimestampModule) Name() string {
return "Timestamp"
}
func (t TimestampModule) Description() string {
return "Displays timestamps"
}
func (t TimestampModule) CanBeDisabled() bool {
return true
}
func (t TimestampModule) UpdateSettings() {
// this intentionally empty
}
func (t TimestampModule) NeedsExternalData() bool {
return false
}
func (t TimestampModule) UpdateExternalData() {
// this intentionally empty
}
func (t TimestampModule) WriteExternalData(_ *os.File) {
// this intentionally empty
}
type TimestampAction struct {
tstype string
value string
}
func (t TimestampAction) GetLabel() string {
return "[timestamp[] " + t.tstype + " " + t.value
}
func (t TimestampAction) Run() string {
return t.tstype + " timestamp: " + t.value
}
func msToTime(ms string) (time.Time, error) {
msInt, err := strconv.ParseInt(ms, 10, 64)
if err != nil {
return time.Time{}, err
}
return time.Unix(0, msInt*int64(time.Millisecond)), nil
}
func (t TimestampModule) CreateActions(tags []Tag) []action {
var actions []action
for _, tag := range tags {
if len(tag.value) == 10 {
val, err := strconv.ParseInt(tag.value, 10, 64)
if err == nil {
tsUnix := time.Unix(val, 0)
actions = append(actions, TimestampAction{
tstype: "UNIX",
value: tsUnix.Format(time.RFC3339),
})
}
}
if len(tag.value) == 13 {
val, err := msToTime(tag.value)
if err == nil {
actions = append(actions, TimestampAction{
tstype: "JAVA",
value: val.Format(time.RFC3339),
})
}
}
}
now := time.Now()
tsUnix := now.Unix()
tsJava := now.UnixNano() / 1e6
strs := []string{"timestamp", "ts", "unix"}
if DoMatch(strs, tags) {
actions = append(actions, TimestampAction{
tstype: "UNIX",
value: strconv.FormatInt(tsUnix, 10),
})
}
strs = []string{"timestamp", "ts", "java"}
if DoMatch(strs, tags) {
actions = append(actions, TimestampAction{
tstype: "JAVA",
value: strconv.FormatInt(tsJava, 10),
})
}
strs = []string{"timestamp", "ts"}
if DoMatch(strs, tags) {
actions = append(actions, TimestampAction{
tstype: "date-time",
value: now.Format("20060102150405"),
})
}
return actions
}
func (t TimestampModule) ReadExternalData(_ []byte) error {
// this intentionally empty
return nil
}