Hello people,
I’m new to Kapacitor, and i need to write a tick script that will rearrange some data from influxdb. The point is, I have a measurement “diff_monthly” with tag “day”. Now in Grafana, i’m showing those measurements like a histogram, i’m grouping the measurement on tags, and making a histogram like statistic. The problem with that is that i got flat values, but i want it to be shown as percentage, so i decided to write a tick script, mostly because i want to start using Kapacitor jobs in my project, and this seems like a simple enough task (well not simple for me). So i wrote a tick script that looks like this:
var values = batch
|query('SELECT sum(value) FROM "telegraf"."default".diff_monthly')
.period(1m)
.every(1m)
.groupBy('day')
var summ = batch
|query('SELECT sum(value) FROM "telegraf"."default".diff_monthly')
.period(1m)
.every(1m)
values
|join(summ)
.as('values', 'summ')
.tolerance(1m)
//Calculate percentage
|eval(lambda: "values.value" / ("summ.sum"))
// Give the resulting field a name
.as('percentage')
|influxDBOut()
.database('telegraf')
.measurement('diff_monthly_percentage')
.tag('day', 'values.day')
My influx measurement “diff_monthly” that i want to rearrange into new measurement looks like this:
time | day | value |
---|---|---|
timestamp | 1 | 10 |
timestamp | 2 | 12 |
timestamp | 1 | 5 |
etc.
So i expect that my new measurement has percentages by those “day” tags.
time | day | percentage |
---|---|---|
timestamp | 1 | 15/27 |
timestamp | 2 | 12/27 |
etc.
Ofcourse, percentage values would be float.
Now, I need the way to iterate through values variable and every time to write into |influxDBOut()
, but i don’t know how to do that in tick script. Also how can i set variable into .tag()
, i tried lambda: “values.sum”, and string(lambda: “values.sum”), but that doesn’t work either, since .tag()
only accepts string as arguments. Could you please help?
Thanks in advance!