From 77bc014d656822c5577a7f69eaf77d1e7abb8938 Mon Sep 17 00:00:00 2001 From: Rune Nielsen Date: Tue, 13 Aug 2024 10:17:02 +0200 Subject: [PATCH] rewrite the trace all customer terminals to use less memory (#135) --- .../Reporting/CustomerTerminationReport.cs | 64 ++++++++++--------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/OpenFTTH.APIGateway/Reporting/CustomerTerminationReport.cs b/OpenFTTH.APIGateway/Reporting/CustomerTerminationReport.cs index 585a053..a6767fc 100644 --- a/OpenFTTH.APIGateway/Reporting/CustomerTerminationReport.cs +++ b/OpenFTTH.APIGateway/Reporting/CustomerTerminationReport.cs @@ -35,7 +35,7 @@ public CustomerTerminationReport(ILogger logger, IEve _routeNetworkState = routeNetworkState; } - public List TraceAllCustomerTerminations() + public IEnumerable TraceAllCustomerTerminations() { _logger.LogInformation("Service terminations trace started..."); var interestsProjection = _eventStore.Projections.Get(); @@ -44,7 +44,7 @@ public List TraceAllCustomerTerminations() var terminalEquipments = _utilityNetwork.TerminalEquipmentByEquipmentId.Values; - var traces = new List(); + var firstLineTrace = true; foreach (var terminalEquipment in terminalEquipments) { @@ -182,15 +182,20 @@ public List TraceAllCustomerTerminations() } } - traces.Add(traceLine); + // We do this to make sure that the CSV header is written first. + if (firstLineTrace) + { + yield return GetCsvHeaderFromTrace(traceLine); + firstLineTrace = false; + } + + yield return GetCsvLineFromTrace(traceLine); } } } } _logger.LogInformation("Service terminations trace finish!"); - - return GetCsvLinesFromTraceResult(traces); } private string GetNodeType(TraceState traceState, RouteNode routeNode, NodeContainer nodeContainer, TerminalEquipment hopEquipment, TerminalStructure hopTerminalStructure, Terminal hopTerminal) @@ -296,7 +301,9 @@ private void RemoveStageExcept(List currentTraceStage, string stage) } } - private bool CheckIfEndTerminalIsWithinRackEquipment(LookupCollection terminalEquipmentSpecifications, IGraphObject[] graphObjects) + private bool CheckIfEndTerminalIsWithinRackEquipment( + LookupCollection terminalEquipmentSpecifications, + IGraphObject[] graphObjects) { if (graphObjects.Length > 0) { @@ -322,14 +329,9 @@ private bool CheckIfEndTerminalIsWithinRackEquipment(LookupCollection GetCsvLinesFromTraceResult(List traces) + private string GetCsvHeaderFromTrace(InstallationTraceResultLine line) { - if (traces.Count == 0) - return new List() { "no installations found" }; - - var resultCsvLines = new List(traces.Count); - - var myType = traces.First().GetType(); + var myType = line.GetType(); var csvHeader = ""; @@ -341,33 +343,33 @@ private List GetCsvLinesFromTraceResult(List