BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-call-model / src / report / markdown.rs

markdown.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
markdown.rs 6.6 KiB Download Raw
1use std::fmt::Write;
2
3use super::document::{FeatureInfo, Range, Report, SdpInfo};
4
5const SUMMARY_METRICS: [(&str, &str); 14] = [
6 ("audio_in_packets", "Audio in, packets"),
7 ("audio_out_packets", "Audio out, packets"),
8 ("audio_in_level", "Audio in, level"),
9 ("audio_out_level", "Audio out, level"),
10 ("video_in_packets", "Video in, packets"),
11 ("video_in_frames_decoded", "Video in, frames decoded"),
12 ("video_out_packets", "Video out, packets"),
13 ("video_out_frames_encoded", "Video out, frames encoded"),
14 ("share_in_packets", "Share in, packets"),
15 ("share_out_packets", "Share out, packets"),
16 ("loss_percent", "Loss, %"),
17 ("round_trip_ms", "Round trip, ms"),
18 ("jitter_ms", "Jitter, ms"),
19 ("received_kbps", "Received, kbit/s"),
20];
21
22fn seconds(milliseconds: Option<u128>) -> String {
23 milliseconds.map_or("n/a".to_owned(), |value| {
24 format!("{:.1} s", value as f64 / 1000.)
25 })
26}
27
28fn number(value: f64) -> String {
29 if value.fract() == 0. {
30 format!("{value:.0}")
31 } else {
32 format!("{value:.3}")
33 }
34}
35
36fn resolution(report: &Report, width: &str, height: &str) -> String {
37 let side = |name: &str| {
38 report
39 .stats
40 .metrics
41 .get(name)
42 .map_or(0., |range| range.last)
43 };
44 let (width, height) = (side(width), side(height));
45 if width == 0. {
46 "none".to_owned()
47 } else {
48 format!("{width:.0}x{height:.0}")
49 }
50}
51
52pub fn render(report: &Report) -> String {
53 let mut text = String::new();
54 let call = &report.call;
55 let _ = writeln!(text, "# Call test report\n");
56 let _ = writeln!(text, "| | |\n|---|---|");
57 let _ = writeln!(text, "| Kind | {} |", call.kind);
58 let _ = writeln!(text, "| Direction | {} |", call.direction);
59 let _ = writeln!(text, "| Started (UTC) | {} |", call.started_utc);
60 let _ = writeln!(text, "| Connect time | {} |", seconds(call.connect_ms));
61 let _ = writeln!(text, "| Connected for | {} |", seconds(call.connected_ms));
62 let _ = writeln!(text, "| Total | {} |", seconds(call.total_ms));
63 let _ = writeln!(text, "| End reason | {} |", call.end_reason);
64 let _ = writeln!(
65 text,
66 "| Media path | {} ({} escalation(s), {} failed) |",
67 report.media.path, report.media.escalations, report.media.escalation_failures
68 );
69 let _ = writeln!(
70 text,
71 "| Participants | {} present at peak, {} known |",
72 report.participants.peak_present,
73 report.participants.known.len()
74 );
75 let _ = writeln!(
76 text,
77 "| Video | in {}, out {} |",
78 resolution(report, "video_in_width", "video_in_height"),
79 resolution(report, "video_out_width", "video_out_height")
80 );
81 let _ = writeln!(
82 text,
83 "| App | {} ({}), {} {}{} |",
84 report.app.version,
85 report.app.build,
86 report.app.os,
87 report.app.arch,
88 if report.app.demo { ", demo mode" } else { "" }
89 );
90 write_problems(&mut text, report);
91 write_stats(&mut text, report);
92 write_features(&mut text, &report.features);
93 write_notices(&mut text, report);
94 write_sdp(&mut text, &report.sdp);
95 let _ = writeln!(
96 text,
97 "\nThe JSON file next to this one holds the full timeline ({} entries).",
98 report.timeline.len()
99 );
100 text
101}
102
103fn write_problems(text: &mut String, report: &Report) {
104 let _ = writeln!(text, "\n## Findings\n");
105 if report.problems.is_empty() {
106 let _ = writeln!(text, "- Nothing suspicious found.");
107 }
108 for problem in &report.problems {
109 let _ = writeln!(text, "- {problem}");
110 }
111}
112
113fn write_stats(text: &mut String, report: &Report) {
114 let _ = writeln!(
115 text,
116 "\n## Stats ({} samples, every 2 s)\n",
117 report.stats.samples
118 );
119 let _ = writeln!(text, "| Metric | Min | Max | Last |\n|---|---|---|---|");
120 for (name, label) in SUMMARY_METRICS {
121 if let Some(Range { min, max, last }) = report.stats.metrics.get(name) {
122 let _ = writeln!(
123 text,
124 "| {label} | {} | {} | {} |",
125 number(*min),
126 number(*max),
127 number(*last)
128 );
129 }
130 }
131 let health: Vec<String> = report
132 .stats
133 .health_samples
134 .iter()
135 .map(|(level, count)| format!("{level} {count}"))
136 .collect();
137 let limits: Vec<String> = report
138 .stats
139 .quality_limitation_samples
140 .iter()
141 .map(|(limit, count)| format!("{limit} {count}"))
142 .collect();
143 let _ = writeln!(
144 text,
145 "\nHealth samples: {}. Quality limitation samples: {}.",
146 health.join(", "),
147 limits.join(", ")
148 );
149}
150
151fn write_features(text: &mut String, features: &[FeatureInfo]) {
152 let _ = writeln!(text, "\n## Features used\n");
153 if features.is_empty() {
154 let _ = writeln!(text, "None.");
155 return;
156 }
157 let _ = writeln!(
158 text,
159 "| At | Feature | Detail | Result |\n|---|---|---|---|"
160 );
161 for feature in features {
162 let result = match &feature.error {
163 Some(error) => format!("failed: {error}"),
164 None => feature.result.to_owned(),
165 };
166 let _ = writeln!(
167 text,
168 "| {} | {} | {} | {} |",
169 seconds(Some(feature.at_ms)),
170 feature.feature,
171 feature.detail,
172 result
173 );
174 }
175}
176
177fn write_notices(text: &mut String, report: &Report) {
178 let _ = writeln!(text, "\n## Notices shown\n");
179 if report.notices.is_empty() {
180 let _ = writeln!(text, "None.");
181 }
182 for notice in &report.notices {
183 let _ = writeln!(text, "- {}: {}", seconds(Some(notice.at_ms)), notice.text);
184 }
185}
186
187fn write_sdp(text: &mut String, sdp: &[SdpInfo]) {
188 let _ = writeln!(text, "\n## SDP (summary only)\n");
189 if sdp.is_empty() {
190 let _ = writeln!(text, "None recorded.");
191 }
192 for summary in sdp {
193 let lines: Vec<String> = summary
194 .lines
195 .iter()
196 .map(|line| {
197 let state = if line.rejected {
198 "rejected".to_owned()
199 } else {
200 line.direction.clone()
201 };
202 format!(
203 "{} mid {} {} [{}]",
204 line.kind,
205 line.mid,
206 state,
207 line.codecs.join(",")
208 )
209 })
210 .collect();
211 let _ = writeln!(
212 text,
213 "- {} at {} ms: {}",
214 summary.role,
215 summary.engine_ms,
216 lines.join("; ")
217 );
218 }
219}