/// <summary> /// Retrieve raw events for a given Time Series Id asynchronously. /// </summary> /// <param name="timeSeriesId">The Time Series Id to retrieve raw events for.</param> /// <param name="startTime">Start timestamp of the time range. Events that have this timestamp are included.</param> /// <param name="endTime">End timestamp of the time range. Events that match this timestamp are excluded.</param> /// <param name="options">Optional parameters to use when querying for 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:TimeSeriesInsightsSampleQueryEvents" language="csharp"> /// Console.WriteLine("\n\nQuery for raw temperature events over the past 10 minutes.\n"); /// /// // Get events from last 10 minute /// DateTimeOffset endTime = DateTime.UtcNow; /// DateTimeOffset startTime = endTime.AddMinutes(-10); /// /// TimeSeriesQueryAnalyzer temperatureEventsQuery = queriesClient.CreateEventsQuery(tsId, startTime, endTime); /// await foreach (TimeSeriesPoint point in temperatureEventsQuery.GetResultsAsync()) /// { /// TimeSeriesValue temperatureValue = point.GetValue("Temperature"); /// /// // Figure out what is the underlying type for the time series value. Since you know your Time Series Insights /// // environment best, you probably do not need this logic and you can skip to directly casting to the proper /// // type. This logic demonstrates how you can figure out what type to cast to in the case where you are not /// // too familiar with the property type. /// if (temperatureValue.Type == typeof(double?)) /// { /// Console.WriteLine($"{point.Timestamp} - Temperature: {point.GetNullableDouble("Temperature")}"); /// } /// else if (temperatureValue.Type == typeof(int?)) /// { /// Console.WriteLine($"{point.Timestamp} - Temperature: {point.GetNullableInt("Temperature")}"); /// } /// else /// { /// Console.WriteLine("The type of the Time Series value for Temperature is not numeric."); /// } /// } /// </code> /// </example> public virtual TimeSeriesQueryAnalyzer CreateEventsQuery( TimeSeriesId timeSeriesId, DateTimeOffset startTime, DateTimeOffset endTime, QueryEventsRequestOptions options = null, CancellationToken cancellationToken = default) { using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TimeSeriesInsightsClient)}.{nameof(GetEvents)}"); scope.Start(); try { var searchSpan = new DateTimeRange(startTime, endTime); var queryRequest = new QueryRequest { GetEvents = new GetEvents(timeSeriesId, searchSpan) }; BuildEventsRequestOptions(options, queryRequest); return(new TimeSeriesQueryAnalyzer(_queryRestClient, queryRequest, options?.Store?.ToString(), cancellationToken)); } catch (Exception ex) { scope.Failed(ex); throw; } }
/// <summary> /// Retrieve raw events for a given Time Series Id over a certain time interval synchronously. /// </summary> /// <param name="timeSeriesId">The Time Series Id to retrieve raw events for.</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 events.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The pageable list <see cref="AsyncPageable{QueryResultPage}"/> of query result frames.</returns> public virtual Pageable <QueryResultPage> GetEvents( TimeSeriesId timeSeriesId, TimeSpan timeSpan, DateTimeOffset?endTime = null, QueryEventsRequestOptions options = null, CancellationToken cancellationToken = default) { using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TimeSeriesInsightsClient)}.{nameof(GetEvents)}"); scope.Start(); try { DateTimeOffset rangeEndTime = endTime ?? DateTimeOffset.UtcNow; DateTimeOffset rangeStartTime = rangeEndTime - timeSpan; var searchSpan = new DateTimeRange(rangeStartTime, rangeEndTime); var queryRequest = new QueryRequest { GetEvents = new GetEvents(timeSeriesId, searchSpan) }; BuildEventsRequestOptions(options, queryRequest); return(QueryInternal(queryRequest, options?.StoreType?.ToString(), cancellationToken)); } catch (Exception ex) { scope.Failed(ex); throw; } }
private static void BuildEventsRequestOptions(QueryEventsRequestOptions options, QueryRequest queryRequest) { if (options != null) { if (options.Filter != null) { queryRequest.GetEvents.Filter = options.Filter; } if (options.ProjectedProperties != null) { foreach (TimeSeriesInsightsEventProperty projectedProperty in options.ProjectedProperties) { queryRequest.GetEvents.ProjectedProperties.Add(projectedProperty); } } queryRequest.GetEvents.Take = options.MaxNumberOfEvents; } }