Search code examples
databaseinfluxdbinfluxdb-python

What is the way to select only first value in InfluxDB sequences?


I have a measurement that indicates the process of changing the state of smth. Every second the system asks whether smth is changing, if so it stores 1 in db, otherwise nothing. So I have sequences of "ones". As here. Distance between points is 1s

I want to get only the time of first point of each "one" sequence. On this particular example it would be

Time                Value
2019-01-01 11:46:55 1
2019-01-01 12:36:45 1

In red squares

Is there a way to do it using queries? Or may be easy python pattern?

P.S. first() selector requires GROUP BY, but I cannot assume that sequences are less then some_time_interval


Solution

  • You could probably achieve what you need with a nested query and the difference function. https://docs.influxdata.com/influxdb/v1.7/query_language/functions/#difference
    For example:
    Let's say your measurement is called change and the field is called value

    SELECT diff FROM
     (SELECT difference(value) as diff FROM
      (SELECT value FROM change WHERE your_time_filter GROUP BY time(1s) fill(0))
     )
     WHERE diff > 0
    

    So you first fill in the empty spaces with zeros. Then take the differences between subsequent values. Then pick the differences that are positive. If the difference is 0 then it is between two zeros or two ones, no change. If it is 1 then the change is from zero to one (what you need). If it is -1 then the change is from one to zero, end of sequence of ones.

    You may need to tweak the query a bit with time intervals and groupings.