Skip to content

Commit 537d317

Browse files
author
Basel Rustum
authored
[TSI] Add Model Settings sample (Azure#20612)
1 parent 0ef016a commit 537d317

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.Text.Json;
7+
using System.Threading.Tasks;
8+
using static Azure.IoT.TimeSeriesInsights.Samples.SampleLogger;
9+
10+
namespace Azure.IoT.TimeSeriesInsights.Samples
11+
{
12+
internal class ModelSettingsSamples
13+
{
14+
/// <summary>
15+
/// This sample demonstrates getting Time Series model settings, updating model settings and changing the default type Id for a model.
16+
/// </summary>
17+
public async Task RunSamplesAsync(TimeSeriesInsightsClient client)
18+
{
19+
PrintHeader("TIME SERIES INSIGHTS MODEL SETTINGS SAMPLE");
20+
21+
#region Snippet:TimeSeriesInsightsSampleGetModelSettings
22+
Response<TimeSeriesModelSettings> getModelSettingsResponse = await client.ModelSettings.GetAsync();
23+
Console.WriteLine($"Retrieved Time Series Insights model settings:\n{JsonSerializer.Serialize(getModelSettingsResponse.Value)}");
24+
#endregion Snippet:TimeSeriesInsightsSampleGetModelSettings
25+
26+
// Store the default type Id so it can be used during clean up
27+
string defaultTypeId = getModelSettingsResponse.Value.DefaultTypeId;
28+
29+
#region Snippet:TimeSeriesInsightsSampleUpdateModelSettingsName
30+
Response<TimeSeriesModelSettings> updateModelSettingsNameResponse = await client.ModelSettings.UpdateNameAsync("NewModelSettingsName");
31+
Console.WriteLine($"Updated Time Series Insights model settings name:\n" +
32+
$"{JsonSerializer.Serialize(updateModelSettingsNameResponse.Value)}");
33+
#endregion Snippet:TimeSeriesInsightsSampleUpdateModelSettingsName
34+
35+
// For every Time Series Insights environment, there is a default type that any newly created Time Series instance will be associated with.
36+
// You can change the default type for a TSI environment by creating a new type and calling the API to update the default type Id.
37+
38+
// Create a Time Series type.
39+
var aggregateVariable = new AggregateVariable(new TimeSeriesExpression("count()"));
40+
var variables = new Dictionary<string, TimeSeriesVariable>
41+
{
42+
{ "aggregateVariableName", aggregateVariable },
43+
};
44+
var type = new TimeSeriesType("tsiTypeName", variables);
45+
var timeSeriesTypes = new List<TimeSeriesType> { type };
46+
string tsiTypeId = null;
47+
Response<TimeSeriesTypeOperationResult[]> createTsiTypeResponse = await client.Types.CreateOrReplaceAsync(timeSeriesTypes);
48+
49+
// Ensure no error was reported as part of the response
50+
if (createTsiTypeResponse.Value[0].Error == null)
51+
{
52+
// Store the Time Series type id to use it for updating default type in model settings
53+
tsiTypeId = createTsiTypeResponse.Value[0].TimeSeriesType.Id;
54+
55+
#region Snippet:TimeSeriesInsightsSampleUpdateModelSettingsDefaultType
56+
Response<TimeSeriesModelSettings> updateDefaultTypeIdResponse = await client.ModelSettings.UpdateDefaultTypeIdAsync(tsiTypeId);
57+
Console.WriteLine($"Updated Time Series Insights model settings default type Id:\n" +
58+
$"{JsonSerializer.Serialize(updateDefaultTypeIdResponse.Value)}");
59+
#endregion Snippet:TimeSeriesInsightsSampleUpdateModelSettingsDefaultType
60+
}
61+
// Clean up
62+
try
63+
{
64+
// Revert back to the original default type Id
65+
await client.ModelSettings.UpdateDefaultTypeIdAsync(defaultTypeId);
66+
67+
// Delete the type created
68+
if (tsiTypeId != null)
69+
{
70+
await client.Types.DeleteByIdAsync(new List<string> { tsiTypeId });
71+
}
72+
}
73+
catch (Exception ex)
74+
{
75+
Console.WriteLine($"Failed at one of the clean up steps: {ex.Message}");
76+
}
77+
}
78+
}
79+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public static async Task Main(string[] args)
4141

4242
var tsiInstancesSamples = new InstancesSamples();
4343
await tsiInstancesSamples.RunSamplesAsync(tsiClient);
44+
45+
var tsiModelSettingsSamples = new ModelSettingsSamples();
46+
await tsiModelSettingsSamples.RunSamplesAsync(tsiClient);
4447
}
4548

4649
/// <summary>

sdk/timeseriesinsights/Azure.IoT.TimeSeriesInsights/src/ModelSettingsClient.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

44
using System;
@@ -41,6 +41,12 @@ internal ModelSettingsClient(ModelSettingsRestClient modelSettingsRestClient, Cl
4141
/// The model settings which includes model display name, Time Series Id properties and default type Id with the
4242
/// http response <see cref="Response{TimeSeriesModelSettings}"/>.
4343
/// </returns>
44+
/// <example>
45+
/// <code snippet="Snippet:TimeSeriesInsightsSampleGetModelSettings">
46+
/// Response&lt;TimeSeriesModelSettings&gt; getModelSettingsResponse = await client.ModelSettings.GetAsync();
47+
/// Console.WriteLine($&quot;Retrieved Time Series Insights model settings:\n{JsonSerializer.Serialize(getModelSettingsResponse.Value)}&quot;);
48+
/// </code>
49+
/// </example>
4450
public virtual async Task<Response<TimeSeriesModelSettings>> GetAsync(CancellationToken cancellationToken = default)
4551
{
4652
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TimeSeriesInsightsClient)}.{nameof(Get)}");
@@ -65,6 +71,9 @@ public virtual async Task<Response<TimeSeriesModelSettings>> GetAsync(Cancellati
6571
/// The model settings which includes model display name, Time Series Id properties and default type Id with the
6672
/// http response <see cref="Response{TimeSeriesModelSettings}"/>.
6773
/// </returns>
74+
/// <seealso cref="GetAsync(CancellationToken)">
75+
/// See the asynchronous version of this method for examples.
76+
/// </seealso>
6877
public virtual Response<TimeSeriesModelSettings> Get(CancellationToken cancellationToken = default)
6978
{
7079
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TimeSeriesInsightsClient)}.{nameof(Get)}");
@@ -87,6 +96,13 @@ public virtual Response<TimeSeriesModelSettings> Get(CancellationToken cancellat
8796
/// <param name="name">Model display name which is mutable by the user. Initial value is &quot;DefaultModel&quot;.</param>
8897
/// <param name="cancellationToken">The cancellation token.</param>
8998
/// <returns>The updated model settings with the http response <see cref="Response{TimeSeriesModelSettings}"/>.</returns>
99+
/// <example>
100+
/// <code snippet="Snippet:TimeSeriesInsightsSampleUpdateModelSettingsName">
101+
/// Response&lt;TimeSeriesModelSettings&gt; updateModelSettingsNameResponse = await client.ModelSettings.UpdateNameAsync(&quot;NewModelSettingsName&quot;);
102+
/// Console.WriteLine($&quot;Updated Time Series Insights model settings name:\n&quot; +
103+
/// $&quot;{JsonSerializer.Serialize(updateModelSettingsNameResponse.Value)}&quot;);
104+
/// </code>
105+
/// </example>
90106
public virtual async Task<Response<TimeSeriesModelSettings>> UpdateNameAsync(string name, CancellationToken cancellationToken = default)
91107
{
92108
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TimeSeriesInsightsClient)}.{nameof(UpdateName)}");
@@ -110,6 +126,9 @@ public virtual async Task<Response<TimeSeriesModelSettings>> UpdateNameAsync(str
110126
/// <param name="name">Model display name which is mutable by the user. Initial value is &quot;DefaultModel&quot;.</param>
111127
/// <param name="cancellationToken">The cancellation token.</param>
112128
/// <returns>The updated model settings with the http response <see cref="Response{TimeSeriesModelSettings}"/>.</returns>
129+
/// <seealso cref="UpdateNameAsync(string, CancellationToken)">
130+
/// See the asynchronous version of this method for examples.
131+
/// </seealso>
113132
public virtual Response<TimeSeriesModelSettings> UpdateName(string name, CancellationToken cancellationToken = default)
114133
{
115134
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TimeSeriesInsightsClient)}.{nameof(UpdateName)}");
@@ -134,6 +153,13 @@ public virtual Response<TimeSeriesModelSettings> UpdateName(string name, Cancell
134153
/// <param name="defaultTypeId">Default type Id of the model that new instances will automatically belong to.</param>
135154
/// <param name="cancellationToken">The cancellation token.</param>
136155
/// <returns>The updated model settings with the http response <see cref="Response{TimeSeriesModelSettings}"/>.</returns>
156+
/// <example>
157+
/// <code snippet="Snippet:TimeSeriesInsightsSampleUpdateModelSettingsDefaultType">
158+
/// Response&lt;TimeSeriesModelSettings&gt; updateDefaultTypeIdResponse = await client.ModelSettings.UpdateDefaultTypeIdAsync(tsiTypeId);
159+
/// Console.WriteLine($&quot;Updated Time Series Insights model settings default type Id:\n&quot; +
160+
/// $&quot;{JsonSerializer.Serialize(updateDefaultTypeIdResponse.Value)}&quot;);
161+
/// </code>
162+
/// </example>
137163
public virtual async Task<Response<TimeSeriesModelSettings>> UpdateDefaultTypeIdAsync(string defaultTypeId, CancellationToken cancellationToken = default)
138164
{
139165
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TimeSeriesInsightsClient)}.{nameof(UpdateDefaultTypeId)}");
@@ -157,6 +183,9 @@ public virtual async Task<Response<TimeSeriesModelSettings>> UpdateDefaultTypeId
157183
/// <param name="defaultTypeId">Default type Id of the model that new instances will automatically belong to.</param>
158184
/// <param name="cancellationToken">The cancellation token.</param>
159185
/// <returns>The updated model settings with the http response <see cref="Response{TimeSeriesModelSettings}"/>.</returns>
186+
/// <seealso cref="UpdateDefaultTypeIdAsync(string, CancellationToken)">
187+
/// See the asynchronous version of this method for examples.
188+
/// </seealso>
160189
public virtual Response<TimeSeriesModelSettings> UpdateDefaultTypeId(string defaultTypeId, CancellationToken cancellationToken = default)
161190
{
162191
using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(TimeSeriesInsightsClient)}.{nameof(UpdateDefaultTypeId)}");

0 commit comments

Comments
 (0)