Skip to content

Commit

Permalink
rewrite the trace all customer terminals to use less memory (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
runeanielsen authored Aug 13, 2024
1 parent ff7df55 commit 77bc014
Showing 1 changed file with 33 additions and 31 deletions.
64 changes: 33 additions & 31 deletions OpenFTTH.APIGateway/Reporting/CustomerTerminationReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public CustomerTerminationReport(ILogger<CustomerTerminationReport> logger, IEve
_routeNetworkState = routeNetworkState;
}

public List<string> TraceAllCustomerTerminations()
public IEnumerable<string> TraceAllCustomerTerminations()
{
_logger.LogInformation("Service terminations trace started...");
var interestsProjection = _eventStore.Projections.Get<InterestsProjection>();
Expand All @@ -44,7 +44,7 @@ public List<string> TraceAllCustomerTerminations()

var terminalEquipments = _utilityNetwork.TerminalEquipmentByEquipmentId.Values;

var traces = new List<InstallationTraceResultLine>();
var firstLineTrace = true;

foreach (var terminalEquipment in terminalEquipments)
{
Expand Down Expand Up @@ -182,15 +182,20 @@ public List<string> 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)
Expand Down Expand Up @@ -296,7 +301,9 @@ private void RemoveStageExcept(List<string> currentTraceStage, string stage)
}
}

private bool CheckIfEndTerminalIsWithinRackEquipment(LookupCollection<TerminalEquipmentSpecification> terminalEquipmentSpecifications, IGraphObject[] graphObjects)
private bool CheckIfEndTerminalIsWithinRackEquipment(
LookupCollection<TerminalEquipmentSpecification> terminalEquipmentSpecifications,
IGraphObject[] graphObjects)
{
if (graphObjects.Length > 0)
{
Expand All @@ -322,14 +329,9 @@ private bool CheckIfEndTerminalIsWithinRackEquipment(LookupCollection<TerminalEq
return false;
}

private List<string> GetCsvLinesFromTraceResult(List<InstallationTraceResultLine> traces)
private string GetCsvHeaderFromTrace(InstallationTraceResultLine line)
{
if (traces.Count == 0)
return new List<string>() { "no installations found" };

var resultCsvLines = new List<string>(traces.Count);

var myType = traces.First().GetType();
var myType = line.GetType();

var csvHeader = "";

Expand All @@ -341,33 +343,33 @@ private List<string> GetCsvLinesFromTraceResult(List<InstallationTraceResultLine
csvHeader += $"\"{prop.Name}\"";
}

resultCsvLines.Add(csvHeader);

foreach (var line in traces)
{
string csvLine = "";
return csvHeader;
}

bool first = true;
private string GetCsvLineFromTrace(InstallationTraceResultLine line)
{
var myType = line.GetType();

foreach (PropertyInfo prop in myType.GetProperties().Reverse())
var first = true;
var csvLine = "";
foreach (PropertyInfo prop in myType.GetProperties().Reverse())
{
if (!first)
{
if (!first)
csvLine += ";";

object propValue = prop.GetValue(line, null);
csvLine += ";";
}

if (propValue != null)
{
csvLine += $"\"{propValue.ToString()}\"";
}
object propValue = prop.GetValue(line, null);

first = false;
if (propValue != null)
{
csvLine += $"\"{propValue.ToString()}\"";
}

resultCsvLines.Add(csvLine);
first = false;
}

return resultCsvLines;
return csvLine;
}
}

Expand Down

0 comments on commit 77bc014

Please sign in to comment.