influxdata / influxdata/kapacitor

Need help writing a tick script

Open
#1,755 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
2.4k
Forks
479
Avg merge
4d 16h
Merged PRs (30d)
4

Description

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 with some tags. 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')
.fill(0)

var summ = batch
|query('SELECT sum(value) FROM "telegraf"."default".diff_monthly')
.period(1m)
.every(1m)
.fill(0)

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. E.g. i want day 1 to be 10\*100/(10+12), and day 2 to be 12\*100/(10+12).

I need to iterate through values 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. Could you please help?

Thanks in advance!

Contributor guide

Open the contributing guide

Research direction

No repository file or test is named. Start with the posted TICK script and its batch queries, join, eval, and influxDBOut pipeline, then verify how grouped values and dynamic tags are supported. Done would be a documented, working approach for producing percentage values by day.

Written by the indexing model from the issue text.

Assessment

Domain
data, databases
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.