datasweet / kibana-datasweet-formula

This Kibana plugin allows calculated metrics on any standard kibana visualizations.
Apache License 2.0
121 stars 34 forks source link

Using javascript function on aggregation date #9

Closed didinyah closed 6 years ago

didinyah commented 6 years ago

Hi,

I'm trying to get the working days between 2 dates.

I managed to get the number of days between 2 dates thank to datasweet with "(agg2 - agg1) / (86400*1000)"

but we lose the data of which day it is. Since we can't declare variables. The best thing to have would be something like this : msPerDay = 86400 * 1000; days = agg2 - agg1 / msPerDay; first_day = agg1.getDay(); second_day = agg2.getDay(); if(first_day - end_day > 1, days = days - 2, days = days -1); etc.. return days;

charlotth commented 6 years ago

Hi @didinyah, For now, Datasweet Formula can't execute a complete script. But, for your example, i think u can develop a new func and add it to the plugin. Regards

didinyah commented 6 years ago

Hi,

Thank for your answer, I'll try to do it this way then :)

Regards

didinyah commented 6 years ago

Mmmmh excuse me I'm trying to do the function but I have trouble debugging this... I created my function, added it to the index.js in the folder "formulas" but it doesn't work.

I tried to do some console.log in the function but it doesn't show in the console.. Same for the other function (max, if, etc...) Can you tell me how can I debug ?

charlotth commented 6 years ago

Hi ! You just have to put your function datediff.js into the folder kibana-datasweet-formula/public/formulas/. No need to ref the function into index.js. To start the dev mode, you need to follow the step from https://github.com/elastic/generator-kibana-plugin To build the plugin, you can have a look to the script kibana-datasweet-formula/build.sh

datediff.js

import { FormulaFunction } from './formula_function';
import { isArray, map } from 'lodash';

function datediff(d1, d2) {
   // Your logic here
}
export default new FormulaFunction('datediff', {
  help: 'Your description',
  fn: function (a, b) {   
    const ia = isArray(a);
    const ib = isArray(b);

    if (ia && ib) {
      const c = new Array();
      const len = Math.max(a.length, b.length);
      for (var i = 0; i < len; i++) {
        c.push(datediff(a[i] || 0, b[i] || 0));
      }
      return c;
    }

    if (ia) {
      return map(a, r => datediff(r, b));
    }

    if (ib) {
      return map(b, r => datediff(a, r));
    }

    return datediff(a, b);       
  }
});
didinyah commented 6 years ago

Hi, thank you for the skeleton and the answer.

So is it mandatory to rebuild after I have added the custom function ? By doing bash build.sh and remove the versions I'm not using ? I thought that the function would be already ready to use once written but thanks ! Moreover, I tried to follow the steps to see the log but with no results. It's well explained for the tests/unit tests but not just the log (also checking the /var/log/kibana.stdout but nothing appears)

Update : I didn't follow all the steps to set up the development environment, it's working fine with your build.sh now, and my custom function is working too. Thank for your time and this plugin again.