plugins init

This commit is contained in:
akvlad
2021-10-19 16:18:25 +03:00
parent b243299df8
commit 62937c9787
7 changed files with 735 additions and 61 deletions

View File

@ -12,6 +12,8 @@ this.readonly = process.env.READONLY || false;
this.http_user = process.env.CLOKI_LOGIN || false;
this.http_pass = process.env.CLOKI_PASSWORD || false;
require("./plugins/engine");
var DATABASE = require("./lib/db/clickhouse");
var UTILS = require("./lib/utils");
const snappy = require("snappy");

672
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -40,7 +40,8 @@
"handlebars": "^4.7.7",
"handlebars-helpers": "^0.10.0",
"snappy": "^7.0.3",
"glob": "^7.2.0"
"glob": "^7.2.0",
"plugnplay": "^3.13.0"
},
"devDependencies": {
"jest": "^27.0.5",

46
plugins/engine.js Normal file
View File

@ -0,0 +1,46 @@
const { PluginManager } = require('plugnplay');
const manager = new PluginManager({
discovery: {
rootPath: `/home/hromozeka/QXIP/+(cLoki/unwrap_registry|test_plugin)`
}
});
const plugins = manager.discoverSync();
/**
*
* @type {Object<string, (int|Function)[][]>}
*/
const filters = {};
/**
*
* @param tag
* @param filter
* @param priority
*/
module.exports.addFilter = (tag, filter, priority) => {
filters[tag] = filters[tag] || [];
filters[tag].push([priority, filter]);
};
/**
*
* @param tag{string}
* @param init {any}
* @returns {any}
*/
module.exports.applyFilter = (tag, init) => {
const fls = filters[tag] || [];
fls.sort();
return fls.reduce((sum, f) => f[1](sum), init);
};
const api = {
addFilter: module.exports.addFilter,
useFilter: module.exports.applyFilter
}
for (const plg of plugins) {
manager.require(plg.id, api);
}

View File

@ -1,29 +1,42 @@
const {PluginLoaderBase} = require('plugnplay');
/**
* @returns {{run: (function(*, number, number): *), approx: (function(*): number)}}
*/
module.exports.derivative = () => {
return {
/**
*
* @param sum {any} previous value for the current time bucket
* @param val {{unwrapped: number}} current values
* @param time {number} timestamp in ms for the current value
* @returns {any}
*/
run: (sum, val, time) => {
sum = sum || {};
sum.first = sum && sum.first && time > sum.first.time ? sum.first : {time: time, val: val.unwrapped};
sum.last = sum && sum.last && time < sum.last ? sum.last : {time: time, val: val.unwrapped};
return sum;
},
/**
* @param sum {any} sum of the time bucket you have created during "run"
* @returns {number}
*/
approx: (sum) => {
return sum && sum.last && sum.first && sum.last.time > sum.first.time ?
(sum.last.val - sum.first.val) / (sum.last.time - sum.first.time) * 1000 : 0;
}
module.exports = class extends PluginLoaderBase {
exportSync(api) {
api.addFilter('unwrap_registry', (obj) => {
return {
...obj,
derivative: {
/**
*
* @param sum {any} previous value for the current time bucket
* @param val {{unwrapped: number}} current values
* @param time {number} timestamp in ms for the current value
* @returns {any}
*/
run: (sum, val, time) => {
sum = sum || {};
sum.first = sum && sum.first && time > sum.first.time ? sum.first : {
time: time,
val: val.unwrapped
};
sum.last = sum && sum.last && time < sum.last ? sum.last : {time: time, val: val.unwrapped};
return sum;
},
/**
* @param sum {any} sum of the time bucket you have created during "run"
* @returns {number}
*/
approx: (sum) => {
return sum && sum.last && sum.first && sum.last.time > sum.first.time ?
(sum.last.val - sum.first.val) / (sum.last.time - sum.first.time) * 1000 : 0;
}
}
}
}, 1);
}
}
}

View File

@ -0,0 +1,5 @@
# /path/to/plugin1/plugnplay.yml
id: plugin1
name: First Plugin
description: This is the first plugin to be tested.
loader: derivative.js # This is the file that contains the plugin loader.

View File

@ -31,4 +31,11 @@ it('should compile strings with escaped quotes', () => {
expect(res3.rootToken.Children('log_stream_selector_rule').map(c => c.value)).toEqual(
[ 'run=`kok\\\\\\`oko`', 'u_ru_ru!="lolol"', 'zozo=~"sssss"' ]
);
});
it('should glob', () => {
const glob = require('glob');
console.log(glob.sync('+(/home/hromozeka/QXIP/cLoki/plugins/unwrap_registry/**/plugnplay.yml|test_plugin/)'));
console.log(glob.sync('/home/hromozeka/QXIP/cLoki/plugins/unwrap_registry/**/plugnplay.yml'));
console.log(glob.sync('/home/hromozeka/QXIP/test_plugin/**/plugnplay.yml'));
});