/
auto_collect.go
70 lines (59 loc) · 1.79 KB
/
auto_collect.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
package metrics
import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
"github.com/machinefi/w3bstream/pkg/models"
"github.com/machinefi/w3bstream/pkg/types"
)
var autoCollectCli = NewSQLBatcher("INSERT INTO ws_metrics.auto_collect_metrics VALUES")
func GeoCollect(ctx context.Context, data []byte) {
var (
l = types.MustLoggerFromContext(ctx)
project = types.MustProjectFromContext(ctx)
eventID = types.MustEventIDFromContext(ctx)
dataStr = string(data)
rawMap = make(map[string]interface{})
noLat = false
noLong = false
)
publisher, ok := types.PublisherFromContext(ctx)
if !ok {
publisher = &models.Publisher{
PublisherInfo: models.PublisherInfo{Key: "unknown"},
}
}
// get lat or latitude key from data
switch {
case gjson.Get(dataStr, "lat").Exists():
rawMap["latitude"] = gjson.Get(dataStr, "lat").Float()
case gjson.Get(dataStr, "latitude").Exists():
rawMap["latitude"] = gjson.Get(dataStr, "latitude").Float()
default:
l.WithValues("eid", eventID).Warn(errors.New("there is no lat info"))
noLat = true
}
// get long or longitude key from data
switch {
case gjson.Get(dataStr, "long").Exists():
rawMap["longitude"] = gjson.Get(dataStr, "long").Float()
case gjson.Get(dataStr, "longitude").Exists():
rawMap["longitude"] = gjson.Get(dataStr, "longitude").Float()
default:
l.WithValues("eid", eventID).Warn(errors.New("there is no long info"))
noLong = true
}
if noLat || noLong {
return
}
rawData, err := json.Marshal(rawMap)
if err != nil {
l.WithValues("eid", eventID).Error(err)
}
if err := autoCollectCli.Insert(fmt.Sprintf(`now(), '%s', '%s', '%s',
'%s'`, project.AccountID.String(), project.Name, publisher.Key, string(rawData))); err != nil {
l.WithValues("eid", eventID).Error(err)
}
}