Skip to content

Commit 8245503

Browse files
committed
Update SQS to collect 365 days of metrics
1 parent 4cb57c9 commit 8245503

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

src/AppCommon/Commands/SqsCommand.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,39 @@ protected override async Task<QueueDetails> GetData(CancellationToken cancellati
7474

7575
var tasks = queueNames.Select(async queueName =>
7676
{
77-
var maxThroughput = await aws.GetMaxThroughput(queueName, cancellationToken).ConfigureAwait(false);
77+
var datapoints = await aws.GetMMetricsData(queueName, cancellationToken).ConfigureAwait(false);
7878

79-
// Since we get 30 days of data, if there's no throughput in that amount of time, hard to legitimately call it an endpoint
79+
var maxThroughput = datapoints is { Count: > 0 } ?
80+
(long)datapoints.Select(d => d.Sum.GetValueOrDefault(0)).Max() : 0L;
81+
// Since we get 365 days of data, if there's no throughput in that amount of time, hard to legitimately call it an endpoint
8082
if (maxThroughput > 0)
8183
{
84+
DateOnly currentDate = aws.StartDate;
85+
var dailyData = new Dictionary<DateOnly, DailyThroughput>();
86+
while (currentDate <= aws.EndDate)
87+
{
88+
dailyData.Add(currentDate, new DailyThroughput { MessageCount = 0, DateUTC = currentDate });
89+
90+
currentDate = currentDate.AddDays(1);
91+
}
92+
93+
foreach (var datapoint in datapoints)
94+
{
95+
// There is a bug in the AWS SDK. The timestamp is actually UTC time, eventhough the DateTime returned type says Local
96+
// See https://github.com/aws/aws-sdk-net/issues/167
97+
// So do not convert the timestamp to UTC time!
98+
if (datapoint.Timestamp.HasValue)
99+
{
100+
currentDate = DateOnly.FromDateTime(datapoint.Timestamp.Value);
101+
dailyData[currentDate] = new DailyThroughput { MessageCount = (long)datapoint.Sum.GetValueOrDefault(0), DateUTC = currentDate };
102+
}
103+
}
104+
82105
data.Add(new QueueThroughput
83106
{
84107
QueueName = queueName,
85-
Throughput = maxThroughput
108+
Throughput = maxThroughput,
109+
DailyThroughputFromBroker = [.. dailyData.Values]
86110
});
87111
}
88112

src/Query/AmazonSQS/AwsQuery.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public AwsQuery()
3535
QueueLimit = int.MaxValue
3636
});
3737
EndDate = DateOnly.FromDateTime(DateTime.UtcNow).AddDays(1);
38-
StartDate = EndDate.AddDays(-30);
38+
StartDate = EndDate.AddDays(-365);
3939

4040
sqs = new AmazonSQSClient();
4141
cloudWatch = new AmazonCloudWatchClient();
@@ -83,7 +83,7 @@ public async Task<List<string>> GetQueueNames(Action<int> onProgress, Cancellati
8383
}
8484
}
8585

86-
public async Task<long> GetMaxThroughput(string queueName, CancellationToken cancellationToken = default)
86+
public async Task<List<Datapoint>> GetMMetricsData(string queueName, CancellationToken cancellationToken = default)
8787
{
8888
var req = new GetMetricStatisticsRequest
8989
{
@@ -100,8 +100,7 @@ public async Task<long> GetMaxThroughput(string queueName, CancellationToken can
100100
using var lease = await rateLimiter.AcquireAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
101101
var resp = await cloudWatch.GetMetricStatisticsAsync(req, cancellationToken).ConfigureAwait(false);
102102

103-
return resp.Datapoints is { Count: > 0 } ?
104-
(long)resp.Datapoints.Select(d => d.Sum.GetValueOrDefault(0)).Max() : 0L;
103+
return resp.Datapoints ?? [];
105104
}
106105
}
107106
}

0 commit comments

Comments
 (0)