-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
297 lines (241 loc) · 6.93 KB
/
index.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
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
var argv = require('minimist')(process.argv.slice(2));
var config = require('histograph-config');
var schemas = require('histograph-schemas');
var fuzzyDates = require('fuzzy-dates');
var Graphmalizer = require('graphmalizer-core');
var H = require('highland');
var Redis = require('redis');
var redisClient = Redis.createClient(config.redis.port, config.redis.host);
var u = require('util');
var defaultMapping = require('./default-mapping')
// Convert any ID, URI, URN to Histograph URN
var normalize = require('histograph-uri-normalizer').normalize;
var elasticsearch = require('elasticsearch');
var esClient = new elasticsearch.Client({
host: config.elasticsearch.host + ':' + config.elasticsearch.port
});
// Create a stream from Redis queue
var redis = H(function redisGenerator(push, next) {
// Function called on each Redis message (or timeout)
function redisCallback(err, data) {
// handle error
if (err) {
push(err);
next();
return;
}
// attempt parse or error
try {
var d = JSON.parse(data[1]);
push(null, d);
next();
}
catch (e) {
push(e);
next();
}
}
// blocking pull from Redis
redisClient.blpop(config.redis.queue, 0, redisCallback);
});
var ACTION_MAP = {
add: 'add',
update: 'add',
delete: 'remove'
};
function getUnixTime(date) {
return new Date(date).getTime() / 1000;
}
function toGraphmalizer(msg) {
function norm(x) {
if (x) {
return normalize(x, msg.dataset);
}
return undefined;
}
var d = msg.data || {};
// dataset is a top-level attribute that we want copied into the 'data' attribute
d.dataset = msg.dataset;
// Parse fuzzy dates to arrays using fuzzy-dates module
if (d.validSince) {
d.validSince = fuzzyDates.convert(d.validSince);
// Add timestamp
// TODO: find more structured way to add extra values/fields
// to Graphmalizer (and Neo4j afterwards) - API needs to remove
// those fields later
d.validSinceTimestamp = getUnixTime(d.validSince[0]);
}
if (d.validUntil) {
d.validUntil = fuzzyDates.convert(d.validUntil);
// Add timestamp
d.validUntilTimestamp = getUnixTime(d.validUntil[1]);
}
return {
operation: ACTION_MAP[msg.action],
dataset: d.dataset,
type: d.type,
// nodes are identified with id's or URI's, we don't care
id: norm(d.id || d.uri),
// formalize source/target id's
source: norm(d.from),
target: norm(d.to),
data: stringifyObjectFields(d)
};
}
// when passed an object, every field that contains an object
// is converted into a JSON-string (*mutates in place*)
function stringifyObjectFields(obj) {
// convert objects to JSONified strings
var d = JSON.parse(JSON.stringify(obj));
if (typeof (d) === 'object') {
Object.keys(d).forEach(function(k) {
var v = d[k];
if (v.constructor === Object) {
d[k] = JSON.stringify(v);
}
});
}
return d;
}
// Index into elasticsearch
var OP_MAP = {
add: "index",
remove: "delete"
};
// index documents into elasticsearch
function toElastic(data) {
// select appropriate ES operation
var operation = OP_MAP[data.operation];
// replace string version with original
if (data.data && data.data.geometry) {
try {
data.data.geometry = JSON.parse(data.data.geometry) ;
} catch (_) {
}
}
// { "action": { _index ... } , see
// https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference-2-0.html
var actionDesc = {};
actionDesc[operation] = {
_index: data.dataset,
_type: data.type,
_id: data.id
};
var thing = [actionDesc];
// when removing no document is needed
if(data.operation === "add")
thing.push(data.data);
return thing;
}
function logError(err) {
console.error(err.stack || err);
}
var commands = redis
.errors(logError)
.map(toGraphmalizer);
var neo4jAuth;
if (config.neo4j.user && config.neo4j.password) {
neo4jAuth = config.neo4j.user + ':' + config.neo4j.password;
}
var gconf = {
types: schemas.graphmalizer,
Neo4J: {
hostname: config.neo4j.host,
port: config.neo4j.port,
auth: neo4jAuth
},
batchSize: argv.batchSize || config.core.batchSize,
batchTimeout: argv.batchTimeout || config.core.batchTimeout
};
var graphmalizer = new Graphmalizer(gconf);
// create named index
var createIndex = H.wrapCallback(esClient.indices.create.bind(esClient.indices))
// find all indices in ES bulk request
function collectIndices(bulk_request)
{
return H(bulk_request)
// we only create indices for `index` events (not deletes)
.filter(H.get('index'))
// take out the elasticsearch index
.map(H.get('index'))
.map(H.get('_index'))
// restrict to unique items
.uniq()
}
function batchIntoElasticsearch(err, x, push, next){
if (err) {
// pass errors along the stream and consume next value
push(err);
next();
return;
}
if (x === H.nil) {
// pass nil (end event) along the stream,
push(null, H.nil);
// dont call next() on finished stream
return;
}
// Create indices, then bulk index
collectIndices(x)
.map(function(indexName){
// turn name into options for `esClient.indices.create`
return {
index: indexName,
body: defaultMapping
}
})
.map(createIndex)
.series()
.errors(function(err){
if(err && /^IndexAlreadyExistsException/.test(err.message)) {
console.log("Index already exists", err);
} else {
console.log("Failed creating index")
console.error(err, err && err.message)
}
})
// collect all results
.collect()
.each(function(results){
// tell it
console.log("Created all indices", results)
// bulk index index into elasticsearch
esClient.bulk({body: x}, function(err, resp){
// ES oopsed, we send the error downstream
if(err) {
push(err);
next();
return;
}
// all went fine, send the respons downstream
push(null, H([resp]));
next();
})
});
}
function flatten(arrays) {
return [].concat.apply([], arrays);
}
graphmalizer.register(commands)
// we only index PiTs into elasticsearch, not relations.
.map(function(d){
return d.request.parameters
})
.filter(function(d){
return d.structure === 'node';
})
// first batch them up
.batchWithTimeOrCount(argv.batchTimeout || config.core.batchTimeout, argv.batchSize || config.core.batchSize)
// convert batch into batch of ES commands
.map(function(pits) {
// turn batch into a list of form [action, doc, action, doc, ...]
return flatten(pits.map(toElastic));
})
.consume(batchIntoElasticsearch)
.series()
.errors(logError)
.each(function(resp) {
var r = resp || {took: 0, errors:false, items: []}
console.log("ES => %d indexed, took %dms, errors: %s", r.items.length, r.took, r.errors)
});
console.log(config.logo.join('\n'));