Failing to understand Grafana's arithmetic

Hi.

I have an InfluxDB table which receives one new value inserted every second. The value is always either 0 or 1.

Therefore adding up an arbitrary number of these values should always produce either 0 or a positive integer.

I’ve set up a query to tell me the sum of these values over the past hour. Grafana’s query inspector shows me that it is doing:

q:“SELECT sum(“currentcalls”) FROM “mqtt_consumer” WHERE (“topic” = ‘PBX/Live’) AND time >= now() - 1h GROUP BY time(1h) fill(null)”

So, how is it possible for Grafana to tell me that the sum of these 3600 integer values is 1.3?

Thanks for any insight, or any pointers to where I’ve set up the query wrongly.

Antony.

InfluxDB is doing all the calculations here.

Okay, I’ll ask on that list, then.

Thanks,

Antony

Can you show raw query result, please? Which panel did you use?

[quote=“pooh, post:1, topic:16463”]
q:“SELECT sum(“currentcalls”) FROM “mqtt_consumer” WHERE (“topic” =
‘PBX/Live’) AND time >= now() - 1h GROUP BY time(1h) fill(null)” [/quote]

Can you show raw query result, please?

By “raw query result”, do you mean I should run this query in the InfluxDB
command line client, or you want me to do something in Grafana (if so, what)?

Which panel did you use?

This query is in a singlestat panel.

Antony.

I think he probably means to select the query in the editor then click Query Inspector. Make sure the refresh rate is low or it will run it again before you have time to look at the results.

[quote=“pooh, post:1, topic:16463”]
q:“SELECT sum(“currentcalls”) FROM “mqtt_consumer” WHERE (“topic” =
‘PBX/Live’) AND time >= now() - 1h GROUP BY time(1h) fill(null)” [/quote]

Can you show raw query result, please?

By “raw query result”, do you mean I should run this query in the InfluxDB
command line client, or you want me to do something in Grafana (if so,
what)?

Right, I’ve just had the opportunity where Grafan shows me “1.5”.

Someone on the Influx list asked me to run the following query in the Influx
command line client:

SELECT currentcalls FROM “mqtt_consumer” WHERE “currentcalls” != 0 and
“currentcalls” !=1 and time >= now() - 1h limit 5

This results in nothing, therefore they are saying it’s a Grafana problem
(which makes good sense to me, given that this query tells me the database
contains nothing but 0 and 1 values, therefore summing them cannot possibly
result in “1.5”).

Which panel did you use?

This query is in a singlestat panel.

Also, based on Clanlaw’s suggestion of looking at the Query Inspector, I get
the following:

xhrStatus:“complete”
request:Object
method:“GET”
url:“api/datasources/proxy/1/query”
params:Object
db:“telegraf”
q:“SELECT sum(“totalcalls”) FROM “mqtt_consumer” WHERE (“topic” = ‘PBX/Live’)
AND time >= now() - 1h GROUP BY time(1h) fill(null)”
epoch:“ms”
data:null
precision:“ms”
response:Object
results:Array[1]
0:Object
statement_id:0
series:Array[1]
0:Object
name:“mqtt_consumer”
columns:Array[2]
0:“time”
1:“sum”
values:Array[2]
0:Array[1555509600000,2]
1:Array[1555513200000,1]

So, what next :slight_smile: ?

Antony.

Why do you need time grouping GROUP BY time(1h) fill(null), when you want just single sum value? You have two records in the query result :

0:Array[1555509600000,2]
1:Array[1555513200000,1]

+ you singlestat is configured to calculate avg value (see Stat config) (2+1)/2=1.5

The best solution - calculate full sum on the query level -> only single value must be returned by the query. Alternative solution: configure summing (Stat: Total) in the singlestat configuration, so it will sum those two records.

Why do you need time grouping GROUP BY time(1h) fill(null), when you want
just single sum value? You have two records in the query result : ```
0:Array[1555509600000,2]
1:Array[1555513200000,1]

Simple answer - I don’t know - that’s the query which Grafana created for me
when I said I wanted the sum of the previous 1 hour’s values.

+ you singlestat is configured to calculate avg value (see Stat config)
(2+1)/2=1.5

All I can say is that it is not at all obvious from the query I have created
that an average will be calculated. The only aggregation function I can see
is “sum(totalcalls)”

The best solution - calculate full sum on the query level -> only single
value must be returned by the query.

So, what should I change in the query I have set up to achieve that?

Are you saying that I should simply remove the “GROUP BY” section of the
query, which then results in the Query Inspector then showing me:

SELECT “totalcalls” FROM “mqtt_consumer” WHERE (“topic” = ‘PBX/Live’) AND
$timeFilter

?

Alternative solution: configure summing (Stat: Total) in the singlestat
configuration, so it will sum those two records.

That sounds like a workaround - if there’s a better solution I’d prefer to use
it.

Antony.

I’m not saying that. I’m saying remove time grouping GROUP BY time(1h) fill(null) -> you may need to update query somehow then, so it may not be so simple just deleting ‘GROUP BY time(1h) fill(null)’. Please don’t expect that I provide ready query for you - please read InfluxDB manual and develop it on your own.

Are you saying that I should simply remove the “GROUP BY” section of the
query, which then results in the Query Inspector then showing me:

SELECT “totalcalls” FROM “mqtt_consumer” WHERE (“topic” = ‘PBX/Live’) AND
$timeFilter

Urgh.

It turns out that deleting the “GROUP BY” section also quietly eliminated my
“sum()” function, but it turns out that I can put that back in again without
getting the GROUP back as well. The Query Inspector now shows me:

q:“SELECT sum(“totalcalls”) FROM “mqtt_consumer” WHERE (“topic” =
‘Tikvox/Live’) AND time >= now() - 1h”

And so far this is showing me the answer I would expect.

Alternative solution: configure summing (Stat: Total) in the
singlestat configuration, so it will sum those two records.

That sounds like a workaround - if there’s a better solution I’d prefer to
use it.

Antony.

I don’t seem to be having a problem with InfluxDB.

Any manual queries I perform in the Influx client give me the results I expect.

My problem seems to be finding out how to get Grafana to create the correct
query without subtly turning it into something I didn’t intend or request
(such as an average, as you pointed out previously).

However, for the time being, the following seems to be doing what I want:

q:“SELECT sum(“totalcalls”) FROM “mqtt_consumer” WHERE (“topic” =
‘PBX/Live’) AND time >= now() - 1h”

Thanks for the suggestions,

Antony.

Switch to RAW query mode and you can write any query directly - even subqueries.

Agreed.

I just thought that what I was trying to do wasn’t so obscure that the
standard Grafana query editor would let me down.

Ho hum.

Antony.

1 Like