在 Azure TSI 预览版中,查询实例最新事件值的最佳方式是什么?
On Azure TSI preview, what is the optimal way to query instance latest event value?
我目前确实有一个基于 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
同Microsoft public example):
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);
上面介绍的方法有点管用,但感觉不是最优的。有没有更简单的方法来查询给定时间序列实例的最新事件及其值?也许要利用 Time Series Expression (Tsx)
功能?
在花了一些时间寻找答案后,我的方法是对最后一个值采用 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;
}
我目前确实有一个基于 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
同Microsoft public example):
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);
上面介绍的方法有点管用,但感觉不是最优的。有没有更简单的方法来查询给定时间序列实例的最新事件及其值?也许要利用 Time Series Expression (Tsx)
功能?
在花了一些时间寻找答案后,我的方法是对最后一个值采用 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;
}