fix(ui): reconcile the log visualizer tick instead of clearing Rows

The 4s reconcile tick called Apply(), which did Rows.Clear() + refill on
every tick -- resetting the user's scroll position and selection while
they're mid-read, exactly what the overlay exists to avoid.

Reconcile granularly instead: find the overlap between the previous and
latest snapshot (the ring buffer is append-mostly, evicting from the
front as entries age out of the 30-min window), trim rows for evicted
entries off the tail, and insert new entries at the head. A tick with no
change now leaves Rows untouched. The WarnErrorOnly toggle still goes
through the full Apply() rebuild, unchanged.
This commit is contained in:
mika kuns
2026-08-11 18:32:39 +02:00
parent 1861832b99
commit d327a1007e
2 changed files with 123 additions and 3 deletions
@@ -54,8 +54,8 @@ public sealed partial class LogVisualizerViewModel : ViewModelBase
[RelayCommand]
public async Task RefreshAsync()
{
_all = await _worker.GetRecentLogsAsync();
Apply();
var latest = await _worker.GetRecentLogsAsync();
Reconcile(latest);
}
partial void OnWarnErrorOnlyChanged(bool value) => Apply();
@@ -68,10 +68,70 @@ public sealed partial class LogVisualizerViewModel : ViewModelBase
: _all;
foreach (var e in items.Reverse())
Rows.Add(new LogVisualizerRow(e.TimestampUtc.ToLocalTime().ToString("HH:mm:ss"), e.Message, e.Level));
UpdateStatusText();
}
// `_all` (oldest-first) mirrors the worker's LogRingBuffer: append-mostly, with the oldest
// entries falling off the front once they age past the 30-min window. A full Rows.Clear()
// on every 4s tick would reset the user's scroll position/selection while they're mid-read,
// so patch just the delta instead: entries still present keep their row, aged-out entries
// are trimmed off the (oldest) tail of Rows, new entries are inserted at the (newest) head.
private void Reconcile(IReadOnlyList<WorkerLogEntry> latest)
{
var previous = _all;
_all = latest;
if (previous.Count == 0)
{
Apply();
return;
}
var overlap = FindOverlap(previous, latest);
var evictedCount = previous.Count - overlap;
var added = latest.Skip(overlap).ToList();
if (evictedCount == 0 && added.Count == 0)
return; // nothing aged out, nothing new — leave Rows (and scroll/selection) untouched
if (evictedCount > 0)
{
var removeCount = WarnErrorOnly
? previous.Take(evictedCount).Count(e => e.Level is WorkerLogLevel.Warn or WorkerLogLevel.Error)
: evictedCount;
for (var i = 0; i < removeCount; i++)
Rows.RemoveAt(Rows.Count - 1);
}
foreach (var e in added)
{
if (WarnErrorOnly && e.Level is not (WorkerLogLevel.Warn or WorkerLogLevel.Error))
continue;
Rows.Insert(0, new LogVisualizerRow(e.TimestampUtc.ToLocalTime().ToString("HH:mm:ss"), e.Message, e.Level));
}
UpdateStatusText();
}
// The longest run of `previous`'s tail that reappears as `latest`'s head — i.e. how much of
// the old snapshot survived unevicted. 0 when nothing overlaps (e.g. right after opening).
private static int FindOverlap(IReadOnlyList<WorkerLogEntry> previous, IReadOnlyList<WorkerLogEntry> latest)
{
for (var k = Math.Min(previous.Count, latest.Count); k > 0; k--)
{
var matches = true;
for (var i = 0; i < k; i++)
{
if (!previous[previous.Count - k + i].Equals(latest[i])) { matches = false; break; }
}
if (matches) return k;
}
return 0;
}
private void UpdateStatusText() =>
StatusText = Rows.Count == 0
? Loc.T("modals.logVisualizer.empty")
: Loc.T("modals.logVisualizer.count", Rows.Count);
}
[RelayCommand]
private void Close()