diff --git a/client/lib/ui/metric_guide_page.dart b/client/lib/ui/metric_guide_page.dart new file mode 100644 index 0000000..42cef5c --- /dev/null +++ b/client/lib/ui/metric_guide_page.dart @@ -0,0 +1,103 @@ +// Copyright (C) 2026 言灸 +// SPDX-License-Identifier: AGPL-3.0-only + +import 'package:flutter/material.dart'; + +import 'quality_charts.dart'; + +class MetricGuidePage extends StatelessWidget { + const MetricGuidePage({super.key}); + + @override + Widget build(BuildContext context) { + final wide = MediaQuery.sizeOf(context).width >= 720; + return Scaffold( + appBar: AppBar( + title: const Text('指标说明'), + backgroundColor: const Color(0xFFF3F1EC), + ), + body: SafeArea( + child: Align( + alignment: Alignment.topCenter, + child: ConstrainedBox( + constraints: const BoxConstraints(maxWidth: 760), + child: ListView( + padding: EdgeInsets.fromLTRB( + wide ? 24 : 12, + 8, + wide ? 24 : 12, + 28, + ), + children: const [ + _Section( + title: '这些数字从哪来', + body: '曲线和百分比只来自已经完成、并且计入正式结果的探测。还在途中的请求不会画成 100%,停止时被取消的请求也不算丢包。没有内置样本。', + ), + _Section( + title: '已发送、已收到、超时', + body: '已发送是计入正式结果的次数。已收到是其中成功的次数:合作反射收到了对上序号的回显,普通 TCP 是连上了,其他类型是收到了符合协议的应答。\n\n合作反射的「超时」只计已经把探测帧写出去、但超时前没有对上序号的次数。还没连上、连接被拒绝,不算进这个超时,也不算进探测无应答。\n\n普通 UDP、DNS、自定义 UDP 和基岩版如果根本没有协议应答,记为「无应答」,超时这一项显示不适用。收到了包但内容不对,记为协议错误。UDP 沉默不能当成已经证实的丢包:对端可以合法地不回答。', + ), + _Section( + title: '探测无应答、失败率、无应答', + body: '合作反射 TCP / UDP:探测无应答 = 超时 ÷(超时 + 成功)× 100%。分母只含已经写出的回显尝试。分母为 0 时显示 —,而不是 0%。TCP 会重传,所以这不是网卡上的物理丢包。\n\n普通 TCP 和 Minecraft Java:失败率 = 未成功的正式尝试 ÷ 已完成的正式尝试 × 100%。这里没有丢包百分比。\n\n其余 UDP 类探测:无应答 = 无应答次数 ÷ 已完成次数 × 100%。它回答的是「没有看到协议应答」,不是「丢了多少包」。', + ), + _Section( + title: '延迟、中位数、P95 和抖动', + body: '延迟只来自成功的样本,单位是毫秒。当前延迟是最近一次成功的往返时间。最小、平均、最大都是这些成功样本的最小、算术平均和最大。\n\n中位数:把成功样本从小到大排列。奇数个取中间那个;偶数个取中间两个的平均。\n\nP95 用最近秩:秩 = ceil(0.95 × 次数),用整数 (95 × 次数 + 99) / 100 计算,从 1 开始,取排序后这个位置的值。次数为 1 时,P95 就是那一次。\n\n抖动按 RFC 3550:D 是相邻两次成功往返时间之差,J = J + (|D| − J) / 16。第一次成功时 J 为 0;没有成功时显示 —。远程探测的往返时间在节点上用单调时钟量好再传回来,不包含手机到节点的控制通道耗时。\n\n旧版本节点不会上报最小、平均、中位、P95 和最大。这时这些格子显示「节点未提供」,当前延迟和抖动仍用它原来上报的值。', + ), + _Section( + title: '用时', + body: '用时是这次会话从按下开始到停止(或到当前)的墙钟时间。它不是各次往返时间相加,也不包括节点在创建任务之前花掉的时间。', + ), + _Section( + title: '选址失败和正式丢包', + body: '一个域名可能同时有 IPv6 和 IPv4。程序会先试一个地址。还没有任何地址应答时,连不上或没有应答会换下一个。这种只为挑选地址而失败的尝试,如果后面有地址成功,就不计入已发送、超时和丢包,界面单独写明回退了哪个地址。\n\n一旦某个地址已经应答,之后对它的超时或失败都算正式结果,不会因为后来又换地址而被抹掉。只有一个地址时,失败从一开始就算正式结果。\n\n如果每一个候选都失败,这些失败会计入正式结果。界面显示失败或 100% 的无应答/失败率,不会显示 0% 成功。\n\n1.1.1 的远程节点还不会做这层区分,它的统计可能仍把第一次选址失败算进去。要分开看,节点也需要 1.1.2。', + ), + _Section( + title: '实际地址和 IPv6 回退', + body: '「实际地址」是这次真正连通或真正发包的 IP,并注明 IPv4 或 IPv6。「解析到」只是域名解析的结果,不会被说成已经在用的地址。\n\n正在尝试、还没有正式结果时,会标明正在尝试,而不是写成已经发往。远程节点如果没有在结果里带上地址(旧版本就是这样),界面写「节点没有报告实际使用的地址」,不会在手机上另做一次解析来冒充。\n\n地址落在 198.18.0.0/15 时,会提示这是 VPN 假地址,只有这个 VPN 开着时才能用。', + ), + _Section( + title: '停止以后', + body: '点停止会保留曲线,并给出开始、结束、用时、目标、类型、来源、实际地址和同一套最终指标。正在进行、因为停止而被取消的请求不计入丢包。重新开始会换一次新的会话,旧曲线不会混进来。\n\n点曲线上的一点可以钉住该时刻的延迟和比率,松手后仍然留着。再点另一点会换。新会话或这个点滚出窗口后,选择会被清掉,不会跳到别的点上。', + ), + ], + ), + ), + ), + ), + ); + } +} + +class _Section extends StatelessWidget { + const _Section({required this.title, required this.body}); + + final String title; + final String body; + + @override + Widget build(BuildContext context) { + return Card( + margin: const EdgeInsets.only(bottom: 12), + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(12), + side: const BorderSide(color: line), + ), + child: Padding( + padding: const EdgeInsets.all(14), + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Text( + title, + style: const TextStyle(fontWeight: FontWeight.w700, color: ink), + ), + const SizedBox(height: 8), + Text(body, style: const TextStyle(height: 1.45, color: ink)), + ], + ), + ), + ); + } +} diff --git a/client/lib/ui/metrics_panel.dart b/client/lib/ui/metrics_panel.dart new file mode 100644 index 0000000..1149d20 --- /dev/null +++ b/client/lib/ui/metrics_panel.dart @@ -0,0 +1,271 @@ +// Copyright (C) 2026 言灸 +// SPDX-License-Identifier: AGPL-3.0-only + +import 'package:flutter/material.dart'; + +import '../metrics/sample_book.dart'; +import '../probe/endpoint_picker.dart'; +import '../probe/probe_controller.dart'; +import 'address_text.dart'; +import 'format.dart'; +import 'quality_charts.dart'; + +class MetricLine { + const MetricLine(this.label, this.value, {this.detail}); + + final String label; + final String value; + final String? detail; +} + +/// Rows use the same summary the live view and the stopped session share. +/// A metric that this profile does not measure is marked unsupported instead +/// of being shown as zero. +List metricLines({ + required ProbeSummary summary, + required String profileId, + required Duration elapsed, +}) { + final lines = [ + MetricLine( + '已发送', + '${summary.sent}', + detail: summary.selectionSkipped > 0 + ? '正式结果 ${summary.sent} 次。另有 ${summary.selectionSkipped} 次选址失败未计入。' + : '只计已经计入正式结果的次数,不含还在途中的探测。', + ), + MetricLine('已收到', '${summary.ok}', detail: '成功收到应答,或成功连通的次数。'), + _timeoutLine(summary, profileId), + _rateLine(summary, profileId), + _latency('当前延迟', summary.rttMs, summary, recent: true), + _latency('最小延迟', summary.minRttMs, summary), + _latency('平均延迟', summary.meanRttMs, summary), + _latency('中位延迟', summary.medianRttMs, summary), + _latency('P95 延迟', summary.p95RttMs, summary), + _latency('最大延迟', summary.maxRttMs, summary), + MetricLine( + '抖动', + summary.jitterMs == null ? '—' : formatMs(summary.jitterMs), + detail: 'RFC 3550:J = J + (|D| − J) / 16。D 是相邻两次成功往返时间之差。只有一次成功时 J 为 0。', + ), + MetricLine( + '用时', + formatDuration(elapsed), + detail: '从这次探测开始到现在或到停止的墙钟时间,不是往返时间相加。', + ), + ]; + return lines; +} + +MetricLine _timeoutLine(ProbeSummary summary, String profileId) { + switch (profileId) { + case 'nq-tcp': + case 'nq-udp': + return MetricLine( + '超时', + '${summary.timeout}', + detail: '已经写出回显帧、在超时前没有对上序号的次数。连接失败另计,不在这里。', + ); + case 'tcp-connect': + case 'minecraft-java': + return const MetricLine('超时', '不适用', detail: '这种探测把连不上记为失败,不单独叫探测超时。'); + default: + return MetricLine( + '超时', + '不适用', + detail: '没有协议应答时记为无应答 ${summary.unknown} 次,不能据此断定丢包。', + ); + } +} + +MetricLine _rateLine(ProbeSummary summary, String profileId) { + switch (profileId) { + case 'nq-tcp': + case 'nq-udp': + final denom = summary.timeout + summary.ok; + return MetricLine( + '探测无应答', + formatPct(summary.probeLossPct), + detail: denom == 0 + ? '还没有完成的回显尝试,所以不显示 0%。' + : '超时 ÷(超时 + 成功)= ${summary.timeout} ÷ $denom。连接失败不在分母里。', + ); + case 'tcp-connect': + case 'minecraft-java': + return MetricLine( + '失败率', + formatPct(summary.failurePct), + detail: '未成功的正式尝试 ÷ 已完成的正式尝试。这不是物理丢包。', + ); + default: + return MetricLine( + '无应答(未知)', + formatPct(summary.unknownPct), + detail: '无应答 ÷ 已完成。沉默不是丢包;明确的协议错误、拒绝和域名失败另看失败次数。', + ); + } +} + +MetricLine _latency( + String label, + double? value, + ProbeSummary summary, { + bool recent = false, +}) { + if (!summary.latencyStats && summary.ok > 0 && !recent) { + return MetricLine(label, '节点未提供', detail: '这份结果来自没有上报延迟分布的节点。当前延迟仍然可用。'); + } + return MetricLine( + label, + formatMs(value), + detail: recent ? '最近一次成功的往返时间。没有成功时为 —。' : '只统计成功样本。没有成功时为 —。', + ); +} + +String formatDuration(Duration duration) { + final ms = duration.inMilliseconds; + if (ms < 0) { + return '—'; + } + if (ms < 1000) { + return '$ms 毫秒'; + } + final seconds = ms / 1000; + if (seconds < 60) { + return '${seconds.toStringAsFixed(1)} 秒'; + } + final minutes = duration.inMinutes; + final rest = duration.inSeconds % 60; + return '$minutes 分 $rest 秒'; +} + +String formatClock(DateTime time) { + final local = time.toLocal(); + String two(int value) => value.toString().padLeft(2, '0'); + return '${local.year}-${two(local.month)}-${two(local.day)} ${two(local.hour)}:${two(local.minute)}:${two(local.second)}'; +} + +class MetricsPanel extends StatelessWidget { + const MetricsPanel({ + super.key, + required this.summary, + required this.profileId, + required this.elapsed, + }); + + final ProbeSummary summary; + final String profileId; + final Duration elapsed; + + @override + Widget build(BuildContext context) { + final lines = metricLines( + summary: summary, + profileId: profileId, + elapsed: elapsed, + ); + return Wrap( + spacing: 8, + runSpacing: 8, + children: [ + for (final line in lines) + Tooltip( + message: line.detail ?? '', + child: _chip('${line.label} ${line.value}'), + ), + ], + ); + } +} + +class SessionSummaryCard extends StatelessWidget { + const SessionSummaryCard({ + super.key, + required this.report, + required this.profileId, + }); + + final SessionReport report; + final String profileId; + + @override + Widget build(BuildContext context) { + final address = describeAddress( + resolved: report.resolved, + usedAddress: report.usedAddress, + usedFamily: report.usedFamily, + fallbackText: report.fallbackText, + addressUnreported: report.addressUnreported, + usedIsFake: report.usedIsFake, + ); + return _panel( + title: '本次结果', + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Text('开始 ${formatClock(report.startedAt)}'), + Text('结束 ${formatClock(report.endedAt)}'), + Text('用时 ${formatDuration(report.elapsed)}'), + Text('目标 ${report.target}'), + Text('类型 ${report.profileLabel}'), + Text('来源 ${report.sourceLabel}'), + if (address.used != null) Text(address.used!), + if (address.resolved != null) Text(address.resolved!), + if (address.unreported != null) Text(address.unreported!), + if (address.fallback != null) Text(address.fallback!), + if (address.fake) + const Text( + clashFakeIpNote, + style: TextStyle(color: Color(0xFF9A3412)), + ), + const SizedBox(height: 8), + MetricsPanel( + summary: report.summary, + profileId: profileId, + elapsed: report.elapsed, + ), + const SizedBox(height: 8), + Text( + summaryLine(report.summary), + style: const TextStyle(color: muted, fontSize: 13), + ), + ], + ), + ); + } +} + +Widget _panel({required String title, required Widget child}) { + return Card( + margin: const EdgeInsets.only(bottom: 12), + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(12), + side: const BorderSide(color: line), + ), + child: Padding( + padding: const EdgeInsets.all(14), + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Text( + title, + style: const TextStyle(fontWeight: FontWeight.w700, color: ink), + ), + const SizedBox(height: 10), + child, + ], + ), + ), + ); +} + +Widget _chip(String text) { + return Container( + padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6), + decoration: BoxDecoration( + color: const Color(0xFFE7EEF5), + borderRadius: BorderRadius.circular(99), + ), + child: Text(text, style: const TextStyle(fontSize: 12, color: ink)), + ); +} diff --git a/client/lib/version.dart b/client/lib/version.dart new file mode 100644 index 0000000..0aef720 --- /dev/null +++ b/client/lib/version.dart @@ -0,0 +1,7 @@ +// Copyright (C) 2026 言灸 +// SPDX-License-Identifier: AGPL-3.0-only + +const appVersion = '1.1.2'; +const appBuild = 5; +const appAttribution = '言灸'; +const appLicense = 'AGPL-3.0-only'; diff --git a/client/test/address_display_test.dart b/client/test/address_display_test.dart new file mode 100644 index 0000000..ab066bd --- /dev/null +++ b/client/test/address_display_test.dart @@ -0,0 +1,99 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:netquality/content/release_history.dart'; +import 'package:netquality/metrics/sample_book.dart'; +import 'package:netquality/ui/address_text.dart'; +import 'package:netquality/ui/chart_selection.dart'; +import 'package:netquality/version.dart'; + +void main() { + test('resolved addresses are not shown as the address in use', () { + const resolved = [ + ResolvedAddress(address: '2001:db8::1', family: 'IPv6'), + ResolvedAddress(address: '203.0.113.5', family: 'IPv4'), + ]; + final onlyResolved = describeAddress(resolved: resolved); + expect(onlyResolved.used, isNull); + expect(onlyResolved.resolved, contains('解析到')); + expect(onlyResolved.resolved, contains('2001:db8::1')); + expect(onlyResolved.resolved, isNot(contains('实际地址'))); + + final used = describeAddress( + resolved: resolved, + usedAddress: '203.0.113.5', + usedFamily: 'IPv4', + fallbackText: '已改用 IPv4', + ); + expect(used.used, '实际地址 203.0.113.5(IPv4)'); + expect(used.resolved, contains('2001:db8::1')); + expect(used.fallback, '已改用 IPv4'); + + final oldNode = describeAddress( + resolved: const [], + addressUnreported: true, + ); + expect(oldNode.used, isNull); + expect(oldNode.unreported, '节点没有报告实际使用的地址'); + + final missing = ProbeSample.fromJson({ + 'seq': 1, + 'status': 'ok', + 'kind': 'probe', + 'rttMs': 3, + 'summary': {'sent': 1, 'ok': 1}, + }); + expect(missing.detail['address'], isNull); + final parsed = readResolvedList({ + 'resolved': [ + {'address': '2001:db8::1', 'family': 'IPv6'}, + ], + }); + expect(parsed.single.address, '2001:db8::1'); + expect(readResolvedList({'jobId': 'abc'}), isEmpty); + }); + + test('a pinned chart point stays put until it scrolls off or the session changes', () { + const first = ChartSelection(sec: 1.5, sessionId: 3); + final kept = nextChartSelection( + current: first, + sessionId: 3, + sampleSecs: [1.5, 2.5, 3.5], + ); + expect(kept, same(first)); + + final moved = nextChartSelection( + current: first, + sessionId: 3, + sampleSecs: [1.5, 2.5, 3.5], + tappedSec: 2.4, + ); + expect(moved!.sec, 2.5); + expect(moved.sessionId, 3); + + expect( + nextChartSelection(current: moved, sessionId: 4, sampleSecs: [0.2, 0.4]), + isNull, + ); + expect( + nextChartSelection(current: first, sessionId: 3, sampleSecs: [2.5, 3.5]), + isNull, + ); + expect( + nextChartSelection(current: first, sessionId: 3, sampleSecs: const []), + isNull, + ); + }); + + test('history only lists the real tag and this version', () { + expect(appVersion, '1.1.2'); + expect(appBuild, 5); + expect(appAttribution, '言灸'); + expect(releaseHistory.map((entry) => entry.version), ['1.1.2', '1.1.1']); + expect(releaseHistory.last.published, '2026-11-09'); + expect(releaseHistory.last.source, contains('v1.1.1')); + expect(releaseHistoryNote, contains('不写成另一个版本')); + for (final entry in releaseHistory) { + expect(entry.version, isNot('1.1.0')); + expect(entry.version, isNot('1.0.0')); + } + }); +}