forked from EGF2/sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
164 lines (151 loc) · 4.69 KB
/
server.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
"use strict";
const config = require("./components").config;
const elastic = require("./components").elasticSearch;
const log = require("./components").logger;
const eventConsumer = require("commons/event-consumer");
const clientData = require("./components").clientData;
// prepare handlers
let handlers = require("./extra");
function addToHandlers(key, handler) {
if (key in handlers) {
let prev = handlers[key];
handlers[key] = event =>
Promise.all([
prev(event),
handler(event)
]);
} else {
handlers[key] = handler;
}
}
Object.keys(config.elastic.indices).forEach(index => {
let indexCfg = config.elastic.indices[index];
if (indexCfg.object_type) {
// add mapping for id field
if (!indexCfg.mapping.id) {
indexCfg.mapping.id = {
type: "string",
index: "not_analyzed"
};
}
// add POST handler
addToHandlers(`POST ${indexCfg.object_type}`, event => {
let body = {};
Object.keys(indexCfg.mapping).forEach(field => {
let selector = indexCfg.mapping[field].field_name || field;
body[field] = getValue(event.current, selector);
});
return elastic.index({
index: index,
type: index,
id: event.object,
body
});
});
// add PUT handler
addToHandlers(`PUT ${indexCfg.object_type}`, event => {
let doc = {};
Object.keys(indexCfg.mapping).forEach(field => {
if (event.current[field] !== event.previous[field]) {
let selector = indexCfg.mapping[field].field_name || field;
doc[field] = getValue(event.current, selector);
}
});
if (Object.keys(doc).length === 0) {
return Promise.resolve();
}
return elastic.update({
index: index,
type: index,
id: event.object,
body: {doc}
});
});
// add DELETE handler
addToHandlers(`DELETE ${indexCfg.object_type}`, event => {
return elastic.delete({
index: index,
type: index,
id: event.object
});
});
}
});
function getValue(obj, field) {
let value = obj;
field.split(".").forEach(field => {
value = value[field];
});
return value;
}
// create indices with mapping
function createIndex(index, mapping, settings) {
let properties = {};
Object.keys(mapping).forEach(field => {
properties[field] = Object.assign({}, mapping[field]);
delete properties[field].field_name;
});
return elastic.indices.exists({index: index}).then(exists => {
if (exists) {
return elastic.indices.putMapping({ // merge mapping
index: index,
type: index,
body: {
properties
}
});
}
let body = {
mappings: {
[index]: {
properties
}
}
};
if (settings) {
body.settings = {
analysis: settings
};
}
// create index
return elastic.indices.create({index, body});
});
}
// mapping indexes in ElasticSearch
function mappingIndexes() {
let promises = Promise.resolve();
Object.keys(config.elastic.indices).forEach(index => {
let indexCfg = config.elastic.indices[index];
promises = promises.then(() =>
createIndex(index, indexCfg.mapping, indexCfg.settings)
);
});
return promises.catch(err => {
log.fatal(err);
process.exit(1);
});
}
// handle events
function eventHandler(event) {
return Promise.resolve(event.method).then(method => {
if (event.object) {
if (event.current) {
return `${method} ${event.current.object_type}`;
}
return `${method} ${event.previous.object_type}`;
}
return Promise.all([
clientData.getObjectType(event.edge.src),
clientData.getObjectType(event.edge.dst)
]).then(types => `${method} ${types[0]}/${event.edge.name}/%{types[1]}`);
}).then(path => path in handlers ? handlers[path](event) : undefined);
}
function errorHandler(error) {
log.error(error);
}
function createServer() {
mappingIndexes().then(() => {
eventConsumer(config, eventHandler, errorHandler);
});
}
module.exports = createServer;