| 1 | // Command quality-summary reads Calls' quality and problem report lines |
| 2 | // (spec §15.26) and prints what happened in each call: who was in it, by |
| 3 | // peer tag and platform; for every direction between two people, the |
| 4 | // worst loss, the worst round trip, the freezes and how much of the time |
| 5 | // the media went through the relay; and the notices people saw, in order. |
| 6 | // From the per-peer lines (§15.29): each side's path and the candidates |
| 7 | // behind it, and so which side's network made a relayed call relayed; |
| 8 | // each direction's camera and screen as sent and as received — size, |
| 9 | // frame rate, freezes, what held the encoder back, the encoders and |
| 10 | // decoders — and the automatic reports a struggling client sent. |
| 11 | // |
| 12 | // It reads `carlos logs` output on standard input, or, with nothing piped |
| 13 | // in, runs `carlos logs` itself: |
| 14 | // |
| 15 | // quality-summary --since 3h # every call in the last three hours |
| 16 | // quality-summary --since 24h --at 15:40 # the call running at 15:40 (Europe/Dublin) |
| 17 | // quality-summary --call 3k2jq7m4xw5zt6ab # one call, by its tag |
| 18 | // quality-summary --problem K7Q2M9PD --since 24h # one problem or automatic report, diagnostics and all |
| 19 | // carlos logs --app calls --account oficina --since 2h -n 5000 | quality-summary |
| 20 | // |
| 21 | // Nothing here knows a meeting, a person or a name: the lines carry none. |
| 22 | package main |
| 23 | |
| 24 | import ( |
| 25 | "bufio" |
| 26 | "encoding/json" |
| 27 | "errors" |
| 28 | "flag" |
| 29 | "fmt" |
| 30 | "io" |
| 31 | "os" |
| 32 | "os/exec" |
| 33 | "regexp" |
| 34 | "slices" |
| 35 | "sort" |
| 36 | "strconv" |
| 37 | "strings" |
| 38 | "time" |
| 39 | |
| 40 | "amadan.net/oficina/calls/protocol" |
| 41 | ) |
| 42 | |
| 43 | func main() { |
| 44 | if err := run(os.Args[1:], os.Stdin, os.Stdout); err != nil { |
| 45 | fmt.Fprintln(os.Stderr, "quality-summary:", err) |
| 46 | os.Exit(1) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | type options struct { |
| 51 | since, app, account, at, call, problem, zone string |
| 52 | } |
| 53 | |
| 54 | func run(args []string, stdin *os.File, out io.Writer) error { |
| 55 | fs := flag.NewFlagSet("quality-summary", flag.ContinueOnError) |
| 56 | var o options |
| 57 | fs.StringVar(&o.since, "since", "2h", "how far back carlos logs reads (a duration, or RFC3339)") |
| 58 | fs.StringVar(&o.app, "app", "calls", "the carlos app") |
| 59 | fs.StringVar(&o.account, "account", "oficina", "the carlos account") |
| 60 | fs.StringVar(&o.at, "at", "", "only the call(s) running at this time, HH:MM in --zone, today (or YYYY-MM-DD HH:MM)") |
| 61 | fs.StringVar(&o.call, "call", "", "only the call with this tag") |
| 62 | fs.StringVar(&o.problem, "problem", "", "print one problem report or automatic report, by its reference, with its diagnostics") |
| 63 | fs.StringVar(&o.zone, "zone", "Europe/Dublin", "the zone times are shown and --at is read in") |
| 64 | if err := fs.Parse(args); err != nil { |
| 65 | return err |
| 66 | } |
| 67 | loc, err := time.LoadLocation(o.zone) |
| 68 | if err != nil { |
| 69 | return err |
| 70 | } |
| 71 | var in io.Reader = stdin |
| 72 | if st, err := stdin.Stat(); err == nil && st.Mode()&os.ModeCharDevice != 0 { |
| 73 | cmd := exec.Command("carlos", carlosArgs(o)...) |
| 74 | cmd.Stderr = os.Stderr |
| 75 | b, err := cmd.Output() |
| 76 | if err != nil { |
| 77 | return fmt.Errorf("carlos logs: %w", err) |
| 78 | } |
| 79 | in = strings.NewReader(string(b)) |
| 80 | } |
| 81 | lines, err := parse(in) |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | if o.problem != "" { |
| 86 | return printProblem(out, lines, strings.ToUpper(o.problem), loc) |
| 87 | } |
| 88 | calls := gather(lines) |
| 89 | var at time.Time |
| 90 | if o.at != "" { |
| 91 | if at, err = parseAt(o.at, loc, time.Now()); err != nil { |
| 92 | return err |
| 93 | } |
| 94 | } |
| 95 | shown := 0 |
| 96 | for _, c := range calls { |
| 97 | if o.call != "" && c.tag != o.call { |
| 98 | continue |
| 99 | } |
| 100 | // A report covers the thirty seconds before it. |
| 101 | if !at.IsZero() && (at.Before(c.first.Add(-30*time.Second)) || at.After(c.last.Add(30*time.Second))) { |
| 102 | continue |
| 103 | } |
| 104 | c.print(out, loc) |
| 105 | shown++ |
| 106 | } |
| 107 | if shown == 0 { |
| 108 | fmt.Fprintln(out, "No calls in these lines match. Try a longer --since; carlos keeps an app's logs for seven days.") |
| 109 | } |
| 110 | return nil |
| 111 | } |
| 112 | |
| 113 | // carlosArgs is the `carlos logs` the summary runs when nothing is piped |
| 114 | // in. A problem report is fetched by its own field: its diagnostics are |
| 115 | // many lines of a few records each, and a window of every quality line |
| 116 | // would push them out of the 5,000 carlos returns at most. |
| 117 | func carlosArgs(o options) []string { |
| 118 | args := []string{"logs", "-app", o.app, "-account", o.account, "-since", o.since, "-n", "5000"} |
| 119 | if o.problem != "" { |
| 120 | return append(args, "-field", "ref="+strings.ToUpper(o.problem)) |
| 121 | } |
| 122 | if o.call != "" { |
| 123 | return append(args, "-field", "call="+o.call) |
| 124 | } |
| 125 | return append(args, "-grep", "^calls: (quality|problem report|auto report)") |
| 126 | } |
| 127 | |
| 128 | // parseAt reads "15:40" as today in loc, or "2026-10-03 15:40". |
| 129 | func parseAt(s string, loc *time.Location, now time.Time) (time.Time, error) { |
| 130 | if t, err := time.ParseInLocation("2006-01-02 15:04", s, loc); err == nil { |
| 131 | return t, nil |
| 132 | } |
| 133 | t, err := time.ParseInLocation("15:04", s, loc) |
| 134 | if err != nil { |
| 135 | return time.Time{}, errors.New("--at wants HH:MM or YYYY-MM-DD HH:MM") |
| 136 | } |
| 137 | n := now.In(loc) |
| 138 | return time.Date(n.Year(), n.Month(), n.Day(), t.Hour(), t.Minute(), 0, 0, loc), nil |
| 139 | } |
| 140 | |
| 141 | // line is one report line, as the app wrote it. |
| 142 | type line struct { |
| 143 | Time time.Time `json:"time"` |
| 144 | Msg string `json:"msg"` |
| 145 | Call string `json:"call"` |
| 146 | Peer string `json:"peer"` |
| 147 | App string `json:"app"` |
| 148 | Version string `json:"version"` |
| 149 | Browser string `json:"browser"` |
| 150 | BrowVer string `json:"browserVersion"` |
| 151 | OS string `json:"os"` |
| 152 | OSVer string `json:"osVersion"` |
| 153 | Final bool `json:"final"` |
| 154 | Ref string `json:"ref"` |
| 155 | Note string `json:"note"` |
| 156 | Live bool `json:"live"` |
| 157 | Part int `json:"part"` |
| 158 | Of int `json:"of"` |
| 159 | Text string `json:"text"` |
| 160 | Trigger string `json:"trigger"` |
| 161 | Remote string `json:"remote"` |
| 162 | // Fields is every top-level scalar field as text, from either shape: |
| 163 | // what a `calls: quality peer` line is read from (§15.29). |
| 164 | Fields map[string]string `json:"-"` |
| 165 | // Head is the line's numeric top-level fields, as carlos prints them |
| 166 | // (freezes, maxLossPct, screenStalls…): all there is of a quality line |
| 167 | // read from carlos, which leaves out `report`. |
| 168 | Head map[string]float64 `json:"-"` |
| 169 | Report struct { |
| 170 | Seconds *float64 `json:"seconds"` |
| 171 | Devices *protocol.Devices `json:"devices"` |
| 172 | Events *protocol.CallEvents `json:"events"` |
| 173 | Peers []protocol.PeerQuality `json:"peers"` |
| 174 | } `json:"report"` |
| 175 | } |
| 176 | |
| 177 | // parse takes every line that carries a report, in either of the two |
| 178 | // shapes it reaches here in. The app writes JSON, and a line piped from |
| 179 | // somewhere that keeps it (the fixture, an export) carries the object |
| 180 | // after whatever prefix: it starts at the first `{"time"`. `carlos logs` |
| 181 | // prints the record instead: its own prefix, the message, then the |
| 182 | // line's top-level fields as key=value in key order, values unquoted and |
| 183 | // any newline in one printed as it is, so a problem report's diagnostics |
| 184 | // run over many lines (spec §15.26; 2026-10-21, quality-summary found no |
| 185 | // report 6BPJVTGF in a file that held all of it). Nested members — the |
| 186 | // quality line's `report` — are not printed at all, so from carlos a |
| 187 | // quality line is its headline fields. |
| 188 | func parse(r io.Reader) ([]line, error) { |
| 189 | var out []line |
| 190 | sc := bufio.NewScanner(r) |
| 191 | sc.Buffer(make([]byte, 0, 64<<10), 4<<20) |
| 192 | var record strings.Builder |
| 193 | inRecord := false |
| 194 | flush := func() { |
| 195 | if !inRecord { |
| 196 | return |
| 197 | } |
| 198 | inRecord = false |
| 199 | if l, ok := parseRecord(record.String()); ok { |
| 200 | out = append(out, l) |
| 201 | } |
| 202 | record.Reset() |
| 203 | } |
| 204 | for sc.Scan() { |
| 205 | s := sc.Text() |
| 206 | if i := strings.Index(s, `{"time"`); i >= 0 { |
| 207 | flush() |
| 208 | var l line |
| 209 | if json.Unmarshal([]byte(s[i:]), &l) == nil && strings.HasPrefix(l.Msg, "calls: ") { |
| 210 | l.Fields, l.Head = jsonFields([]byte(s[i:])) |
| 211 | out = append(out, l) |
| 212 | } |
| 213 | continue |
| 214 | } |
| 215 | if m := carlosPrefix.FindStringIndex(s); m != nil { |
| 216 | flush() |
| 217 | rest := s[m[1]:] |
| 218 | if strings.HasPrefix(rest, "calls: ") { |
| 219 | record.WriteString(rest) |
| 220 | inRecord = true |
| 221 | } |
| 222 | continue |
| 223 | } |
| 224 | // carlos's own footer, when it held lines back. |
| 225 | if strings.HasPrefix(s, "(showing the newest ") { |
| 226 | flush() |
| 227 | continue |
| 228 | } |
| 229 | // Anything else continues the record before it: a newline inside |
| 230 | // a value, printed as it is. |
| 231 | if inRecord { |
| 232 | record.WriteString("\n") |
| 233 | record.WriteString(s) |
| 234 | } |
| 235 | } |
| 236 | flush() |
| 237 | return out, sc.Err() |
| 238 | } |
| 239 | |
| 240 | // jsonFields are a JSON line's top-level scalars as carlos would print |
| 241 | // them, and the numeric ones as Head. |
| 242 | func jsonFields(raw []byte) (map[string]string, map[string]float64) { |
| 243 | var m map[string]any |
| 244 | fields, head := map[string]string{}, map[string]float64{} |
| 245 | if json.Unmarshal(raw, &m) != nil { |
| 246 | return fields, head |
| 247 | } |
| 248 | for k, v := range m { |
| 249 | switch v := v.(type) { |
| 250 | case string: |
| 251 | fields[k] = v |
| 252 | case bool: |
| 253 | fields[k] = strconv.FormatBool(v) |
| 254 | case float64: |
| 255 | fields[k] = strconv.FormatFloat(v, 'f', -1, 64) |
| 256 | if !freeText[k] { |
| 257 | head[k] = v |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | return fields, head |
| 262 | } |
| 263 | |
| 264 | // carlosPrefix is what `carlos logs` puts before each record: the time it |
| 265 | // arrived, the stream, and the instance's host. |
| 266 | var carlosPrefix = regexp.MustCompile(`^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d \[[a-z]+\] \S+\s+`) |
| 267 | |
| 268 | // recordKey is one ` key=` in a printed record. |
| 269 | var recordKey = regexp.MustCompile(` ([A-Za-z][A-Za-z0-9]*)=`) |
| 270 | |
| 271 | // freeText are the fields whose values are free text, and so may hold |
| 272 | // spaces, newlines and anything that looks like ` key=`. |
| 273 | var freeText = map[string]bool{"note": true, "text": true} |
| 274 | |
| 275 | // parseRecord reads one printed record: "calls: quality app=mac |
| 276 | // call=… time=2026-…Z version=0.4.10". carlos prints the fields in key |
| 277 | // order, which is what tells a field from text inside a value: a key |
| 278 | // only starts a field if it sorts after the one before it, and a free |
| 279 | // text value runs to the earliest point after which the rest of the |
| 280 | // record is nothing but plain fields. |
| 281 | func parseRecord(s string) (line, bool) { |
| 282 | var l line |
| 283 | keys := recordKey.FindAllStringSubmatchIndex(s, -1) |
| 284 | if len(keys) == 0 { |
| 285 | return l, false |
| 286 | } |
| 287 | l.Msg = s[:keys[0][0]] |
| 288 | fields := map[string]string{} |
| 289 | prev := "" |
| 290 | for i := 0; i < len(keys); i++ { |
| 291 | k := s[keys[i][2]:keys[i][3]] |
| 292 | if k <= prev { |
| 293 | continue |
| 294 | } |
| 295 | start := keys[i][1] |
| 296 | end := len(s) |
| 297 | next := -1 |
| 298 | for j := i + 1; j < len(keys); j++ { |
| 299 | nk := s[keys[j][2]:keys[j][3]] |
| 300 | if nk <= k { |
| 301 | continue |
| 302 | } |
| 303 | if freeText[k] && !plainTail(s[keys[j][0]:], k) { |
| 304 | continue |
| 305 | } |
| 306 | if !freeText[k] && strings.ContainsAny(s[start:keys[j][0]], " \n") { |
| 307 | // A plain value has no space: this key is inside it, or |
| 308 | // the one before was free text after all. |
| 309 | continue |
| 310 | } |
| 311 | next = j |
| 312 | end = keys[j][0] |
| 313 | break |
| 314 | } |
| 315 | fields[k] = s[start:end] |
| 316 | prev = k |
| 317 | if next < 0 { |
| 318 | break |
| 319 | } |
| 320 | i = next - 1 |
| 321 | } |
| 322 | t, err := time.Parse(time.RFC3339Nano, fields["time"]) |
| 323 | if err != nil { |
| 324 | return l, false |
| 325 | } |
| 326 | l.Time = t |
| 327 | l.Call, l.Peer, l.App, l.Version = fields["call"], fields["peer"], fields["app"], fields["version"] |
| 328 | l.Browser, l.BrowVer, l.OS, l.OSVer = fields["browser"], fields["browserVersion"], fields["os"], fields["osVersion"] |
| 329 | l.Ref, l.Note, l.Text = fields["ref"], fields["note"], fields["text"] |
| 330 | l.Trigger, l.Remote, l.Fields = fields["trigger"], fields["remote"], fields |
| 331 | l.Final, l.Live = fields["final"] == "true", fields["live"] == "true" |
| 332 | l.Part, _ = strconv.Atoi(fields["part"]) |
| 333 | l.Of, _ = strconv.Atoi(fields["of"]) |
| 334 | l.Head = map[string]float64{} |
| 335 | for k, v := range fields { |
| 336 | if f, err := strconv.ParseFloat(v, 64); err == nil && !freeText[k] { |
| 337 | l.Head[k] = f |
| 338 | } |
| 339 | } |
| 340 | return l, true |
| 341 | } |
| 342 | |
| 343 | // plainTail says whether s (" key=value key=value…") is nothing but plain |
| 344 | // fields, each sorting after the last and after `after`. |
| 345 | func plainTail(s, after string) bool { |
| 346 | prev := after |
| 347 | for _, tok := range strings.Split(strings.TrimPrefix(s, " "), " ") { |
| 348 | k, _, ok := strings.Cut(tok, "=") |
| 349 | if !ok || k <= prev || strings.Contains(tok, "\n") || !recordKey.MatchString(" "+k+"=") { |
| 350 | return false |
| 351 | } |
| 352 | prev = k |
| 353 | } |
| 354 | return true |
| 355 | } |
| 356 | |
| 357 | type person struct { |
| 358 | tag, app, version, platform string |
| 359 | devices string |
| 360 | reports int |
| 361 | // head sums (or, for the max* fields, maxes) each quality line's |
| 362 | // headline fields: all a line read from carlos has. |
| 363 | head map[string]float64 |
| 364 | // §15.30 (2026-10-29): the machine itself, at its worst window. |
| 365 | machine machine |
| 366 | } |
| 367 | |
| 368 | // machine is one person's machine across the call: the worst window for |
| 369 | // Calls' own CPU and for the thermal state, the most memory and load, |
| 370 | // and whether Low Power Mode was ever on. seen is false for a client |
| 371 | // that reported none of it. |
| 372 | type machine struct { |
| 373 | seen bool |
| 374 | cpu, mem, load float64 |
| 375 | cpuAt, thermalAt time.Time |
| 376 | thermal int // 1 + the index in thermalWords; 0 for none |
| 377 | lowPower, lowPowerKnown bool |
| 378 | } |
| 379 | |
| 380 | var thermalWords = []string{"nominal", "fair", "serious", "critical"} |
| 381 | |
| 382 | func (m *machine) add(l line) { |
| 383 | f := l.Fields |
| 384 | num := func(k string) (float64, bool) { |
| 385 | v, err := strconv.ParseFloat(f[k], 64) |
| 386 | return v, err == nil |
| 387 | } |
| 388 | if v, ok := num("procCpuPct"); ok { |
| 389 | m.seen = true |
| 390 | if v > m.cpu || m.cpuAt.IsZero() { |
| 391 | m.cpu, m.cpuAt = v, l.Time |
| 392 | } |
| 393 | } |
| 394 | if v, ok := num("procMemMB"); ok { |
| 395 | m.seen = true |
| 396 | m.mem = max(m.mem, v) |
| 397 | } |
| 398 | if v, ok := num("sysLoad1"); ok { |
| 399 | m.seen = true |
| 400 | m.load = max(m.load, v) |
| 401 | } |
| 402 | if t := slices.Index(thermalWords, f["thermal"]) + 1; t > 0 { |
| 403 | m.seen = true |
| 404 | if t > m.thermal { |
| 405 | m.thermal, m.thermalAt = t, l.Time |
| 406 | } |
| 407 | } |
| 408 | if v := f["lowPower"]; v != "" { |
| 409 | m.seen = true |
| 410 | m.lowPowerKnown = true |
| 411 | m.lowPower = m.lowPower || v == "true" |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | // words is the machine line, or "" for a client that said nothing. |
| 416 | func (m machine) words(loc *time.Location) string { |
| 417 | if !m.seen { |
| 418 | return "" |
| 419 | } |
| 420 | var parts []string |
| 421 | if !m.cpuAt.IsZero() { |
| 422 | parts = append(parts, fmt.Sprintf("Calls' CPU up to %g%% (worst window %s)", m.cpu, m.cpuAt.In(loc).Format("15:04:05"))) |
| 423 | } |
| 424 | if m.mem > 0 { |
| 425 | parts = append(parts, fmt.Sprintf("memory up to %g MB", m.mem)) |
| 426 | } |
| 427 | if m.load > 0 { |
| 428 | parts = append(parts, fmt.Sprintf("load up to %g", m.load)) |
| 429 | } |
| 430 | if m.thermal > 0 { |
| 431 | parts = append(parts, fmt.Sprintf("thermal worst %s (%s)", thermalWords[m.thermal-1], m.thermalAt.In(loc).Format("15:04:05"))) |
| 432 | } |
| 433 | if m.lowPowerKnown { |
| 434 | if m.lowPower { |
| 435 | parts = append(parts, "Low Power Mode on") |
| 436 | } else { |
| 437 | parts = append(parts, "Low Power Mode off") |
| 438 | } |
| 439 | } |
| 440 | return strings.Join(parts, ", ") |
| 441 | } |
| 442 | |
| 443 | // direction is what one person received from another, over every window. |
| 444 | type direction struct { |
| 445 | windows, pathWindows, relayWindows int |
| 446 | paths map[string]int |
| 447 | worstLoss float64 |
| 448 | worstLossOf string |
| 449 | worstRTT float64 |
| 450 | freezes, freezeSeconds float64 |
| 451 | samples, concealed float64 |
| 452 | cantHear float64 |
| 453 | reconnecting float64 |
| 454 | reconnectingSeconds float64 |
| 455 | noSound, stalled, blocked int |
| 456 | screenSteps float64 |
| 457 | // §15.30: seconds the first asked the second for each lower level |
| 458 | // (reduced, minimal, audio), and seconds the first sent the second less |
| 459 | // because the second asked. |
| 460 | asked, sent [protocol.VideoLevelCount]float64 |
| 461 | } |
| 462 | |
| 463 | type event struct { |
| 464 | at time.Time |
| 465 | text string |
| 466 | } |
| 467 | |
| 468 | type call struct { |
| 469 | tag string |
| 470 | first, last time.Time |
| 471 | people map[string]*person |
| 472 | order []string |
| 473 | dirs map[[2]string]*direction |
| 474 | events []event |
| 475 | problems []line |
| 476 | autos []line |
| 477 | // From the peer lines (§15.29): what each side said about one remote |
| 478 | // peer, keyed [reporter, remote]. |
| 479 | sides map[[2]string]*side |
| 480 | } |
| 481 | |
| 482 | func gather(lines []line) []*call { |
| 483 | byTag := map[string]*call{} |
| 484 | var calls []*call |
| 485 | for _, l := range lines { |
| 486 | if l.Call == "" || strings.HasSuffix(l.Msg, " diagnostics") { |
| 487 | continue |
| 488 | } |
| 489 | c := byTag[l.Call] |
| 490 | if c == nil { |
| 491 | c = &call{tag: l.Call, first: l.Time, last: l.Time, people: map[string]*person{}, dirs: map[[2]string]*direction{}, sides: map[[2]string]*side{}} |
| 492 | byTag[l.Call] = c |
| 493 | calls = append(calls, c) |
| 494 | } |
| 495 | if l.Time.Before(c.first) { |
| 496 | c.first = l.Time |
| 497 | } |
| 498 | if l.Time.After(c.last) { |
| 499 | c.last = l.Time |
| 500 | } |
| 501 | p := c.person(l) |
| 502 | switch l.Msg { |
| 503 | case "calls: problem report": |
| 504 | c.problems = append(c.problems, l) |
| 505 | case "calls: quality": |
| 506 | c.addQuality(p, l) |
| 507 | case "calls: quality peer": |
| 508 | c.addPeerLine(l) |
| 509 | case "calls: auto report": |
| 510 | c.autos = append(c.autos, l) |
| 511 | c.events = append(c.events, event{l.Time, fmt.Sprintf("%s sent automatic report %s (%s)", short(l.Peer), l.Ref, orDash(l.Trigger))}) |
| 512 | } |
| 513 | } |
| 514 | sort.Slice(calls, func(i, j int) bool { return calls[i].first.Before(calls[j].first) }) |
| 515 | return calls |
| 516 | } |
| 517 | |
| 518 | func (c *call) person(l line) *person { |
| 519 | p := c.people[l.Peer] |
| 520 | if p == nil { |
| 521 | p = &person{tag: l.Peer} |
| 522 | c.people[l.Peer] = p |
| 523 | c.order = append(c.order, l.Peer) |
| 524 | } |
| 525 | // The quality lines say what the client is; a problem report's head |
| 526 | // says it too, and fills in only for someone who sent no other line. |
| 527 | if l.App != "" && (l.Msg == "calls: quality" || p.app == "") { |
| 528 | p.app, p.version = l.App, l.Version |
| 529 | switch { |
| 530 | case l.Browser != "": |
| 531 | p.platform = strings.TrimSpace(fmt.Sprintf("%s %s on %s %s", l.Browser, l.BrowVer, l.OS, l.OSVer)) |
| 532 | case l.OS != "": |
| 533 | p.platform = strings.TrimSpace(l.OS + " " + l.OSVer) |
| 534 | } |
| 535 | } |
| 536 | return p |
| 537 | } |
| 538 | |
| 539 | func val(p *float64) float64 { |
| 540 | if p == nil { |
| 541 | return 0 |
| 542 | } |
| 543 | return *p |
| 544 | } |
| 545 | |
| 546 | func lossPct(packets, lost *float64) float64 { |
| 547 | n, l := val(packets), val(lost) |
| 548 | if n+l == 0 { |
| 549 | return 0 |
| 550 | } |
| 551 | return 100 * l / (n + l) |
| 552 | } |
| 553 | |
| 554 | func (c *call) addQuality(p *person, l line) { |
| 555 | p.reports++ |
| 556 | p.machine.add(l) |
| 557 | if p.head == nil { |
| 558 | p.head = map[string]float64{} |
| 559 | } |
| 560 | for k, v := range l.Head { |
| 561 | switch { |
| 562 | case k == "remotes": |
| 563 | p.head[k] = max(p.head[k], v) |
| 564 | case strings.HasPrefix(k, "max"): |
| 565 | p.head[k] = max(p.head[k], v) |
| 566 | default: |
| 567 | p.head[k] += v |
| 568 | } |
| 569 | } |
| 570 | if l.Head["screenStalls"] > 0 || l.Head["screenRebuilds"] > 0 || l.Head["screenReshares"] > 0 || l.Head["screenFailures"] > 0 { |
| 571 | c.events = append(c.events, event{l.Time, short(p.tag) + ": screen " + headScreen(l.Head)}) |
| 572 | } |
| 573 | r := l.Report |
| 574 | if r.Devices != nil { |
| 575 | p.devices = fmt.Sprintf("input %s, output %s", orDash(r.Devices.Input), orDash(r.Devices.Output)) |
| 576 | } |
| 577 | if e := r.Events; e != nil { |
| 578 | if val(e.Reconnecting) > 0 { |
| 579 | c.events = append(c.events, event{l.Time, fmt.Sprintf("%s saw \"Reconnecting…\" for the whole call (%gx, %.1f s)", short(p.tag), val(e.Reconnecting), val(e.ReconnectingSeconds))}) |
| 580 | } |
| 581 | if e.AudioBlocked { |
| 582 | c.events = append(c.events, event{l.Time, short(p.tag) + "'s browser blocked sound"}) |
| 583 | } |
| 584 | } |
| 585 | for _, q := range r.Peers { |
| 586 | key := [2]string{p.tag, string(q.Peer)} |
| 587 | d := c.dirs[key] |
| 588 | if d == nil { |
| 589 | d = &direction{paths: map[string]int{}} |
| 590 | c.dirs[key] = d |
| 591 | } |
| 592 | d.windows++ |
| 593 | if q.Path != nil { |
| 594 | d.pathWindows++ |
| 595 | d.paths[fmt.Sprintf("%s→%s %s", orDash(q.Path.Local), orDash(q.Path.Remote), orDash(q.Path.Protocol))]++ |
| 596 | if q.Path.Local == "relay" || q.Path.Remote == "relay" { |
| 597 | d.relayWindows++ |
| 598 | } |
| 599 | } |
| 600 | d.worstRTT = max(d.worstRTT, val(q.RTT)) |
| 601 | if q.Out != nil { |
| 602 | for _, o := range []*protocol.OutStream{q.Out.Audio, q.Out.Video, q.Out.Screen} { |
| 603 | if o != nil { |
| 604 | d.worstRTT = max(d.worstRTT, val(o.RTT)) |
| 605 | } |
| 606 | } |
| 607 | if s := q.Out.Screen; s != nil { |
| 608 | d.screenSteps += val(s.Rebuilds) + val(s.Reshares) + val(s.Failures) |
| 609 | } |
| 610 | } |
| 611 | if in := q.In; in != nil { |
| 612 | if a := in.Audio; a != nil { |
| 613 | if x := lossPct(a.Packets, a.Lost); x > d.worstLoss { |
| 614 | d.worstLoss, d.worstLossOf = x, "audio" |
| 615 | } |
| 616 | d.samples += val(a.Samples) |
| 617 | d.concealed += val(a.Concealed) |
| 618 | } |
| 619 | for kind, v := range map[string]*protocol.InVideo{"video": in.Video, "screen": in.Screen} { |
| 620 | if v == nil { |
| 621 | continue |
| 622 | } |
| 623 | if x := lossPct(v.Packets, v.Lost); x > d.worstLoss { |
| 624 | d.worstLoss, d.worstLossOf = x, kind |
| 625 | } |
| 626 | d.freezes += val(v.Freezes) |
| 627 | d.freezeSeconds += val(v.FreezeSeconds) |
| 628 | } |
| 629 | } |
| 630 | if e := q.Events; e != nil { |
| 631 | for i, x := range e.AskedSeconds { |
| 632 | if i < len(d.asked) { |
| 633 | d.asked[i] += val(x) |
| 634 | } |
| 635 | } |
| 636 | for i, x := range e.SentSeconds { |
| 637 | if i < len(d.sent) { |
| 638 | d.sent[i] += val(x) |
| 639 | } |
| 640 | } |
| 641 | d.cantHear += val(e.CantHearSeconds) |
| 642 | d.reconnecting += val(e.Reconnecting) |
| 643 | d.reconnectingSeconds += val(e.ReconnectingSeconds) |
| 644 | var said []string |
| 645 | if val(e.Reconnecting) > 0 { |
| 646 | said = append(said, fmt.Sprintf("\"Reconnecting to %s…\" (%.1f s)", short(string(q.Peer)), val(e.ReconnectingSeconds))) |
| 647 | } |
| 648 | if val(e.CantHearSeconds) > 0 { |
| 649 | said = append(said, fmt.Sprintf("\"%s can't hear you yet\" (%.1f s)", short(string(q.Peer)), val(e.CantHearSeconds))) |
| 650 | } |
| 651 | if e.NoSound { |
| 652 | d.noSound++ |
| 653 | said = append(said, "no sound from "+short(string(q.Peer))) |
| 654 | } |
| 655 | if e.NoSoundFromUs { |
| 656 | said = append(said, short(string(q.Peer))+" said they get no sound from them") |
| 657 | } |
| 658 | if e.ScreenStalled { |
| 659 | d.stalled++ |
| 660 | said = append(said, short(string(q.Peer))+"'s screen stalled") |
| 661 | } |
| 662 | if e.AudioBlocked { |
| 663 | d.blocked++ |
| 664 | said = append(said, short(string(q.Peer))+"'s browser blocks sound") |
| 665 | } |
| 666 | if len(said) > 0 { |
| 667 | c.events = append(c.events, event{l.Time, short(p.tag) + ": " + strings.Join(said, "; ")}) |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | if l.Final { |
| 672 | c.events = append(c.events, event{l.Time, short(p.tag) + " left (final report)"}) |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | func orDash(s string) string { |
| 677 | if s == "" { |
| 678 | return "-" |
| 679 | } |
| 680 | return s |
| 681 | } |
| 682 | |
| 683 | // short is a tag as the summary prints it: six characters are plenty to |
| 684 | // tell four people apart, and the full tag is in --call's output header. |
| 685 | func short(tag string) string { |
| 686 | if len(tag) > 6 { |
| 687 | return tag[:6] |
| 688 | } |
| 689 | return tag |
| 690 | } |
| 691 | |
| 692 | func (c *call) print(out io.Writer, loc *time.Location) { |
| 693 | f, l := c.first.In(loc), c.last.In(loc) |
| 694 | fmt.Fprintf(out, "Call %s %s – %s %s (%s), %d people, %d problem report(s)", |
| 695 | c.tag, f.Format("2006-01-02 15:04:05"), l.Format("15:04:05"), loc, l.Sub(f).Round(time.Second), len(c.people), len(c.problems)) |
| 696 | if len(c.autos) > 0 { |
| 697 | fmt.Fprintf(out, ", %d automatic report(s)", len(c.autos)) |
| 698 | } |
| 699 | fmt.Fprintln(out) |
| 700 | fmt.Fprintln(out, " People") |
| 701 | for _, tag := range c.order { |
| 702 | p := c.people[tag] |
| 703 | fmt.Fprintf(out, " %s %s %s %s %d reports", short(tag), orDash(p.app), p.version, p.platform, p.reports) |
| 704 | if p.devices != "" { |
| 705 | fmt.Fprintf(out, " (%s)", p.devices) |
| 706 | } |
| 707 | fmt.Fprintln(out) |
| 708 | if h := p.head; len(h) > 0 { |
| 709 | fmt.Fprintf(out, " worst loss %g%%, worst RTT %g ms, freezes in %g, \"Reconnecting…\" %g", h["maxLossPct"], h["maxRtt"], h["freezes"], h["reconnecting"]) |
| 710 | if s := headScreen(h); s != "" { |
| 711 | fmt.Fprintf(out, ", screen %s", s) |
| 712 | } |
| 713 | if h["videoAsked"] > 0 || h["videoSent"] > 0 { |
| 714 | fmt.Fprintf(out, ", asked someone for less video %s, sent someone less %s", minutes(h["videoAsked"]), minutes(h["videoSent"])) |
| 715 | } |
| 716 | fmt.Fprintln(out) |
| 717 | } |
| 718 | if m := p.machine.words(loc); m != "" { |
| 719 | fmt.Fprintf(out, " machine: %s\n", m) |
| 720 | } |
| 721 | } |
| 722 | if len(c.dirs) > 0 { |
| 723 | fmt.Fprintln(out, " Connections (what the first heard from the second)") |
| 724 | keys := make([][2]string, 0, len(c.dirs)) |
| 725 | for k := range c.dirs { |
| 726 | keys = append(keys, k) |
| 727 | } |
| 728 | sort.Slice(keys, func(i, j int) bool { return keys[i][0]+keys[i][1] < keys[j][0]+keys[j][1] }) |
| 729 | for _, k := range keys { |
| 730 | d := c.dirs[k] |
| 731 | path, n := "", 0 |
| 732 | for p, m := range d.paths { |
| 733 | if m > n || (m == n && p < path) { |
| 734 | path, n = p, m |
| 735 | } |
| 736 | } |
| 737 | relay := "-" |
| 738 | if d.pathWindows > 0 { |
| 739 | relay = fmt.Sprintf("%.0f%%", 100*float64(d.relayWindows)/float64(d.pathWindows)) |
| 740 | } |
| 741 | fmt.Fprintf(out, " %s ← %s worst loss %.1f%%%s, worst RTT %.0f ms, freezes %g (%.1f s)", |
| 742 | short(k[0]), short(k[1]), d.worstLoss, ofWhat(d.worstLossOf), d.worstRTT, d.freezes, d.freezeSeconds) |
| 743 | if d.samples > 0 { |
| 744 | fmt.Fprintf(out, ", audio concealed %.2f%%", 100*d.concealed/d.samples) |
| 745 | } |
| 746 | fmt.Fprintf(out, ", path %s, relay %s", orDash(path), relay) |
| 747 | if d.screenSteps > 0 { |
| 748 | fmt.Fprintf(out, ", screen recovery steps %g", d.screenSteps) |
| 749 | } |
| 750 | if s := videoLevels(d.asked); s != "" { |
| 751 | fmt.Fprintf(out, ", %s asked %s for %s", short(k[0]), short(k[1]), s) |
| 752 | } |
| 753 | if s := videoLevels(d.sent); s != "" { |
| 754 | fmt.Fprintf(out, ", %s sent %s %s because they asked", short(k[0]), short(k[1]), s) |
| 755 | } |
| 756 | fmt.Fprintln(out) |
| 757 | } |
| 758 | } |
| 759 | c.printSides(out) |
| 760 | if len(c.events) > 0 { |
| 761 | fmt.Fprintln(out, " Events") |
| 762 | sort.SliceStable(c.events, func(i, j int) bool { return c.events[i].at.Before(c.events[j].at) }) |
| 763 | for _, e := range c.events { |
| 764 | fmt.Fprintf(out, " %s %s\n", e.at.In(loc).Format("15:04:05"), e.text) |
| 765 | } |
| 766 | } |
| 767 | for _, p := range c.problems { |
| 768 | note := p.Note |
| 769 | if note == "" { |
| 770 | note = "(no note)" |
| 771 | } |
| 772 | fmt.Fprintf(out, " Problem %s at %s from %s (%s): %q\n quality-summary --problem %s --since <far enough back>\n", |
| 773 | p.Ref, p.Time.In(loc).Format("15:04:05"), short(p.Peer), p.App, note, p.Ref) |
| 774 | } |
| 775 | for _, a := range c.autos { |
| 776 | fmt.Fprintf(out, " Automatic report %s at %s from %s (%s), trigger %s\n quality-summary --problem %s --since <far enough back>\n", |
| 777 | a.Ref, a.Time.In(loc).Format("15:04:05"), short(a.Peer), orDash(a.App), orDash(a.Trigger), a.Ref) |
| 778 | } |
| 779 | fmt.Fprintln(out) |
| 780 | } |
| 781 | |
| 782 | // videoLevels is §15.30's seconds at each level in words — "Minimal for |
| 783 | // 4m0s, Audio only for 30s" — or "" when it was normal throughout. |
| 784 | func videoLevels(secs [protocol.VideoLevelCount]float64) string { |
| 785 | names := [protocol.VideoLevelCount]string{"Reduced", "Minimal", "Audio only"} |
| 786 | var parts []string |
| 787 | for i, v := range secs { |
| 788 | if v > 0 { |
| 789 | parts = append(parts, names[i]+" for "+minutes(v)) |
| 790 | } |
| 791 | } |
| 792 | return strings.Join(parts, ", ") |
| 793 | } |
| 794 | |
| 795 | // minutes is a number of seconds as a duration, to the second. |
| 796 | func minutes(secs float64) string { |
| 797 | return (time.Duration(secs * float64(time.Second))).Round(time.Second).String() |
| 798 | } |
| 799 | |
| 800 | // headScreen is a quality line's screen fields in words (spec §15.22's |
| 801 | // ladder and the receiver's watch), or "" when there are none. |
| 802 | func headScreen(h map[string]float64) string { |
| 803 | var parts []string |
| 804 | for _, f := range []struct{ key, words string }{ |
| 805 | {"screenStalls", "stalls reported here"}, |
| 806 | {"screenStallsNoPackets", "with nothing arriving"}, |
| 807 | {"screenStallsUndecoded", "arriving, not decoded"}, |
| 808 | {"screenReattaches", "pictures re-attached"}, |
| 809 | {"screenKeyframes", "keyframes sent for reports"}, |
| 810 | {"screenRebuilds", "rebuilds"}, |
| 811 | {"screenReshares", "shares again"}, |
| 812 | {"screenFailures", "gave up"}, |
| 813 | {"screenDamped", "steps held back as a loop"}, |
| 814 | {"screenEncoderStalls", "times the screen stopped leaving"}, |
| 815 | } { |
| 816 | if v := h[f.key]; v > 0 { |
| 817 | parts = append(parts, fmt.Sprintf("%g %s", v, f.words)) |
| 818 | } |
| 819 | } |
| 820 | return strings.Join(parts, ", ") |
| 821 | } |
| 822 | |
| 823 | func ofWhat(kind string) string { |
| 824 | if kind == "" { |
| 825 | return "" |
| 826 | } |
| 827 | return " (" + kind + ")" |
| 828 | } |
| 829 | |
| 830 | func printProblem(out io.Writer, lines []line, ref string, loc *time.Location) error { |
| 831 | var head *line |
| 832 | parts := map[int]string{} |
| 833 | of := 0 |
| 834 | for i := range lines { |
| 835 | l := &lines[i] |
| 836 | if l.Ref != ref { |
| 837 | continue |
| 838 | } |
| 839 | switch l.Msg { |
| 840 | case "calls: problem report", "calls: auto report": |
| 841 | head = l |
| 842 | case "calls: problem report diagnostics", "calls: auto report diagnostics": |
| 843 | parts[l.Part] = l.Text |
| 844 | of = l.Of |
| 845 | } |
| 846 | } |
| 847 | if head == nil { |
| 848 | return fmt.Errorf("no problem or automatic report %s in these lines (try a longer --since)", ref) |
| 849 | } |
| 850 | kind := "Problem report" |
| 851 | if head.Msg == "calls: auto report" { |
| 852 | kind = "Automatic report" |
| 853 | } |
| 854 | fmt.Fprintf(out, "%s %s, %s\n", kind, ref, head.Time.In(loc).Format("2006-01-02 15:04:05 MST")) |
| 855 | fmt.Fprintf(out, "call %s, peer %s, %s %s %s %s %s %s, live %v\n", head.Call, head.Peer, head.App, head.Version, |
| 856 | head.Browser, head.BrowVer, head.OS, head.OSVer, head.Live) |
| 857 | if head.Msg == "calls: auto report" { |
| 858 | fmt.Fprintf(out, "trigger: %s (sent by the client itself; nobody typed a note)\n\n", orDash(head.Trigger)) |
| 859 | } else { |
| 860 | fmt.Fprintf(out, "note: %s\n\n", orDash(head.Note)) |
| 861 | } |
| 862 | for i := 1; i <= of; i++ { |
| 863 | t, ok := parts[i] |
| 864 | if !ok { |
| 865 | fmt.Fprintf(out, "\n[part %d of %d is missing]\n", i, of) |
| 866 | continue |
| 867 | } |
| 868 | fmt.Fprint(out, t) |
| 869 | } |
| 870 | fmt.Fprintln(out) |
| 871 | return nil |
| 872 | } |
| 873 | |