Skip to content

Commit 2dc3a05

Browse files
Birger SchachtWagner
authored andcommitted
ENH: adapt the manager for the new API
IntelMQ changed the way it handles the configuration of bots, queues and default values. IntelMQ-API followed this change and now only provides endpoints for the runtime and the position. This commit implements this change in the javascript code and also updates various functions to reflect other minor changes (i.e. the bot id is now called bot_id instead of id).
1 parent f8c0326 commit 2dc3a05

6 files changed

Lines changed: 111 additions & 130 deletions

File tree

intelmq_manager/static/js/config.js

Whitespace-only changes.

intelmq_manager/static/js/configs.js

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ function load_html_elements() {
8787

8888
function load_bots(config) {
8989
// Build side menu
90-
console.log(config);
9190
for (let bot_group of Object.keys(config).reverse()) {
9291
let $bot_group = $("#templates > ul.side-menu > li").clone().prependTo("#side-menu").css("border-bottom-color", GROUP_COLORS[bot_group][0]);
9392
$bot_group.find("> a").prepend(bot_group);
@@ -184,10 +183,11 @@ function load_bots(config) {
184183

185184
function fill_editDefault(data) {
186185
table.innerHTML = '';
187-
186+
insertBorder(BORDER_TYPES.DEFAULT);
188187
for (let key in data) {
189-
insertKeyValue(key, data[key], 'defaultConfig', false);
188+
insertKeyValue(key, data[key], BORDER_TYPES.DEFAULT, true);
190189
}
190+
191191
// to enable scroll bar
192192
popup.setAttribute('class', "with-bot");
193193
}
@@ -250,29 +250,18 @@ function save_data_on_files() {
250250
}
251251

252252
Promise.all([
253-
authenticatedAjax({"type": "POST", "url": API + '/save?file=runtime', "data": generate_runtime_conf(app.nodes)})
253+
authenticatedAjax({"type": "POST", "url": API + '/runtime', "contentType": "application/json", "data": generate_runtime_conf(app.nodes, app.defaults)})
254254
.done(saveSucceeded)
255255
.fail(() => {
256256
alert_error('runtime', ...arguments)
257257
}),
258-
authenticatedAjax({"type": "POST", "url": API + '/save?file=pipeline', "data": generate_pipeline_conf(app.edges)})
259-
.done(saveSucceeded)
260-
.fail(() => {
261-
alert_error('pipeline', ...arguments)
262-
}),
263-
authenticatedAjax({"type": "POST", "url": API + '/save?file=positions', "data": generate_positions_conf()})
258+
authenticatedAjax({"type": "POST", "url": API + '/positions', "contentType": "application/json", "data": generate_positions_conf()})
264259
.done(saveSucceeded)
265260
.fail(() => {
266261
alert_error('positions', ...arguments)
267-
}),
268-
authenticatedAjax({"type": "POST", "url": API + '/save?file=defaults', "data": generate_defaults_conf(app.defaults)})
269-
.done(saveSucceeded)
270-
.fail(() => {
271-
alert_error('defaults', ...arguments)
272-
}),])
262+
})])
273263
.then(function () {
274264
// all files were correctly saved
275-
app.nodes = add_defaults_to_nodes(app.nodes, app.defaults);
276265
$saveButton.unblinking();
277266
});
278267
}
@@ -312,8 +301,8 @@ function convert_nodes(nodes, includePositions) {
312301

313302
for (index in nodes) {
314303
var new_node = {};
315-
new_node.id = nodes[index]['id'];
316-
new_node.label = nodes[index]['id'];
304+
new_node.id = nodes[index]['bot_id'];
305+
new_node.label = nodes[index]['bot_id'];
317306
new_node.group = nodes[index]['group'];
318307

319308
if (includePositions === true) {
@@ -359,7 +348,7 @@ function fill_bot(id, group, name) {
359348

360349
app.bot_before_altering = bot;
361350

362-
insertKeyValue('id', bot['id'], 'id', false);
351+
insertKeyValue('id', bot['bot_id'], 'id', false);
363352
insertBorder(BORDER_TYPES.GENERIC);
364353
for (let key in bot) {
365354
if (STARTUP_KEYS.includes(key)) {
@@ -370,10 +359,6 @@ function fill_bot(id, group, name) {
370359
for (let key in bot.parameters) {
371360
insertKeyValue(key, bot.parameters[key], BORDER_TYPES.RUNTIME, true);
372361
}
373-
insertBorder(BORDER_TYPES.DEFAULT);
374-
for (let key in bot.defaults) {
375-
insertKeyValue(key, bot.defaults[key], BORDER_TYPES.DEFAULT, false);
376-
}
377362

378363
const periodpos = bot['module'].lastIndexOf('.');
379364
modulename = bot['module'].substring(0, periodpos).replace(/\./g, "-").replace(/_/g, "-");
@@ -403,6 +388,8 @@ function insertBorder(border_type) {
403388
break;
404389
case BORDER_TYPES.DEFAULT:
405390
new_row.setAttribute('class', BORDER_TYPE_CLASSES.DEFAULT);
391+
$(addButtonCell).append($("#templates > .new-key-btn").clone().click(addNewDefaultKey));
392+
new_row.setAttribute('id', border_type);
406393
break;
407394
default:
408395
new_row.setAttribute('class', BORDER_TYPE_CLASSES.OTHERS);
@@ -441,25 +428,14 @@ function insertKeyValue(key, value, section, allowXButtons, insertAt) {
441428
if (allowXButtons === true) {
442429
var xButton = document.createElement('button');
443430
var xButtonSpan = document.createElement('span');
444-
if (key in app.defaults) {
445-
xButtonSpan.setAttribute('class', 'glyphicon glyphicon-refresh');
446-
xButton.setAttribute('class', 'btn btn-default');
447-
xButton.setAttribute('title', 'reset to default');
448-
xButton.addEventListener('click', function (resetToDefault, key) {
449-
return function () {
450-
parameter_func(resetToDefault, key)
451-
}
452-
}(resetToDefault, key))
453-
} else {
454-
xButtonSpan.setAttribute('class', 'glyphicon glyphicon-remove-circle');
455-
xButton.setAttribute('class', 'btn btn-danger');
456-
xButton.setAttribute('title', 'delete parameter');
457-
xButton.addEventListener('click', function (deleteParameter, key) {
458-
return function () {
459-
parameter_func(deleteParameter, key)
460-
}
461-
}(deleteParameter, key))
462-
}
431+
xButtonSpan.setAttribute('class', 'glyphicon glyphicon-remove-circle');
432+
xButton.setAttribute('class', 'btn btn-danger');
433+
xButton.setAttribute('title', 'delete parameter');
434+
xButton.addEventListener('click', function (deleteParameter, key) {
435+
return function () {
436+
parameter_func(deleteParameter, key)
437+
}
438+
}(deleteParameter, key))
463439

464440
xButton.appendChild(xButtonSpan);
465441
xButtonCell.appendChild(xButton);
@@ -471,7 +447,9 @@ function insertKeyValue(key, value, section, allowXButtons, insertAt) {
471447
if (value !== null && typeof value === "object") {
472448
value = JSON.stringify(value);
473449
}
474-
valueInput.setAttribute('value', value);
450+
if (value !== null) {
451+
valueInput.setAttribute('value', value);
452+
}
475453
}
476454

477455
function resetToDefault(input_id) {
@@ -504,6 +482,28 @@ function addNewKey() {
504482
}
505483
});
506484
}
485+
// same as above, with another border type
486+
function addNewDefaultKey() {
487+
let $el = $("#templates .modal-add-new-key").clone();
488+
popupModal("Add key", $el, () => {
489+
var current_index = $('#' + BORDER_TYPES.RUNTIME).index();
490+
var $key = $el.find("[name=newKeyInput]");
491+
var val = $el.find("[name=newValueInput]").val();
492+
493+
if (!PARAM_KEY_REGEX.test($key.val())) {
494+
show_error("Parameter names can only be composed of numbers, letters, hiphens and underscores");
495+
$key.focus();
496+
return false;
497+
} else {
498+
// inserts new value and focus the field
499+
insertKeyValue($key.val(), val, BORDER_TYPES.DEFAULT, true, current_index + 1);
500+
// a bootstrap guru or somebody might want to rewrite this line without setTimeout
501+
setTimeout(() => {
502+
$('#network-popUp .new-key-btn').closest("tr").next("tr").find("input").focus()
503+
}, 300);
504+
}
505+
});
506+
}
507507

508508
$(document).keydown(function (event) {
509509
if (event.keyCode === 27) {
@@ -551,7 +551,7 @@ function saveFormData() {
551551

552552
switch (keyCell.id) {
553553
case 'id':
554-
node[key] = value;
554+
node['bot_id'] = value;
555555
break;
556556
case 'generic':
557557
node[key] = value;
@@ -561,7 +561,7 @@ function saveFormData() {
561561
break;
562562
case 'border':
563563
break;
564-
case 'defaultConfig':
564+
case 'default':
565565
app.defaults[key] = value;
566566
break;
567567
default:
@@ -578,18 +578,18 @@ function saveData(data, callback) {
578578
saveFormData();
579579

580580
// check inputs beeing valid
581-
if (node.id == '' && node.group == '') {
581+
if (node.bot_id == '' && node.group == '') {
582582
show_error('fields id and group must not be empty!');
583583
return;
584584
}
585585

586-
if (node.id != app.bot_before_altering.id) {
586+
if (node.bot_id != app.bot_before_altering.bot_id) {
587587
if (!confirm("When you edit an ID what you are doing in fact is to create a clone of the current bot. You will have to delete the old one manually. Proceed with the operation?")) {
588588
return;
589589
}
590590
}
591591

592-
if (!BOT_ID_REGEX.test(node.id)) {
592+
if (!BOT_ID_REGEX.test(node.bot_id)) {
593593
show_error("Bot ID's can only be composed of numbers, letters and hiphens");
594594
return;
595595
}
@@ -616,13 +616,13 @@ function saveData(data, callback) {
616616
}
617617
}
618618

619-
data.id = node.id;
620-
data.label = node.id
619+
data.bot_id = node.bot_id;
620+
data.label = node.bot_id
621621
data.group = node.group;
622622
data.level = GROUP_LEVELS[data.group];
623623
data.title = JSON.stringify(node, undefined, 2).replace(/\n/g, '\n<br>').replace(/ /g, "&nbsp;");
624624

625-
app.nodes[node.id] = node;
625+
app.nodes[node.bot_id] = node;
626626

627627
$saveButton.blinking();
628628
clearPopUp(data, callback);

intelmq_manager/static/js/defaults.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ function generate_defaults_conf(defaults) {
88
}
99

1010
function read_defaults_conf(config) {
11+
global = {};
1112

12-
for (key in config) {
13+
for (key in config['global']) {
1314
try {
14-
config[key] = JSON.parse(config[key]);
15+
global[key] = JSON.parse(config['global'][key]);
1516
} catch (err) {
16-
config[key] = config[key];
17+
global[key] = config['global'][key];
1718
}
1819
}
1920

20-
return config
21+
return global
2122
}
2223

2324
function remove_defaults(nodes) {
@@ -27,22 +28,3 @@ function remove_defaults(nodes) {
2728

2829
return nodes;
2930
}
30-
31-
function add_defaults_to_nodes(nodes, defaultConfig) {
32-
var defaults = 'defaults';
33-
for (id in nodes) {
34-
var node=nodes[id];
35-
node.id = id;
36-
node.defaults = {};
37-
38-
for (key in defaultConfig) {
39-
if (key in node.parameters) {
40-
continue;
41-
} else {
42-
node.defaults[key] = defaultConfig[key];
43-
}
44-
}
45-
}
46-
47-
return nodes;
48-
}

intelmq_manager/static/js/pipeline.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,33 +108,35 @@ function generate_pipeline_conf(edges) {
108108
return JSON.stringify(new_edges, undefined, 4);
109109
}
110110

111-
function read_pipeline_conf(config, nodes) {
111+
function read_pipeline_conf(config) {
112112
var edges = {};
113113
var i = 0;
114114

115115
for (let from in config) {
116-
if (config[from]['destination-queues'] !== undefined) {
117-
let queues = (new DestinationQueues(config[from]['destination-queues'])).getFullForm();
118-
for (let path in queues) {
119-
for (let queue of queues[path]) {
120-
var to_node = queue.replace(/-queue$/, "");
121-
if (nodes[from] !== undefined && nodes[to_node] !== undefined) {
122-
var edge_id = 'edge' + i++;
123-
var new_edge = {
124-
'id': edge_id,
125-
'from': from,
126-
'to': to_node
127-
};
128-
if (path !== "_default") {
129-
new_edge["path"] = path;
116+
if (config[from]['parameters'] != undefined) {
117+
if (config[from]['parameters']['destination_queues'] !== undefined) {
118+
let queues = (new DestinationQueues(config[from]['parameters']['destination_queues'])).getFullForm();
119+
for (let path in queues) {
120+
for (let queue of queues[path]) {
121+
var to_node = queue.replace(/-queue$/, "");
122+
if (config[from] !== undefined && config[to_node] !== undefined) {
123+
var edge_id = 'edge' + i++;
124+
var new_edge = {
125+
'id': edge_id,
126+
'from': from,
127+
'to': to_node
128+
};
129+
if (path !== "_default") {
130+
new_edge["path"] = path;
131+
}
132+
133+
edges[edge_id] = new_edge;
130134
}
131-
132-
edges[edge_id] = new_edge;
133135
}
134136
}
135137
}
136-
}
138+
}
137139
}
138140

139141
return edges;
140-
}
142+
}

0 commit comments

Comments
 (0)