Fix storage metrics not being accurate
This commit is contained in:
@@ -13,6 +13,8 @@ namespace SharepointToolbox.Services.Export;
|
||||
public class StorageHtmlExportService
|
||||
{
|
||||
private int _togIdx;
|
||||
private string[] _kindLabels = Array.Empty<string>();
|
||||
private string[] _kindLabelsHtml = Array.Empty<string>();
|
||||
|
||||
/// <summary>
|
||||
/// Builds a self-contained HTML report with one collapsible row per
|
||||
@@ -21,10 +23,18 @@ public class StorageHtmlExportService
|
||||
/// breakdown section is desired.
|
||||
/// </summary>
|
||||
public string BuildHtml(IReadOnlyList<StorageNode> nodes, ReportBranding? branding = null)
|
||||
{
|
||||
var sb = new StringBuilder(3072 + nodes.Count * 340);
|
||||
BuildHtmlCore(sb, nodes, branding);
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private void BuildHtmlCore(StringBuilder sb, IReadOnlyList<StorageNode> nodes, ReportBranding? branding)
|
||||
{
|
||||
var T = TranslationSource.Instance;
|
||||
_togIdx = 0;
|
||||
var sb = new StringBuilder();
|
||||
_kindLabels = BuildKindLabelCache();
|
||||
_kindLabelsHtml = BuildHtmlEncodedCache(_kindLabels);
|
||||
|
||||
sb.AppendLine("<!DOCTYPE html>");
|
||||
sb.AppendLine("<html lang=\"en\">");
|
||||
@@ -60,11 +70,18 @@ public class StorageHtmlExportService
|
||||
sb.Append(BrandingHtmlHelper.BuildBrandingHeader(branding));
|
||||
sb.AppendLine($"<h1>{T["report.title.storage"]}</h1>");
|
||||
|
||||
// Summary cards
|
||||
var rootNodes0 = nodes.Where(n => n.IndentLevel == 0).ToList();
|
||||
long siteTotal0 = rootNodes0.Sum(n => n.TotalSizeBytes);
|
||||
long versionTotal0 = rootNodes0.Sum(n => n.VersionSizeBytes);
|
||||
long fileTotal0 = rootNodes0.Sum(n => n.TotalFileCount);
|
||||
// Single-pass root aggregation: replaces 4 separate enumerations
|
||||
// (.Where().ToList() + 3× .Sum() + a final .Where() during render).
|
||||
var rootNodes0 = new List<StorageNode>(Math.Min(nodes.Count, 64));
|
||||
long siteTotal0 = 0, versionTotal0 = 0, fileTotal0 = 0;
|
||||
foreach (var n in nodes)
|
||||
{
|
||||
if (n.IndentLevel != 0) continue;
|
||||
rootNodes0.Add(n);
|
||||
siteTotal0 += n.TotalSizeBytes;
|
||||
versionTotal0 += n.VersionSizeBytes;
|
||||
fileTotal0 += n.TotalFileCount;
|
||||
}
|
||||
|
||||
sb.AppendLine($"""
|
||||
<div style="display:flex;gap:16px;margin:16px 0;flex-wrap:wrap">
|
||||
@@ -90,10 +107,10 @@ public class StorageHtmlExportService
|
||||
<tbody>
|
||||
""");
|
||||
|
||||
// Only iterate root-level nodes; RenderNode recurses into Children
|
||||
// inline. Iterating the flat list would render every descendant a
|
||||
// second time as a top-level row.
|
||||
foreach (var node in nodes.Where(n => n.IndentLevel == 0))
|
||||
// Render only the pre-materialized root list — recursing into
|
||||
// Children handles descendants. Iterating the flat list would render
|
||||
// every descendant a second time as a top-level row.
|
||||
foreach (var node in rootNodes0)
|
||||
{
|
||||
RenderNode(sb, node);
|
||||
}
|
||||
@@ -105,18 +122,24 @@ public class StorageHtmlExportService
|
||||
|
||||
sb.AppendLine($"<p class=\"generated\">{T["report.text.generated_colon"]} {DateTime.Now:yyyy-MM-dd HH:mm}</p>");
|
||||
sb.AppendLine("</body></html>");
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds an HTML report including a file-type breakdown chart section.
|
||||
/// </summary>
|
||||
public string BuildHtml(IReadOnlyList<StorageNode> nodes, IReadOnlyList<FileTypeMetric> fileTypeMetrics, ReportBranding? branding = null)
|
||||
{
|
||||
var sb = new StringBuilder(4096 + nodes.Count * 340 + fileTypeMetrics.Count * 220);
|
||||
BuildHtmlCore(sb, nodes, fileTypeMetrics, branding);
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private void BuildHtmlCore(StringBuilder sb, IReadOnlyList<StorageNode> nodes, IReadOnlyList<FileTypeMetric> fileTypeMetrics, ReportBranding? branding)
|
||||
{
|
||||
var T = TranslationSource.Instance;
|
||||
_togIdx = 0;
|
||||
var sb = new StringBuilder();
|
||||
_kindLabels = BuildKindLabelCache();
|
||||
_kindLabelsHtml = BuildHtmlEncodedCache(_kindLabels);
|
||||
|
||||
sb.AppendLine("<!DOCTYPE html>");
|
||||
sb.AppendLine("<html lang=\"en\">");
|
||||
@@ -163,11 +186,17 @@ public class StorageHtmlExportService
|
||||
sb.Append(BrandingHtmlHelper.BuildBrandingHeader(branding));
|
||||
sb.AppendLine($"<h1>{T["report.title.storage"]}</h1>");
|
||||
|
||||
// ── Summary cards ──
|
||||
var rootNodes = nodes.Where(n => n.IndentLevel == 0).ToList();
|
||||
long siteTotal = rootNodes.Sum(n => n.TotalSizeBytes);
|
||||
long versionTotal = rootNodes.Sum(n => n.VersionSizeBytes);
|
||||
long fileTotal = rootNodes.Sum(n => n.TotalFileCount);
|
||||
// ── Summary cards (single-pass aggregation) ──
|
||||
var rootNodes = new List<StorageNode>(Math.Min(nodes.Count, 64));
|
||||
long siteTotal = 0, versionTotal = 0, fileTotal = 0;
|
||||
foreach (var n in nodes)
|
||||
{
|
||||
if (n.IndentLevel != 0) continue;
|
||||
rootNodes.Add(n);
|
||||
siteTotal += n.TotalSizeBytes;
|
||||
versionTotal += n.VersionSizeBytes;
|
||||
fileTotal += n.TotalFileCount;
|
||||
}
|
||||
|
||||
sb.AppendLine("<div class=\"stats\">");
|
||||
sb.AppendLine($" <div class=\"stat-card\"><div class=\"value\">{FormatSize(siteTotal)}</div><div class=\"label\">{T["report.stat.total_size"]}</div></div>");
|
||||
@@ -227,10 +256,10 @@ public class StorageHtmlExportService
|
||||
<tbody>
|
||||
""");
|
||||
|
||||
// Only iterate root-level nodes; RenderNode recurses into Children
|
||||
// inline. Iterating the flat list would render every descendant a
|
||||
// second time as a top-level row.
|
||||
foreach (var node in nodes.Where(n => n.IndentLevel == 0))
|
||||
// Render only the pre-materialized root list — recursing into
|
||||
// Children handles descendants. Iterating the flat list would render
|
||||
// every descendant a second time as a top-level row.
|
||||
foreach (var node in rootNodes)
|
||||
{
|
||||
RenderNode(sb, node);
|
||||
}
|
||||
@@ -242,22 +271,24 @@ public class StorageHtmlExportService
|
||||
|
||||
sb.AppendLine($"<p class=\"generated\">{T["report.text.generated_colon"]} {DateTime.Now:yyyy-MM-dd HH:mm}</p>");
|
||||
sb.AppendLine("</body></html>");
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
/// <summary>Writes the library-only HTML report to <paramref name="filePath"/>.</summary>
|
||||
public async Task WriteAsync(IReadOnlyList<StorageNode> nodes, string filePath, CancellationToken ct, ReportBranding? branding = null)
|
||||
{
|
||||
var html = BuildHtml(nodes, branding);
|
||||
await File.WriteAllTextAsync(filePath, html, Encoding.UTF8, ct);
|
||||
// Build into StringBuilder, stream chunks straight to disk —
|
||||
// skips a full-document char-array copy from sb.ToString().
|
||||
var sb = new StringBuilder(3072 + nodes.Count * 340);
|
||||
BuildHtmlCore(sb, nodes, branding);
|
||||
await ExportFileWriter.WriteHtmlChunksAsync(filePath, sb, ct);
|
||||
}
|
||||
|
||||
/// <summary>Writes the HTML report including the file-type breakdown chart.</summary>
|
||||
public async Task WriteAsync(IReadOnlyList<StorageNode> nodes, IReadOnlyList<FileTypeMetric> fileTypeMetrics, string filePath, CancellationToken ct, ReportBranding? branding = null)
|
||||
{
|
||||
var html = BuildHtml(nodes, fileTypeMetrics, branding);
|
||||
await File.WriteAllTextAsync(filePath, html, Encoding.UTF8, ct);
|
||||
var sb = new StringBuilder(4096 + nodes.Count * 340 + fileTypeMetrics.Count * 220);
|
||||
BuildHtmlCore(sb, nodes, fileTypeMetrics, branding);
|
||||
await ExportFileWriter.WriteHtmlChunksAsync(filePath, sb, ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -313,21 +344,7 @@ public class StorageHtmlExportService
|
||||
? $"<button class=\"toggle-btn\" onclick=\"toggle({myIdx})\">▶</button>{HtmlEncode(node.Name)}"
|
||||
: $"<span style=\"margin-left:{node.IndentLevel * 16}px\">{HtmlEncode(node.Name)}</span>";
|
||||
|
||||
string lastMod = node.LastModified.HasValue
|
||||
? node.LastModified.Value.ToString("yyyy-MM-dd")
|
||||
: string.Empty;
|
||||
|
||||
sb.AppendLine($"""
|
||||
<tr>
|
||||
<td>{nameCell}</td>
|
||||
<td>{HtmlEncode(KindLabel(node.Kind))}</td>
|
||||
<td>{HtmlEncode(node.SiteTitle)}</td>
|
||||
<td class="num">{node.TotalFileCount:N0}</td>
|
||||
<td class="num">{FormatSize(node.TotalSizeBytes)}</td>
|
||||
<td class="num">{FormatSize(node.VersionSizeBytes)}</td>
|
||||
<td>{lastMod}</td>
|
||||
</tr>
|
||||
""");
|
||||
AppendRow(sb, node, nameCell);
|
||||
|
||||
if (hasChildren)
|
||||
{
|
||||
@@ -352,21 +369,7 @@ public class StorageHtmlExportService
|
||||
? $"<span style=\"{indent}\"><button class=\"toggle-btn\" onclick=\"toggle({myIdx})\">▶</button>{HtmlEncode(node.Name)}</span>"
|
||||
: $"<span style=\"{indent}\">{HtmlEncode(node.Name)}</span>";
|
||||
|
||||
string lastMod = node.LastModified.HasValue
|
||||
? node.LastModified.Value.ToString("yyyy-MM-dd")
|
||||
: string.Empty;
|
||||
|
||||
sb.AppendLine($"""
|
||||
<tr>
|
||||
<td>{nameCell}</td>
|
||||
<td>{HtmlEncode(KindLabel(node.Kind))}</td>
|
||||
<td>{HtmlEncode(node.SiteTitle)}</td>
|
||||
<td class="num">{node.TotalFileCount:N0}</td>
|
||||
<td class="num">{FormatSize(node.TotalSizeBytes)}</td>
|
||||
<td class="num">{FormatSize(node.VersionSizeBytes)}</td>
|
||||
<td>{lastMod}</td>
|
||||
</tr>
|
||||
""");
|
||||
AppendRow(sb, node, nameCell);
|
||||
|
||||
if (hasChildren)
|
||||
{
|
||||
@@ -381,6 +384,35 @@ public class StorageHtmlExportService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Appends one data row given the pre-rendered name cell. Hot path:
|
||||
/// pulls localized kind labels from <see cref="_kindLabelsHtml"/> instead
|
||||
/// of going through <c>ResourceManager.GetString</c> + <c>HtmlEncode</c>
|
||||
/// per row.
|
||||
/// </summary>
|
||||
private void AppendRow(StringBuilder sb, StorageNode node, string nameCell)
|
||||
{
|
||||
int kindIdx = (int)node.Kind;
|
||||
string kindLabel = (uint)kindIdx < (uint)_kindLabelsHtml.Length
|
||||
? _kindLabelsHtml[kindIdx]
|
||||
: HtmlEncode(node.Kind.ToString());
|
||||
string lastMod = node.LastModified.HasValue
|
||||
? node.LastModified.Value.ToString("yyyy-MM-dd")
|
||||
: string.Empty;
|
||||
|
||||
sb.AppendLine($"""
|
||||
<tr>
|
||||
<td>{nameCell}</td>
|
||||
<td>{kindLabel}</td>
|
||||
<td>{HtmlEncode(node.SiteTitle)}</td>
|
||||
<td class="num">{node.TotalFileCount:N0}</td>
|
||||
<td class="num">{FormatSize(node.TotalSizeBytes)}</td>
|
||||
<td class="num">{FormatSize(node.VersionSizeBytes)}</td>
|
||||
<td>{lastMod}</td>
|
||||
</tr>
|
||||
""");
|
||||
}
|
||||
|
||||
private static string FormatSize(long bytes)
|
||||
{
|
||||
if (bytes >= 1_073_741_824L) return $"{bytes / 1_073_741_824.0:F2} GB";
|
||||
@@ -406,4 +438,28 @@ public class StorageHtmlExportService
|
||||
_ => kind.ToString()
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pre-resolves localized labels for every <see cref="StorageNodeKind"/>
|
||||
/// once per export. Cached array index lookup avoids
|
||||
/// <c>ResourceManager.GetString</c> per row in hot rendering loops.
|
||||
/// </summary>
|
||||
private static string[] BuildKindLabelCache()
|
||||
{
|
||||
var values = (StorageNodeKind[])Enum.GetValues(typeof(StorageNodeKind));
|
||||
int max = 0;
|
||||
foreach (var v in values) { int i = (int)v; if (i > max) max = i; }
|
||||
var cache = new string[max + 1];
|
||||
for (int i = 0; i < cache.Length; i++) cache[i] = ((StorageNodeKind)i).ToString();
|
||||
foreach (var v in values) cache[(int)v] = KindLabel(v);
|
||||
return cache;
|
||||
}
|
||||
|
||||
/// <summary>HTML-encodes each entry of <paramref name="raw"/> once.</summary>
|
||||
private static string[] BuildHtmlEncodedCache(string[] raw)
|
||||
{
|
||||
var encoded = new string[raw.Length];
|
||||
for (int i = 0; i < raw.Length; i++) encoded[i] = HtmlEncode(raw[i]);
|
||||
return encoded;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user