Search code examples
grafanagraphite

Counting the number of consecutive points where each series in a set are all defined


If I had two (or more...) "sparse" series (i.e. not necessarily defined at every instant) and a particular time range, what trick could I use to find the largest number of defined data points within a "synchronized interval" (i.e. where all of the series are either defined or undefined)?

e.g. for the following three series:

    t: 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
   ----------------------------------------------------
   s0: 1  -  -  1  6  -  2  -  2  8  -  9  0  -  -  -
   s1: 3  -  -  4  0  -  3  2  3  3  -  1  1  -  -  7
   s2: 1  -  -  5  9  -  2  4  -  3  -  2  4  4  -  -
       ^-----------------^        ^--------^
count: 1........2..3....[4]       1.....2.[3]

we would find a maximum of 4 data points that were defined within a "synchronized interval."


Solution

  • after much experimentation, i arrived at a solution -- assuming two series s0 and s1 (although this generalizes to any number of series):

    $NUM_SERIES = 2
    $NUM_SERIES_RECIPROCAL = 1/2
    sum = sumSeries(isNonNull(s0),isNonNull(s1))
    
    removeBelowValue(
      derivative(
        transformNull(
          keepLastValue(
            multiplySeries(
              transformNull(
                keepLastValue(
                  integral(
                    scale(
                      removeBelowValue(sum, $NUM_SERIES),
                      $NUM_SERIES_RECIPROCAL
                    )
                  )
                ),
                0
              ),
              scale(
                removeAboveValue(
                  derivative(
                    keepLastValue(
                      removeBelowValue(
                        sum,
                        1
                      )
                    )
                  ),
                  -1
                ),
                -1
              )
            )
          ),
          0
        )
      ),
      1
    )