Count values for percentage

I want to create a panel in grafana but I am not able to get the exact data even from Influxdb itself.
I have some jenkins pipelines and I want first to count them all and then the ones that are successful in order to have a percentage of how successful was the whole pipeline. I have managed to get the 2 results in 2 queries but I cannot do the division afterward.

from(bucket: "influxdb")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "jenkins_data")
  |> filter(fn: (r) => r["_field"] =~ /own_tags|build_result_ordinal/)
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> group(columns: ["_measurement"])
  |> filter(fn: (r) => r.own_tags =~ /bla.*foo/ and r.build_result_ordinal == 0)
  |> count(column: "build_result_ordinal")

from(bucket: "influxdb")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "jenkins_data")
  |> filter(fn: (r) => r["_field"] =~ /own_tags|build_result_ordinal/)
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> group(columns: ["_measurement"])
  |> filter(fn: (r) => r.own_tags =~ /bla.*foo/)
  |> count(column: "build_result_ordinal")

I have put them also in 2 variables but doing
x.build_result_ordinal/z.build_result_ordinal * 100
does not yield any results.

I figured it out myself.
For the next person

r0 = from(bucket: "influxdb")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "jenkins_data")
  |> filter(fn: (r) => r["_field"] =~ /own_tags|build_result_ordinal/)
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> group(columns: ["_measurement"])
  |> filter(fn: (r) => r.own_tags =~ /bla.*foo/)
  |> count(column: "build_result_ordinal")

r1 = from(bucket: "influxdb")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "jenkins_data")
  |> filter(fn: (r) => r["_field"] =~ /own_tags|build_result_ordinal/)
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> group(columns: ["_measurement"])
  |> filter(fn: (r) => r.own_tags =~ /bla.*foo/ and r.build_result_ordinal == 0)
  |> count(column: "build_result_ordinal")
  |> rename(columns: {build_result_ordinal: "build_result_ordinal_0"})

join(tables: {r0:r0, r1:r1}, on:["_measurement"])
  |> map(fn: (r) => ({_measurement: r._measurement, _value: float(v: r.build_result_ordinal_0)/float(v: r.build_result_ordinal) * 100.0 }))

Also, I discovered that there is an error in the documentation of Flux on github:
At basic syntax here the map on join tables has some errors in the parenthesis

Hello @gkoloven,
Thanks for sharing your solution!!

1 Like