Skip to content

Commit

Permalink
Support visualization and dynamic in AMC (Azure#21856)
Browse files Browse the repository at this point in the history
  • Loading branch information
pakrym authored Jun 15, 2021
1 parent 14db3dc commit 06b6b7a
Show file tree
Hide file tree
Showing 29 changed files with 845 additions and 565 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public partial class LogsQueryOptions
public LogsQueryOptions() { }
public System.Collections.Generic.IList<string> AdditionalWorkspaces { get { throw null; } }
public bool IncludeStatistics { get { throw null; } set { } }
public bool IncludeVisualization { get { throw null; } set { } }
public System.TimeSpan? ServerTimeout { get { throw null; } set { } }
}
public partial class MetricsQueryClient
Expand Down Expand Up @@ -68,7 +69,7 @@ public MetricsQueryOptions() { }
}
public static partial class QueryModelFactory
{
public static Azure.Monitor.Query.Models.LogsQueryResult LogsQueryResult(System.Collections.Generic.IReadOnlyList<Azure.Monitor.Query.Models.LogsQueryResultTable> tables = null, System.Text.Json.JsonElement Statistics = default(System.Text.Json.JsonElement)) { throw null; }
public static Azure.Monitor.Query.Models.LogsQueryResult LogsQueryResult(System.Collections.Generic.IReadOnlyList<Azure.Monitor.Query.Models.LogsQueryResultTable> tables = null, System.Text.Json.JsonElement Statistics = default(System.Text.Json.JsonElement), System.Text.Json.JsonElement Visualization = default(System.Text.Json.JsonElement)) { throw null; }
public static Azure.Monitor.Query.Models.LogsQueryResultColumn LogsQueryResultColumn(string name = null, Azure.Monitor.Query.Models.LogColumnTypes type = default(Azure.Monitor.Query.Models.LogColumnTypes)) { throw null; }
public static Azure.Monitor.Query.Models.LogsQueryResultTable LogsQueryResultTable(string name = null, System.Collections.Generic.IReadOnlyList<Azure.Monitor.Query.Models.LogsQueryResultColumn> columns = null, System.Text.Json.JsonElement internalRows = default(System.Text.Json.JsonElement)) { throw null; }
public static Azure.Monitor.Query.Models.MetricAvailability MetricAvailability(System.TimeSpan? timeGrain = default(System.TimeSpan?), System.TimeSpan? retention = default(System.TimeSpan?)) { throw null; }
Expand Down Expand Up @@ -113,6 +114,7 @@ internal LogsQueryResult() { }
public Azure.Monitor.Query.Models.LogsQueryResultTable PrimaryTable { get { throw null; } }
public System.BinaryData Statistics { get { throw null; } }
public System.Collections.Generic.IReadOnlyList<Azure.Monitor.Query.Models.LogsQueryResultTable> Tables { get { throw null; } }
public System.BinaryData Visualization { get { throw null; } }
}
public partial class LogsQueryResultColumn
{
Expand All @@ -134,6 +136,8 @@ internal LogsQueryResultRow() { }
public decimal GetDecimal(string name) { throw null; }
public double GetDouble(int index) { throw null; }
public double GetDouble(string name) { throw null; }
public System.BinaryData GetDynamic(int index) { throw null; }
public System.BinaryData GetDynamic(string name) { throw null; }
public System.Guid GetGuid(int index) { throw null; }
public System.Guid GetGuid(string name) { throw null; }
public int GetInt32(int index) { throw null; }
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 34 additions & 4 deletions sdk/monitor/Azure.Monitor.Query/src/LogsQueryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -215,23 +216,52 @@ internal static QueryBody CreateQueryBody(string query, DateTimeRange timeRange,
queryBody.Timespan = timeRange.ToString();
}

if (options != null)
{
queryBody.Workspaces = options.AdditionalWorkspaces;
}

prefer = null;

StringBuilder preferBuilder = null;

if (options?.ServerTimeout is TimeSpan timeout)
{
prefer = "wait=" + (int) timeout.TotalSeconds;
preferBuilder ??= new();
preferBuilder.Append("wait=");
preferBuilder.Append((int) timeout.TotalSeconds);
}

if (options?.IncludeStatistics == true)
{
prefer += " include-statistics=true";
if (preferBuilder == null)
{
preferBuilder = new();
}
else
{
preferBuilder.Append(',');
}

preferBuilder.Append("include-statistics=true");
}

if (options != null)
if (options?.IncludeVisualization == true)
{
queryBody.Workspaces = options.AdditionalWorkspaces;
if (preferBuilder == null)
{
preferBuilder = new();
}
else
{
preferBuilder.Append(',');
}

preferBuilder.Append("include-render=true");
}

prefer = preferBuilder?.ToString();

return queryBody;
}

Expand Down
7 changes: 7 additions & 0 deletions sdk/monitor/Azure.Monitor.Query/src/LogsQueryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public class LogsQueryOptions
/// </summary>
public bool IncludeStatistics { get; set; }

/// <summary>
/// Gets or sets the value indicating whether to include query visualization as part of the response.
/// Visualization can be retrieved via the <see cref="LogsQueryResult.Visualization"/> property.
/// Defaults to <c>false</c>.
/// </summary>
public bool IncludeVisualization { get; set; }

