Skip to content

LogQL Plugins

Lorenzo Mangani edited this page Oct 10, 2021 · 11 revisions

cLoki Plugins

  • WORK IN PROGRESS!

Missing a LogQL function in cLoki? Extend functionality in in no time using cLoki Plugins

Example

unwrap registry function

/**
 * @returns {{run: (function(*, number, number): *), approx: (function(*): number)}}
 */
module.exports.derivative = () => {
    return {
        /**
         *
         * @param sum {any} previous value for the current time bucket
         * @param val {number} current value
         * @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};
            sum.last = sum && sum.last && time < sum.last ? sum.last : {time: time, val: val};
            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;
        }
    }
}
Clone this wiki locally