New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Make $interval available in milliseconds for math operations #6582

Closed
stefan-baranoff opened this Issue Nov 15, 2016 · 3 comments

Comments

Projects
None yet
2 participants
@stefan-baranoff

stefan-baranoff commented Nov 15, 2016

Feature Request:
We are currently using version 3.0.4 backed by InfluxDB running on CentOS 7.2. I would like to handle 'rate' data in a normalized manner. Specifically I'd like to execute a query with $interval set to 'auto' that normalizes data against that interval. Something like this:
SELECT sum(some_field_value)/($intervalInMS / 1000) FROM default WHERE ... GROUP BY time($interval), ...
If I set interval manually to a specific value (30s, 5m) I can manually enter that interval in the division: sum(some_field_value)/30 or sum(some_field_value)/300. That 'normalizes' the data to a 'per second' rate.

Use Case:
I am feeding the data into InfluxDB with statsd so I get both a raw 'count per interval' and a 'rate per second' value. I want rates (packets, bytes, clicks, events, etc.) aggregated across multiple tags (hosts, ports, uris, etc.) to give a total rate per second. E.g. a cluster of 4 web servers each with 4 network ports and I want the total data rate (bits / second) into the cluster.

If I take the mean of the raw rate I get the data I want but cannot then aggregate that further (say sum of packet rate across all ports in a network device or all hosts in a cluster) because InfluxDB does not support nested queries. If I take the sum of the 'count per interval' then I get a rate per interval but have no way to normalize that to a rate per second (or some other unit of my choice).


This is probably really an InfluxDB shortcoming but is easily worked around for simple use cases by exposing something like an $intervalMs variable in Grafana.

@stefan-baranoff

This comment has been minimized.

Show comment
Hide comment
@stefan-baranoff

stefan-baranoff Jan 4, 2017

We came up with another use case here - calculating rates with Elasticsearch source doing date histogram aggregation where what we really mean is "sum_per_date_bucket / $interval_per_date_bucket". Again using static intervals works but "auto" intervals doesn't.

stefan-baranoff commented Jan 4, 2017

We came up with another use case here - calculating rates with Elasticsearch source doing date histogram aggregation where what we really mean is "sum_per_date_bucket / $interval_per_date_bucket". Again using static intervals works but "auto" intervals doesn't.

@stefan-baranoff

This comment has been minimized.

Show comment
Hide comment
@stefan-baranoff

stefan-baranoff Jan 8, 2017

I believe this resolves the issue possibly but don't understand everything enough to be sure.

diff --git a/public/app/plugins/datasource/influxdb/datasource.ts b/public/app/plugins/datasource/influxdb/datasource.ts
index 28f1375..686b860 100644
--- a/public/app/plugins/datasource/influxdb/datasource.ts
+++ b/public/app/plugins/datasource/influxdb/datasource.ts
@@ -4,6 +4,7 @@ import angular from 'angular';
 import _ from 'lodash';
 
 import * as dateMath from 'app/core/utils/datemath';
+import kbn from 'app/core/utils/kbn';
 import InfluxSeries from './influx_series';
 import InfluxQuery from './influx_query';
 import ResponseParser from './response_parser';
@@ -58,6 +59,9 @@ export default class InfluxDatasource {
 
       // build query
       scopedVars.interval = {value: target.interval || options.interval};
+      if (scopedVars.interval !== null && scopedVars.interval !== undefined) {
+        scopedVars.intervalMS = {value: kbn.interval_to_ms(scopedVars.interval.value).toString()};
+      }
 
       queryModel = new InfluxQuery(target, this.templateSrv, scopedVars);
       return queryModel.render(true);

stefan-baranoff commented Jan 8, 2017

I believe this resolves the issue possibly but don't understand everything enough to be sure.

diff --git a/public/app/plugins/datasource/influxdb/datasource.ts b/public/app/plugins/datasource/influxdb/datasource.ts
index 28f1375..686b860 100644
--- a/public/app/plugins/datasource/influxdb/datasource.ts
+++ b/public/app/plugins/datasource/influxdb/datasource.ts
@@ -4,6 +4,7 @@ import angular from 'angular';
 import _ from 'lodash';
 
 import * as dateMath from 'app/core/utils/datemath';
+import kbn from 'app/core/utils/kbn';
 import InfluxSeries from './influx_series';
 import InfluxQuery from './influx_query';
 import ResponseParser from './response_parser';
@@ -58,6 +59,9 @@ export default class InfluxDatasource {
 
       // build query
       scopedVars.interval = {value: target.interval || options.interval};
+      if (scopedVars.interval !== null && scopedVars.interval !== undefined) {
+        scopedVars.intervalMS = {value: kbn.interval_to_ms(scopedVars.interval.value).toString()};
+      }
 
       queryModel = new InfluxQuery(target, this.templateSrv, scopedVars);
       return queryModel.render(true);

@torkelo

This comment has been minimized.

Show comment
Hide comment
@torkelo

torkelo Jan 11, 2017

Member

implemented in #7190

Member

torkelo commented Jan 11, 2017

implemented in #7190

@torkelo torkelo closed this Jan 11, 2017

@torkelo torkelo added this to the 4.2.0 milestone Jan 11, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment