我目前确实有一种工作方法,它基于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)
功能?