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

Add QueryTimeSpan type #20841

Merged
merged 5 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 10 additions & 8 deletions sdk/monitor/Azure.Monitor.Query/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ You can query logs using the `LogsClient.QueryAsync`. The result would be return
```C# Snippet:QueryLogsAsTable
LogsClient client = new LogsClient(new DefaultAzureCredential());
string workspaceId = "<workspace_id>";
Response<LogsQueryResult> response = await client.QueryAsync(workspaceId, "AzureActivity | top 10 by TimeGenerated");
Response<LogsQueryResult> response = await client.QueryAsync(workspaceId, "AzureActivity | top 10 by TimeGenerated", TimeSpan.FromDays(1));

LogsQueryResultTable table = response.Value.PrimaryTable;

Expand Down Expand Up @@ -93,7 +93,8 @@ string workspaceId = "<workspace_id>";

// Query TOP 10 resource groups by event count
Response<IReadOnlyList<MyLogEntryModel>> response = await client.QueryAsync<MyLogEntryModel>(workspaceId,
"AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count");
"AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count",
TimeSpan.FromDays(1));

foreach (var logEntryModel in response.Value)
{
Expand All @@ -111,7 +112,8 @@ string workspaceId = "<workspace_id>";

// Query TOP 10 resource groups by event count
Response<IReadOnlyList<string>> response = await client.QueryAsync<string>(workspaceId,
"AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count | project ResourceGroup");
"AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count | project ResourceGroup",
TimeSpan.FromDays(1));

foreach (var resourceGroup in response.Value)
{
Expand All @@ -130,8 +132,8 @@ string workspaceId = "<workspace_id>";
// Query TOP 10 resource groups by event count
// And total event count
LogsBatchQuery batch = client.CreateBatchQuery();
string countQueryId = batch.AddQuery(workspaceId, "AzureActivity | count");
string topQueryId = batch.AddQuery(workspaceId, "AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count");
string countQueryId = batch.AddQuery(workspaceId, "AzureActivity | count", TimeSpan.FromDays(1));
string topQueryId = batch.AddQuery(workspaceId, "AzureActivity | summarize Count = count() by ResourceGroup | top 10 by Count", TimeSpan.FromDays(1));

Response<LogsBatchQueryResult> response = await batch.SubmitAsync();

Expand All @@ -152,7 +154,7 @@ You can also dynamically inspect the list of columns. The following example prin
```C# Snippet:QueryLogsPrintTable
LogsClient client = new LogsClient(new DefaultAzureCredential());
string workspaceId = "<workspace_id>";
Response<LogsQueryResult> response = await client.QueryAsync(workspaceId, "AzureActivity | top 10 by TimeGenerated");
Response<LogsQueryResult> response = await client.QueryAsync(workspaceId, "AzureActivity | top 10 by TimeGenerated", TimeSpan.FromDays(1));

LogsQueryResultTable table = response.Value.PrimaryTable;

