Sum values based on Timestamp for InfluxDB (Flux)根据时间戳汇总数值。

huangapple go评论106阅读模式
英文:

Sum values based on Timestamp for InfluxDb (Flux)

问题

I have data for executions running on different machines(Agents). The data is like:

_time AgentName RunningSessions
001 Agent1 1
002 Agent1 4
003 Agent1 10
004 Agent1 12
005 Agent1 15
001 Agent2 1
002 Agent2 5
003 Agent2 8
004 Agent2 10
005 Agent2 15

For the above data, my plot is like this (showing two colors for different Agents)

Sum values based on Timestamp for InfluxDB (Flux)根据时间戳汇总数值。

I need to combine the data based on _time so that the final data is like:

_time RunningSessions
001 2
002 9
003 18
004 22
005 30

What would be an optimal flux query to join the source data to produce the sum of RunningSessions based on time.

Note: There is a complexity here for data missing for a time instant from an Agent. Suppose for time=006, Agent1 is having 35 sessions, but Agent2 hasn't reported any change, so for time=006 Agent2 must still be running the last 30 sessions.

This is my current query:

|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "RunningSessions")
|> filter(fn: (r) => r["_field"] == "totalSessionsCount")
|> group(columns: ["_time"])
|> sum(column: "_value")
|> group()

Update (1): Adding current Table view & query
Sum values based on Timestamp for InfluxDB (Flux)根据时间戳汇总数值。

英文:

I have data for executions running on different machines(Agents). The data is like:

_time AgentName RunningSessions
001 Agent1 1
002 Agent1 4
003 Agent1 10
004 Agent1 12
005 Agent1 15
001 Agent2 1
002 Agent2 5
003 Agent2 8
004 Agent2 10
005 Agent2 15

For the above data, my plot is like this(showing two colors for different Agents)

Sum values based on Timestamp for InfluxDB (Flux)根据时间戳汇总数值。

I need to combine the data based on _time so that the final data is like:

_time RunningSessions
001 2
002 9
003 18
004 22
005 30

What would be an optimal flux query to join the source data to produce the sum of RunningSessions based on time.

Note: There is a complexity here for data missing for a time instant from an Agent. Suppose for time=006, Agent1 is having 35 sessions, but Agent2 hasn't reported any change, so for time=006 Agent2 must still be running the last() 30 sessions.

This is my current query:

  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "RunningSessions")
  |> filter(fn: (r) => r["_field"] == "totalSessionsCount")
  |> group(columns: ["_time"])
  |> sum(column: "_value")
  |> group()

Update (1): Adding current Table view & query
Sum values based on Timestamp for InfluxDB (Flux)根据时间戳汇总数值。

答案1

得分: 0

我不熟悉flux,但你可以在Grafana中对时间序列进行求和。

进入面板编辑模式 > 转换:

  • 添加“从计算添加字段”变换,
  • 选择“计算”:总和,
  • 启用“替换所有字段”选项。
英文:

I'm not familiar with flux, but you can sum your time series at Grafana.

Go to panel edit mode > Transform:

  • Add "Add field from calculation" transformation,
  • select Calculation: Total,
  • enable option Replace all fields.

答案2

得分: 0

我错失了毫秒级的时间精度,尽管在界面上没有显示出来,但确实存在,因此我无法与_time列分组。

> 截断时间列(单位:1秒)

Sum values based on Timestamp for InfluxDB (Flux)根据时间戳汇总数值。

from(bucket: "${bucket}")
|> 范围(start: v.timeRangeStart, stop: v.timeRangeStop)
|> 过滤(fn: (r) => r["_measurement"] == "RunningSessions")
|> 过滤(fn: (r) => r["_field"] == "totalSessionsCount")
|> 截断时间列(单位:1秒)
|> 聚合窗口(every: 5秒, fn: last, createEmpty: true)
|> 分组(columns: ["_time"])
|> 求和(column: "_value")
|> 分组()
|> 输出(name: "Total VU Load")

英文:

I was missing the time precision of milliseconds which was not being shown in the UI, but it was there due to which I couldn't group with the _time column.

> truncateTimeColumn(unit: 1s)

Sum values based on Timestamp for InfluxDB (Flux)根据时间戳汇总数值。

from(bucket: "${bucket}")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "RunningSessions")
  |> filter(fn: (r) => r["_field"] == "totalSessionsCount")
  |> truncateTimeColumn(unit: 1s)
  |> aggregateWindow(every: 5s, fn: last, createEmpty: true)
  |> group(columns: ["_time"])
  |> sum(column: "_value")
  |> group()
  |> yield(name: "Total VU Load")

huangapple
  • 本文由 发表于 2023年4月10日 19:04:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定