-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
164 lines (141 loc) · 3.77 KB
/
main.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/robfig/cron/v3"
)
var client http.Client
func main() {
client = http.Client{
Timeout: time.Second * 120,
}
c := cron.New()
c.AddJob(" */1 * * * *", customJob{})
c.Start()
for {
}
}
type BulkIngestRequest struct {
UUID string `json:"uuid"`
DocumentType string `json:"documentType"`
MergeConfig []Config `json:"merge"`
Docs []Document `json:"docs"`
}
type Document struct {
ID string `json:"id"`
Source interface{} `json:"source"`
}
type Config struct {
Mode string `json:"mode"`
ExistingAsMaster bool `json:"existingAsMaster"`
Type string `json:"type,omitempty"`
LinkKey string `json:"linkKey,omitempty"`
Groups []Group `json:"groups,omitempty"`
}
type Group struct {
Condition string `json:"condition,omitempty"`
FieldReplace []string `json:"fieldReplace,omitempty"`
FieldReplaceIfMissing []string `json:"fieldReplaceIfMissing,omitempty"`
FieldMerge []string `json:"fieldMerge,omitempty"`
FieldMath []FieldMath `json:"fieldMath,omitempty"`
FieldKeepLatest []string `json:"fieldKeepLatest,omitempty"`
FieldKeepEarliest []string `json:"fieldKeepEarliest,omitempty"`
}
// FieldMath specify a merge rule using a math expression
type FieldMath struct {
Expression string `json:"expression"`
OutputField string `json:"outputField"`
}
var stationIDs = map[string]string{"perpignan": "817", "narbonne": "1135", "montpellier": "1176"}
var url = "https://holfuy.com/puget/mjso.php?k="
var ingesterUrl = "http://localhost:9001/api/v1/ingester/data"
var pushObjectsUrl = "http://localhost:9000/api/v4/service/objects?fact="
type customJob struct{}
func (j customJob) Run() {
t := time.Now().Truncate(1 * time.Minute).UTC()
var docs []Document
for station, id := range stationIDs {
response, _ := http.Get(url + id)
responseData, _ := ioutil.ReadAll(response.Body)
var data map[string]interface{}
json.Unmarshal(responseData, &data)
delete(data, "valid")
delete(data, "updated")
data["datetime"] = t.Format("2006-01-02T15:04:05.000")
doc := Document{
ID: fmt.Sprintf("%s-%s", id, t.Format("2006-01-02")),
Source: data,
}
b, _ := json.Marshal([]Document{doc})
factName := "current_" + station
client.Post(pushObjectsUrl+factName, "application/json", bytes.NewBuffer(b))
docs = append(docs, doc)
}
bir := BulkIngestRequest{
UUID: "",
DocumentType: "measure",
MergeConfig: []Config{
{
Type: "measure",
Mode: "self",
ExistingAsMaster: true,
Groups: []Group{
{
Condition: "datemillis(New.datetime) > datemillis(Existing.datetime)",
FieldReplace: []string{
"speed",
"dir",
"dir_str",
"gust",
"temperature",
"wind_chill",
"avg_speed",
"max_gust",
"avg_dir",
"avg_dir_str",
"dir_rate",
"dir_tendency",
"speed_rate",
"speed_tendency",
"daily_avg_speed",
"daily_max_wind",
"daily_max_temp",
"daily_min_temp",
"datetime",
},
},
{
FieldReplaceIfMissing: []string{
"speed",
"dir",
"dir_str",
"gust",
"temperature",
"wind_chill",
"avg_speed",
"max_gust",
"avg_dir",
"avg_dir_str",
"dir_rate",
"dir_tendency",
"speed_rate",
"speed_tendency",
"daily_avg_speed",
"daily_max_wind",
"daily_max_temp",
"daily_min_temp",
"datetime",
},
},
},
},
},
Docs: docs,
}
b, _ := json.Marshal(bir)
client.Post(ingesterUrl, "application/json", bytes.NewBuffer(b))
}