-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
318 lines (277 loc) · 7.12 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package main
import (
"bufio"
"context"
"encoding/json"
"github.com/NavExplorer/navexplorer-api-go/elasticsearch"
"github.com/NavExplorer/node-indexer/config"
"github.com/fsnotify/fsnotify"
"github.com/olivere/elastic"
"log"
"os"
"regexp"
"strconv"
"strings"
"time"
)
func waitUntilFind(filename string) error {
for {
time.Sleep(1 * time.Second)
_, err := os.Stat(filename)
if err != nil {
if os.IsNotExist(err) {
continue
} else {
return err
}
}
break
}
return nil
}
func main() {
log.Println("Starting node indexer")
filename := config.Get().SeedFile
// parse the file on first load
parse()
err := waitUntilFind(filename)
if err != nil {
log.Fatalln(err)
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalln(err)
}
defer watcher.Close()
err = watcher.Add(filename)
if err != nil {
log.Fatalln(err)
}
renameCh := make(chan bool)
removeCh := make(chan bool)
errCh := make(chan error)
go func() {
for {
select {
case event := <-watcher.Events:
switch {
case event.Op&fsnotify.Write == fsnotify.Write:
log.Printf("Write: %s: %s", event.Op, event.Name)
parse()
case event.Op&fsnotify.Create == fsnotify.Create:
log.Printf("Create: %s: %s", event.Op, event.Name)
case event.Op&fsnotify.Rename == fsnotify.Rename:
log.Printf("Rename: %s: %s", event.Op, event.Name)
parse()
renameCh <- true
}
case err := <-watcher.Errors:
errCh <- err
}
}
}()
go func() {
for {
select {
case <-renameCh:
err = waitUntilFind(filename)
if err != nil {
log.Fatalln(err)
}
err = watcher.Add(filename)
if err != nil {
log.Fatalln(err)
}
case <-removeCh:
err = waitUntilFind(filename)
if err != nil {
log.Fatalln(err)
}
err = watcher.Add(filename)
if err != nil {
log.Fatalln(err)
}
}
}
}()
log.Fatalln(<-errCh)
}
func parse() {
log.Printf("Parsing file: %s", config.Get().SeedFile)
file, err := os.Open(config.Get().SeedFile)
if err != nil {
log.Println("Failed to locate dump")
return
}
defer file.Close()
var nodes []Node
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
log.Printf("Working on line: %s\n", line)
words := strings.Fields(line)
if words[0] != "#" && len(words) == 12 {
log.Printf("Loading Node: %s", words[0])
var node Node
node.Address = words[0]
node.Good = words[1] == "1"
i, err := strconv.ParseInt(words[2], 10, 64)
if err != nil {
log.Fatal(err)
}
node.LastSuccess = time.Unix(i, 0)
percentReg, err := regexp.Compile("[^0-9.]+")
if err != nil {
log.Fatal(err)
}
percent2h, err := strconv.ParseFloat(percentReg.ReplaceAllString(words[3], ""), 64)
if err != nil {
log.Fatal(err)
}
node.Percent2h = percent2h
percent8h, err := strconv.ParseFloat(percentReg.ReplaceAllString(words[4], ""), 64)
if err != nil {
log.Fatal(err)
}
node.Percent8h = percent8h
percent1d, err := strconv.ParseFloat(percentReg.ReplaceAllString(words[5], ""), 64)
if err != nil {
log.Fatal(err)
}
node.Percent1d = percent1d
percent7d, err := strconv.ParseFloat(percentReg.ReplaceAllString(words[6], ""), 64)
if err != nil {
log.Fatal(err)
}
node.Percent7d = percent7d
percent30d, err := strconv.ParseFloat(percentReg.ReplaceAllString(words[7], ""), 64)
if err != nil {
log.Fatal(err)
}
node.Percent30d = percent30d
node.Blocks, err = strconv.ParseInt(words[8], 10, 64)
node.Svcs = words[9]
node.Version = words[10]
node.UserAgent = strings.Trim(words[11], "\"/")
userAgentVersion, err := regexp.Compile("[^0-9.]+")
if err != nil {
log.Fatal(err)
}
node.UserAgentVersion = userAgentVersion.ReplaceAllString(node.UserAgent, "")
nodes = append(nodes, node)
} else {
log.Printf("Found %d words on line", len(words))
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
if len(nodes) == 0 {
log.Println("Didnt find any nodes...")
return
}
log.Printf("Found %d nodes", len(nodes))
IndexNodes(nodes)
}
func IndexNodes(nodes []Node) {
ctx := context.Background()
client, err := elasticsearch.NewClient()
if err != nil {
log.Fatal(err)
}
now := time.Now().UTC().Truncate(time.Second)
from := time.Date(now.Year(), now.Month(), now.Day()-1, now.Hour(), now.Minute(), 0, 0, now.Location())
results, err := client.Search("mainnet.nodes").
Size(10000).
Query(elastic.NewRangeQuery("lastSeen").Gt(from)).
Do(ctx)
if err != nil {
log.Fatal("FATAL: ", err)
}
var knownNodes []Node
log.Printf("LOG: Found %d known nodes", results.Hits.TotalHits)
for _, hit := range results.Hits.Hits {
var knownNode Node
err := json.Unmarshal(*hit.Source, &knownNode)
if err == nil {
knownNodes = append(knownNodes, knownNode)
}
}
for _, node := range knownNodes {
node.Stale = isNodeNew(node, nodes)
}
bulkRequest := client.Bulk()
for _, node := range nodes {
var req elastic.BulkableRequest
node.LastSeen = now
if isNodeKnown(node, knownNodes) {
log.Printf("LOG: Updating node %s\n", node.Address)
req = elastic.NewBulkUpdateRequest().Index("mainnet.nodes").Type("_doc").Id(node.Address).Doc(node)
} else {
log.Printf("LOG: Inserting node %s\n", node.Address)
req = elastic.NewBulkIndexRequest().Index("mainnet.nodes").Type("_doc").Id(node.Address).Doc(node)
}
bulkRequest = bulkRequest.Add(req)
}
bulkResponse, err := bulkRequest.Do(ctx)
if err != nil {
log.Println("LOG: ", err)
}
if bulkResponse.Errors {
log.Fatal("FATAL", "Error performing bulk insert")
}
}
func isNodeKnown(node Node, knownNodes []Node) bool {
for _, v := range knownNodes {
if v.Address == node.Address {
return true
}
}
return false
}
func isNodeNew(node Node, newNodes []Node) bool {
for _, v := range newNodes {
if v.Address == node.Address {
return true
}
}
return false
}
//func cleanIndex() {
// ctx := context.Background()
//
// client, err := elasticsearch.NewClient()
// if err != nil {
// log.Fatal(err)
// }
//
// mapping, err := ioutil.ReadFile("node.json")
// if err != nil {
// log.Fatal(err)
// }
// deleteIndex, _ := client.DeleteIndex("mainnet.nodes").Do(ctx)
// if deleteIndex != nil && deleteIndex.Acknowledged {
// log.Println("Deleted index")
// }
// createIndex, err := client.CreateIndex("mainnet.nodes").BodyString(string(mapping)).Do(ctx)
// if !createIndex.Acknowledged {
// log.Fatal("FATAL", "Failed to create temp index")
// }
//}
type Node struct {
Address string `json:"address"`
Good bool `json:"good"`
LastSuccess time.Time `json:"lastSuccess"`
LastSeen time.Time `json:"lastSeen"`
Percent2h float64 `json:"percent2h"`
Percent8h float64 `json:"percent8h"`
Percent1d float64 `json:"percent1d"`
Percent7d float64 `json:"percent7d"`
Percent30d float64 `json:"percent30d"`
Blocks int64 `json:"blocks"`
Svcs string `json:"svcs"`
Version string `json:"version"`
UserAgent string `json:"userAgent"`
UserAgentVersion string `json:"userAgentVersion"`
Stale bool `json:"stale"`
}