|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Azure.IoT.TimeSeriesInsights.Models; |
| 8 | +using static Azure.IoT.TimeSeriesInsights.Samples.SampleLogger; |
| 9 | + |
| 10 | +namespace Azure.IoT.TimeSeriesInsights.Samples |
| 11 | +{ |
| 12 | + internal class HierarchiesSamples |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// This sample demonstrates usage of Time Series Insights hierarchy APIs. |
| 16 | + /// </summary> |
| 17 | + public async Task RunSamplesAsync(TimeSeriesInsightsClient client) |
| 18 | + { |
| 19 | + // For the purpose of keeping code snippets readable to the user, hardcoded string literals are used in place of assigned variables, eg Ids. |
| 20 | + // 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. |
| 21 | + |
| 22 | + PrintHeader("TIME SERIES INSIGHTS HIERARCHIES SAMPLE"); |
| 23 | + |
| 24 | + #region Snippet:TimeSeriesInsightsSampleCreateHierarchies |
| 25 | + var tsiHierarchyName = "sampleHierarchy"; |
| 26 | + var tsiInstanceField1 = "hierarchyLevel1"; |
| 27 | + var hierarchySource = new TimeSeriesHierarchySource(); |
| 28 | + hierarchySource.InstanceFieldNames.Add(tsiInstanceField1); |
| 29 | + |
| 30 | + var tsiHierarchy = new TimeSeriesHierarchy(tsiHierarchyName, hierarchySource); |
| 31 | + tsiHierarchy.Id = "sampleHierarchyId"; |
| 32 | + |
| 33 | + var timeSeriesHierarchies = new List<TimeSeriesHierarchy> |
| 34 | + { |
| 35 | + tsiHierarchy |
| 36 | + }; |
| 37 | + |
| 38 | + // Create Time Series hierarchies |
| 39 | + Response<TimeSeriesHierarchyOperationResult[]> createHierarchiesResult = await client |
| 40 | + .Hierarchies |
| 41 | + .CreateOrReplaceAsync(timeSeriesHierarchies) |
| 42 | + .ConfigureAwait(false); |
| 43 | + |
| 44 | + // The response of calling the API contains a list of error objects corresponding by position to the input parameter array in the request. |
| 45 | + // If the error object is set to null, this means the operation was a success. |
| 46 | + for (int i = 0; i < createHierarchiesResult.Value.Length; i++) |
| 47 | + { |
| 48 | + if (createHierarchiesResult.Value[i].Error == null) |
| 49 | + { |
| 50 | + Console.WriteLine($"Created Time Series hierarchy successfully."); |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + Console.WriteLine($"Failed to create a Time Series hierarchy: {createHierarchiesResult.Value[i].Error.Message}."); |
| 55 | + } |
| 56 | + } |
| 57 | + #endregion Snippet:TimeSeriesInsightsSampleCreateHierarchies |
| 58 | + |
| 59 | + #region Snippet:TimeSeriesInsightsSampleGetAllHierarchies |
| 60 | + // Get all Time Series hierarchies in the environment |
| 61 | + AsyncPageable<TimeSeriesHierarchy> getAllHierarchies = client.Hierarchies.GetAsync(); |
| 62 | + await foreach (TimeSeriesHierarchy hierarchy in getAllHierarchies) |
| 63 | + { |
| 64 | + Console.WriteLine($"Retrieved Time Series Insights hierarchy with Id: '{hierarchy.Id}' and Name: '{hierarchy.Name}'."); |
| 65 | + } |
| 66 | + #endregion Snippet:TimeSeriesInsightsSampleGetAllHierarchies |
| 67 | + |
| 68 | + #region Snippet:TimeSeriesInsightsSampleReplaceHierarchies |
| 69 | + // Update hierarchies with adding a new instance field |
| 70 | + var tsiInstanceField2 = "hierarchyLevel2"; |
| 71 | + foreach (TimeSeriesHierarchy hierarchy in timeSeriesHierarchies) |
| 72 | + { |
| 73 | + hierarchy.Source.InstanceFieldNames.Add(tsiInstanceField2); |
| 74 | + } |
| 75 | + |
| 76 | + Response<TimeSeriesHierarchyOperationResult[]> updateHierarchiesResult = await client |
| 77 | + .Hierarchies |
| 78 | + .CreateOrReplaceAsync(timeSeriesHierarchies) |
| 79 | + .ConfigureAwait(false); |
| 80 | + |
| 81 | + // The response of calling the API contains a list of error objects corresponding by position to the input parameter array in the request. |
| 82 | + // If the error object is set to null, this means the operation was a success. |
| 83 | + for (int i = 0; i < updateHierarchiesResult.Value.Length; i++) |
| 84 | + { |
| 85 | + if (updateHierarchiesResult.Value[i].Error == null) |
| 86 | + { |
| 87 | + Console.WriteLine($"Updated Time Series hierarchy successfully."); |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + Console.WriteLine($"Failed to update a Time Series Insights hierarchy due to: {updateHierarchiesResult.Value[i].Error.Message}."); |
| 92 | + } |
| 93 | + } |
| 94 | + #endregion Snippet:TimeSeriesInsightsSampleReplaceHierarchies |
| 95 | + |
| 96 | + #region Snippet:TimeSeriesInsightsSampleGetHierarchiesById |
| 97 | + var tsiHierarchyIds = new List<string> |
| 98 | + { |
| 99 | + "sampleHierarchyId" |
| 100 | + }; |
| 101 | + |
| 102 | + Response<TimeSeriesHierarchyOperationResult[]> getHierarchiesByIdsResult = await client |
| 103 | + .Hierarchies |
| 104 | + .GetByIdAsync(tsiHierarchyIds) |
| 105 | + .ConfigureAwait(false); |
| 106 | + |
| 107 | + // 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. |
| 108 | + // If the error object is set to null, this means the operation was a success. |
| 109 | + for (int i = 0; i < getHierarchiesByIdsResult.Value.Length; i++) |
| 110 | + { |
| 111 | + if (getHierarchiesByIdsResult.Value[i].Error == null) |
| 112 | + { |
| 113 | + Console.WriteLine($"Retrieved Time Series hieararchy with Id: '{getHierarchiesByIdsResult.Value[i].Hierarchy.Id}'."); |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + Console.WriteLine($"Failed to retrieve a Time Series hieararchy due to '{getHierarchiesByIdsResult.Value[i].Error.Message}'."); |
| 118 | + } |
| 119 | + } |
| 120 | + #endregion Snippet:TimeSeriesInsightsSampleGetHierarchiesById |
| 121 | + |
| 122 | + // Clean up |
| 123 | + try |
| 124 | + { |
| 125 | + #region Snippet:TimeSeriesInsightsSampleDeleteHierarchiesById |
| 126 | + // Delete Time Series hierarchies with Ids |
| 127 | + var tsiHierarchyIdsToDelete = new List<string> |
| 128 | + { |
| 129 | + "sampleHiearchyId" |
| 130 | + }; |
| 131 | + |
| 132 | + Response<TimeSeriesOperationError[]> deleteHierarchiesResponse = await client |
| 133 | + .Hierarchies |
| 134 | + .DeleteByIdAsync(tsiHierarchyIdsToDelete) |
| 135 | + .ConfigureAwait(false); |
| 136 | + |
| 137 | + // The response of calling the API contains a list of error objects corresponding by position to the input parameter |
| 138 | + // array in the request. If the error object is set to null, this means the operation was a success. |
| 139 | + foreach (TimeSeriesOperationError result in deleteHierarchiesResponse.Value) |
| 140 | + { |
| 141 | + if (result != null) |
| 142 | + { |
| 143 | + Console.WriteLine($"Failed to delete a Time Series Insights hierarchy: {result.Message}."); |
| 144 | + } |
| 145 | + else |
| 146 | + { |
| 147 | + Console.WriteLine($"Deleted a Time Series Insights hierarchy successfully."); |
| 148 | + } |
| 149 | + } |
| 150 | + #endregion Snippet:TimeSeriesInsightsSampleDeleteHierarchiesById |
| 151 | + } |
| 152 | + catch (Exception ex) |
| 153 | + { |
| 154 | + Console.WriteLine($"Failed to delete Time Series Insights hierarchy: {ex.Message}"); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments