/// <summary>
        /// This sample demonstrates usage of Time Series Insights types APIs.
        /// </summary>
        public async Task RunSamplesAsync(TimeSeriesInsightsClient client)
        {
            // For the purpose of keeping code snippets readable to the user, hardcoded string literals are used in place of assigned variables, eg Ids.
            // Despite not being a good code practice, this prevents code snippets from being out of context for the user when making API calls that accept Ids as parameters.

            PrintHeader("TIME SERIES INSIGHTS TYPES SAMPLE");

            #region Snippet:TimeSeriesInsightsSampleCreateType
            TimeSeriesInsightsTypes typesClient = client.GetTypesClient();

            // Create a type with an aggregate variable
            var timeSeriesTypes = new List <TimeSeriesType>();

            var countExpression   = new TimeSeriesExpression("count()");
            var aggregateVariable = new AggregateVariable(countExpression);
            var variables         = new Dictionary <string, TimeSeriesVariable>();
            variables.Add("aggregateVariable", aggregateVariable);

            timeSeriesTypes.Add(new TimeSeriesType("Type1", variables)
            {
                Id = "Type1Id"
            });
            timeSeriesTypes.Add(new TimeSeriesType("Type2", variables)
            {
                Id = "Type2Id"
            });

            Response <TimeSeriesTypeOperationResult[]> createTypesResult = await typesClient
                                                                           .CreateOrReplaceAsync(timeSeriesTypes);

            // The response of calling the API contains a list of error objects corresponding by position to the input parameter array in the request.
            // If the error object is set to null, this means the operation was a success.
            for (int i = 0; i < createTypesResult.Value.Length; i++)
            {
                if (createTypesResult.Value[i].Error == null)
                {
                    Console.WriteLine($"Created Time Series type successfully.");
                }
                else
                {
                    Console.WriteLine($"Failed to create a Time Series Insights type: {createTypesResult.Value[i].Error.Message}.");
                }
            }
            #endregion Snippet:TimeSeriesInsightsSampleCreateType

            #region Snippet:TimeSeriesInsightsSampleGetTypeById
            // Code snippet below shows getting a default Type using Id
            // The default type Id can be obtained programmatically by using the ModelSettings client.

            TimeSeriesInsightsModelSettings modelSettingsClient = client.GetModelSettingsClient();
            TimeSeriesModelSettings         modelSettings       = await modelSettingsClient.GetAsync();

            Response <TimeSeriesTypeOperationResult[]> getTypeByIdResults = await typesClient
                                                                            .GetByIdAsync(new string[] { modelSettings.DefaultTypeId });

            // The response of calling the API contains a list of type or error objects corresponding by position to the input parameter array in the request.
            // If the error object is set to null, this means the operation was a success.
            for (int i = 0; i < getTypeByIdResults.Value.Length; i++)
            {
                if (getTypeByIdResults.Value[i].Error == null)
                {
                    Console.WriteLine($"Retrieved Time Series type with Id: '{getTypeByIdResults.Value[i].TimeSeriesType.Id}'.");
                }
                else
                {
                    Console.WriteLine($"Failed to retrieve a Time Series type due to '{getTypeByIdResults.Value[i].Error.Message}'.");
                }
            }
            #endregion Snippet:TimeSeriesInsightsSampleGetTypeById

            #region Snippet:TimeSeriesInsightsSampleReplaceType
            // Update variables with adding a new variable
            foreach (TimeSeriesType type in timeSeriesTypes)
            {
                type.Description = "Description";
            }

            Response <TimeSeriesTypeOperationResult[]> updateTypesResult = await typesClient
                                                                           .CreateOrReplaceAsync(timeSeriesTypes);

            // The response of calling the API contains a list of error objects corresponding by position to the input parameter array in the request.
            // If the error object is set to null, this means the operation was a success.
            for (int i = 0; i < updateTypesResult.Value.Length; i++)
            {
                if (updateTypesResult.Value[i].Error == null)
                {
                    Console.WriteLine($"Updated Time Series type successfully.");
                }
                else
                {
                    Console.WriteLine($"Failed to update a Time Series Insights type due to: {updateTypesResult.Value[i].Error.Message}.");
                }
            }
            #endregion Snippet:TimeSeriesInsightsSampleReplaceType

            #region Snippet:TimeSeriesInsightsSampleGetAllTypes
            // Get all Time Series types in the environment
            AsyncPageable <TimeSeriesType> getAllTypesResponse = typesClient.GetTypesAsync();

            await foreach (TimeSeriesType tsiType in getAllTypesResponse)
            {
                Console.WriteLine($"Retrieved Time Series Insights type with Id: '{tsiType?.Id}' and Name: '{tsiType?.Name}'");
            }
            #endregion Snippet:TimeSeriesInsightsSampleGetAllTypes

            // Clean up
            try
            {
                #region Snippet:TimeSeriesInsightsSampleDeleteTypeById

                // Delete Time Series types with Ids

                var typesIdsToDelete = new List <string> {
                    "Type1Id", " Type2Id"
                };
                Response <TimeSeriesOperationError[]> deleteTypesResponse = await typesClient
                                                                            .DeleteByIdAsync(typesIdsToDelete);

                // The response of calling the API contains a list of error objects corresponding by position to the input parameter
                // array in the request. If the error object is set to null, this means the operation was a success.
                foreach (var result in deleteTypesResponse.Value)
                {
                    if (result != null)
                    {
                        Console.WriteLine($"Failed to delete a Time Series Insights type: {result.Message}.");
                    }
                    else
                    {
                        Console.WriteLine($"Deleted a Time Series Insights type successfully.");
                    }
                }
                #endregion Snippet:TimeSeriesInsightsSampleDeleteTypeById
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Failed to delete Time Series Insights type: {ex.Message}");
            }
        }
