1

我目前确实有一种工作方法,它基于https://github.com/Azure-Samples/Azure-Time-Series-Insights/tree/master/csharp-tsi-preview-sample上当前发布的示例代码

以下方法中使用的类型是按照 GitHub 示例中的指导使用 AutoRest 创建的:https ://github.com/Azure/azure-rest-api-specs/tree/master/specification/timeseriesinsights/data-plane

我最初的尝试如下:

 public async Task<T> GetLatestEventValue<T>(object[] timeSeriesId, string tsiPropertyName,
     DateTimeRange searchSpan)
 {
     var client = await _tsiClientFactory.GetTimeSeriesInsightsClient();
     var propertyType = GetPropertyType(typeof(T));
     if (propertyType == null) throw new InvalidOperationException($"Unsupported property type (${typeof(T)})");

     string continuationToken = null;
     do
     {
         QueryResultPage queryResponse = await client.Query.ExecuteAsync(
             new QueryRequest(
                 getEvents: new GetEvents(
                     timeSeriesId: timeSeriesId,
                     searchSpan: searchSpan,
                     filter: null,
                     projectedProperties: new List<EventProperty>()
                         {new EventProperty(tsiPropertyName, propertyType)})),
             continuationToken: continuationToken);

         var latestEventIndex = GetLatestEventPropertyIndex(queryResponse.Timestamps);
         var lastValue = queryResponse.Properties
             .FirstOrDefault()
             ?.Values[latestEventIndex];

         if (lastValue != null)
         {
             return (T)lastValue;
         }

         continuationToken = queryResponse.ContinuationToken;
     } while (continuationToken != null);

     return default;
 }

方法的使用(timeSeriesId微软公开的例子一样):

 var repository = new TsiRepository(_factory);
 object[] timeSeriesId = new object[] { "2da181d7-8346-4cf2-bd94-a17742237429" };
 var today = DateTime.Now;
 var earlierDateTime = today.AddDays(-1);
 var searchSpan = new DateTimeRange(earlierDateTime.ToUniversalTime(), today.ToUniversalTime());
 var result = await repository.GetLatestEventValue<double>(timeSeriesId, "data", searchSpan);

上面介绍的方法有点工作,但感觉不是最佳的。是否有更简单的方法来查询给定时间序列实例的最新事件及其值?也许是为了提前使用时间序列表达式 (Tsx)功能?

4

1 回答 1

2

在花了一些时间寻找答案之后,我的方法是提前使用 TSX 查询语法来获取最后一个值,并添加一个附加参数,该参数决定查询仅在 TSI 的热存储上运行。这似乎可以很好地加快速度。默认为冷存储。

public async Task<T> RunAggregateSeriesLastValueAsync<T>(object[] timeSeriesId, DateTimeRange searchSpan)
{
    var interval = searchSpan.To - searchSpan.FromProperty;
    string continuationToken = null;
    object lastValue;
    do
    {
        QueryResultPage queryResponse = await _tsiClient.Query.ExecuteAsync(
          new QueryRequest(
              aggregateSeries: new AggregateSeries(
              timeSeriesId: timeSeriesId,
              searchSpan: searchSpan,
              filter: null,
              interval: interval,
              projectedVariables: new[] { "Last_Numeric" },
              inlineVariables: new Dictionary<string, Variable>()
              {
                  ["Last_Numeric"] = new NumericVariable(
                  value: new Tsx("$event.value"),
                  aggregation: new Tsx("last($value)"))
              })),
          storeType: "WarmStore", // Speeds things up since only warm storage is used
          continuationToken: continuationToken)
        lastValue = queryResponse.Properties
          .FirstOrDefault()
          ?.Values.LastOrDefault(v => v != null)
        continuationToken = queryResponse.ContinuationToken;
    } while (continuationToken != null)
    return (T)lastValue;
}
于 2020-06-04T10:59:44.347 回答