[solved] Compare a date field with now in Kapacitor

Hello,

I have a measurement “subscription” with a value “expiration” that’s an expiration date, stored as a unix timestamp.

In Kapacitor, is it possible to compare it with now() to create alerts ? For instance, I would like to send alerts when a subscription has expired (so expiration < now())

Is there any way to achieve that ? (even with another type than a unix timestamp stored as a numeric value)

I have already tried something like this:

batch
  |query('''SELECT * FROM "mydb"."autogen"."subscription"''')
    .period(10h)
    .every(10s)
  |alert()
    .crit(lambda: "expiration" < now())
    .log('/tmp/dates-alerts.log')

Thank you

This feature was just added WIP: Add built in functions to convert timestamps to integers by desa · Pull Request #1390 · influxdata/kapacitor · GitHub which allows you to convert a timestamp into an integer representing the number of nanoseconds since the UNIX epoch.

With that feature you could do something like this:

batch
  |query('''SELECT * FROM "mydb"."autogen"."subscription"''')
    .period(10h)
    .every(10s)
  |alert()
    .crit(lambda: "expiration" < unixNano(now()))
    .log('/tmp/dates-alerts.log')

Thanks a lot! I’m looking forward to using it

Is there a now() function available in Kapacitor ? Because for me it complains with the following message: “Failed to handle 1 argument: undefined function: “now””

I don’t think so, I have that error too. I’m quite surprised now that I think of it because in the source code defining functions, on master branch, there is no now() function, even though there is unixNano(). So maybe it is located elsewhere, or someone forgot to add it too…