-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscanner.js
More file actions
192 lines (169 loc) · 5.52 KB
/
Copy pathscanner.js
File metadata and controls
192 lines (169 loc) · 5.52 KB
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
const net = require('net');
const fs = require('fs');
const os = require('os');
const readline = require('readline');
const CryptoCollector = require('./crypto_collector');
"use strict";
const Analyser = function() {
const TEN_MINUTES = 600 * 1000;
const HOUR = 6 * TEN_MINUTES;
const DAY = 24 * HOUR / TEN_MINUTES;
const data = {
low: new Map(),
last_lowest_times: new Map(), //for preventing one-by-one alerting of a single event
last_time: null
};
this.getData = function () {
return data;
};
this.restoreData = function(import_data) {
for(let i in import_data) {
if(!import_data.hasOwnProperty(i)) {
continue;
}
data[i] = Array.isArray(import_data[i]) ? new Map(import_data[i]) : import_data[i];
}
};
this.push = function(value, time) {
data.last_time = Math.floor(time / TEN_MINUTES);
let lowest = data.low.get(data.last_time);
if(lowest === undefined || lowest > value) {
data.low.set(data.last_time, value);
}
};
this.findLastMin = function() {
let min_key = data.last_time;
let now = min_key;
let times = new Map([[DAY * 3, null], [DAY * 2, null], [DAY, null], [DAY * 0.5, null]]);
for(let [key, val] of data.low.entries()) {
for(let [time_i, cur_min] of times) {
if(now - key <= time_i) {
if(cur_min === null || cur_min[1] > val) {
times.set(time_i, [key, val]);
}
}
}
}
//@todo move to a separated method
//preparing for sending
let result = [];
for(let [key, val] of times.entries()) {
let last_min = data.last_lowest_times.get(key);
if(val[0] === min_key
&& (last_min === undefined || last_min[0] !== min_key)) {
result.push(key / DAY);
}
}
data.last_lowest_times = times;
return result;
};
this.clean = function() {
let lowest_time = data.last_time - 3 * DAY;
for(let key of data.low.keys()) {
if(key < lowest_time) {
data.delete(key);
}
}
};
};
const PriceCollector = {
backup_file: __dirname + '/backup_data',
data: {},
socket_client: null,
push: function(market, pair, price) {
const timestamp = Date.now();
this._initStructure(market, pair);
this.data[market][pair].push(price, timestamp);
//@todo make by timer
if(this.socket_client !== null) {
let last_find = this.data[market][pair].findLastMin();
if(last_find.length !== 0) {
let data = JSON.stringify([market, pair, price, timestamp, last_find]) + os.EOL;
this.socket_client.write(data);
}
}
},
_initStructure: function(market, pair) {
if(!this.data[market]) {
this.data[market] = {};
}
if(!this.data[market][pair]) {
this.data[market][pair] = new Analyser();
setInterval(() => {
this.data[market][pair].clean();
}, 60000);
}
},
extract: function(callback) {
let wstream = fs.createWriteStream(this.backup_file, {flags: 'w'});
for (let i in this.data) {
for (let j in this.data[i]) {
wstream.write(JSON.stringify([i, j, JSON.stringify(this.data[i][j].getData(), (key, val) => {
if (typeof val === "object" && val !== null
&& val.__proto__.toString() === "[object Map]") {
return Array.from(val);
}
return val;
})]) + os.EOL);
}
}
wstream.end(() => {
if(callback && typeof callback === 'function') {
callback();
}
});
},
restore: function() {
fs.stat(this.backup_file, err => {
if (err) {
return;
}
const read_stream = fs.createReadStream(this.backup_file);
const rl = readline.createInterface({
input: read_stream
});
rl.on('line', (line) => {
let obj = JSON.parse(line);
this._initStructure(obj[0], obj[1]);
this.data[obj[0]][obj[1]].restoreData(JSON.parse(obj[2]));
});
});
}
};
const Scanner = function() {
let collector = new CryptoCollector();
collector.on('ticker', (data) => {
PriceCollector.push(data['exchange'], data['label'], data['price']);
});
collector.run();
};
PriceCollector.restore();
const socket_path = __dirname + '/pipe.sock';
fs.stat(socket_path, err => {
if (!err) {
fs.unlinkSync(socket_path);
}
const unix_server = net.createServer(client => {
console.log('unix socket server');
client.on('close', () => {
PriceCollector.socket_client = null;
});
PriceCollector.socket_client = client;
});
unix_server.listen(socket_path);
unix_server.on('error', err => {
console.log(err);
}).on('close', () => {
PriceCollector.socket_client = null;
})
});
Scanner();
process.on('SIGINT', () => {
PriceCollector.extract(() => {
process.exit();
});
});
process.on('uncaughtException', err => {
console.log(err);
PriceCollector.extract();
});