Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TICK script - how to find elapsed time between two measurement points #384

Closed
vlasov01 opened this issue Mar 28, 2016 · 3 comments
Closed
Milestone

Comments

@vlasov01
Copy link

I'm having difficulties to create a TICK script, that can find elapsed time between two measurement points. My Influx Measurement contains state points with values "on" and "off" (always in this order). I'd like to trigger event once "off" state arrives and calculates elapsed time between "on" and "off" state.

@nathanielc
Copy link
Contributor

@vlasov01 There is no clean way to do this within Kapacitor at the moment.

One way I can think to do this is to use the derivative method, its really hacky but if on had a value of 0 and off had a value of 1 then when the derivative was computed it would do:
(1 - 0) / elapsed and then you could use an eval node to invert the result and get elapsed back out.

Since the above method is really confusing and fragile we should probably add a method for computing elapsed times. Maybe something as simple as:

stream
     .from()....
     .elapsed()
          .as('elapsed')
     .alert()
         .crit(lambda: "status" == 'off')
         .message('Elapsed time: {{ index .Fields "elapsed" }}')

Would that fit your needs?

Can you provide more context about what you are trying to do with the elapsed time as this seems like an interesting use case?

@vlasov01
Copy link
Author

@nathanielc Thank you for the prompt reply!
Your proposal of a new elapsed() method I think going to satisfy my requirements.

I've implemented script based on your code sample.

var online = stream
    .from().measurement('watersensor')
    .where(lambda: "last" == 'offline')
    .elapsed()
         .as('elapsed')
    .alert()
         .info(lambda: "state" == 'offline')
         .message('Elapsed time: {{ index .Fields "elapsed" }}')
         .log('/var/log/kapacitor/summary.log')

A bit about my use case:
The water sensor controller is switches into an "online" state when water is switched on and switches to an "offline" state once water flow stops. I'd like to measure the elapsed time of water flow. These points are accumulated in the "watersensor" measurement.
At the same time, controller is publishing every 5 seconds amount of water used. These points are accumulated in the "water" measurement.
Once water flow stops ("watersensor" state=="offline") I'd like to calculate summary of water used and how much time it was on and publish info/warn/crit alert,

@vlasov01
Copy link
Author

@nathanielc It seems the proposal #388 looks a bit similar.
But now I'm thinking may be it makes more sense to implement it directly as a continuous query as per https://docs.influxdata.com/influxdb/v0.11/query_language/continuous_queries/ ?

@nathanielc nathanielc added this to the v0.14 milestone Apr 5, 2016
This was referenced Apr 14, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants