Ejemplo n.º 1
0
        /// <summary>
        /// Retrieve series 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="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 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:TimeSeriesInsightsSampleQuerySeriesWithInlineVariables" language="csharp">
        /// Console.WriteLine(&quot;\n\nQuery for temperature series in Celsius and Fahrenheit over the past 10 minutes.\n&quot;);
        ///
        /// var celsiusVariable = new NumericVariable(
        ///     new TimeSeriesExpression(&quot;$event.Temperature&quot;),
        ///     new TimeSeriesExpression(&quot;avg($value)&quot;));
        /// var fahrenheitVariable = new NumericVariable(
        ///     new TimeSeriesExpression(&quot;$event.Temperature * 1.8 + 32&quot;),
        ///     new TimeSeriesExpression(&quot;avg($value)&quot;));
        ///
        /// var querySeriesRequestOptions = new QuerySeriesRequestOptions();
        /// querySeriesRequestOptions.InlineVariables[&quot;TemperatureInCelsius&quot;] = celsiusVariable;
        /// querySeriesRequestOptions.InlineVariables[&quot;TemperatureInFahrenheit&quot;] = fahrenheitVariable;
        ///
        /// TimeSeriesQueryAnalyzer seriesQuery = queriesClient.CreateSeriesQuery(
        ///     tsId,
        ///     TimeSpan.FromMinutes(10),
        ///     null,
        ///     querySeriesRequestOptions);
        ///
        /// await foreach (TimeSeriesPoint point in seriesQuery.GetResultsAsync())
        /// {
        ///     double? tempInCelsius = (double?)point.GetValue(&quot;TemperatureInCelsius&quot;);
        ///     double? tempInFahrenheit = (double?)point.GetValue(&quot;TemperatureInFahrenheit&quot;);
        ///
        ///     Console.WriteLine($&quot;{point.Timestamp} - Average temperature in Celsius: {tempInCelsius}. Average temperature in Fahrenheit: {tempInFahrenheit}.&quot;);
        /// }
        /// </code>
        /// </example>
        public virtual TimeSeriesQueryAnalyzer CreateSeriesQuery(
            TimeSeriesId timeSeriesId,
            TimeSpan timeSpan,
            DateTimeOffset?endTime              = null,
            QuerySeriesRequestOptions options   = null,
            CancellationToken cancellationToken = default)
        {
            using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TimeSeriesInsightsClient)}.{nameof(GetSeries)}");
            scope.Start();

            try
            {
                DateTimeOffset rangeEndTime   = endTime ?? DateTimeOffset.UtcNow;
                DateTimeOffset rangeStartTime = rangeEndTime - timeSpan;
                var            searchSpan     = new DateTimeRange(rangeStartTime, rangeEndTime);
                var            queryRequest   = new QueryRequest
                {
                    GetSeries = new GetSeries(timeSeriesId, searchSpan)
                };

                BuildSeriesRequestOptions(options, queryRequest);

                return(new TimeSeriesQueryAnalyzer(_queryRestClient, queryRequest, options?.Store?.ToString(), cancellationToken));
            }
            catch (Exception ex)
            {
                scope.Failed(ex);
                throw;
            }
        }
Ejemplo n.º 2
0
        private static void BuildSeriesRequestOptions(QuerySeriesRequestOptions options, QueryRequest queryRequest)
        {
            if (options != null)
            {
                if (options.Filter != null)
                {
                    queryRequest.GetSeries.Filter = options.Filter;
                }

                if (options.ProjectedVariableNames != null)
                {
                    foreach (string projectedVariable in options.ProjectedVariableNames)
                    {
                        queryRequest.GetSeries.ProjectedVariables.Add(projectedVariable);
                    }
                }

                if (options.InlineVariables != null)
                {
                    foreach (string inlineVariableKey in options.InlineVariables.Keys)
                    {
                        queryRequest.GetSeries.InlineVariables[inlineVariableKey] = options.InlineVariables[inlineVariableKey];
                    }
                }

                queryRequest.GetSeries.Take = options.MaxNumberOfEvents;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Retrieve series events for a given Time Series Id synchronously.
        /// </summary>
        /// <param name="timeSeriesId">The Time Series Id to retrieve series 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 series 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> GetSeries(
            TimeSeriesId timeSeriesId,
            DateTimeOffset startTime,
            DateTimeOffset endTime,
            QuerySeriesRequestOptions options   = null,
            CancellationToken cancellationToken = default)
        {
            using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TimeSeriesInsightsClient)}.{nameof(GetSeries)}");
            scope.Start();

            try
            {
                var searchSpan   = new DateTimeRange(startTime, endTime);
                var queryRequest = new QueryRequest
                {
                    GetSeries = new GetSeries(timeSeriesId, searchSpan)
                };

                BuildSeriesRequestOptions(options, queryRequest);

                return(QueryInternal(queryRequest, options?.StoreType?.ToString(), cancellationToken));
            }
            catch (Exception ex)
            {
                scope.Failed(ex);
                throw;
            }
        }