-
Notifications
You must be signed in to change notification settings - Fork 2
/
EDDN.js
133 lines (110 loc) · 3.99 KB
/
EDDN.js
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
/*
* Copyright © 2018 Phelbore <[email protected]>
* This work is free. You can redistribute it and/or modify it under the
* terms of the Do What The Fuck You Want To Public License, Version 2,
* as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
*/
const Database = require('better-sqlite3');
const zlib = require('zlib');
const zmq = require('zeromq');
const sock = zmq.socket('sub');
const moment = require('moment');
const path = require('path');
const db = new Database('database/systems.db');
const util = require('util');
/* Handle signals for clean shutdowns
* List of signals
*/
var shutdown_signals = {
'SIGHUP': 1,
'SIGINT': 2,
'SIGTERM': 15
};
const shutdown = (signal, value) => {
console.log("Shutting down...");
// Perform necessary cleanup
sock.close();
db.close();
console.log("Exiting.");
process.exit(128 + value);
};
// Create a listener for each signal
Object.keys(shutdown_signals).forEach((signal) => {
process.on(signal, () => {
console.log(`process received "shutdown" ${signal} signal`);
shutdown(signal, shutdown_signals[signal]);
});
});
var msgStats = ['1', '5', '10', '15', '30', '60',
'120', '180', '240', '300', '360',
'420', '480', '540', '600'];
var lock = false;
const thanks = ['Garud',
'Lyrae Cursorius'];
config();
console.log('EDDN Listener started');
function config() {
sock.setsockopt(zmq.ZMQ_RCVHWM, 50);
eddnConnector();
}
function eddnConnector() {
sock.connect('tcp://eddn.edcd.io:9500');
sock.subscribe('');
sock.on('close', (fd, ep) => {setTimeout(eddnConnector(), 60000)});
sock.on('close_error', (fd, ep) => {setTimeout(eddnConnector(), 60000)});
sock.on('disconnect', (fd, ep) => {setTimeout(eddnConnector(), 60000)});
sock.on('message', topic=> {
try {
storeEntry(zlib.inflateSync(topic));
} catch (err) {}
});
}
function storeEntry(entry) {
let parsed = null;
try {
parsed = JSON.parse(entry);
} catch(err) {
console.log(`err`);
}
if(parsed && parsed.header.SoftwareName == 'EDDiscovery') {
return; // Banning EDDiscovery due to their cavalier attitude towards sending bad data.
// https://discord.com/channels/164411426939600896/419456725075099648/827919387443986442
}
if(parsed && parsed.$schemaRef == 'https://eddn.edcd.io/schemas/journal/1') {
let systemID = parsed.message.SystemAddress;
let systemName = parsed.message.StarSystem;
let systemX = parsed.message.StarPos[0];
let systemY = parsed.message.StarPos[1];
let systemZ = parsed.message.StarPos[2];
let factions = parsed.message.Factions;
if(systemID && systemName && factions && systemX && systemY && systemZ) {
let mTime = moment(parsed.message.timestamp);
let gwTime = moment(parsed.header.gatewayTimestamp);
let dTime = moment(gwTime).diff(mTime, 'seconds');
updateStats(dTime, parsed.header.softwareName, parsed.header.softwareVersion);
if(dTime < 6000 && parsed.message.timestamp && parsed.header.gatewayTimestamp) {
let sql = `INSERT INTO RAW (TIMESTAMP, GW_TIMESTAMP, SOFTWARE, VERSION, MESSAGE) VALUES(?, ?, ?, ?, ?)`;
db.prepare(sql).run(parsed.message.timestamp, parsed.header.gatewayTimestamp, parsed.header.softwareName, parsed.header.softwareVersion, entry);
}
}
}
}
function updateStats(dTime, name, version) {
let msgCountSql = `UPDATE MSG_STATS SET COUNT = COUNT + 1 WHERE ROWID = 1`;
let msgOldestSql = `UPDATE MSG_STATS SET OLDEST = ? WHERE ROWID = 1`;
let softwareCountSql = `INSERT INTO SOFTWARE(SOFTWARE, VERSION, COUNT) VALUES(?, ?, 1)
ON CONFLICT(SOFTWARE, VERSION) DO UPDATE SET COUNT=COUNT+1;`;
var oldest = db.prepare('SELECT OLDEST FROM MSG_STATS WHERE ROWID = 1').get().OLDEST;
db.prepare(softwareCountSql).run(name, version);
for (let i in msgStats) {
let delay = msgStats[i];
if(Math.abs(dTime) <= delay*60) {
let msgDelaySql = `UPDATE MSG_STATS SET '${delay}' = MSG_STATS.'${delay}' + 1 WHERE ROWID = 1`;
db.prepare(msgDelaySql).run();
}
}
db.prepare(msgCountSql).run();
if(dTime>oldest) {
db.prepare(msgOldestSql).run(dTime);
}
}