Skip to content

Commit 339eaaf

Browse files
authored
[TSI] Bring sample changes to master (Azure#20755)
1 parent bfd0e2f commit 339eaaf

File tree

7 files changed

+577
-7
lines changed

7 files changed

+577
-7
lines changed

sdk/timeseriesinsights/Azure.IoT.TimeSeriesInsights/README.md

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ In order to interact with the Azure Time Series Insights service, you will need
2525

2626
## Key concepts
2727

28-
Coming soon.
28+
The main concepts of Time Series Insights client include:
29+
30+
- Instances client: To perform operations such as creating, listing, replacing and deleting Time Series instances.
31+
- Types client: To perform operations such as creating, listing, replacing and deleting Time Series types.
32+
- Hierarchies client: To perform operations such as creating, listing, replacing and deleting Time Series hierarchies.
33+
- Model Settings client: To perform operations such as getting and updating Time Series Model configuration settings.
34+
- Query client: To query for events, series and aggregate series on Time Series Insights.
2935

3036
### Thread safety
31-
We guarantee that all client instance methods are thread-safe and independent of each other ([guideline](https://azure.github.io/azure-sdk/dotnet_introduction.html#dotnet-service-methods-thread-safety)). This ensures that the recommendation of reusing client instances is always safe, even across threads.
37+
We guarantee that all client instance methods are thread-safe and independent of each other ([guideline](https://azure.github.io/azure-sdk/dotnet_introduction.html#dotnet-service-methods-thread-safety)). This ensures that reusing client instances is always safe, even across threads.
3238

3339
### Additional concepts
3440
<!-- CLIENT COMMON BAR -->
@@ -43,7 +49,7 @@ We guarantee that all client instance methods are thread-safe and independent of
4349

4450
## Examples
4551

46-
Coming soon.
52+
You can familiarize yourself with different APIs using [samples for Time Series Insights](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/timeseriesinsights/Azure.IoT.TimeSeriesInsights/samples).
4753

4854
## Source code folder structure
4955

@@ -78,7 +84,34 @@ Assembly properties required for running unit tests.
7884

7985
## Troubleshooting
8086

81-
Coming soon.
87+
Time Series Insights service operation failures are usually returned to the user as [TimeSeriesOperationError](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/timeseriesinsights/Azure.IoT.TimeSeriesInsights/src/Generated/Models/TimeSeriesOperationError.cs). The TimeSeriesOperationError response is either returned directly by the client library API, or as a nested property within the actual response for the client library API. For example, the DeleteByName API that is part of the hierarchies client returns a TimeSeriesOperationError directly. Whereas, the Replace API that is part of the instances client returns a [InstancesOperationResult](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/timeseriesinsights/Azure.IoT.TimeSeriesInsights/src/Generated/Models/InstancesOperationResult.cs), which has a TimeSeriesOperationError property nested within it.
88+
89+
Example below shows use of TimeSeriesInsightsSampleGetTypeById operation, iterate through response error to find out if a type does not exist.
90+
91+
```C# Snippet:TimeSeriesInsightsSampleGetTypeById
92+
// Code snippet below shows getting a default Type using Id
93+
// The default type Id can be obtained programmatically by using the ModelSettings client.
94+
95+
TimeSeriesModelSettings modelSettings = await client.ModelSettings.GetAsync().ConfigureAwait(false);
96+
Response<TimeSeriesTypeOperationResult[]> getTypeByIdResults = await client
97+
.Types
98+
.GetByIdAsync(new string[] { modelSettings.DefaultTypeId })
99+
.ConfigureAwait(false);
100+
101+
// 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.
102+
// If the error object is set to null, this means the operation was a success.
103+
for (int i = 0; i < getTypeByIdResults.Value.Length; i++)
104+
{
105+
if (getTypeByIdResults.Value[i].Error == null)
106+
{
107+
Console.WriteLine($"Retrieved Time Series type with Id: '{getTypeByIdResults.Value[i].TimeSeriesType.Id}'.");
108+
}
109+
else
110+
{
111+
Console.WriteLine($"Failed to retrieve a Time Series type due to '{getTypeByIdResults.Value[i].Error.Message}'.");
112+
}
113+
}
114+
```
82115

83116
## Next steps
84117

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
}

sdk/timeseriesinsights/Azure.IoT.TimeSeriesInsights/samples/TimeSeriesInsightsClientSample/Program.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public static async Task Main(string[] args)
4242
var tsiInstancesSamples = new InstancesSamples();
4343
await tsiInstancesSamples.RunSamplesAsync(tsiClient);
4444

45+
var tsiTypesSamples = new TypesSamples();
46+
await tsiTypesSamples.RunSamplesAsync(tsiClient);
47+
48+
var tsiHierarchiesSamples = new HierarchiesSamples();
49+
await tsiHierarchiesSamples.RunSamplesAsync(tsiClient);
50+
4551
var tsiModelSettingsSamples = new ModelSettingsSamples();
4652
await tsiModelSettingsSamples.RunSamplesAsync(tsiClient);
4753
}

sdk/timeseriesinsights/Azure.IoT.TimeSeriesInsights/samples/TimeSeriesInsightsClientSample/TimeSeriesInsightsClientSample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>

0 commit comments

Comments
 (0)