Skip to content
Merged
Next Next commit
fixed string format
  • Loading branch information
Piotr Wieczorek committed Sep 17, 2024
commit 637b2f90cb03d5fa411b8f36e0870c4f7a81c457
8 changes: 6 additions & 2 deletions sdk/monitor/Azure.Monitor.Query/src/MetricsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,12 @@ private async Task<Response<MetricsQueryResourcesResult>> ExecuteBatchAsync(IEnu
{
if (options.TimeRange != null)
{
startTime = options.TimeRange.Value.Start.ToString();
endTime = options.TimeRange.Value.End.ToString();
DateTimeOffset? startTimeDto = options.TimeRange.Value.Start;
DateTimeOffset? endTimeDto = options.TimeRange.Value.End;
TimeSpan duration = options.TimeRange.Value.Duration;

startTime = startTimeDto.ToIsoString() ?? endTimeDto?.Subtract(duration).ToIsoString();
endTime = endTimeDto.ToIsoString() ?? startTimeDto?.Add(duration).ToIsoString();
}
aggregations = MetricsClientExtensions.CommaJoin(options.Aggregations);
top = options.Size;
Expand Down
17 changes: 17 additions & 0 deletions sdk/monitor/Azure.Monitor.Query/src/MetricsClientExtensions.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.Globalization;
using System.Linq;
using System.Text;
using Azure.Monitor.Query.Models;
Expand Down Expand Up @@ -51,5 +52,21 @@ internal static IList<string> CommaSplit(string value) =>
new List<string>() :
// TODO: #10600 - Verify we don't need to worry about escaping
new List<string>(value.Split(','));

internal static string ToIsoString(this DateTimeOffset value)
{
if (value.Offset == TimeSpan.Zero)
{
// Some Azure service required 0-offset dates to be formatted without the
// -00:00 part
const string roundtripZFormat = "yyyy-MM-ddTHH:mm:ss.fffffffZ";
return value.ToString(roundtripZFormat, CultureInfo.InvariantCulture);
}

return value.ToString("O", CultureInfo.InvariantCulture);
}

internal static string ToIsoString(this DateTimeOffset? value)
=> value != null ? value.Value.ToIsoString() : null;
}
}
18 changes: 2 additions & 16 deletions sdk/monitor/Azure.Monitor.Query/src/QueryTimeRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using System;
using System.Globalization;
using System.Xml;
using Azure.Core;

Expand Down Expand Up @@ -105,21 +104,8 @@ public override string ToString()

internal string ToIsoString()
{
string ToString(DateTimeOffset value)
{
if (value.Offset == TimeSpan.Zero)
{
// Some Azure service required 0-offset dates to be formatted without the
// -00:00 part
const string roundtripZFormat = "yyyy-MM-ddTHH:mm:ss.fffffffZ";
return value.ToString(roundtripZFormat, CultureInfo.InvariantCulture);
}

return value.ToString("O", CultureInfo.InvariantCulture);
}

var startTime = Start != null ? ToString(Start.Value) : null;
var endTime = End != null ? ToString(End.Value) : null;
var startTime = Start.ToIsoString();
var endTime = End.ToIsoString();
var duration = XmlConvert.ToString(Duration);

switch (startTime, endTime, duration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,5 +368,37 @@ public void MetricsQueryResourcesInvalid()
metricNames: new List<string> { "Ingress" },
metricNamespace: "Microsoft.Storage/storageAccounts"));
}

[RecordedTest]
public async Task MetricsQueryMetricsStartEnd()
{
var client = CreateMetricsClient();
var queryOptions = new MetricsQueryResourcesOptions
{
TimeRange = new QueryTimeRange(DateTimeOffset.MinValue, DateTimeOffset.MaxValue)
};
var resourceId = TestEnvironment.StorageAccountId;

Response<MetricsQueryResourcesResult> metricsResultsResponse = await client.QueryResourcesAsync(
resourceIds: new List<ResourceIdentifier> { new ResourceIdentifier(resourceId) },
metricNames: new List<string> { "Ingress" },
metricNamespace: "Microsoft.Storage/storageAccounts",
options: queryOptions).ConfigureAwait(false);

MetricsQueryResourcesResult metricsQueryResults = metricsResultsResponse.Value;
Assert.AreEqual(1, metricsQueryResults.Values.Count);
Assert.AreEqual(TestEnvironment.StorageAccountId + "/providers/Microsoft.Insights/metrics/Ingress", metricsQueryResults.Values[0].Metrics[0].Id);
Assert.AreEqual("Microsoft.Storage/storageAccounts", metricsQueryResults.Values[0].Namespace);
for (int i = 0; i < metricsQueryResults.Values.Count; i++)
{
foreach (MetricResult value in metricsQueryResults.Values[i].Metrics)
{
for (int j = 0; j < value.TimeSeries.Count; j++)
{
Assert.GreaterOrEqual(value.TimeSeries[j].Values[i].Total, 0);
}
}
}
}
}
}