Skip to content

Commit f41a824

Browse files
committed
Ported changes from Particular/ServiceControl#5234 to keep both aligned
1 parent d3ddc65 commit f41a824

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

src/AppCommon/Commands/SqsCommand.cs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -74,41 +74,37 @@ protected override async Task<QueueDetails> GetData(CancellationToken cancellati
7474

7575
var tasks = queueNames.Select(async queueName =>
7676
{
77-
var datapoints = (await aws.GetMMetricsData(queueName, cancellationToken)).OrderBy(d => d.Timestamp).ToArray();
77+
var response = await aws.GetMMetricsData(queueName, cancellationToken);
78+
var datapoints = response.ToDictionary(
79+
k => DateOnly.FromDateTime(k.Timestamp.Value),
80+
v => (long)v.Sum.GetValueOrDefault(0)
81+
);
82+
83+
var maxThroughput = datapoints.Values.Max();
7884

79-
var maxThroughput = datapoints is { Length: > 0 } ?
80-
(long)datapoints.Select(d => d.Sum.GetValueOrDefault(0)).Max() : 0L;
8185
// Since we get 365 days of data, if there's no throughput in that amount of time, hard to legitimately call it an endpoint
8286
if (maxThroughput > 0)
8387
{
84-
var startTime = DateOnly.FromDateTime(datapoints.First().Timestamp.Value);
85-
var endTime = DateOnly.FromDateTime(datapoints.Last().Timestamp.Value);
86-
DateOnly currentDate = startTime;
87-
var dailyData = new Dictionary<DateOnly, DailyThroughput>();
88-
while (currentDate <= endTime)
89-
{
90-
dailyData.Add(currentDate, new DailyThroughput { MessageCount = 0, DateUTC = currentDate });
88+
var startTime = datapoints.Keys.Min();
89+
var endTime = datapoints.Keys.Max();
9190

92-
currentDate = currentDate.AddDays(1);
93-
}
91+
var dailyData = new List<DailyThroughput>(endTime.DayNumber - startTime.DayNumber + 1);
9492

95-
foreach (var datapoint in datapoints)
93+
for (var currentDate = startTime; currentDate <= endTime; currentDate = currentDate.AddDays(1))
9694
{
97-
// There is a bug in the AWS SDK. The timestamp is actually UTC time, eventhough the DateTime returned type says Local
98-
// See https://github.com/aws/aws-sdk-net/issues/167
99-
// So do not convert the timestamp to UTC time!
100-
if (datapoint.Timestamp.HasValue)
95+
datapoints.TryGetValue(currentDate, out var count);
96+
dailyData.Add(new DailyThroughput
10197
{
102-
currentDate = DateOnly.FromDateTime(datapoint.Timestamp.Value);
103-
dailyData[currentDate] = new DailyThroughput { MessageCount = (long)datapoint.Sum.GetValueOrDefault(0), DateUTC = currentDate };
104-
}
98+
MessageCount = count,
99+
DateUTC = currentDate
100+
});
105101
}
106102

107103
data.Add(new QueueThroughput
108104
{
109105
QueueName = queueName,
110106
Throughput = maxThroughput,
111-
DailyThroughputFromBroker = [.. dailyData.Values]
107+
DailyThroughputFromBroker = [.. dailyData]
112108
});
113109
}
114110

src/Query/AmazonSQS/AwsQuery.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ public async Task<List<Datapoint>> GetMMetricsData(string queueName, Cancellatio
8989
{
9090
Namespace = "AWS/SQS",
9191
MetricName = "NumberOfMessagesDeleted",
92-
StartTime = StartDate.ToDateTime(TimeOnly.MinValue),
93-
EndTime = EndDate.ToDateTime(TimeOnly.MaxValue),
92+
StartTime = StartDate.ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc),
93+
EndTime = EndDate.AddDays(1).ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc), // Exclusive
9494
Period = 24 * 60 * 60, // 1 day
9595
Statistics = ["Sum"],
9696
Dimensions = [

0 commit comments

Comments
 (0)