예제 #1
0
        /// <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(&quot;\n\nQuery for raw temperature events over the past 10 minutes.\n&quot;);
        ///
        /// // 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(&quot;Temperature&quot;);
        ///
        ///     // 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($&quot;{point.Timestamp} - Temperature: {point.GetNullableDouble(&quot;Temperature&quot;)}&quot;);
        ///     }
        ///     else if (temperatureValue.Type == typeof(int?))
        ///     {
        ///         Console.WriteLine($&quot;{point.Timestamp} - Temperature: {point.GetNullableInt(&quot;Temperature&quot;)}&quot;);
        ///     }
        ///     else
        ///     {
        ///         Console.WriteLine(&quot;The type of the Time Series value for Temperature is not numeric.&quot;);
        ///     }
        /// }
        /// </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;
            }
        }
 // The use of fully qualified name for IUtf8JsonSerializable is a work around until this
 // issue is fixed: https://github.com/Azure/autorest.csharp/issues/793
 void global::Azure.Core.IUtf8JsonSerializable.Write(Utf8JsonWriter writer)
 {
     writer.WriteStartObject();
     writer.WritePropertyName("timeSeriesId");
     writer.WriteStartArray();
     foreach (string item in TimeSeriesId.ToArray())
     {
         writer.WriteObjectValue(item);
     }
     writer.WriteEndArray();
     writer.WritePropertyName("searchSpan");
     writer.WriteObjectValue(SearchSpan);
     if (Optional.IsDefined(Filter))
     {
         writer.WritePropertyName("filter");
         writer.WriteObjectValue(Filter);
     }
     if (Optional.IsCollectionDefined(ProjectedProperties))
     {
         writer.WritePropertyName("projectedProperties");
         writer.WriteStartArray();
         foreach (EventProperty item in ProjectedProperties)
         {
             writer.WriteObjectValue(item);
         }
         writer.WriteEndArray();
     }
     if (Optional.IsDefined(Take))
     {
         writer.WritePropertyName("take");
         writer.WriteNumberValue(Take.Value);
     }
     writer.WriteEndObject();
 }
예제 #3
0
        /// <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(&quot;\n\nQuery for the average temperature over the past 30 seconds, in 2-second time slots.\n&quot;);
        ///
        /// var numericVariable = new NumericVariable(
        ///     new TimeSeriesExpression(&quot;$event.Temperature&quot;),
        ///     new TimeSeriesExpression(&quot;avg($value)&quot;));
        ///
        /// var requestOptions = new QueryAggregateSeriesRequestOptions();
        /// requestOptions.InlineVariables[&quot;Temperature&quot;] = numericVariable;
        /// requestOptions.ProjectedVariableNames.Add(&quot;Temperature&quot;);
        ///
        /// TimeSeriesQueryAnalyzer aggregateSeriesQuery = queriesClient.CreateAggregateSeriesQuery(
        ///     tsId,
        ///     TimeSpan.FromSeconds(2),
        ///     TimeSpan.FromSeconds(30),
        ///     null,
        ///     requestOptions);
        ///
        /// await foreach (TimeSeriesPoint point in aggregateSeriesQuery.GetResultsAsync())
        /// {
        ///     double? averageTemperature = point.GetNullableDouble(&quot;Temperature&quot;);
        ///     if (averageTemperature != null)
        ///     {
        ///         Console.WriteLine($&quot;{point.Timestamp} - Average temperature: {averageTemperature}.&quot;);
        ///     }
        /// }
        /// </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;
            }
        }
예제 #4
0
 /// <summary>
 /// Initializes a new instance of TimeSeriesInstance.
 /// </summary>
 /// <param name="timeSeriesId">
 /// Time Series Id that uniquely identifies the instance. It matches Time Series Id properties in
 /// an environment. Immutable, never null.
 /// </param>
 /// <param name="typeId">
 /// This represents the type that this instance belongs to. Never null.
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="timeSeriesId"/> or <paramref name="typeId"/> is null.
 /// </exception>
 public TimeSeriesInstance(TimeSeriesId timeSeriesId, string typeId)
 {
     TimeSeriesId   = timeSeriesId ?? throw new ArgumentNullException(nameof(timeSeriesId));
     TypeId         = typeId ?? throw new ArgumentNullException(nameof(typeId));
     HierarchyIds   = new ChangeTrackingList <string>();
     InstanceFields = new ChangeTrackingDictionary <string, object>();
 }
