Skip to content

Commit

Permalink
[TSI] Address Managed Team API Review comments (#20879)
Browse files Browse the repository at this point in the history
  • Loading branch information
Basel Rustum authored May 6, 2021
1 parent 4635967 commit 1c3405f
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public TimeSeriesInsightsClient(string environmentFqdn, Azure.Core.TokenCredenti
public virtual Azure.IoT.TimeSeriesInsights.TimeSeriesInsightsHierarchies Hierarchies { get { throw null; } }
public virtual Azure.IoT.TimeSeriesInsights.TimeSeriesInsightsInstances Instances { get { throw null; } }
public virtual Azure.IoT.TimeSeriesInsights.TimeSeriesInsightsModelSettings ModelSettings { get { throw null; } }
public virtual Azure.IoT.TimeSeriesInsights.TimeSeriesInsightsQuery Query { get { throw null; } }
public virtual Azure.IoT.TimeSeriesInsights.TimeSeriesInsightsQueries Queries { get { throw null; } }
public virtual Azure.IoT.TimeSeriesInsights.TimeSeriesInsightsTypes Types { get { throw null; } }
}
public partial class TimeSeriesInsightsClientOptions : Azure.Core.ClientOptions
Expand Down Expand Up @@ -274,9 +274,9 @@ protected TimeSeriesInsightsModelSettings() { }
public virtual Azure.Response<Azure.IoT.TimeSeriesInsights.TimeSeriesModelSettings> UpdateName(string name, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.IoT.TimeSeriesInsights.TimeSeriesModelSettings>> UpdateNameAsync(string name, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
}
public partial class TimeSeriesInsightsQuery
public partial class TimeSeriesInsightsQueries
{
protected TimeSeriesInsightsQuery() { }
protected TimeSeriesInsightsQueries() { }
public virtual Azure.IoT.TimeSeriesInsights.QueryAnalyzer CreateAggregateSeriesQueryAnalyzer(Azure.IoT.TimeSeriesInsights.TimeSeriesId timeSeriesId, System.DateTimeOffset startTime, System.DateTimeOffset endTime, System.TimeSpan interval, Azure.IoT.TimeSeriesInsights.QueryAggregateSeriesRequestOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.IoT.TimeSeriesInsights.QueryAnalyzer CreateAggregateSeriesQueryAnalyzer(Azure.IoT.TimeSeriesInsights.TimeSeriesId timeSeriesId, System.TimeSpan interval, System.TimeSpan timeSpan, System.DateTimeOffset? endTime = default(System.DateTimeOffset?), Azure.IoT.TimeSeriesInsights.QueryAggregateSeriesRequestOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.IoT.TimeSeriesInsights.QueryAnalyzer CreateEventsQueryAnalyzer(Azure.IoT.TimeSeriesInsights.TimeSeriesId timeSeriesId, System.DateTimeOffset startTime, System.DateTimeOffset endTime, Azure.IoT.TimeSeriesInsights.QueryEventsRequestOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ Use `Instances` in [TimeSeriesInsightsClient](https://github.com/Azure/azure-sdk

This code snippet demonstrates retrieving all created instances in your TSI environment.
```C# Snippet:TimeSeriesInsightsGetAllInstances
// Get all instances for the Time Series Insigths environment
// Get all instances for the Time Series Insights environment
AsyncPageable<TimeSeriesInstance> tsiInstances = client.Instances.GetAsync();
await foreach (TimeSeriesInstance tsiInstance in tsiInstances)
{
Expand Down Expand Up @@ -549,14 +549,27 @@ for (int i = 0; i < updateHierarchiesResult.Value.Length; i++)

## Time Series Insights Query

Use `Query` in [TimeSeriesInsightsClient](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/timeseriesinsights/Azure.IoT.TimeSeriesInsights/src/TimeSeriesInsightsClient.cs) to query for:
Use `Queries` in [TimeSeriesInsightsClient](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/timeseriesinsights/Azure.IoT.TimeSeriesInsights/src/TimeSeriesInsightsClient.cs) to query for:
- Raw events for a given Time Series ID and search span.
- Computed values and the associated event timestamps by applying calculations defined by variables on raw events. These variables can be defined in either the Time Series Model or provided inline in the query.
- Aggregated values and the associated interval timestamps by applying calculations defined by variables on raw events. These variables can be defined in either the Time Series Model or provided inline in the query.

Response for the `Query` APIs are of type `QueryAnalyzer`. The QueryAnalyzer allows a developer to query for pages of results, while being able to perform operations on the result set as a whole. For example, to get a list of `TimeSeriesPoint` in pages, call the `GetResultsAsync` method on the `QueryAnalyzer` object. You can enumerate an AsyncPageable object using the `async foreach` loop.

This code snippets demonstrates retrieving raw events from Time Series Insights environment using a start and end time.
This code snippet demonstrates querying for raw events with using a time span interval.

```C# Snippet:TimeSeriesInsightsSampleQueryEventsUsingTimeSpan
Console.WriteLine("\n\nQuery for raw humidity events over the past 30 seconds.\n");

QueryAnalyzer humidityEventsQueryAnalyzer = client.Queries.CreateEventsQueryAnalyzer(tsId, TimeSpan.FromSeconds(30));
await foreach (TimeSeriesPoint point in humidityEventsQueryAnalyzer.GetResultsAsync())
{
double? humidityValue = (double?)point.GetValue("Humidity");
Console.WriteLine($"{point.Timestamp} - Humidity: {humidityValue}");
}
```

The client library also provides a way to query for raw events using using a start and end time, as demonstrated in this code snippet.

```C# Snippet:TimeSeriesInsightsSampleQueryEvents
Console.WriteLine("\n\nQuery for raw temperature events over the past 10 minutes.\n");
Expand All @@ -565,34 +578,60 @@ Console.WriteLine("\n\nQuery for raw temperature events over the past 10 minutes
DateTimeOffset endTime = DateTime.UtcNow;
DateTimeOffset startTime = endTime.AddMinutes(-10);

QueryAnalyzer temperatureEventsQueryAnalyzer = client.Query.CreateEventsQueryAnalyzer(tsId, startTime, endTime);
QueryAnalyzer temperatureEventsQueryAnalyzer = client.Queries.CreateEventsQueryAnalyzer(tsId, startTime, endTime);
await foreach (TimeSeriesPoint point in temperatureEventsQueryAnalyzer.GetResultsAsync())
{
double? temperatureValue = (double?)point.GetValue("Temperature");
Console.WriteLine($"{point.Timestamp} - Temperature: {temperatureValue}");
TimeSeriesValue temperatureValue = point.GetValue("Temperature");

// Figure out what is the underlying type for the time series value. Since you know your Time Series Insights
// environment best, you probably do not need this logic and you can skip to directly casting to the proper
// type. This logic demonstrates how you can figure out what type to cast to in the case where you are not
// too familiar with the property type
Type valueType = temperatureValue.Type;
if (valueType == typeof(double?))
{
Console.WriteLine($"{point.Timestamp} - Temperature: {(double?)temperatureValue}");
}
else if (valueType == typeof(int?))
{
Console.WriteLine($"{point.Timestamp} - Temperature: {(int?)temperatureValue}");
}
else
{
Console.WriteLine("The type of the Time Series value for Temperature is not numeric.");
}
}
```

The client library also provides a way to query for raw events with using a time span interval.
```C# Snippet:TimeSeriesInsightsSampleQueryEventsUsingTimeSpan
Console.WriteLine("\n\nQuery for raw humidity events over the past 30 seconds.\n");
This code snippet demonstrates querying for series events. In this snippet, we query for the temperature both in Celsius and fahrenheit. The Time Series instance that we query from has predefined numeric variables, one for the Celsius and the other for Fahrenheit.

QueryAnalyzer humidityEventsQueryAnalyzer = client.Query.CreateEventsQueryAnalyzer(tsId, TimeSpan.FromSeconds(30));
await foreach (TimeSeriesPoint point in humidityEventsQueryAnalyzer.GetResultsAsync())
```C# Snippet:TimeSeriesInsightsSampleQuerySeries
Console.WriteLine($"\n\nQuery for temperature series in Celsius and Fahrenheit over the past 10 minutes. " +
$"The Time Series instance belongs to a type that has predefined numeric variable that represents the temperature " +
$"in Celsuis, and a predefined numeric variable that represents the temperature in Fahrenheit.\n");

DateTimeOffset endTime = DateTime.UtcNow;
DateTimeOffset startTime = endTime.AddMinutes(-10);
QueryAnalyzer seriesQueryAnalyzer = client.Queries.CreateSeriesQueryAnalyzer(
tsId,
startTime,
endTime);

await foreach (TimeSeriesPoint point in seriesQueryAnalyzer.GetResultsAsync())
{
double? humidityValue = (double?)point.GetValue("Humidity");
Console.WriteLine($"{point.Timestamp} - Humidity: {humidityValue}");
double? tempInCelsius = (double?)point.GetValue(celsiusVariableName);
double? tempInFahrenheit = (double?)point.GetValue(fahrenheitVariableName);

Console.WriteLine($"{point.Timestamp} - Average temperature in Celsius: {tempInCelsius}. " +
$"Average temperature in Fahrenheit: {tempInFahrenheit}.");
}
```

This code snippet demonstrates querying for series events. In this snippet, we query for the temperature both in Celsius and fahrenheit. Hence, we create two [numeric variables][tsi_numeric_variables], one for the Celsius and the other for Fahrenheit. These variables are then added as inline variables to the request options.
You can also query for series events with variables defined in the request options. In this snippet, we create two [numeric variables][tsi_numeric_variables], one for the Celsius and the other for Fahrenheit. These variables are then added as inline variables to the request options.

```C# Snippet:TimeSeriesInsightsSampleQuerySeries
```C# Snippet:TimeSeriesInsightsSampleQuerySeriesWithInlineVariables
Console.WriteLine("\n\nQuery for temperature series in Celsius and Fahrenheit over the past 10 minutes.\n");

DateTimeOffset endTime = DateTime.UtcNow;
DateTimeOffset startTime = endTime.AddMinutes(-10);

var celsiusVariable = new NumericVariable(
new TimeSeriesExpression("$event.Temperature"),
new TimeSeriesExpression("avg($value)"));
Expand All @@ -604,10 +643,10 @@ var querySeriesRequestOptions = new QuerySeriesRequestOptions();
querySeriesRequestOptions.InlineVariables["TemperatureInCelsius"] = celsiusVariable;
querySeriesRequestOptions.InlineVariables["TemperatureInFahrenheit"] = fahrenheitVariable;

QueryAnalyzer seriesQueryAnalyzer = client.Query.CreateSeriesQueryAnalyzer(
QueryAnalyzer seriesQueryAnalyzer = client.Queries.CreateSeriesQueryAnalyzer(
tsId,
startTime,
endTime,
TimeSpan.FromMinutes(10),
null,
querySeriesRequestOptions);

await foreach (TimeSeriesPoint point in seriesQueryAnalyzer.GetResultsAsync())
Expand All @@ -631,11 +670,13 @@ DateTimeOffset startTime = endTime.AddMinutes(-3);
var aggregateVariable = new AggregateVariable(
new TimeSeriesExpression("count()"));

var countVariableName = "Count";

var aggregateSeriesRequestOptions = new QueryAggregateSeriesRequestOptions();
aggregateSeriesRequestOptions.InlineVariables["Count"] = aggregateVariable;
aggregateSeriesRequestOptions.ProjectedVariables.Add("Count");
aggregateSeriesRequestOptions.InlineVariables[countVariableName] = aggregateVariable;
aggregateSeriesRequestOptions.ProjectedVariables.Add(countVariableName);

QueryAnalyzer aggregateSeriesQueryAnalyzer = client.Query.CreateAggregateSeriesQueryAnalyzer(
QueryAnalyzer aggregateSeriesQueryAnalyzer = client.Queries.CreateAggregateSeriesQueryAnalyzer(
tsId,
startTime,
endTime,
Expand All @@ -644,7 +685,7 @@ QueryAnalyzer aggregateSeriesQueryAnalyzer = client.Query.CreateAggregateSeriesQ

await foreach (TimeSeriesPoint point in aggregateSeriesQueryAnalyzer.GetResultsAsync())
{
long? temperatureCount = (long?)point.GetValue("Count");
long? temperatureCount = (long?)point.GetValue(countVariableName);
Console.WriteLine($"{point.Timestamp} - Temperature count: {temperatureCount}");
}
```
Expand All @@ -659,4 +700,4 @@ await foreach (TimeSeriesPoint point in aggregateSeriesQueryAnalyzer.GetResultsA
[tsi_id_learn_more]: https://docs.microsoft.com/azure/time-series-insights/how-to-select-tsid
[tsi_hierarchies_learn_more]: https://docs.microsoft.com/azure/time-series-insights/concepts-model-overview#time-series-model-hierarchies
[tsi_numeric_variables]: https://docs.microsoft.com/azure/time-series-insights/concepts-variables#numeric-variables
[tsi_aggregate_variables]: https://docs.microsoft.com/azure/time-series-insights/concepts-variables#aggregate-variables
[tsi_aggregate_variables]: https://docs.microsoft.com/azure/time-series-insights/concepts-variables#aggregate-variables
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public async Task RunSamplesAsync(TimeSeriesInsightsClient client)

#region Snippet:TimeSeriesInsightsGetAllInstances

// Get all instances for the Time Series Insigths environment
// Get all instances for the Time Series Insights environment
AsyncPageable<TimeSeriesInstance> tsiInstances = client.Instances.GetAsync();
await foreach (TimeSeriesInstance tsiInstance in tsiInstances)
{
Expand Down
Loading

0 comments on commit 1c3405f

Please sign in to comment.