Grafana: Evaluate date name (Flux): Unterschied zwischen den Versionen
Aus Wiki-WebPerfect
Admin (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „'''Goal:''' Add two columns (''namedMonth'' and ''namedDay'') with name of the month and day. <br> '''Steps:''' *Aggregate the data with an '''window of 1d'''…“) |
Admin (Diskussion | Beiträge) |
||
Zeile 1: | Zeile 1: | ||
'''Goal:''' Add two columns (''namedMonth'' and ''namedDay'') with name of the month and day. <br> | '''Goal:''' Add two columns (''namedMonth'' and ''namedDay'') with name of the month and day. <br> | ||
'''Steps:''' | '''Steps:''' | ||
− | *Aggregate the data with an '''window of 1d''' and the function '''last'''. | + | *'''Aggregate''' the data with an '''window of 1d''' and the function '''last'''. |
− | *Group the data by '''_time'''. | + | *'''Group''' the data by '''_time'''. |
*'''Calculate the sum''' for each timeseries (foreach grouped _time). | *'''Calculate the sum''' for each timeseries (foreach grouped _time). | ||
*'''Truncate''' the data to '''whole days'''. | *'''Truncate''' the data to '''whole days'''. | ||
*Select ''first'' to '''remove the last not completed day'''. | *Select ''first'' to '''remove the last not completed day'''. | ||
*'''Evaluate the month and the day''' and add these as new columns. | *'''Evaluate the month and the day''' and add these as new columns. | ||
+ | |||
+ | |||
+ | <pre> | ||
+ | import "date" //import library to add date functions | ||
+ | timewindow = 1d //timewindow size | ||
+ | |||
+ | from(bucket: <YOUR_BUCKET>) | ||
+ | |> range(start: v.timeRangeStart, stop: v.timeRangeStop) | ||
+ | |> filter(fn: (r) => | ||
+ | r._measurement == "<YOUR_MEASUREMENT>" | ||
+ | ) | ||
+ | |> aggregateWindow(every: timewindow, fn: last, createEmpty: false) | ||
+ | |> group(columns: ["_time"]) | ||
+ | |> sum() | ||
+ | |> truncateTimeColumn(unit: timewindow) //truncate to whole days | ||
+ | |> first() //remove the calculated to now() | ||
+ | |> map(fn: (r) => ({ r with //Valuemapping of monthnumber to monthname | ||
+ | namedMonth: | ||
+ | if (date.month(t: r._time)) == 1 then "January" | ||
+ | else if (date.month(t: r._time)) == 2 then "February" | ||
+ | else if (date.month(t: r._time)) == 3 then "March" | ||
+ | else if (date.month(t: r._time)) == 4 then "April" | ||
+ | else if (date.month(t: r._time)) == 5 then "May" | ||
+ | else if (date.month(t: r._time)) == 6 then "June" | ||
+ | else if (date.month(t: r._time)) == 7 then "July" | ||
+ | else if (date.month(t: r._time)) == 8 then "August" | ||
+ | else if (date.month(t: r._time)) == 9 then "September" | ||
+ | else if (date.month(t: r._time)) == 10 then "October" | ||
+ | else if (date.month(t: r._time)) == 11 then "November" | ||
+ | else if (date.month(t: r._time)) == 12 then "December" | ||
+ | else "error", | ||
+ | namedDay: | ||
+ | if (date.weekDay(t: r._time)) == 0 then "Sunday" | ||
+ | else if (date.weekDay(t: r._time)) == 1 then "Monday" | ||
+ | else if (date.weekDay(t: r._time)) == 2 then "Tuesday" | ||
+ | else if (date.weekDay(t: r._time)) == 3 then "Wednesday" | ||
+ | else if (date.weekDay(t: r._time)) == 4 then "Thursday" | ||
+ | else if (date.weekDay(t: r._time)) == 5 then "Friday" | ||
+ | else if (date.weekDay(t: r._time)) == 6 then "Saturday" | ||
+ | else "error" | ||
+ | })) | ||
+ | </pre> | ||
Version vom 21. Januar 2021, 10:10 Uhr
Goal: Add two columns (namedMonth and namedDay) with name of the month and day.
Steps:
- Aggregate the data with an window of 1d and the function last.
- Group the data by _time.
- Calculate the sum for each timeseries (foreach grouped _time).
- Truncate the data to whole days.
- Select first to remove the last not completed day.
- Evaluate the month and the day and add these as new columns.
import "date" //import library to add date functions timewindow = 1d //timewindow size from(bucket: <YOUR_BUCKET>) |> range(start: v.timeRangeStart, stop: v.timeRangeStop) |> filter(fn: (r) => r._measurement == "<YOUR_MEASUREMENT>" ) |> aggregateWindow(every: timewindow, fn: last, createEmpty: false) |> group(columns: ["_time"]) |> sum() |> truncateTimeColumn(unit: timewindow) //truncate to whole days |> first() //remove the calculated to now() |> map(fn: (r) => ({ r with //Valuemapping of monthnumber to monthname namedMonth: if (date.month(t: r._time)) == 1 then "January" else if (date.month(t: r._time)) == 2 then "February" else if (date.month(t: r._time)) == 3 then "March" else if (date.month(t: r._time)) == 4 then "April" else if (date.month(t: r._time)) == 5 then "May" else if (date.month(t: r._time)) == 6 then "June" else if (date.month(t: r._time)) == 7 then "July" else if (date.month(t: r._time)) == 8 then "August" else if (date.month(t: r._time)) == 9 then "September" else if (date.month(t: r._time)) == 10 then "October" else if (date.month(t: r._time)) == 11 then "November" else if (date.month(t: r._time)) == 12 then "December" else "error", namedDay: if (date.weekDay(t: r._time)) == 0 then "Sunday" else if (date.weekDay(t: r._time)) == 1 then "Monday" else if (date.weekDay(t: r._time)) == 2 then "Tuesday" else if (date.weekDay(t: r._time)) == 3 then "Wednesday" else if (date.weekDay(t: r._time)) == 4 then "Thursday" else if (date.weekDay(t: r._time)) == 5 then "Friday" else if (date.weekDay(t: r._time)) == 6 then "Saturday" else "error" }))