Referencing time range and filter from Controller


(Yu Watanabe) #1

Hi!

Is is possible to reference the time range and filter values in dashboard panel from the controller of custom plugin ?

I am planning to pass two values to the query for elasticsearch in my controller for custom plugin.


(Tim Sullivan) #2

Yes, for time range, use the timefilter service:

module.controller('PluginController', function (timefilter) {
  timefilter.getBounds().min; // {_isAMomentObject: true, _isUTC: false, _pf: Object, _locale: A, _d: Sat Jun 11 2016 19:28:57 GMT-0700 (MST)…}
  timefilter.getBounds().max; // {_isAMomentObject: true, _isUTC: false, _pf: Object, _locale: A, _d: Wed Aug 10 2016 19:29:01 GMT-0700 (MST)…}
});

max and min are moment objects, so check out http://momentjs.com/docs/

In the Kibana master branch, the service is here: https://github.com/elastic/kibana/blob/master/src/ui/public/timefilter/timefilter.js

As for the pinned filter values, it looks like there is no consumable module for it, but you could try doing similar to how the filter bar directive works, using getAppState:

$scope.state = getAppState();
...
$scope.$watch('state.$newFilters', function (filters) {
  // filters is an array of objects containing latest filters
});

See https://github.com/elastic/kibana/blob/master/src/ui/public/filter_bar/filter_bar.js#L103


(Yu Watanabe) #3

Hi @tsullivan

The timefilter service worked perfectly.

However, the getAppState did not work as I expected.

Below is a test I have done to see if I can detect the changes in the filters object

When I click the item in data table , the filters object is initiated and I could see number of filters correctly.

However, when I delete a filter using the trashcan icon it fails to get the number of filters.

What I expect is get number of filter on every change in the status bar.
Is there such watchExpression that I can use to detect the change?

Below is my code now.

module.controller('PluginController', function ($scope,timefilter,getAppState,Private) {
  // Example of getting time range
  $scope.min = timefilter.getBounds().min;
  $scope.max = timefilter.getBounds().max;

  $scope.state = getAppState();

  $scope.$watch('state.$newFilters', function (filters) {

    if(!filters) {
      $scope.status = "Number of filters specified in the status bar : 0";
    } else if(filters.length == 1 ) {
      $scope.status = "Number of filters specified in the status bar : 1";
    } else if(filters.length == 2 ) {
      $scope.status = "Number of filters specified in the status bar : 2";
    }
  });

});

(Tim Sullivan) #4

This makes some sense, as you are watching for changes to state.$newFilters. I am not sure if there is something else you should be watching, or even if there is a way to do what you need.

You could try asking in our IRC channel: https://webchat.freenode.net/#kibana and one of the developers might be able to give better assistance.


(system) #5