예제 #2
0
        public async Task TimeSeriesInsightsTypes_Lifecycle()
        {
            // Arrange
            TimeSeriesInsightsClient client      = GetClient();
            TimeSeriesInsightsTypes  typesClient = client.GetTypesClient();
            var timeSeriesTypes           = new List <TimeSeriesType>();
            var tsiTypeNamePrefix         = "type";
            int numOfTypesCreated         = 0;
            var timeSeriesTypesProperties = new Dictionary <string, string>
            {
                { Recording.GenerateAlphaNumericId(tsiTypeNamePrefix), Recording.GenerateId() },
                { Recording.GenerateAlphaNumericId(tsiTypeNamePrefix), Recording.GenerateId() }
            };

            // Build aggregate variable
            var countExpression    = new TimeSeriesExpression("count()");
            var aggregateVariable  = new AggregateVariable(countExpression);
            var variables          = new Dictionary <string, TimeSeriesVariable>();
            var variableNamePrefix = "aggregateVariable";

            variables.Add(Recording.GenerateAlphaNumericId(variableNamePrefix), aggregateVariable);

            foreach (KeyValuePair <string, string> property in timeSeriesTypesProperties)
            {
                var type = new TimeSeriesType(property.Key, variables)
                {
                    Id = property.Value
                };
                timeSeriesTypes.Add(type);
                numOfTypesCreated++;
            }

            // Act and assert
            try
            {
                // Get all Time Series types in the environment
                AsyncPageable <TimeSeriesType> getAllTypesResponse = typesClient.GetTypesAsync();

                await foreach (TimeSeriesType tsiType in getAllTypesResponse)
                {
                    tsiType.Should().NotBeNull();
                }

                // Create Time Series types
                Response <TimeSeriesTypeOperationResult[]> createTypesResult = await typesClient
                                                                               .CreateOrReplaceAsync(timeSeriesTypes)
                                                                               .ConfigureAwait(false);

                // Assert that the result error array does not contain any object that is set
                createTypesResult.Value.Should().OnlyContain((errorResult) => errorResult.Error == null);
                Response <TimeSeriesTypeOperationResult[]> getTypesByNamesResult;

                // This retry logic was added as the TSI types are not immediately available after creation
                await TestRetryHelper.RetryAsync <Response <TimeSeriesTypeOperationResult[]> >(async() =>
                {
                    // Get the created types by names
                    getTypesByNamesResult = await typesClient
                                            .GetByNameAsync(timeSeriesTypesProperties.Keys)
                                            .ConfigureAwait(false);

                    getTypesByNamesResult.Value.Should().OnlyContain((errorResult) => errorResult.Error == null);
                    getTypesByNamesResult.Value.Length.Should().Be(timeSeriesTypes.Count);
                    foreach (TimeSeriesTypeOperationResult typesResult in getTypesByNamesResult.Value)
                    {
                        typesResult.Error.Should().BeNull();
                        typesResult.TimeSeriesType.Should().NotBeNull();
                        typesResult.TimeSeriesType.Id.Should().NotBeNullOrEmpty();
                        typesResult.TimeSeriesType.Variables.Count.Should().Be(1);
                        typesResult.TimeSeriesType.Variables.IsSameOrEqualTo(variables);
                    }
                    return(null);
                }, MaxNumberOfRetries, s_retryDelay);

                // Update variables with adding a new variable
                foreach (TimeSeriesType type in timeSeriesTypes)
                {
                    type.Description = "Description";
                }

                Response <TimeSeriesTypeOperationResult[]> updateTypesResult = await typesClient
                                                                               .CreateOrReplaceAsync(timeSeriesTypes)
                                                                               .ConfigureAwait(false);

                updateTypesResult.Value.Should().OnlyContain((errorResult) => errorResult.Error == null);
                updateTypesResult.Value.Length.Should().Be(timeSeriesTypes.Count);

                // This retry logic was added as the TSI types are not immediately available after creation
                await TestRetryHelper.RetryAsync <Response <TimeSeriesTypeOperationResult[]> >(async() =>
                {
                    // Get type by Id
                    Response <TimeSeriesTypeOperationResult[]> getTypeByIdResult = await typesClient
                                                                                   .GetByIdAsync(timeSeriesTypesProperties.Values)
                                                                                   .ConfigureAwait(false);

                    getTypeByIdResult.Value.Length.Should().Be(numOfTypesCreated);
                    foreach (TimeSeriesTypeOperationResult typeOperationResult in getTypeByIdResult.Value)
                    {
                        typeOperationResult.TimeSeriesType.Should().NotBeNull();
                        typeOperationResult.Error.Should().BeNull();
                        typeOperationResult.TimeSeriesType.Name.Should().StartWith(tsiTypeNamePrefix);
                        typeOperationResult.TimeSeriesType.Id.Should().NotBeNull();
                    }

                    return(null);
                }, MaxNumberOfRetries, s_retryDelay);
            }
            finally
            {
                // clean up
                try
                {
                    Response <TimeSeriesOperationError[]> deleteTypesResponse = await typesClient
                                                                                .DeleteByIdAsync(timeSeriesTypesProperties.Values)
                                                                                .ConfigureAwait(false);

                    // Assert that the response array does not have any error object set
                    deleteTypesResponse.Value.Should().OnlyContain((errorResult) => errorResult == null);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Test clean up failed: {ex.Message}");
                    throw;
                }
            }
        }