Azure-Samples / Azure-Time-Series-Insights

Sample modules written in different languages that can be used as templates for applications needing to query Time Series Insights
MIT License
21 stars 31 forks source link

Get latest collected data for an instance based on timestamp? #31

Open MichalLechowski opened 3 years ago

MichalLechowski commented 3 years ago

Is it possible to query a single TimeSeriesId with something like filter max($event.timestamp) to get only the latest collected data (measurements from a single sensor)? I don't have any environment, we're just making a proof of concept through Postman to check if we can make the same queries as we do in current CosmosDb environment.

The model is for instance something like that, the sensor is feeding the data every 2 minutes and I need only the last measurement:
image

Is that currently possible?

UPDATE/

I have found one way to do it. The request is for aggregateSeries:

  "aggregateSeries": {
    "timeSeriesId": [
          null, "4B6F6C6F00856F5A"
    ],
    "searchSpan": {
      "from": "2021-02-22T00:00:00Z",
      "to": "2021-02-26T23:59:59Z"
    },
    "interval": "P1M",
    "inlineVariables": {
      "temps": {
        "kind": "numeric",
        "value": {
          "tsx": "$event.object.temp.Double"
        },
        "filter": null,
        "aggregation": {
          "tsx": "last($value)"
        }
      }
    },
    "projectedVariables": [
      "temps"
    ]
  }
} 

As long as I make the interval 'wider' than the searchspan, I can retrieve the last value using aggregate last(value). Is it the optimal way, tho? Also this method requires to last(value) every measured property I need to collect which is doable but not very convenient.