Calculate sorted Rank and store back into InfluxDB using Kapacitor

Is there a way to use Kapacitor to sort by a field and store the rank back as a field?

I have a tick script which is using a window to sum the number of events over the last 24 hour for several thousand entities every 5 minutes and store this back into InfluxDB. I would also like to store the position of each entity when they are sorted by this value.

I’m hoping there’s a way within Kapacitor to do this. If not is can I somehow subscribe to this data from Kapacitor, sort it myself and then post new entries into InfluxDB?

I’ve just stumbled across Flux which has a sort function. Could this be used? If so can it be used within Kapacitor to create new entries in InfluxDB every 5 minutes?

Hello @andsee,

I’m a little confused about what you’re trying to do . What do you mean by store the position of each entity? Are you looking to add a tag that indexes the value? Can you please give me an example of your input and desired output? Thanks!

I have a source which creates a point in InfluxDB every time an event occurs. The point has a tag ‘Event’ containing the event id. and a field ‘Score’ containing 1.

In Kapacitor I can create a query which calculates the number of times each event occurred over the last 24 hours every 5 minutes and store this back.

However I wish to use these values almost like a score in a leaderboard and create a point for each event id containing it’s rank.

Today I used flux to create this data using the following:

from(bucket: "blvdts/autogen")
  |> range(start: -1h)
  |> filter(fn: (r) => r._measurement == "Test")
  |> sum(columns: ["_value"])
  |> map(fn: (r) => ({sum: r._value, event:r.Event, rank:1}), mergeKey: false)
  |> sort(columns: ["sum"], desc:true)
  |> cumulativeSum(columns:["rank"])

but I’ve found no way to use flux in Kapacitor, so the search continues.