Expand Down Expand Up @@ -182,7 +184,7 @@ Some queries take longer to execute than the default service timeout allows. You
```C# Snippet:QueryLogsPrintTable
LogsClient client = new LogsClient(new DefaultAzureCredential());
string workspaceId = "<workspace_id>";
Response<LogsQueryResult> response = await client.QueryAsync(workspaceId, "AzureActivity | top 10 by TimeGenerated");
Response<LogsQueryResult> response = await client.QueryAsync(workspaceId, "AzureActivity | top 10 by TimeGenerated", TimeSpan.FromDays(1));

LogsQueryResultTable table = response.Value.PrimaryTable;

Expand Down Expand Up @@ -218,7 +220,7 @@ string workspaceId = "<workspace_id>";
LogsClient client = new LogsClient(new DefaultAzureCredential());
try
{
await client.QueryAsync(workspaceId, "My Not So Valid Query");
await client.QueryAsync(workspaceId, "My Not So Valid Query", TimeSpan.FromDays(1));
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
namespace Azure.Monitor.Query
{
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public readonly partial struct DateTimeRange : System.IEquatable<Azure.Monitor.Query.DateTimeRange>
{
public DateTimeRange(System.DateTimeOffset startTime, System.DateTimeOffset endTime) { throw null; }
public DateTimeRange(System.DateTimeOffset startTime, System.TimeSpan duration) { throw null; }
public DateTimeRange(System.TimeSpan duration) { throw null; }
public DateTimeRange(System.TimeSpan duration, System.DateTimeOffset endTime) { throw null; }
public System.TimeSpan Duration { get { throw null; } }
public System.DateTimeOffset? EndTime { get { throw null; } }
public static Azure.Monitor.Query.DateTimeRange MaxValue { get { throw null; } }
public System.DateTimeOffset? StartTime { get { throw null; } }
public bool Equals(Azure.Monitor.Query.DateTimeRange other) { throw null; }
public override bool Equals(object obj) { throw null; }
public override int GetHashCode() { throw null; }
public static bool operator ==(Azure.Monitor.Query.DateTimeRange left, Azure.Monitor.Query.DateTimeRange right) { throw null; }
public static implicit operator Azure.Monitor.Query.DateTimeRange (System.TimeSpan timeSpan) { throw null; }
public static bool operator !=(Azure.Monitor.Query.DateTimeRange left, Azure.Monitor.Query.DateTimeRange right) { throw null; }
public override string ToString() { throw null; }
}
public partial class LogsBatchQuery
{
protected LogsBatchQuery() { }
public virtual string AddQuery(string workspaceId, string query, System.TimeSpan? timeSpan = default(System.TimeSpan?), Azure.Monitor.Query.LogsQueryOptions options = null) { throw null; }
public virtual string AddQuery(string workspaceId, string query, Azure.Monitor.Query.DateTimeRange timeRange, Azure.Monitor.Query.LogsQueryOptions options = null) { throw null; }
public virtual Azure.Response<Azure.Monitor.Query.Models.LogsBatchQueryResult> Submit(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Monitor.Query.Models.LogsBatchQueryResult>> SubmitAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
}
Expand All @@ -13,10 +32,10 @@ protected LogsClient() { }
public LogsClient(Azure.Core.TokenCredential credential) { }
public LogsClient(Azure.Core.TokenCredential credential, Azure.Monitor.Query.LogsClientOptions options) { }
public virtual Azure.Monitor.Query.LogsBatchQuery CreateBatchQuery() { throw null; }
public virtual Azure.Response<Azure.Monitor.Query.Models.LogsQueryResult> Query(string workspaceId, string query, System.TimeSpan? timeSpan = default(System.TimeSpan?), Azure.Monitor.Query.LogsQueryOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Monitor.Query.Models.LogsQueryResult>> QueryAsync(string workspaceId, string query, System.TimeSpan? timeSpan = default(System.TimeSpan?), Azure.Monitor.Query.LogsQueryOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<System.Collections.Generic.IReadOnlyList<T>>> QueryAsync<T>(string workspaceId, string query, System.TimeSpan? timeSpan = default(System.TimeSpan?), Azure.Monitor.Query.LogsQueryOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response<System.Collections.Generic.IReadOnlyList<T>> Query<T>(string workspaceId, string query, System.TimeSpan? timeSpan = default(System.TimeSpan?), Azure.Monitor.Query.LogsQueryOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response<Azure.Monitor.Query.Models.LogsQueryResult> Query(string workspaceId, string query, Azure.Monitor.Query.DateTimeRange timeRange, Azure.Monitor.Query.LogsQueryOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Monitor.Query.Models.LogsQueryResult>> QueryAsync(string workspaceId, string query, Azure.Monitor.Query.DateTimeRange timeRange, Azure.Monitor.Query.LogsQueryOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<System.Collections.Generic.IReadOnlyList<T>>> QueryAsync<T>(string workspaceId, string query, Azure.Monitor.Query.DateTimeRange timeRange, Azure.Monitor.Query.LogsQueryOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response<System.Collections.Generic.IReadOnlyList<T>> Query<T>(string workspaceId, string query, Azure.Monitor.Query.DateTimeRange timeRange, Azure.Monitor.Query.LogsQueryOptions options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
}
public partial class LogsClientOptions : Azure.Core.ClientOptions
{
Expand All @@ -36,13 +55,11 @@ public partial class MetricQueryOptions
{
public MetricQueryOptions() { }
public System.Collections.Generic.IList<Azure.Monitor.Query.Models.MetricAggregationType> Aggregations { get { throw null; } }
public System.TimeSpan? Duration { get { throw null; } set { } }
public System.DateTimeOffset? EndTime { get { throw null; } set { } }
public string Filter { get { throw null; } set { } }
public System.TimeSpan? Interval { get { throw null; } set { } }
public string MetricNamespace { get { throw null; } set { } }
public string OrderBy { get { throw null; } set { } }
public System.DateTimeOffset? StartTime { get { throw null; } set { } }
public Azure.Monitor.Query.DateTimeRange? TimeSpan { get { throw null; } set { } }
public int? Top { get { throw null; } set { } }
}
public partial class MetricsClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<Compile Include="$(AzureCoreSharedSources)HttpMessageSanitizer.cs" LinkBase="Shared" />
<Compile Include="$(AzureCoreSharedSources)TaskExtensions.cs" LinkBase="Shared" />
<Compile Include="$(AzureCoreSharedSources)OperationHelpers.cs" LinkBase="Shared" />
<Compile Include="$(AzureCoreSharedSources)HashCodeBuilder.cs" LinkBase="Shared" />
<Compile Include="$(AzureCoreSharedSources)AzureResourceProviderNamespaceAttribute.cs" LinkBase="Shared" />
</ItemGroup>

Expand Down
150 changes: 150 additions & 0 deletions sdk/monitor/Azure.Monitor.Query/src/DateTimeRange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Azure.Core;

namespace Azure.Monitor.Query
{
/// <summary>
/// Represents a span of time over which the query would be executed.
/// </summary>
public readonly struct DateTimeRange : IEquatable<DateTimeRange>
{
/// <summary>
/// Represents the maximum <see cref="DateTimeRange"/>.
/// </summary>
public static DateTimeRange MaxValue => new DateTimeRange(TimeSpan.MaxValue);

/// <summary>
/// Gets the duration of the range.
/// </summary>
public TimeSpan Duration { get; }

/// <summary>
/// Gets the start time of the range.
/// </summary>
public DateTimeOffset? StartTime { get; }

/// <summary>
/// Gets the end time of the range.
/// </summary>
public DateTimeOffset? EndTime { get; }

/// <summary>
/// Initializes an instance of <see cref="DateTimeRange"/> using a duration value.
/// The exact query range would be determined by the service when executing the query.
/// </summary>
/// <param name="duration">The duration of the range.</param>
public DateTimeRange(TimeSpan duration)
{
Duration = duration;
StartTime = null;
EndTime = null;
}

/// <summary>
/// Initializes an instance of <see cref="DateTimeRange"/> using a start time and a duration value.
/// </summary>
/// <param name="startTime">The start of the range.</param>
/// <param name="duration">The duration of the range.</param>
public DateTimeRange(DateTimeOffset startTime, TimeSpan duration)
{
Duration = duration;
StartTime = startTime;
EndTime = null;
}

/// <summary>
/// Initializes an instance of <see cref="DateTimeRange"/> using a duration and an end time.
/// </summary>
/// <param name="duration">The duration of the range.</param>
/// <param name="endTime">The end of the range.</param>
public DateTimeRange(TimeSpan duration, DateTimeOffset endTime)
{
Duration = duration;
StartTime = null;
EndTime = endTime;
}

/// <summary>
/// Initializes an instance of <see cref="DateTimeRange"/> using a start time and an end time.
/// </summary>
/// <param name="startTime">The start of the range.</param>
/// <param name="endTime">The end of the range.</param>
public DateTimeRange(DateTimeOffset startTime, DateTimeOffset endTime)
{
Duration = endTime - startTime;
StartTime = startTime;
EndTime = endTime;
}

/// <summary>
/// Converts a <see cref="TimeSpan"/> to a <see cref="DateTimeRange"/>.
/// </summary>
/// <param name="timeSpan">The <see cref="TimeSpan"/> value to convert.</param>
public static implicit operator DateTimeRange(TimeSpan timeSpan) => new DateTimeRange(timeSpan);

/// <inheritdoc />
public override string ToString()
{
var startTime = StartTime != null ? TypeFormatters.ToString(StartTime.Value, "o") : null;
var endTime = EndTime != null ? TypeFormatters.ToString(EndTime.Value, "o") : null;
var duration = TypeFormatters.ToString(Duration, "P");

switch (startTime, endTime, duration)
{
case (string, string, _):
return $"{startTime}/{endTime}";
case (string, null, string):
return $"{startTime}/{duration}";
case (null, string, string):
return $"{duration}/{endTime}";
default:
return duration;
}
}

/// <inheritdoc />
public bool Equals(DateTimeRange other)
{
return Duration.Equals(other.Duration) &&
Nullable.Equals(StartTime, other.StartTime) &&
Nullable.Equals(EndTime, other.EndTime);
}

/// <inheritdoc />
public override bool Equals(object obj)
{
return obj is DateTimeRange other && Equals(other);
}

/// <summary>
/// Determines if two <see cref="DateTimeRange"/> values are the same.
/// </summary>
/// <param name="left">The first <see cref="DateTimeRange"/> to compare.</param>
/// <param name="right">The second <see cref="DateTimeRange"/> to compare.</param>
/// <returns>True if <paramref name="left"/> and <paramref name="right"/> are the same; otherwise, false.</returns>
public static bool operator ==(DateTimeRange left, DateTimeRange right)
{
return left.Equals(right);
}

/// <summary>
/// Determines if two <see cref="DateTimeRange"/> values are different.
/// </summary>
/// <param name="left">The first <see cref="DateTimeRange"/> to compare.</param>
/// <param name="right">The second <see cref="DateTimeRange"/> to compare.</param>
/// <returns>True if <paramref name="left"/> and <paramref name="right"/> are different; otherwise, false.</returns>
public static bool operator !=(DateTimeRange left, DateTimeRange right)
{
return !left.Equals(right);
}

/// <inheritdoc />
public override int GetHashCode()
{
return HashCodeBuilder.Combine(StartTime, EndTime, Duration);
}
}
}
Loading