perf(adminui): virtualize the CSV import grid; download exports from an endpoint (R04 P6)

The import review grid rendered every parsed row and re-scanned all rows three
times per render for its counts; RawCsvReviewView recomputes rows + counts once
per change and the grid renders through Virtualize. The export modal embedded the
whole CSV twice (textarea + base64 data: URI); it now shows a 50-line preview
and links to GET /api/raw/tags.csv (ConfigEditor, like /raw), which runs the same
RawTagCsvExport builder and sends it as an attachment.

Refs #745
Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
This commit is contained in:
Joseph Doherty
2026-10-22 17:40:40 -04:00
parent ce9679b671
commit 36e6ab590b
7 changed files with 356 additions and 70 deletions
@@ -2,15 +2,15 @@
Reads the device's tags (via the WP5-owned RawTagCsvExportReader — no IRawTreeService extension) and
renders them in the same column shape RawCsvImportModal consumes (T0-2 CsvWriter, per-driver
CsvColumnMap). The CSV is offered as a self-contained data-URI download anchor (no JS interop, no
external host — matches the app's CSP posture) plus a preview textarea.
CsvColumnMap). The download is a plain same-origin link to GET /api/raw/tags.csv (R04 P6) — the
file used to be embedded in the circuit twice (textarea + base64 data: URI); the modal now shows only
a bounded preview of the first RawTagCsvExport.PreviewLines lines.
Modal contract (the RawTree coordinator wires this in place of the OnExportCsv stub):
<RawCsvExportModal @bind-Visible="_exportVisible"
DeviceId="@_exportDeviceId" (may be blank if TagGroupId is supplied)
TagGroupId="@_exportTagGroupId" (when set, only that group's subtree is exported)
DriverType="@_exportDriverType" /> *@
@using System.Text
@using ZB.MOM.WW.OtOpcUa.AdminUI.Uns
@inject RawTagCsvExportReader ExportReader
@@ -43,7 +43,11 @@
<span>, group <span class="mono">@_groupFilter</span></span>
}
</p>
<textarea class="form-control mono small" rows="10" readonly>@_csv</textarea>
@if (_truncated)
{
<p class="small text-muted mb-1">Preview — first @RawTagCsvExport.PreviewLines lines. Download for the full file.</p>
}
<textarea class="form-control mono small" rows="10" readonly>@_preview</textarea>
}
</div>
<div class="modal-footer">
@@ -51,7 +55,7 @@
<button type="button" class="btn btn-outline-secondary" @onclick="Close">Close</button>
@if (!_loading && _error is null)
{
<a class="btn btn-primary" download="@_fileName" href="@_dataUri">Download CSV</a>
<a class="btn btn-primary" download="@_fileName" href="@_downloadUrl">Download CSV</a>
}
</div>
</div>
@@ -79,8 +83,9 @@
private bool _loading;
private bool _built;
private string? _error;
private string _csv = "";
private string _dataUri = "";
private string _preview = "";
private bool _truncated;
private string _downloadUrl = "";
private string _fileName = "tags.csv";
private int _rowCount;
private string? _groupFilter;
@@ -110,48 +115,22 @@
_error = null;
try
{
var deviceId = DeviceId;
_groupFilter = null;
if (!string.IsNullOrEmpty(TagGroupId))
{
var ctx = await ExportReader.ResolveGroupContextAsync(TagGroupId!);
if (IsStale(generation)) { return; }
if (ctx is { } c)
{
_groupFilter = c.GroupPath;
if (string.IsNullOrEmpty(deviceId))
{
deviceId = c.DeviceId;
}
}
}
if (string.IsNullOrEmpty(deviceId))
// The same builder the download endpoint runs, so the preview matches the file.
var export = await RawTagCsvExport.BuildAsync(ExportReader, DeviceId, TagGroupId, DriverType);
if (IsStale(generation)) { return; }
if (export is null)
{
_error = "Could not resolve the target device.";
return;
}
var tags = await ExportReader.ReadDeviceTagsAsync(deviceId);
if (IsStale(generation)) { return; }
// When exporting a single group, keep only that group's subtree (its path + descendants).
if (!string.IsNullOrEmpty(_groupFilter))
{
var prefix = _groupFilter;
tags = tags
.Where(t => t.TagGroupPath is not null &&
(t.TagGroupPath == prefix || t.TagGroupPath.StartsWith(prefix + "/", StringComparison.Ordinal)))
.ToList();
}
_rowCount = tags.Count;
_csv = RawTagCsvMapper.Export(DriverType, tags);
_fileName = BuildFileName(deviceId);
var base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(_csv));
_dataUri = $"data:text/csv;charset=utf-8;base64,{base64}";
_groupFilter = export.GroupPath;
_rowCount = export.RowCount;
_fileName = export.FileName;
// Only the bounded preview is kept on the component; the full CSV is not held by the circuit.
(_preview, _truncated) = RawTagCsvExport.Preview(export.Csv);
_downloadUrl = RawTagCsvExport.DownloadUrl(DeviceId, TagGroupId, DriverType);
}
catch (Exception ex)
{
@@ -163,18 +142,13 @@
}
}
private static string BuildFileName(string deviceId)
{
var safe = new string(deviceId.Select(c => char.IsLetterOrDigit(c) ? c : '-').ToArray());
return $"tags-{safe}.csv";
}
private async Task Close()
{
_built = false;
_openGeneration++;
_csv = "";
_dataUri = "";
_preview = "";
_truncated = false;
_downloadUrl = "";
Visible = false;
await VisibleChanged.InvokeAsync(false);
}
@@ -14,6 +14,7 @@
For a Device node: DeviceId = node.EntityId, TagGroupId = null.
For a TagGroup node: TagGroupId = node.EntityId (DeviceId is resolved from it if not passed). *@
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using ZB.MOM.WW.OtOpcUa.AdminUI.Uns
@inject IRawTreeService Svc
@inject RawTagCsvExportReader ExportReader
@@ -82,10 +83,10 @@
</tr>
</thead>
<tbody>
@foreach (var row in _result.Rows)
{
if (_removed.Contains(row.RowNumber)) { continue; }
<tr class="@(row.Ok ? "" : "table-danger")">
@* Virtualized (R04 P6): only the rows in the scrolled view render. *@
<Virtualize Items="@_view.VisibleRows" ItemSize="34"
SpacerElement="tr" Context="row">
<tr @key="row.RowNumber" class="@(row.Ok ? "" : "table-danger")">
<td>@(row.Ok ? "✓" : "✕")</td>
<td class="mono">@row.RowNumber</td>
<td class="mono">@row.Name</td>
@@ -124,7 +125,7 @@
@onclick="() => RemoveRow(row.RowNumber)">✕</button>
</td>
</tr>
}
</Virtualize>
</tbody>
</table>
</div>
@@ -184,7 +185,8 @@
[Parameter] public EventCallback OnSaved { get; set; }
private RawCsvParseResult? _result;
private readonly HashSet<int> _removed = new();
// The grid's filtered rows + counts, recomputed per change rather than per render (R04 P6).
private readonly RawCsvReviewView _view = new();
private List<string> _commitErrors = new();
private string? _fatal;
private bool _parsing;
@@ -202,9 +204,9 @@
private string TargetLabel =>
string.IsNullOrEmpty(_groupPrefix) ? _effectiveDeviceId : $"{_effectiveDeviceId} / {_groupPrefix}";
private int OkCount => _result?.Rows.Count(r => r.Ok && !_removed.Contains(r.RowNumber)) ?? 0;
private int ErrorCount => _result?.Rows.Count(r => !r.Ok && !_removed.Contains(r.RowNumber)) ?? 0;
private int RemainingCount => _result?.Rows.Count(r => !_removed.Contains(r.RowNumber)) ?? 0;
private int OkCount => _view.OkCount;
private int ErrorCount => _view.ErrorCount;
private int RemainingCount => _view.RemainingCount;
// Commit is unblocked only when every remaining row is valid AND at least one row remains.
private bool CanCommit =>
@@ -252,7 +254,7 @@
{
_fatal = null;
_commitErrors = new();
_removed.Clear();
_view.Set(null);
_parsing = true;
var generation = _openGeneration;
@@ -263,12 +265,14 @@
var text = await reader.ReadToEndAsync();
if (IsStale(generation)) { return; }
_result = RawTagCsvMapper.Parse(DriverType, text, _groupPrefix);
_view.Set(_result);
_fatal = _result.FatalError;
}
catch (Exception ex)
{
_fatal = $"Could not read the file: {ex.Message}";
_result = null;
_view.Set(null);
}
finally
{
@@ -276,14 +280,14 @@
}
}
private void RemoveRow(int rowNumber) => _removed.Add(rowNumber);
private void RemoveRow(int rowNumber) => _view.Remove(rowNumber);
private void Reset()
{
_result = null;
_fatal = null;
_commitErrors = new();
_removed.Clear();
_view.Set(null);
}
private async Task Commit()
@@ -299,10 +303,7 @@
var targetDeviceId = _effectiveDeviceId;
try
{
var rows = _result.Rows
.Where(r => r is { Ok: true, ImportRow: not null } && !_removed.Contains(r.RowNumber))
.Select(r => r.ImportRow!)
.ToList();
var rows = _view.CommitRows();
var outcome = await Svc.ImportTagsAsync(targetDeviceId, rows);
// The write has landed whether or not the modal was closed/reopened meanwhile, so the parent must