Need help over a tick script rearranging influx data


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')

var summ = batch
    |query('SELECT sum(value) FROM "telegraf"."default".diff_monthly')

        .as('values', 'summ')
    //Calculate percentage
    |eval(lambda: "values.value" / ("summ.sum"))
        // Give the resulting field a name
        .tag('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


So i expect that my new measurement has percentages by those “day” tags.

time day percentage
timestamp 1 15/27
timestamp 2 12/27


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!