Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TSI] Samples for types #20514

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public static async Task Main(string[] args)

var tsiInstancesSamples = new InstancesSamples();
await tsiInstancesSamples.RunSamplesAsync(tsiClient);

var tsiTypesSamples = new TypesSamples();
await tsiTypesSamples.RunSamplesAsync(tsiClient);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using static Azure.IoT.TimeSeriesInsights.Samples.SampleLogger;

namespace Azure.IoT.TimeSeriesInsights.Samples
{
internal class TypesSamples
{
/// <summary>
/// This sample demonstrates usage of Time Series Insights types APIs.
/// </summary>
public async Task RunSamplesAsync(TimeSeriesInsightsClient client)
{
PrintHeader("TIME SERIES INSIGHTS TYPES SAMPLE");

// Get model settings
TimeSeriesModelSettings modelSettings = await client.ModelSettings.GetAsync().ConfigureAwait(false);

#region Snippet:TimeSeriesInsightsSampleCreateType
// Create an aggregate type
var timeSeriesTypes = new List<TimeSeriesType>();

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

var timeSeriesTypesProperties = new Dictionary<string, string>
{
{ "Type1", Guid.NewGuid().ToString()},
{ "Type2", Guid.NewGuid().ToString()}
};

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

Response<TimeSeriesTypeOperationResult[]> createTypesResult = await client
.Types
.CreateOrReplaceAsync(timeSeriesTypes)
.ConfigureAwait(false);

// 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}.");
}
}
bikamani marked this conversation as resolved.
Show resolved Hide resolved
#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.
Response<TimeSeriesTypeOperationResult[]> getTypeByIdResults = await client
bikamani marked this conversation as resolved.
Show resolved Hide resolved
.Types
.GetByIdAsync(new string[] { modelSettings.DefaultTypeId })
.ConfigureAwait(false);

// 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 client
.Types
.CreateOrReplaceAsync(timeSeriesTypes)
.ConfigureAwait(false);

// 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 = client.Types.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 types with list of Ids created above.
Response<TimeSeriesOperationError[]> deleteTypesResponse = await client
.Types
.DeleteByIdAsync(timeSeriesTypesProperties.Values)
bikamani marked this conversation as resolved.
Show resolved Hide resolved
.ConfigureAwait(false);

// 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}.");
}
}
#endregion Snippet:TimeSeriesInsightsSampleDeleteTypeById
}
catch (Exception ex)
{
Console.WriteLine($"Failed to delete Time Series Insights type: {ex.Message}");
}
}
}
}