Translate a InfluxDB query to a Postgres query with variables in grafana

i have a query that is working well in Influx:

SELECT delta  FROM "measures"  WHERE  $timeFilter  AND ("operation_id" =~ /^$operationId$/) AND ("meter_id" =~ /^$meterId$/) GROUP BY meter_id

With variable operationId and meterId having “all” value

In postgres ( timescaleDB ), this is my equivalent:

SELECT time_bucket('30 minutes', time) AS "time",
  sum(delta), meter_id
FROM measures
WHERE
  $__timeFilter("time") AND
  operation_id = $operationId AND
  meter_id = $meterId AND
GROUP BY time, meter_id
ORDER BY time;

As I have a value each 30 min, it can be considerated as equivalent, even if I should improve this query.

Here, the graf is displaying, but when I set $meterId to “all” value, I will have an error in the query:

pq: syntax error at or near ","

Not very verbose.

I tried to use the same regex for postgres:

"operation_id" =~ /^$operationId$/

instead of

operation_id = $operationId

but it is not working.

Also, a weird thing that maybe not related to the issue, is that I have autocompletion for operationId variable but not for meterId variable.

Any idea how to translate the influx regex ? Thing is I don’t really understand this regex, so it makes me difficult translate it.

1 Like