/// <summary>
/// Gets a list of additional workspaces names to include in the query.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal static LogsBatchQueryResultInternal DeserializeLogsBatchQueryResultInte
Optional<ErrorInfo> error = default;
IReadOnlyList<LogsQueryResultTable> tables = default;
Optional<JsonElement> statistics = default;
Optional<JsonElement> render = default;

// This is the workaround to remove the double-encoding
if (element.ValueKind == JsonValueKind.String)
Expand Down Expand Up @@ -61,8 +62,13 @@ internal static LogsBatchQueryResultInternal DeserializeLogsBatchQueryResultInte
statistics = property.Value.Clone();
continue;
}
if (property.NameEquals("render"))
{
render = property.Value.Clone();
continue;
}
}
return new LogsBatchQueryResultInternal(tables, statistics, error.Value);
return new LogsBatchQueryResultInternal(tables, statistics, render, error.Value);
}
}
}
16 changes: 16 additions & 0 deletions sdk/monitor/Azure.Monitor.Query/src/Models/LogsQueryResultRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ internal LogsQueryResultRow(Dictionary<string, int> columnMap, IReadOnlyList<Log
/// <returns>The <see cref="Guid"/> value of the column.</returns>
public Guid GetGuid(int index) => _row[index].GetGuid();

/// <summary>
/// Gets the value of the column at the specified index as <see cref="BinaryData"/>.
/// </summary>
/// <param name="index">The column index.</param>
/// <returns>The <see cref="BinaryData"/> value of the column.</returns>
public BinaryData GetDynamic(int index) => new BinaryData(_row[index].GetString());

/// <summary>
/// Returns true if the value of the column at the specified index is null, otherwise false.
/// </summary>
Expand Down Expand Up @@ -163,6 +170,13 @@ internal LogsQueryResultRow(Dictionary<string, int> columnMap, IReadOnlyList<Log
/// <returns>The <see cref="Guid"/> value of the column.</returns>
public Guid GetGuid(string name) => GetGuid(_columnMap[name]);

/// <summary>
/// Gets the value of the column with the specified name as <see cref="Guid"/>.
/// </summary>
/// <param name="name">The column name.</param>
/// <returns>The <see cref="BinaryData"/> value of the column.</returns>
public BinaryData GetDynamic(string name) => GetDynamic(_columnMap[name]);

/// <summary>
/// Returns true if the value of the column with the specified name is null, otherwise false.
/// </summary>
Expand Down Expand Up @@ -203,6 +217,8 @@ public object GetObject(int index)
return GetTimeSpan(index);
case LogColumnTypes.DecimalTypeValue:
return GetDecimal(index);
case LogColumnTypes.DynamicValueTypeValue:
return GetDynamic(index);
}

switch (element.ValueKind)
Expand Down
10 changes: 9 additions & 1 deletion sdk/monitor/Azure.Monitor.Query/src/Models/LogsQueryResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,23 @@ public partial class LogsQueryResult
[CodeGenMember("Statistics")]
private readonly JsonElement _statistics;

[CodeGenMember("render")]
private readonly JsonElement _visualization;

// TODO: Handle not found
/// <summary>
/// Returns the primary result of the query.
/// </summary>
public LogsQueryResultTable PrimaryTable => Tables.Single(t => t.Name == "PrimaryResult");

/// <summary>
/// Returns the query statistics if the <see cref="LogsQueryOptions.IncludeStatistics"/> is set to <c>true</c>.
/// Returns the query statistics if the <see cref="LogsQueryOptions.IncludeStatistics"/> is set to <c>true</c>. Null otherwise.
/// </summary>
public BinaryData Statistics => _statistics.ValueKind == JsonValueKind.Undefined ? null : new BinaryData(_statistics.ToString());

/// <summary>
/// Returns the query visualization if the <see cref="LogsQueryOptions.IncludeVisualization"/> is set to <c>true</c>. Null otherwise.
/// </summary>
public BinaryData Visualization => _visualization.ValueKind == JsonValueKind.Undefined ? null : new BinaryData(_visualization.ToString());
}
}
1 change: 1 addition & 0 deletions sdk/monitor/Azure.Monitor.Query/src/RowBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ protected override bool TryGet<T>(BoundMemberInfo memberInfo, LogsQueryResultRow
else if (typeof(T) == typeof(Guid)) value = (T)(object)source.GetGuid(column);
else if (typeof(T) == typeof(DateTimeOffset)) value = (T)(object)source.GetDateTimeOffset(column);
else if (typeof(T) == typeof(TimeSpan)) value = (T)(object)source.GetTimeSpan(column);
else if (typeof(T) == typeof(BinaryData)) value = (T)(object)source.GetDynamic(column);

else if (typeof(T) == typeof(int?)) value = (T)(object)(int?)source.GetInt32(column);
else if (typeof(T) == typeof(bool?)) value = (T)(object)(bool?)source.GetBoolean(column);
Expand Down
12 changes: 11 additions & 1 deletion sdk/monitor/Azure.Monitor.Query/src/autorest.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,24 @@ directive:
}
```
### Add statistics
### Add statistics and render
``` yaml
directive:
- from: swagger-document
where: $.definitions.logQueryResult
transform: >
$.properties["statistics"] = { "type": "object" };
$.properties["render"] = { "type": "object" };
```
``` yaml
directive:
- from: swagger-document
where: $.definitions.queryResults
transform: >
$.properties["statistics"] = { "type": "object" };
$.properties["render"] = { "type": "object" };
```
### Make properties required
Expand Down
Loading

0 comments on commit 06b6b7a

Please sign in to comment.