InfluxDb Continuous Query that excludes zeros - influxdb

I'm trying to create a continuous query in InfluxDb to downsample the measurement data to the hourly mean values. I can do that with the continuous query below.
CREATE CONTINUOUS QUERY "cq_test_1h" ON "db-name"
BEGIN
SELECT mean("value") AS "mean_value"
INTO "downsampled"."downsampled_measurement"
FROM "autogen"."measurement"
GROUP BY time(1h)
END
But I also want that if the hourly mean equals zero, the result is excluded; so the downsampled_measurement series does not contain any zero values. I can make a (nested) query that does what I want, but I don't know how to make this into a continuous query.
SELECT mean_value
FROM
(SELECT mean(value) AS mean_value
FROM "measurement"
WHERE time<now()
GROUP BY time(1h))
WHERE mean_value>0
The query above works, but to make it a continuous query it needs an aggregator, GROUP BY clause and a duration argument in the WHERE clause:
SELECT mean(mean_value)
FROM
(SELECT mean(value) AS mean_value FROM "db-name"."autogen"."measurement"
WHERE time<now()
GROUP BY time(1h))
WHERE mean_value>0 AND time<now()
GROUP BY time(1h)
However, this query no longer returns any values. How can I make a continuous query that excludes zeros?

Related

Combining LAST and Cumulative SUM on influxdb subquery data

I have some telemetry data that looks like this:
> select * from connected_users LIMIT 100
name: connected_users
time event_id value
---- -------- -----
1605485019101319658 13759 2
1605485299566004832 13759 0
1605490011182967926 13760 4
1605490171409428188 13760 0
1605490207031398071 13760 7
1605490246151204709 13760 0
1605491054403726308 13761 1
1605491403050521520 13761 0
1605491433407347399 13762 2
1605491527773331816 13762 3
1605492020976088377 12884 1
1605492219827002782 13761 1
1605492613984911844 13763 1
1605492806683323942 13763 0
...
These writes only occur when something changes on the event (i.e. are not at fixed intervals). I want to write a query which will give me a cumulative sum per-minute of the current "value" on all the event_ids. However, because I can't guarantee that a new data value will have been written in the preceding 60 seconds, I use LAST to get whatever was last set per event_id
So far I got to:
SELECT SUM(*) FROM
(SELECT LAST("value") FROM
"connected_users" WHERE
time <= now() AND
time >= now() - 3h
GROUP BY time(1m), "event_id" fill(previous))
GROUP BY time(1m)
But this seems to give me a much lower outer "value" than expected, and a lot of duplicate time entries (and thus a lot of duplicate entries in the output data.)
I can see that the inner query is correct, because If I just run that in isolation, I can stack the output data in Grafana and manually see the correct total value in the graph. However, I want to have a single series rather than a grouped set of series, and I can't wrap my head around how to transform the data to do that.
EDIT: To give more context:
This is the inner query (Grafana, hence $timeFilter):
SELECT last("value") FROM "connected_users" WHERE $timeFilter GROUP BY time(1m), "event_id" fill(previous)
This produces a chart which I can stack and is correct:
If I then wrap that inner query in a SUM and GROUP BY time(1m) again, I can isolate a single series:
SELECT SUM(*) FROM (SELECT last("value") FROM "connected_users" WHERE $timeFilter AND ("event_id = '9970') GROUP BY time(1m), "event_id" fill(previous)) GROUP BY time(1m)
However, If I remove the AND and attempt to SUM all series values, I just end up with both a jumbled mess (presumably because there are duplicate/overlapping time values?) and also a lower max value than expected (expecting 18, got 8):
SELECT SUM(*) FROM (SELECT last("value") FROM "connected_users" WHERE $timeFilter GROUP BY time(1m), "event_id" fill(previous)) GROUP BY time(1m)

Apply a SUM function on the product of two fields in InfluxDB

I have the following query:
SELECT sum("field1" * "field2") FROM "my_db"."autogen"."data" GROUP BY time(1d) FILL(null)
In short I would like to perform the operation sum on the product of two fields field and field2.
The above query returns an error: expected field argument in sum().
Is this kind of thing at all possible in InfluxDB?
Here's a idea: try Sub Query
Note:I don't have editor right now so it might give error too
SELECT SUM(Multiplication) FROM
(SELECT "field1" * "field2" as Multiplication, time(1d) as Days FROM
"my_db"."autogen"."data" GROUP BY time(1d) FILL(null)
) GROUP BY Days

Subtract two count values with different where clause in influxDb

I have a measurement in influxDb with two keys: operation and count. The operation key can store two different values: 'add' and 'delete'.
I want to subtract the sum(count) value when operation='delete' to sum(count) value when operation='add'.
The following query is supported in mysql but it throws and error in influxql:
select (select sum(count) from measurement where operation='add') - (select sum(count) from measurement where operation='delete');
How can this be done using a single influxql query ? I don't think influxql allows two different where clauses in this case.
InfluxQL doesn't support this kind of multiquery math. You will need to calculate it on the app level.

InfluxDB Query to fetch count of distinct values for Grafana

I have a collector which collects three fields from a log file and saves it to influxDB in following format:
FeildA FeildB FeildC
------- -------- --------
A 00 123B 02 100A 00 13A 00 123
I want to plot graph in Grafana such that I get count of occurrence of "A" and "B" (FeildA)
IMP: FeildA can have multiple values, not known before-hand. Hence writing query with "where" clause is not an option.
If FeildA is only defined as field in measurement schema you can use regexp in "where" clause and these queries might work for you:
```
SELECT COUNT(FeildA) FROM "logdata" WHERE $timeFilter and FeildA::field =~ /^A$/
SELECT COUNT(FeildA) FROM "logdata" WHERE $timeFilter and FeildA::field =~ /^A$/
SELECT COUNT(FeildA) FROM "logdata" WHERE $timeFilter and FeildA =~ /^(A|B)$/
```
If the number of expected distinct values of FeildA (cardinality) is resonable the real solution would be to make FeildA a "Tag" instead of "Field". Then you can use "group by tag" in query. For example, query:
```
SELECT COUNT(FeildA) FROM "logdata" WHERE $timeFilter AND "FeildA" =~ /^(A|B|C|D)$/ GROUP BY time(1m), FeildA fill(null)
```
will give counts of occurrence of "A","B","C","D". But this require changes in collector.
FeildA can be both a "tag" and a "field" in influxdb but it is better when names are different to avoid collision and simplify syntax in queries.

Influx + Grafana aggregate / add 2 sets of data?

Is it possible to form a query that will add the results of these 2 queries together?
SELECT "output1" FROM "output1"
SELECT "output2" FROM "output2"
It is to display a Singlestat value in Grafana.
Thanks
Unfortunately there isn't a way to join data across two measurements.
As a work around you could use a continuos query to write data into the same measurement like so
CREATE CONTINUOUS QUERY join_output1_output2 ON mydb BEGIN
SELECT "output2" INTO "output1" FROM "output2"
END
Then the query
SELECT "output1" + "output2" FROM "output1"

Resources