private static void PrintResponse(TimeSeriesHierarchy hierarchy)
 {
     Console.WriteLine("Hierarchy:");
     Console.WriteLine("  ID: {0}", hierarchy.Id);
     Console.WriteLine("  Name: {0}", hierarchy.Name);
     Console.WriteLine("  Source: {0}", string.Join(" > ", hierarchy.Source.InstanceFieldNames));
 }
Exemple #2
0
        public async Task TimeSeriesInsightsHierarchies_Lifecycle()
        {
            // Arrange
            TimeSeriesInsightsClient      client            = GetClient();
            TimeSeriesInsightsHierarchies hierarchiesClient = client.GetHierarchiesClient();
            var tsiHiearchyNamePrefix = "hierarchy";
            var tsiSourceNamePrefix   = "hierarchySource";
            var tsiHierarchyName      = Recording.GenerateAlphaNumericId(tsiHiearchyNamePrefix);
            var hierarchyNames        = new List <string>
            {
                tsiHierarchyName
            };

            var hierarchySource = new TimeSeriesHierarchySource();

            hierarchySource.InstanceFieldNames.Add(Recording.GenerateAlphaNumericId(tsiSourceNamePrefix));
            var tsiHierarchy = new TimeSeriesHierarchy(tsiHierarchyName, hierarchySource);

            tsiHierarchy.Id = Recording.GenerateId();
            var timeSeriesHierarchies = new List <TimeSeriesHierarchy>
            {
                tsiHierarchy
            };

            // Act and assert
            try
            {
                // Create Time Series hierarchies
                Response <TimeSeriesHierarchyOperationResult[]> createHierarchiesResult = await hierarchiesClient
                                                                                          .CreateOrReplaceAsync(timeSeriesHierarchies)
                                                                                          .ConfigureAwait(false);

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

                // This retry logic was added as the TSI hierarchies are not immediately available after creation
                await TestRetryHelper.RetryAsync <Response <TimeSeriesHierarchyOperationResult[]> >(async() =>
                {
                    // Get the created hierarchies by names
                    getHierarchiesByNamesResult = await hierarchiesClient
                                                  .GetByNameAsync(new List <string>()
                    {
                        tsiHierarchyName
                    })
                                                  .ConfigureAwait(false);

                    getHierarchiesByNamesResult.Value.Should().OnlyContain((errorResult) => errorResult.Error == null);
                    getHierarchiesByNamesResult.Value.Length.Should().Be(timeSeriesHierarchies.Count);
                    foreach (TimeSeriesHierarchyOperationResult hierarchiesResult in getHierarchiesByNamesResult.Value)
                    {
                        hierarchiesResult.Error.Should().BeNull();
                        hierarchiesResult.Hierarchy.Should().NotBeNull();
                        hierarchiesResult.Hierarchy.Id.Should().NotBeNullOrEmpty();
                    }
                    return(null);
                }, MaxNumberOfRetries, s_retryDelay);

                // Update hierarchies with adding a source and set Id
                var updateTsiName = Recording.GenerateAlphaNumericId(tsiHiearchyNamePrefix);
                hierarchyNames.Add(updateTsiName);

                hierarchySource.InstanceFieldNames.Add(Recording.GenerateAlphaNumericId(tsiSourceNamePrefix));
                var tsiHierarchyToAdd = new TimeSeriesHierarchy(updateTsiName, hierarchySource);
                tsiHierarchyToAdd.Id = Recording.GenerateId();
                timeSeriesHierarchies.Add(tsiHierarchyToAdd);

                Response <TimeSeriesHierarchyOperationResult[]> updateHierarchiesResult = await hierarchiesClient
                                                                                          .CreateOrReplaceAsync(timeSeriesHierarchies)
                                                                                          .ConfigureAwait(false);

                updateHierarchiesResult.Value.Should().OnlyContain((errorResult) => errorResult.Error == null);
                updateHierarchiesResult.Value.Length.Should().Be(timeSeriesHierarchies.Count);

                // This retry logic was added as the TSI hierarchies are not immediately available after creation
                await TestRetryHelper.RetryAsync <Response <TimeSeriesHierarchyOperationResult[]> >(async() =>
                {
                    // Get hierarchy by Id
                    Response <TimeSeriesHierarchyOperationResult[]> getHierarchyByIdResult = await hierarchiesClient
                                                                                             .GetByIdAsync(new string[] { tsiHierarchy.Id })
                                                                                             .ConfigureAwait(false);

                    getHierarchyByIdResult.Value.Length.Should().Be(1);
                    foreach (TimeSeriesHierarchyOperationResult hierarchyOperationResult in getHierarchyByIdResult.Value)
                    {
                        hierarchyOperationResult.Hierarchy.Should().NotBeNull();
                        hierarchyOperationResult.Error.Should().BeNull();
                        hierarchyOperationResult.Hierarchy.Name.Should().StartWith(tsiHiearchyNamePrefix);
                        hierarchyOperationResult.Hierarchy.Id.Should().Be(tsiHierarchy.Id);
                    }

                    return(null);
                }, MaxNumberOfRetries, s_retryDelay);

                // Get all Time Series hierarchies in the environment
                AsyncPageable <TimeSeriesHierarchy> getAllHierarchies = hierarchiesClient.GetAsync();
                await foreach (TimeSeriesHierarchy hierarchy in getAllHierarchies)
                {
                    hierarchy.Should().NotBeNull();
                }
            }
            finally
            {
                // clean up
                try
                {
                    Response <TimeSeriesOperationError[]> deleteHierarchiesResponse = await hierarchiesClient
                                                                                      .DeleteByNameAsync(hierarchyNames)
                                                                                      .ConfigureAwait(false);

                    // Assert that the response array does not have any error object set
                    deleteHierarchiesResponse.Value.Should().OnlyContain((errorResult) => errorResult == null);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Test clean up failed: {ex.Message}");
                    throw;
                }
            }
        }
