# Referencing time range and filter from Controller

**URL:** <https://discuss.elastic.co/t/referencing-time-range-and-filter-from-controller/57683>\
**Category:** Kibana\
**Created:** [August 10, 2016, 10:11am UTC](https://discuss.elastic.co/t/referencing-time-range-and-filter-from-controller/57683 "2016-08-10T10:11:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![YuWatanabe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuwatanabe/32/13259_2.png) [@YuWatanabe](https://discuss.elastic.co/u/YuWatanabe)\
**Post date:** [August 10, 2016, 10:11am UTC](https://discuss.elastic.co/t/referencing-time-range-and-filter-from-controller/57683/1 "2016-08-10T10:11:49Z")

</div>

Hi!

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

 ![](https://us1.discourse-cdn.com/elastic/original/2X/3/39d1444be7f95b3127ca0e99ef1c195f3f4595fe.PNG)

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

---

<div class="post-metadata">

**Author:** ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)\
**Post date:** [August 11, 2016, 2:59am UTC](https://discuss.elastic.co/t/referencing-time-range-and-filter-from-controller/57683/2 "2016-08-11T02:59:51Z")

</div>

Yes, for time range, use the timefilter service:

```auto
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/](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](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`:

```auto
$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](https://github.com/elastic/kibana/blob/master/src/ui/public/filter_bar/filter_bar.js#L103)

---

<div class="post-metadata">

**Author:** ![YuWatanabe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuwatanabe/32/13259_2.png) [@YuWatanabe](https://discuss.elastic.co/u/YuWatanabe)\
**Post date:** [August 12, 2016, 6:53am UTC](https://discuss.elastic.co/t/referencing-time-range-and-filter-from-controller/57683/3 "2016-08-12T06:53:13Z")

</div>

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.

 ![](https://us1.discourse-cdn.com/elastic/original/2X/2/2dc9afa2b7a932d46ff77ed02ab1e5a6aab3a42d.PNG)

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

 ![](https://us1.discourse-cdn.com/elastic/original/2X/4/4fd2c5cf544e09e671a6dbe62b6faed3fd372924.PNG)

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";
    }
  });

});
```

---

<div class="post-metadata">

**Author:** ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)\
**Post date:** [August 31, 2016, 6:47pm UTC](https://discuss.elastic.co/t/referencing-time-range-and-filter-from-controller/57683/4 "2016-08-31T18:47:37Z")

</div>

> [@YuWatanabe](#):
>
> However, the getAppState did not work as I expected.

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: [Kiwi IRC](https://webchat.freenode.net/#kibana) and one of the developers might be able to give better assistance.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 1:39pm UTC](https://discuss.elastic.co/t/referencing-time-range-and-filter-from-controller/57683/5 "2017-07-06T13:39:47Z")

</div>


