-
Notifications
You must be signed in to change notification settings - Fork 3
/
record.go
68 lines (53 loc) · 1.34 KB
/
record.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
package bigquery
import (
"time"
"cloud.google.com/go/bigquery"
"storj.io/eventkit/pb"
)
var _ bigquery.ValueSaver = &Record{}
type Record struct {
Application Application
Source Source
ReceivedAt time.Time
Timestamp time.Time
Correction time.Duration
Tags []*pb.Tag
}
type Application struct {
Name string
Version string
}
type Source struct {
Instance string
Address string
}
func (r *Record) Save() (map[string]bigquery.Value, string, error) {
fields := make(map[string]bigquery.Value)
fields["application_name"] = r.Application.Name
fields["application_version"] = r.Application.Version
fields["source_instance"] = r.Source.Instance
fields["source_ip"] = r.Source.Address
fields["received_at"] = r.ReceivedAt
fields["timestamp"] = r.Timestamp
fields["correction"] = r.Correction
for _, tag := range r.Tags {
field := tagFieldName(tag.Key)
switch v := tag.Value.(type) {
case *pb.Tag_Bool:
fields[field] = v.Bool
case *pb.Tag_Bytes:
fields[field] = v.Bytes
case *pb.Tag_Double:
fields[field] = v.Double
case *pb.Tag_DurationNs:
fields[field] = v.DurationNs
case *pb.Tag_Int64:
fields[field] = v.Int64
case *pb.Tag_String_:
fields[field] = string(v.String_)
case *pb.Tag_Timestamp:
fields[field] = time.Unix(v.Timestamp.Seconds, int64(v.Timestamp.Nanos))
}
}
return fields, "", nil
}