private static void BuildAggregateSeriesRequestOptions(QueryAggregateSeriesRequestOptions options, QueryRequest queryRequest) { if (options != null) { if (options.Filter != null) { queryRequest.AggregateSeries.Filter = options.Filter; } if (options.ProjectedVariableNames != null) { foreach (string projectedVariable in options.ProjectedVariableNames) { queryRequest.AggregateSeries.ProjectedVariables.Add(projectedVariable); } } if (options.InlineVariables != null) { foreach (string inlineVariableKey in options.InlineVariables.Keys) { queryRequest.AggregateSeries.InlineVariables[inlineVariableKey] = options.InlineVariables[inlineVariableKey]; } } } }
/// <summary> /// Retrieve aggregated time series from events for a given Time Series Id over a specified time interval asynchronously. /// </summary> /// <param name="timeSeriesId">The Time Series Id to retrieve series events for.</param> /// <param name="interval">Interval size used to group events by.</param> /// <param name="timeSpan">The time interval over which to query data.</param> /// <param name="endTime">End timestamp of the time range. Events that match this timestamp are excluded. If null is provided, <c>DateTimeOffset.UtcNow</c> is used.</param> /// <param name="options">Optional parameters to use when querying for aggregated series events.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The <see cref="TimeSeriesQueryAnalyzer"/> object that can be used to retrieve the pageable list <see cref="AsyncPageable{TimeSeriesPoint}"/>.</returns> /// <example> /// <code snippet="Snippet:TimeSeriesInsightsSampleQueryAggregateSeriesWithNumericVariable" language="csharp"> /// Console.WriteLine("\n\nQuery for the average temperature over the past 30 seconds, in 2-second time slots.\n"); /// /// var numericVariable = new NumericVariable( /// new TimeSeriesExpression("$event.Temperature"), /// new TimeSeriesExpression("avg($value)")); /// /// var requestOptions = new QueryAggregateSeriesRequestOptions(); /// requestOptions.InlineVariables["Temperature"] = numericVariable; /// requestOptions.ProjectedVariableNames.Add("Temperature"); /// /// TimeSeriesQueryAnalyzer aggregateSeriesQuery = queriesClient.CreateAggregateSeriesQuery( /// tsId, /// TimeSpan.FromSeconds(2), /// TimeSpan.FromSeconds(30), /// null, /// requestOptions); /// /// await foreach (TimeSeriesPoint point in aggregateSeriesQuery.GetResultsAsync()) /// { /// double? averageTemperature = point.GetNullableDouble("Temperature"); /// if (averageTemperature != null) /// { /// Console.WriteLine($"{point.Timestamp} - Average temperature: {averageTemperature}."); /// } /// } /// </code> /// </example> public virtual TimeSeriesQueryAnalyzer CreateAggregateSeriesQuery( TimeSeriesId timeSeriesId, TimeSpan interval, TimeSpan timeSpan, DateTimeOffset?endTime = null, QueryAggregateSeriesRequestOptions options = null, CancellationToken cancellationToken = default) { using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TimeSeriesInsightsClient)}.{nameof(CreateAggregateSeriesQuery)}"); scope.Start(); try { DateTimeOffset rangeEndTime = endTime ?? DateTimeOffset.UtcNow; DateTimeOffset rangeStartTime = rangeEndTime - timeSpan; var searchSpan = new DateTimeRange(rangeStartTime, rangeEndTime); var queryRequest = new QueryRequest { AggregateSeries = new AggregateSeries(timeSeriesId, searchSpan, interval) }; BuildAggregateSeriesRequestOptions(options, queryRequest); return(new TimeSeriesQueryAnalyzer(_queryRestClient, queryRequest, options?.Store?.ToString(), cancellationToken)); } catch (Exception ex) { scope.Failed(ex); throw; } }