Windows 0.3.1 clean monitor source delta; no private history or other platform changes

This commit is contained in:
2026-10-27 02:00:33 +00:00
parent 366b3c5c9a
commit 1cf54a46e4
40 changed files with 983 additions and 206 deletions
+96 -22
View File
@@ -5,24 +5,24 @@ namespace UCVL.VPNPlus;
// Reads only existing proxy-outbound counters; never the physical adapter.
internal sealed class TrafficChart : Control
{
private readonly Queue<(DateTimeOffset Time, double Up, double Down)> _points = new();
private readonly TrafficChartTimeline _timeline;
private TimeSpan _period = TimeSpan.FromMinutes(5);
internal int PeriodMinutes => (int)_period.TotalMinutes;
internal int PointCount => _points.Count;
internal int PointCount => _timeline.PointCount;
internal int VisiblePointCount => VisiblePoints().Length;
internal bool HasFreshSample => _timeline.HasFreshSample;
internal DateTimeOffset? LastSampleAt => _timeline.LastSampleAt;
internal DateTimeOffset TimelineEnd => _timeline.End;
internal void SetPeriodMinutes(int minutes)
{
if (minutes is not (1 or 5 or 15 or 60)) throw new ArgumentOutOfRangeException(nameof(minutes));
_period = TimeSpan.FromMinutes(minutes);
Invalidate();
}
private (DateTimeOffset Time, double Up, double Down)[] VisiblePoints()
{
var end = _points.Count == 0 ? DateTimeOffset.Now : _points.Last().Time;
return _points.Where(point => end - point.Time <= _period).ToArray();
}
internal TrafficChart()
private TrafficChartTimeline.Sample[] VisiblePoints() => _timeline.Visible(_period);
internal TrafficChart(Func<DateTimeOffset>? clock = null)
{
_timeline = new TrafficChartTimeline(clock);
DoubleBuffered = true;
SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.Opaque, false);
@@ -31,35 +31,109 @@ internal sealed class TrafficChart : Control
}
internal void Add(double up, double down, DateTimeOffset? time = null)
{
var at = time ?? DateTimeOffset.Now;
double Clean(double n) => double.IsFinite(n) ? Math.Max(0, n) : 0;
_points.Enqueue((at, Clean(up), Clean(down)));
// Bounded current-session samples only: never turn daily accounting
// totals into a fictitious speed curve. Changing range preserves samples.
while (_points.Count > 3600 || (_points.Count > 0 && at - _points.Peek().Time > TimeSpan.FromMinutes(60))) _points.Dequeue();
_timeline.Add(up, down, time);
Invalidate();
}
internal void Reset() { _points.Clear(); Invalidate(); }
// Called by the existing sampling timer even when the reader is busy or
// returns no counters. Neither method invents a zero-speed sample or bytes.
internal void AdvanceClock(DateTimeOffset? now = null) { _timeline.AdvanceClock(now); Invalidate(); }
internal void MarkUnavailable(DateTimeOffset? now = null) { _timeline.MarkUnavailable(now); Invalidate(); }
internal void Reset() { _timeline.Reset(); Invalidate(); }
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
_timeline.AdvanceClock();
var points = VisiblePoints();
var peak = points.Length == 0 ? 1024 : Math.Max(1024, points.Max(p => Math.Max(p.Up, p.Down)));
var chart = ChartAxis.Draw(e.Graphics, ClientRectangle, Font, DeviceDpi, peak, speed: true);
if (chart.Plot.Width < 1 || chart.Plot.Height < 1) return;
var end = points.Length == 0 ? DateTimeOffset.Now : points[^1].Time;
var end = _timeline.End;
ChartAxis.TimeLabels(e.Graphics, chart.Plot, Font, [end.Subtract(_period).ToString("HH:mm"), end.Subtract(_period / 2).ToString("HH:mm"), end.ToString("HH:mm")]);
if (points.Length == 0)
{
TextRenderer.DrawText(e.Graphics, "等待代理流量采样", Font, Rectangle.Round(chart.Plot), Brand.Muted,
TextRenderer.DrawText(e.Graphics, LastSampleAt.HasValue ? "采样暂不可用" : "等待代理流量采样", Font, Rectangle.Round(chart.Plot), Brand.Muted,
ChartAxis.PaintFlags | TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
return;
}
PointF[] Line(bool up) => points.Select(p => new PointF(
chart.Plot.Left + (float)Math.Clamp(1 - (end - p.Time).TotalSeconds / _period.TotalSeconds, 0, 1) * chart.Plot.Width,
chart.Plot.Bottom - (float)((up ? p.Up : p.Down) / chart.Maximum) * chart.Plot.Height)).ToArray();
ChartAxis.Curve(e.Graphics, Line(true), Brand.RoyalBlue, DeviceDpi, dots: points.Length == 1);
ChartAxis.Curve(e.Graphics, Line(false), Brand.Cyan, DeviceDpi, dots: points.Length == 1);
foreach (var segment in points.GroupBy(point => point.Segment))
{
var samples = segment.ToArray();
PointF[] Line(bool up) => samples.Select(p => new PointF(
chart.Plot.Left + (float)Math.Clamp(1 - (end - p.Time).TotalSeconds / _period.TotalSeconds, 0, 1) * chart.Plot.Width,
chart.Plot.Bottom - (float)((up ? p.Up : p.Down) / chart.Maximum) * chart.Plot.Height)).ToArray();
ChartAxis.Curve(e.Graphics, Line(true), Brand.RoyalBlue, DeviceDpi, dots: samples.Length == 1);
ChartAxis.Curve(e.Graphics, Line(false), Brand.Cyan, DeviceDpi, dots: samples.Length == 1);
}
if (!HasFreshSample)
TextRenderer.DrawText(e.Graphics, "采样暂不可用", Font, Rectangle.Round(chart.Plot), Brand.Muted,
ChartAxis.PaintFlags | TextFormatFlags.Right | TextFormatFlags.Top | TextFormatFlags.SingleLine);
}
}
// Pure presentation history: no proxy/adapter reads, cumulative byte totals or
// timers. An injected clock makes missing-sample behavior deterministic in tests.
internal sealed class TrafficChartTimeline
{
internal readonly record struct Sample(DateTimeOffset Time, double Up, double Down, long Segment);
internal static readonly TimeSpan MaximumSampleAge = TimeSpan.FromSeconds(15);
private static readonly TimeSpan Retention = TimeSpan.FromMinutes(60);
private readonly Queue<Sample> _points = new();
private readonly Func<DateTimeOffset> _clock;
private DateTimeOffset? _end, _lastSampleAt;
private bool _gapPending, _unavailable = true;
private long _segment;
internal TrafficChartTimeline(Func<DateTimeOffset>? clock = null) => _clock = clock ?? (() => DateTimeOffset.Now);
internal int PointCount => _points.Count;
internal DateTimeOffset End => _end ?? _clock();
internal DateTimeOffset? LastSampleAt => _lastSampleAt;
internal bool HasFreshSample => !_unavailable && _lastSampleAt is { } last && End - last <= MaximumSampleAge;
private void MoveEnd(DateTimeOffset at)
{
if (!_end.HasValue || at > _end.Value) _end = at;
while (_points.Count > 3600 || (_points.Count > 0 && End - _points.Peek().Time > Retention)) _points.Dequeue();
}
internal void AdvanceClock(DateTimeOffset? now = null)
{
MoveEnd(now ?? _clock());
if (_lastSampleAt is { } last && End - last > MaximumSampleAge)
_gapPending = _unavailable = true;
}
internal void MarkUnavailable(DateTimeOffset? now = null)
{
AdvanceClock(now);
_gapPending = _unavailable = true;
}
internal bool Add(double up, double down, DateTimeOffset? time = null)
{
var at = time ?? _clock();
if (!double.IsFinite(up) || !double.IsFinite(down) || up < 0 || down < 0)
{
MarkUnavailable(at);
return false;
}
// Late/duplicate callbacks must not move the axis backwards, inject a
// second point or turn a newer good sample into an unavailable one.
if (_lastSampleAt is { } previous && at <= previous) return false;
if (_lastSampleAt is { } last && (_gapPending || at - last > MaximumSampleAge)) _segment++;
_points.Enqueue(new Sample(at, up, down, _segment));
_lastSampleAt = at;
_gapPending = _unavailable = false;
MoveEnd(at);
return true;
}
internal Sample[] Visible(TimeSpan period) => _points.Where(point => End - point.Time <= period).ToArray();
internal void Reset()
{
_points.Clear();
_end = _lastSampleAt = null;
_gapPending = false;
_unavailable = true;
_segment = 0;
}
}