fix PowerOutageSummary and optimize ALertService structure

This commit is contained in:
2026-06-01 19:26:59 +03:30
parent 40fe6e897a
commit 3f7815874a
7 changed files with 612 additions and 402 deletions

View File

@@ -129,11 +129,12 @@ public class PowerOutageController : ControllerBase
return BadRequest(new { error = "شناسه دستگاه نامعتبر است" });
}
// Get all outage events within the date range
var filter = new PowerOutageFilter
{
StartDateUtc = startUtc,
EndDateUtc = endUtc,
PageSize = int.MaxValue // Get all records for summary
PageSize = int.MaxValue
};
var result = await _powerOutageLogService.ListAsync(deviceId, filter, cancellationToken);
@@ -141,6 +142,26 @@ public class PowerOutageController : ControllerBase
var outages = result.Items.Where(x => x.CurrentPower == 0).ToList();
var connections = result.Items.Where(x => x.CurrentPower == 1).ToList();
// For outages that don't have a matching connect within the range,
// look beyond the end date to find when power was restored
var unmatchedOutages = outages
.Where(o => !connections.Any(c => c.TimestampUtc > o.TimestampUtc))
.ToList();
if (unmatchedOutages.Count > 0 && endUtc.HasValue)
{
// Fetch any connect events after the range end for unmatched outages
var extendedFilter = new PowerOutageFilter
{
StartDateUtc = endUtc.Value,
PageSize = int.MaxValue
};
var extendedResult = await _powerOutageLogService.ListAsync(deviceId, extendedFilter, cancellationToken);
var futureConnections = extendedResult.Items.Where(x => x.CurrentPower == 1).ToList();
connections.AddRange(futureConnections);
}
// Calculate total downtime by pairing disconnects with subsequent connects
long totalDowntimeSeconds = 0;
var pairedOutages = new List<PowerOutagePairDto>();
@@ -167,7 +188,7 @@ public class PowerOutageController : ControllerBase
}
else
{
// Still disconnected (no subsequent connect)
// Still disconnected (no subsequent connect found anywhere)
pairedOutages.Add(new PowerOutagePairDto
{
DisconnectedAt = outage.TimestampUtc,