Skip to content

Commit e36f244

Browse files
committed
Remove while loop
1 parent 92c5aa9 commit e36f244

File tree

1 file changed

+8
-25
lines changed

1 file changed

+8
-25
lines changed

src/ServiceControl.Transports.SQS/AmazonSQSQuery.cs

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -238,36 +238,19 @@ public override async IAsyncEnumerable<QueueThroughput> GetThroughputPerDay(IBro
238238
};
239239

240240
var resp = await cloudWatch!.GetMetricStatisticsAsync(req, cancellationToken);
241+
var dataPoints = resp.Datapoints.ToDictionary(x => DateOnly.FromDateTime(x.Timestamp!.Value.Date), x => (long)(x.Sum ?? 0));
241242

242-
DateOnly currentDate = startDate;
243-
var data = new Dictionary<DateOnly, QueueThroughput>();
244-
while (currentDate <= endDate)
243+
for (DateOnly currentDate = startDate; currentDate <= endDate; currentDate = currentDate.AddDays(1))
245244
{
246-
data.Add(currentDate, new QueueThroughput { TotalThroughput = 0, DateUTC = currentDate });
245+
dataPoints.TryGetValue(currentDate, out var sum);
247246

248-
currentDate = currentDate.AddDays(1);
249-
}
247+
logger.LogTrace("Queue throughput {QueueName} {Date} {Total}", brokerQueue.QueueName, currentDate, sum);
250248

251-
// Cloudwatch returns data points per 5 minutes in UTC
252-
foreach (var datapoint in resp.Datapoints ?? [])
253-
{
254-
logger.LogTrace("Datapoint {Timestamp:O} {Sum:N0}", datapoint.Timestamp, datapoint.Sum);
255-
if (datapoint.Timestamp.HasValue)
249+
yield return new QueueThroughput
256250
{
257-
if (data.TryGetValue(DateOnly.FromDateTime(datapoint.Timestamp.Value), out var queueThroughput))
258-
{
259-
queueThroughput.TotalThroughput = (long)datapoint.Sum.GetValueOrDefault(0);
260-
}
261-
else
262-
{
263-
logger.LogWarning("Datapoint for unknown date {Timestamp:O}", datapoint.Timestamp);
264-
}
265-
}
266-
}
267-
268-
foreach (QueueThroughput queueThroughput in data.Values)
269-
{
270-
yield return queueThroughput;
251+
TotalThroughput = sum,
252+
DateUTC = currentDate
253+
};
271254
}
272255
}
273256

0 commit comments

Comments
 (0)