Grafana: Evaluate date name (Flux)
Aus Wiki-WebPerfect
Version vom 21. Januar 2021, 10:22 Uhr von Admin (Diskussion | Beiträge)
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.
- Add pivot function to make the format working with Grafana.
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" })) |> pivot(rowKey: ["_time"], columnKey: ["namedMonth", "namedDay"], valueColumn: "_value") //formating for Grafana