예제 #5
0
 /// <summary>
 /// Initializes a new instance of GetSeries.
 /// </summary>
 /// <param name="timeSeriesId">
 /// Time Series Id that uniquely identifies the instance. It matches Time Series Id properties in
 /// an environment. Immutable, never null.
 /// </param>
 /// <param name="searchSpan">
 /// The range of time on which the query is executed. Cannot be null.
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="searchSpan"/> is null.
 /// </exception>
 public GetSeries(TimeSeriesId timeSeriesId, DateTimeRange searchSpan)
 {
     SearchSpan         = searchSpan ?? throw new ArgumentNullException(nameof(searchSpan));
     TimeSeriesId       = timeSeriesId;
     ProjectedVariables = new ChangeTrackingList <string>();
     InlineVariables    = new ChangeTrackingDictionary <string, TimeSeriesVariable>();
 }
예제 #6
0
        /// <summary>
        /// Retrieve series events for a given Time Series Id over a certain time interval synchronously.
        /// </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 pageable list <see cref="AsyncPageable{QueryResultPage}"/> of query result frames.</returns>
        public virtual Pageable <QueryResultPage> GetSeries(
            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(QueryInternal(queryRequest, options?.StoreType?.ToString(), cancellationToken));
            }
            catch (Exception ex)
            {
                scope.Failed(ex);
                throw;
            }
        }
 /// <summary>
 /// Initializes a new instance of AggregateSeries.
 /// </summary>
 /// <param name="timeSeriesId">
 /// Time Series Id that uniquely identifies the instance. It matches Time Series Id properties in
 /// an environment. Immutable, never null.
 /// </param>
 /// <param name="searchSpan">
 /// The range of time on which the query is executed. Cannot be null.
 /// </param>
 /// <param name="interval">
 /// Interval size used to group events by.
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="timeSeriesId"/> or <paramref name="searchSpan"/> is null.
 /// </exception>
 public AggregateSeries(TimeSeriesId timeSeriesId, DateTimeRange searchSpan, TimeSpan interval)
 {
     TimeSeriesId       = timeSeriesId ?? throw new ArgumentNullException(nameof(timeSeriesId));
     SearchSpan         = searchSpan ?? throw new ArgumentNullException(nameof(searchSpan));
     Interval           = interval;
     ProjectedVariables = new ChangeTrackingList <string>();
     InlineVariables    = new ChangeTrackingDictionary <string, TimeSeriesVariable>();
 }
