Windows 0.3.3 dashboard layout and speed presentation source delta; no private history or other platform changes
This commit is contained in:
@@ -48,6 +48,7 @@ internal static class NodeTrafficPresentation
|
||||
&& row.IntervalSeconds is > 0 and <= 30 && !string.IsNullOrWhiteSpace(row.CounterEpoch);
|
||||
internal static string Bytes(long? bytes, bool speed = false)
|
||||
{
|
||||
if (speed) return SpeedDisplay.Format(bytes);
|
||||
if (bytes is null or < 0) return "—";
|
||||
var value = (double)bytes.Value;
|
||||
string[] units = ["B", "KB", "MB", "GB", "TB"];
|
||||
@@ -55,40 +56,109 @@ internal static class NodeTrafficPresentation
|
||||
while (value >= 1024 && unit < units.Length - 1) { value /= 1024; unit++; }
|
||||
return $"{value:0.##} {units[unit]}" + (speed ? "/s" : "");
|
||||
}
|
||||
// The contract describes the host interface: receiving is ingress and
|
||||
// transmitting is egress, independently of a client's upload direction.
|
||||
internal static NodeTrafficBarRates BarRates(NodeTrafficSnapshot? snapshot, NodeTrafficReading? row,
|
||||
DateTimeOffset now, bool samplingAvailable = true) => samplingAvailable && snapshot is not null && row is not null
|
||||
&& Valid(snapshot, now) && Fresh(row, now)
|
||||
? new(row.DownloadRate, row.UploadRate) : new(null, null);
|
||||
internal static double BarMaximum(IEnumerable<NodeTrafficBarRates> rows)
|
||||
{
|
||||
var peak = rows.SelectMany(row => new[] { row.Ingress, row.Egress })
|
||||
.Where(rate => rate is >= 0).Select(rate => (double)rate!.Value).DefaultIfEmpty(0).Max();
|
||||
return ChartAxisY.Create(peak, true).MaximumBytes;
|
||||
}
|
||||
internal static int? BarWidth(long? rate, double maximum, int width) => rate is null or < 0 ? null
|
||||
: rate == 0 ? 0 : Math.Clamp((int)Math.Round(Math.Clamp(rate.Value / maximum, 0, 1) * width), 1, Math.Max(1, width));
|
||||
}
|
||||
|
||||
internal readonly record struct NodeTrafficBarRates(long? Ingress, long? Egress);
|
||||
|
||||
// One buffered surface, not a table of native child HWNDs. A snapshot swaps
|
||||
// atomically on the UI thread and never changes row count or window height.
|
||||
internal sealed class NodeTrafficPanel : CardPanel
|
||||
{
|
||||
private readonly Func<DateTimeOffset> _clock;
|
||||
private readonly ToolTip _details = new() { InitialDelay = 350, ReshowDelay = 100, AutoPopDelay = 15_000 };
|
||||
private NodeTrafficSnapshot? _snapshot;
|
||||
private string _status = "等待服务器采样";
|
||||
private string _detailText = "等待服务器采样";
|
||||
internal void SetSnapshot(NodeTrafficSnapshot value) { _snapshot = value; _status = "每 5 秒更新"; Invalidate(); }
|
||||
internal void Unavailable(string status, bool clear = false) { if (clear) _snapshot = null; _status = status; Invalidate(); }
|
||||
internal NodeTrafficPanel() => AccessibleName = "VPN 服务器上行下行实时速度与累计流量";
|
||||
internal NodeTrafficPanel(Func<DateTimeOffset>? clock = null)
|
||||
{
|
||||
_clock = clock ?? (() => DateTimeOffset.UtcNow);
|
||||
AccessibleName = "VPN 服务器流入流出实时速度与累计流量,上入下出细柱";
|
||||
}
|
||||
internal static (Rectangle Title, Rectangle[] Rows) TextLayout(Size size, int dpi, int fontHeight)
|
||||
{
|
||||
var pad = Math.Max(2, 4 * dpi / 96);
|
||||
var contentHeight = Math.Max(0, size.Height - pad * 2);
|
||||
// A 7.5pt YaHei line is 16px at native 96 DPI. Keep two complete
|
||||
// lines in the compact 38px card; fractional-DPI fonts vary slightly.
|
||||
var verticalPad = Math.Max(2, 3 * dpi / 96);
|
||||
var contentHeight = Math.Max(0, size.Height - verticalPad * 2);
|
||||
var title = contentHeight >= fontHeight * 3;
|
||||
var height = contentHeight / (title ? 3 : 2);
|
||||
Rectangle Line(int row) => new(pad, pad + row * height, Math.Max(0, size.Width - pad * 2), height);
|
||||
Rectangle Line(int row) => new(pad, verticalPad + row * height, Math.Max(0, size.Width - pad * 2), height);
|
||||
return (title ? Line(0) : Rectangle.Empty, [Line(title ? 1 : 0), Line(title ? 2 : 1)]);
|
||||
}
|
||||
internal static (Rectangle Text, Rectangle Ingress, Rectangle Egress) RowLayout(Rectangle row, int dpi)
|
||||
{
|
||||
var scale = Math.Max(1f, dpi / 96f);
|
||||
var width = Math.Min((int)Math.Round(54 * scale), Math.Max(1, row.Width / 5));
|
||||
var gap = Math.Max(1, (int)Math.Round(2 * scale));
|
||||
var height = Math.Max(1, Math.Min((int)Math.Round(3 * scale), (row.Height - gap) / 2));
|
||||
var top = row.Top + Math.Max(0, (row.Height - height * 2 - gap) / 2);
|
||||
var left = row.Right - width;
|
||||
return (new(row.Left, row.Top, Math.Max(0, left - gap * 2 - row.Left), row.Height),
|
||||
new(left, top, width, height), new(left, top + height + gap, width, height));
|
||||
}
|
||||
internal static void PaintBars(Graphics graphics, Rectangle ingress, Rectangle egress,
|
||||
NodeTrafficBarRates rates, double maximum)
|
||||
{
|
||||
Draw(ingress, rates.Ingress, Brand.Cyan);
|
||||
Draw(egress, rates.Egress, Brand.RoyalBlue);
|
||||
void Draw(Rectangle track, long? rate, Color color)
|
||||
{
|
||||
using var background = new SolidBrush(Color.FromArgb(38, color));
|
||||
graphics.FillRectangle(background, track);
|
||||
var width = NodeTrafficPresentation.BarWidth(rate, maximum, track.Width);
|
||||
if (width is > 0)
|
||||
{
|
||||
using var fill = new SolidBrush(color);
|
||||
graphics.FillRectangle(fill, new Rectangle(track.Location, new Size(width.Value, track.Height)));
|
||||
}
|
||||
else if (width is null)
|
||||
{
|
||||
// Missing/stale data has its own marker, never a fake zero bar.
|
||||
using var unavailable = new Pen(Brand.Muted);
|
||||
var center = track.Left + track.Width / 2;
|
||||
graphics.DrawLine(unavailable, center - 2, track.Top + track.Height / 2, center + 2, track.Top + track.Height / 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing) _details.Dispose();
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
protected override void OnPaint(PaintEventArgs e)
|
||||
{
|
||||
base.OnPaint(e);
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var now = _clock();
|
||||
var compact=Width<720*DeviceDpi/96;
|
||||
using var font = Brand.Font(compact?6.5f:7.1f);
|
||||
using var font = Brand.Font(7.5f);
|
||||
var layout = TextLayout(ClientSize, DeviceDpi, TextRenderer.MeasureText(e.Graphics, "VPN002 上传", font).Height);
|
||||
var flags = ChartAxis.PaintFlags | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine | TextFormatFlags.EndEllipsis;
|
||||
if (!layout.Title.IsEmpty) TextRenderer.DrawText(e.Graphics, "服务器整机流量 · 主 WAN 网卡 · 累计自网卡启动 / 重置", font,
|
||||
if (!layout.Title.IsEmpty) TextRenderer.DrawText(e.Graphics, "服务器整机流量 · 细柱上入下出 · 累计自网卡启动 / 重置", font,
|
||||
layout.Title, Brand.Muted, flags);
|
||||
var rows = NodeTrafficPresentation.Slugs.Select(slug => _snapshot?.Nodes.SingleOrDefault(n => n.Slug == slug)).ToArray();
|
||||
var rates = rows.Select(row => NodeTrafficPresentation.BarRates(_snapshot, row, now, _status == "每 5 秒更新")).ToArray();
|
||||
var maximum = NodeTrafficPresentation.BarMaximum(rates);
|
||||
var details = new List<string>();
|
||||
for (var i = 0; i < 2; i++)
|
||||
{
|
||||
var slug = NodeTrafficPresentation.Slugs[i];
|
||||
var row = _snapshot?.Nodes.SingleOrDefault(n => n.Slug == slug);
|
||||
var row = rows[i];
|
||||
var fresh = row is not null && _snapshot is not null && NodeTrafficPresentation.Valid(_snapshot, now)
|
||||
&& NodeTrafficPresentation.Fresh(row, now) && _status == "每 5 秒更新";
|
||||
var name = i == 0 ? "VPN002 东京" : "VPN003 美国";
|
||||
@@ -96,13 +166,23 @@ internal sealed class NodeTrafficPanel : CardPanel
|
||||
var down = fresh ? row!.DownloadRate : null;
|
||||
var status = fresh ? row!.SampledAt!.Value.LocalDateTime.ToString("HH:mm:ss")
|
||||
: row?.SampledAt is { } oldTime ? $"过期 {oldTime.LocalDateTime:HH:mm:ss}" : "等待上报";
|
||||
var text = $"{name} ↑ {NodeTrafficPresentation.Bytes(up, true)} ↓ {NodeTrafficPresentation.Bytes(down, true)}"
|
||||
+ $" 累计 ↑ {NodeTrafficPresentation.Bytes(row?.TotalUpload)} ↓ {NodeTrafficPresentation.Bytes(row?.TotalDownload)} {status}";
|
||||
if(compact)text=$"{(i==0?"VPN002 东京":"VPN003 美国")} {(fresh?status:row?.SampledAt is { } age?$"过期{age.LocalDateTime:HH:mm}":"待上报")} ↑{NodeTrafficPresentation.Bytes(up,true)} ↓{NodeTrafficPresentation.Bytes(down,true)} 累计↑{NodeTrafficPresentation.Bytes(row?.TotalUpload)} ↓{NodeTrafficPresentation.Bytes(row?.TotalDownload)}";
|
||||
var text = $"{name} {status} 入{NodeTrafficPresentation.Bytes(down, true)} 出{NodeTrafficPresentation.Bytes(up, true)}"
|
||||
+ $" 累计入{NodeTrafficPresentation.Bytes(row?.TotalDownload)} 出{NodeTrafficPresentation.Bytes(row?.TotalUpload)}";
|
||||
details.Add(text);
|
||||
if(compact)text=$"{(i==0?"VPN002东京":"VPN003美国")} {(fresh?status:row?.SampledAt is { } age?$"过期{age.LocalDateTime:HH:mm}":"待上报")} 入{NodeTrafficPresentation.Bytes(down,true)} 出{NodeTrafficPresentation.Bytes(up,true)} Σ入{NodeTrafficPresentation.Bytes(row?.TotalDownload)} 出{NodeTrafficPresentation.Bytes(row?.TotalUpload)}";
|
||||
var rowLayout = RowLayout(layout.Rows[i], DeviceDpi);
|
||||
TextRenderer.DrawText(e.Graphics, text, font,
|
||||
layout.Rows[i], Brand.Text, flags);
|
||||
rowLayout.Text, Brand.Text, flags);
|
||||
PaintBars(e.Graphics, rowLayout.Ingress, rowLayout.Egress, rates[i], maximum);
|
||||
}
|
||||
AccessibleDescription = _status + ";细柱上方为流入,下方为流出,两台使用同一量程,上限 "
|
||||
+ SpeedDisplay.Format(maximum) + "。速率为采样区间平均。服务器整机网卡数据,不是本账号代理流量,不计入套餐用量;过期累计保留采样时间。"
|
||||
+ Environment.NewLine + string.Join(Environment.NewLine, details);
|
||||
if (_detailText != AccessibleDescription)
|
||||
{
|
||||
_detailText = AccessibleDescription;
|
||||
_details.SetToolTip(this, _detailText);
|
||||
}
|
||||
AccessibleDescription = _status + ";速率为采样区间平均。服务器整机网卡数据,不是本账号代理流量,不计入套餐用量;过期累计保留采样时间。";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user