Convert from InfluxQL to FLUX query (Please Help)

Hello All,

@ [Anaisdg]
Can someone please help me to convert my old InfluxQL query to Flux query?

These query is being used in Grafana-7.x and InfluxDB 2.x.

InfluQL Query : SELECT mean(“value”) FROM /container.ops.*/ WHERE “environment” = ‘$env’ AND “region” =~ /^(?i)$region$/ AND “container” =~ /(?i)$container/ AND $timeFilter GROUP BY time($__interval) fill(null)

I tried below, but it’s not working as intended and i may be way off.

from(bucket: “bucket_name”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == /container.ops.*/)
|> filter(fn: (r) => r[“container”] =~ /(?i)$container/)
|> filter(fn: (r) => r[“environment”] == “$env”)
|> filter(fn: (r) => r[“region”] =~ /^(?i)$region$/ )
|> group(columns: ["_time"])
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> yield(name: “mean”)

Kindly help me out converting these RegEx pattern properly.
Thanks for your time !

@Artificial_Brain Where is this query being executed? Grafana?

@scott Yes, it’s being used in Grafana-7.x. kindly help.

Hello @Artificial_Brain,
aggregateWindow() automatically groups data by time windows so you can calculate the mean over those windows.

where as

|> group(columns: ["_time"])

will group data on individual timestamps.

Can you try removing the group() function? Does that help you get what you’re looking for?

@Anaisdg thanks for your advice. But there’s a problem in RegEx. Hence query is not working properly.

Hello @Artificial_Brain,
It’s hard for me to understand how I can fix the regex without knowing what values you want to receive.
Are you aware of the strings package?
https://docs.influxdata.com/flux/v0.x/stdlib/strings/

You might find success using
https://docs.influxdata.com/flux/v0.x/stdlib/strings/containsstr/

@Artificial_Brain,
Can you describe what you’re trying to filter for with regex so I can help you craft the correct regex? I find using a regex tester like

helpful.
Thanks!

@Anaisdg Thank you for replying.

I don’t want to put below hard coded values, this making query running very slow and spacious too.

|> filter(fn: (r) => r["_measurement"] == “container.ops.data.issueRates” or r["_measurement"] == “container.ops.data.ratePersmissions” or r["_measurement"] == “container.ops.data.financialSteps” or r["_measurement"] == “container.ops.data.examineDepth” or r["_measurement"] == “container.ops.data.localPayment” or r["_measurement"] == “container.ops.data.globalWellness” …so on )

|> filter(fn: (r) => r[“container”] == “PO-PS-ALOCA_AIRIAN” or r[“container”] == “PO-PS-ALOCA_CQPRXN” or r[“container”] == “PO-PS-ALOCA_FBSELO” or r[“container”] == “PO-PS-ALOCA_JSBORKF” or r[“container”] == “PO-PS-ALOCA_HBWSHJI” or r[“container”] == “PO-PS-ALOCA_NREPXL”)

|> filter(fn: (r) => r[“environment”] == “BETA” or r[“environment”] == “DEV” or r[“environment”] == “PROD” or r[“environment”] == “QA”)

|> filter(fn: (r) => r[“region”] == “rams” or r[“region”] == “paca” or r[“region”] == “manoa” or r[“region”] == “lapea”)

Therefore trying to use regex.

Hello @Artificial_Brain,
Yes I understand why you need regex. Thanks for sharing your original query so we can find the right regex. Alternatively I suggest using the string package shared above instead of regex as it might be easier but here’s a regex solution.

|> filter(fn: (r) => r["_measurement"] =~ /^container.ops.data./)
|> filter(fn: (r) => r["container"] =~ /^PO-PS-ALOCA_/)
|> filter(fn: (r) => r[“environment”] == “BETA” or r[“environment”] == “DEV” or r[“environment”] == “PROD” or r[“environment”] == “QA”)
|> filter(fn: (r) => r[“region”] == “rams” or r[“region”] == “paca” or r[“region”] == “manoa” or r[“region”] == “lapea”)

The issue being ^(?i)$ matches an empty string, but the additional <string>$ prevents it from matching anything. I don’t understand why grafana is generating those regex.

1 Like

Are $container and $region variables in Grafana? If so, what values do they have?