-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
200 lines (190 loc) · 5.62 KB
/
app.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
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
const _ = require('lodash');
const async = require('async');
const es = require('elasticsearch');
const fs = require('fs');
const request = require('request');
const configs = require('./configs').configs;
configs.forEach(function(config) {
const esClient = new es.Client({
host: config.es.host,
requestTimeout: 60000
});
if (config.rest.contextFilename) {
fs.readFile(config.rest.contextFilename, function(readErr, data) {
let prevContext = null;
if (readErr) {
console.warn(`${config.es.index}: Unable to read context file, error: ${readErr}`);
} else {
try {
console.info(`${config.es.index}: Loaded previous context, ${data}`);
prevContext = JSON.parse(data);
} catch (parseErr) {
console.warn(`${config.es.index}: Unable to parse context file, error: ${parseErr}`);
}
}
rest2es(prevContext, esClient, config);
});
} else {
rest2es(null, esClient, config);
}
});
function rest2es(prevContext, esClient, config) {
const nextUrl = config.rest.next(prevContext);
if (nextUrl) {
http2es(nextUrl, esClient, config, function(err, newContext, numLoaded) {
let wait = 0;
let context = newContext;
if (err) {
console.warn(`${config.es.index}: Unable to pull data from ${nextUrl}. Error: ${err}. Sleeping before next poll`);
wait = config.rest.sleep;
context = prevContext;
} else if (numLoaded === 0) {
console.info(`${config.es.index}: Received no results on last poll attempt, sleeping before next poll`);
wait = config.rest.sleep;
context = prevContext;
} else if (newContext && config.rest.contextFilename) {
fs.writeFile(config.rest.contextFilename, JSON.stringify(newContext, null, ' '), function(writeErr) {
if (writeErr)
console.warn(`${config.es.index}: Unable to write context to file, error: ${writeErr}`);
});
}
setTimeout(
function() {
rest2es(context, esClient, config);
},
wait
);
});
} else {
console.info(`${config.es.index}: nextUrl returned null, sleeping before next poll`);
setTimeout(
function() {
rest2es(prevContext, esClient, config);
},
config.rest.sleep
);
}
}
/**
* Pull data from http endpoint
* tranform each document into ES document
* Track min/max values of watched parameters (context for next request)
* bulk load docs into ES
*/
function http2es(url, esClient, config, callback) {
let bulk = [];
const respContext = context_factory(config.rest.watchlist);
console.info(`${config.es.index}: Polling data from ${url}`);
request(url, function(requestErr, resp, body) {
if (requestErr) {
callback(requestErr);
return;
}
if (resp.statusCode !== 200) {
callback(resp.statusCode);
return;
}
let rows = [];
try {
rows = config.extractRows(JSON.parse(body));
console.info(`${config.es.index}: received ${rows.length} documents`);
} catch (parseErr) {
callback(`Unable to parse response body, error: ${parseErr}`);
return;
}
let count = 0;
async.forEachLimit(rows, 1,
function(item, forEachCallback) {
count++;
config.transform(item, function(esDoc) {
respContext.update(esDoc);
const insertCmd = {
index: {
_index : config.es.index,
_type: config.es.type
}
};
if (esDoc._id) {
insertCmd.index._id = esDoc._id;
delete esDoc._id;
}
bulk.push(insertCmd);
bulk.push(esDoc);
if (bulk.length >= 200) {
bulkLoad(config.es.index, esClient, bulk, function(bulkErr) {
bulk = [];
forEachCallback(bulkErr);
});
} else {
forEachCallback();
}
});
},
function(forEachErr) {
if (forEachErr) {
callback(forEachErr);
return;
}
bulkLoad(config.es.index, esClient, bulk, function(bulkErr) {
callback(bulkErr, respContext.getContext(), count);
});
}
);
});
}
function bulkLoad(indexName, esClient, bulk, callback) {
if (bulk.length === 0) {
callback();
return;
}
const numLoaded = bulk.length/2;
console.info(`${indexName}: Starting bulk load, num documents: ${numLoaded}`);
esClient.bulk(
{
body: bulk
},
function(esErr, resp) {
if (esErr) {
callback(`bulk load failed: ${esErr}`);
} else if (resp.errors) {
callback(`bulk load failed: ${JSON.stringify(resp.errors, null, '')}`);
} else {
console.info(`${indexName}: bulk load complete`);
callback();
}
}
);
}
function context_factory(watchlist) {
return {
context: {},
getContext: function() {
return this.context;
},
watchlist: watchlist,
update: function(item) {
const _this = this;
this.watchlist.forEach(function(key) {
if (_.has(item, key)) {
const newValue = _.get(item, key);
if (_.has(_this.context, key)) {
//update min and max as appropiate
const stored = _.get(_this.context, key);
if (newValue < stored.min) {
stored.min = newValue;
}
if (newValue > stored.max) {
stored.max = newValue;
}
} else {
//first time key seen - init min and max
_this.context[key] = {
min: newValue,
max: newValue
}
}
}
});
}
}
}