Leo Torres
Leo Torres

Reputation: 690

InfluxDB convert a difference query from InFluxQL to Flux

I am converting reports from InfluxQL to Flux I cant seem to be able to do this correctly for a difference query.

This is my difference query I need to convert

SELECT difference(last("value")) / ($__interval_ms/1000) FROM "sqlserver_performance" WHERE ("sql_instance" =~ /^$Instance$/ AND "counter" = 'Logouts/sec') AND $timeFilter GROUP BY time($__interval) fill(null)

Looking at docs only this below is how I understood it. The query does not fail but it returns NULL I would expect 0's at the very least which leads me to believe something just is not right with this query

from(bucket: "mssql")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "sqlserver_performance")
  |> filter(fn: (r) => r["sql_instance"] == "ServerName")
  |> filter(fn: (r) => r["_field"] == "value")
  |> filter(fn: (r) => r["counter"] == "Logouts/sec")
  |> map(fn: (r) => ({r with _value: r["Logouts/sec"]/1000}) )
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> yield(name: "mean")

I am trying to graph the logouts per second so i can graph it on a line graph.

Upvotes: 0

Views: 452

Answers (1)

Munin
Munin

Reputation: 1649

You could try following query:

from(bucket: "mssql")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "sqlserver_performance")
  |> filter(fn: (r) => r["sql_instance"] == "ServerName")
  |> filter(fn: (r) => r["counter"] == "Logouts/sec")
  |> map(fn: (r) => ({r with _value: r["Logouts/sec"]/1000}) )
  |> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)
  |> difference(initialZero: true)
  1. change the fn inside aggregateWindow to be the last according to https://docs.influxdata.com/flux/v0.x/stdlib/universe/aggregatewindow/#fn
  2. make use of difference and the initialZero parameter to make sure the output values are expected according to https://docs.influxdata.com/flux/v0.x/stdlib/universe/difference/#initialzero

Upvotes: 2

Related Questions