예제 #8
0
 /// <summary>
 /// Initializes a new instance of TimeSeriesInstance.
 /// </summary>
 /// <param name="timeSeriesId">
 /// Time Series Id that uniquely identifies the instance. It matches Time Series Id properties in
 /// an environment. Immutable, never null.
 /// </param>
 /// <param name="typeId">This represents the type that this instance belongs to. Never null. </param>
 /// <param name="name">
 /// Optional name of the instance which is unique in an environment. Names acts as a mutable alias
 /// or display name of the time series instance. Mutable, may be null.
 /// </param>
 /// <param name="description">This optional field contains description about the instance. </param>
 /// <param name="hierarchyIds">Set of time series hierarchy Ids that the instance belong to. May be null. </param>
 /// <param name="instanceFields">
 /// Set of key-value pairs that contain user-defined instance properties.
 /// It may be null. Supported property value types are: bool, string, long, double and it cannot be nested or null.
 /// </param>
 internal TimeSeriesInstance(
     TimeSeriesId timeSeriesId,
     string typeId,
     string name,
     string description,
     IList <string> hierarchyIds,
     IDictionary <string, object> instanceFields)
 {
     TimeSeriesId   = timeSeriesId;
     TypeId         = typeId;
     Name           = name;
     Description    = description;
     HierarchyIds   = hierarchyIds;
     InstanceFields = instanceFields;
 }
 // The use of fully qualified name for IUtf8JsonSerializable is a work around until this
 // issue is fixed: https://github.com/Azure/autorest.csharp/issues/793
 void global::Azure.Core.IUtf8JsonSerializable.Write(Utf8JsonWriter writer)
 {
     writer.WriteStartObject();
     writer.WritePropertyName("timeSeriesId");
     writer.WriteStartArray();
     foreach (object item in TimeSeriesId.ToArray())
     {
         writer.WriteObjectValue(item);
     }
     writer.WriteEndArray();
     writer.WritePropertyName("typeId");
     writer.WriteStringValue(TypeId);
     if (Optional.IsDefined(Name))
     {
         writer.WritePropertyName("name");
         writer.WriteStringValue(Name);
     }
     if (Optional.IsDefined(Description))
     {
         writer.WritePropertyName("description");
         writer.WriteStringValue(Description);
     }
     if (Optional.IsCollectionDefined(HierarchyIds))
     {
         writer.WritePropertyName("hierarchyIds");
         writer.WriteStartArray();
         foreach (var item in HierarchyIds)
         {
             writer.WriteStringValue(item);
         }
         writer.WriteEndArray();
     }
     if (Optional.IsCollectionDefined(InstanceFields))
     {
         writer.WritePropertyName("instanceFields");
         writer.WriteStartObject();
         foreach (KeyValuePair <string, object> item in InstanceFields)
         {
             writer.WritePropertyName(item.Key);
             writer.WriteObjectValue(item.Value);
         }
         writer.WriteEndObject();
     }
     writer.WriteEndObject();
 }
 // The use of fully qualified name for IUtf8JsonSerializable is a work around until this
 // issue is fixed: https://github.com/Azure/autorest.csharp/issues/793
 void global::Azure.Core.IUtf8JsonSerializable.Write(Utf8JsonWriter writer)
 {
     writer.WriteStartObject();
     writer.WritePropertyName("timeSeriesId");
     writer.WriteStartArray();
     foreach (var item in TimeSeriesId.ToArray())
     {
         writer.WriteObjectValue(item);
     }
     writer.WriteEndArray();
     writer.WritePropertyName("searchSpan");
     writer.WriteObjectValue(SearchSpan);
     if (Optional.IsDefined(Filter))
     {
         writer.WritePropertyName("filter");
         writer.WriteObjectValue(Filter);
     }
     writer.WritePropertyName("interval");
     writer.WriteStringValue(Interval, "P");
     if (Optional.IsCollectionDefined(ProjectedVariables))
     {
         writer.WritePropertyName("projectedVariables");
         writer.WriteStartArray();
         foreach (var item in ProjectedVariables)
         {
             writer.WriteStringValue(item);
         }
         writer.WriteEndArray();
     }
     if (Optional.IsCollectionDefined(InlineVariables))
     {
         writer.WritePropertyName("inlineVariables");
         writer.WriteStartObject();
         foreach (var item in InlineVariables)
         {
             writer.WritePropertyName(item.Key);
             writer.WriteObjectValue(item.Value);
         }
         writer.WriteEndObject();
     }
     writer.WriteEndObject();
 }
        internal static TimeSeriesInstance DeserializeTimeSeriesInstance(JsonElement element)
        {
            TimeSeriesId               timeSeriesId = default;
            string                     typeId       = default;
            Optional <string>          name         = default;
            Optional <string>          description  = default;
            Optional <IList <string> > hierarchyIds = default;
            Optional <IDictionary <string, object> > instanceFields = default;

            foreach (JsonProperty property in element.EnumerateObject())
            {
                if (property.NameEquals("timeSeriesId"))
                {
                    var array = new List <string>();
                    foreach (JsonElement item in property.Value.EnumerateArray())
                    {
                        array.Add(item.GetString());
                    }

                    timeSeriesId = array.Count switch
                    {
                        1 => new TimeSeriesId(array[0]),
                        2 => new TimeSeriesId(array[0], array[1]),
                        3 => new TimeSeriesId(array[0], array[1], array[2]),
                        _ => throw new Exception($"Invalid number of Time Series Insights Id properties."),
                    };
                    continue;
                }
                if (property.NameEquals("typeId"))
                {
                    typeId = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("name"))
                {
                    name = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("description"))
                {
                    description = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("hierarchyIds"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    List <string> array = new List <string>();
                    foreach (JsonElement item in property.Value.EnumerateArray())
                    {
                        array.Add(item.GetString());
                    }
                    hierarchyIds = array;
                    continue;
                }
                if (property.NameEquals("instanceFields"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    Dictionary <string, object> dictionary = new Dictionary <string, object>();
                    foreach (JsonProperty property0 in property.Value.EnumerateObject())
                    {
                        dictionary.Add(property0.Name, property0.Value.GetObject());
                    }
                    instanceFields = dictionary;
                    continue;
                }
            }

            return(new TimeSeriesInstance(
                       timeSeriesId,
                       typeId,
                       name.Value,
                       description.Value,
                       Optional.ToList(hierarchyIds),
                       Optional.ToDictionary(instanceFields)));
        }
예제 #12
0
 /// <summary>
 /// Initializes a new instance of GetEvents.
 /// </summary>
 /// <param name="timeSeriesId">
 /// Time Series Id that uniquely identifies the instance.It matches Time Series Id properties in
 /// an environment. Immutable, never null.
 /// </param>
 /// <param name="searchSpan">
 /// The range of time on which the query is executed. Cannot be null.
 /// </param>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="timeSeriesId"/> or <paramref name="searchSpan"/> is null.
 /// </exception>
 public GetEvents(TimeSeriesId timeSeriesId, DateTimeRange searchSpan)
 {
     TimeSeriesId        = timeSeriesId ?? throw new ArgumentNullException(nameof(timeSeriesId));
     SearchSpan          = searchSpan ?? throw new ArgumentNullException(nameof(searchSpan));
     ProjectedProperties = new ChangeTrackingList <EventProperty>();
 }