forked from gavinmcnair/jsontimestampfilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsontimestampfilter.go
66 lines (50 loc) · 1.05 KB
/
jsontimestampfilter.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"strings"
"time"
)
const ISO8601 = "2006-01-02T15:04Z"
type Msg struct {
Timestamp int64
}
func main() {
args := os.Args[1]
s := strings.Split(args, "/")
start, end := s[0], s[1]
startTime := convertDateToMillis(start)
endTime := convertDateToMillis(end)
fi, err := os.Stdin.Stat()
if err != nil {
panic(err)
}
if fi.Mode()&os.ModeNamedPipe == 0 {
fmt.Println("stdin is empty")
} else {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
ts := getTimeStampFromLine(line)
if (ts > startTime) && (ts < endTime) {
fmt.Println(line)
}
}
if err := scanner.Err(); err != nil {
fmt.Println("Error")
}
}
}
func convertDateToMillis(date string) int64 {
var formattedTime time.Time
formattedTime, _ = time.Parse(ISO8601, date)
nanotime := formattedTime.UnixNano()
return nanotime / 1000000
}
func getTimeStampFromLine(jsonInput string) int64 {
var msg Msg
json.Unmarshal([]byte(jsonInput), &msg)
return msg.Timestamp
}