Exemple #3
0
 internal TimeSeriesHierarchyOrError(TimeSeriesHierarchy hierarchy, TsiErrorBody error)
 {
     Hierarchy = hierarchy;
     Error     = error;
 }
Exemple #4
0
 internal TimeSeriesHierarchyOrError(TimeSeriesHierarchy hierarchy, InstancesOperationError error)
 {
     Hierarchy = hierarchy;
     Error     = error;
 }
        /// <summary>
        /// This sample demonstrates usage of Time Series Insights hierarchy 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 HIERARCHIES SAMPLE");

            #region Snippet:TimeSeriesInsightsSampleCreateHierarchies
            TimeSeriesInsightsHierarchies hierarchiesClient = client.GetHierarchiesClient();

            var hierarchySource = new TimeSeriesHierarchySource();
            hierarchySource.InstanceFieldNames.Add("hierarchyLevel1");

            var tsiHierarchy = new TimeSeriesHierarchy("sampleHierarchy", hierarchySource)
            {
                Id = "sampleHierarchyId"
            };

            var timeSeriesHierarchies = new List <TimeSeriesHierarchy>
            {
                tsiHierarchy
            };

            // Create Time Series hierarchies
            Response <TimeSeriesHierarchyOperationResult[]> createHierarchiesResult = await hierarchiesClient
                                                                                      .CreateOrReplaceAsync(timeSeriesHierarchies);

            // 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 < createHierarchiesResult.Value.Length; i++)
            {
                if (createHierarchiesResult.Value[i].Error == null)
                {
                    Console.WriteLine($"Created Time Series hierarchy successfully.");
                }
                else
                {
                    Console.WriteLine($"Failed to create a Time Series hierarchy: {createHierarchiesResult.Value[i].Error.Message}.");
                }
            }
            #endregion Snippet:TimeSeriesInsightsSampleCreateHierarchies

            #region Snippet:TimeSeriesInsightsSampleGetAllHierarchies
            // Get all Time Series hierarchies in the environment
            AsyncPageable <TimeSeriesHierarchy> getAllHierarchies = hierarchiesClient.GetAsync();
            await foreach (TimeSeriesHierarchy hierarchy in getAllHierarchies)
            {
                Console.WriteLine($"Retrieved Time Series Insights hierarchy with Id: '{hierarchy.Id}' and Name: '{hierarchy.Name}'.");
            }
            #endregion Snippet:TimeSeriesInsightsSampleGetAllHierarchies

            #region Snippet:TimeSeriesInsightsSampleReplaceHierarchies
            // Update hierarchies with adding a new instance field
            foreach (TimeSeriesHierarchy hierarchy in timeSeriesHierarchies)
            {
                hierarchy.Source.InstanceFieldNames.Add("hierarchyLevel2");
            }

            Response <TimeSeriesHierarchyOperationResult[]> updateHierarchiesResult = await hierarchiesClient
                                                                                      .CreateOrReplaceAsync(timeSeriesHierarchies);

            // 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 < updateHierarchiesResult.Value.Length; i++)
            {
                if (updateHierarchiesResult.Value[i].Error == null)
                {
                    Console.WriteLine($"Updated Time Series hierarchy successfully.");
                }
                else
                {
                    Console.WriteLine($"Failed to update a Time Series Insights hierarchy due to: {updateHierarchiesResult.Value[i].Error.Message}.");
                }
            }
            #endregion Snippet:TimeSeriesInsightsSampleReplaceHierarchies

            #region Snippet:TimeSeriesInsightsSampleGetHierarchiesById
            var tsiHierarchyIds = new List <string>
            {
                "sampleHierarchyId"
            };

            Response <TimeSeriesHierarchyOperationResult[]> getHierarchiesByIdsResult = await hierarchiesClient
                                                                                        .GetByIdAsync(tsiHierarchyIds);

            // The response of calling the API contains a list of hieararchy 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 < getHierarchiesByIdsResult.Value.Length; i++)
            {
                if (getHierarchiesByIdsResult.Value[i].Error == null)
                {
                    Console.WriteLine($"Retrieved Time Series hieararchy with Id: '{getHierarchiesByIdsResult.Value[i].Hierarchy.Id}'.");
                }
                else
                {
                    Console.WriteLine($"Failed to retrieve a Time Series hieararchy due to '{getHierarchiesByIdsResult.Value[i].Error.Message}'.");
                }
            }
            #endregion Snippet:TimeSeriesInsightsSampleGetHierarchiesById

            // Clean up
            try
            {
                #region Snippet:TimeSeriesInsightsSampleDeleteHierarchiesById
                // Delete Time Series hierarchies with Ids
                var tsiHierarchyIdsToDelete = new List <string>
                {
                    "sampleHiearchyId"
                };

                Response <TimeSeriesOperationError[]> deleteHierarchiesResponse = await hierarchiesClient
                                                                                  .DeleteByIdAsync(tsiHierarchyIdsToDelete);

                // 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 (TimeSeriesOperationError result in deleteHierarchiesResponse.Value)
                {
                    if (result != null)
                    {
                        Console.WriteLine($"Failed to delete a Time Series Insights hierarchy: {result.Message}.");
                    }
                    else
                    {
                        Console.WriteLine($"Deleted a Time Series Insights hierarchy successfully.");
                    }
                }
                #endregion Snippet:TimeSeriesInsightsSampleDeleteHierarchiesById
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Failed to delete Time Series Insights hierarchy: {ex.Message}");
            }
        }
 internal TimeSeriesHierarchyOperationResult(TimeSeriesHierarchy hierarchy, TimeSeriesOperationError error)
 {
     Hierarchy = hierarchy;
     Error     = error;
 }