Hey,
I recently started using influxDB 2.0.8 with flux (we are planning migration from MYSQL to influx) integrated with grafana. I want to replicate something like this in flux (__timeGroupAlias
is a grafana function for grouping data into intervals in MYSQL):
SELECT
$__timeGroupAlias(date, ${Interval}),
MEAN(mem_fragmentation_ratio) AS "ALIAS_1",
MEAN(hit_rate) AS "ALIAS_2",
SUM(keyspace_hits_diff) / (SUM(keyspace_misses_diff) + SUM(keyspace_hits_diff)) AS "ALIAS_3"
FROM MY_TABLE
GROUP BY 1
But I don’t know how, I tried using pivot (it is not ideal as i need to calculate MEAN of MEANs instead of MEAN of SUMs, but it would be something to start with):
common = from(bucket: "redis_stats")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "redis_info")
|> filter(fn: (r) => r["role"] == "cluster")
|> filter(fn: (r) => r["clusterName"] == "mobile_cluster")
|> drop(columns: ["clusterName", "role"])
new_columns = common
|> filter(fn: (r) => r["_field"] == "keyspace_hits_diff" or r["_field"] == "keyspace_misses_diff")
|> pivot(
rowKey:["_time"],
columnKey: ["_field"],
valueColumn: "_value"
)
|> map(fn: (r) => ({r with
_value: (float(v: r.keyspace_hits_diff) / float(v: (r.keyspace_hits_diff + r.keyspace_misses_diff))),
_field: "current_hit_ratio"
})
)
|> drop(columns: ["keyspace_hits_diff", "keyspace_misses_diff"])
old_columns = common
|> filter(fn: (r) => r["_field"] == "mem_fragmentation_ratio" or r["_field"] == "hit_rate")
union(tables: [new_columns, old_columns])
|> aggregateWindow(every: 10s, fn: mean, createEmpty: true)
|> yield()
But this does not work:
In data Explorer _field is null instead of “current_hit_ratio”. (Btw there is also a bug with _value column values are shown in millions while they are floats of 0.74 and 0.53).
In Grafana there is no value bug, but name is set to redis_info insted of desired “current_hit_ratio”.
I am guessing that empty field may be because for table new_columns
column “_field” does not belong to group key of that table. But I don’t know how to deal with that.
Is there a way to fix my approach and is there a better one. I can only think of custom aggregation function, but I would need to read more into that in order to judge, whether it’s a viable approach.
Also I don’t know how to alias fields in flux, but that is less important.