emlop 0.6.1

A fast, accurate, ergonomic emerge.log parser
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
use crate::{datetime::*, parse::*, proces::*, table::*, *};
use std::{collections::{BTreeMap, HashMap},
          io::stdin};

/// Straightforward display of merge events
///
/// We store the start times in a hashmap to compute/print the duration when we reach a stop event.
pub fn cmd_list(args: &ArgMatches) -> Result<bool, Error> {
    let st = &Styles::from_args(args);
    let show = *args.get_one("show").unwrap();
    let hist = get_hist(args.get_one::<String>("logfile").unwrap().to_owned(),
                        value_opt(args, "from", parse_date, st.date_offset),
                        value_opt(args, "to", parse_date, st.date_offset),
                        show,
                        args.get_one::<String>("search").map(|s| s.as_str()),
                        args.get_flag("exact"))?;
    let first = *args.get_one("first").unwrap_or(&usize::MAX);
    let last = *args.get_one("last").unwrap_or(&usize::MAX);
    let stt = args.get_flag("starttime");
    let mut merges: HashMap<String, i64> = HashMap::new();
    let mut unmerges: HashMap<String, i64> = HashMap::new();
    let mut found = 0;
    let mut sync_start: Option<i64> = None;
    let mut tbl = Table::new(st).align_left(0).align_left(2).margin(2, " ").last(last);
    tbl.header(["Date", "Duration", "Package/Repo"]);
    for p in hist {
        match p {
            Hist::MergeStart { ts, key, .. } => {
                // This'll overwrite any previous entry, if a merge started but never finished
                merges.insert(key, ts);
            },
            Hist::MergeStop { ts, ref key, .. } => {
                found += 1;
                let started = merges.remove(key).unwrap_or(ts + 1);
                tbl.row([&[&FmtDate(if stt { started } else { ts })],
                         &[&FmtDur(ts - started)],
                         &[&st.merge, &p.ebuild_version()]]);
            },
            Hist::UnmergeStart { ts, key, .. } => {
                // This'll overwrite any previous entry, if an unmerge started but never finished
                unmerges.insert(key, ts);
            },
            Hist::UnmergeStop { ts, ref key, .. } => {
                found += 1;
                let started = unmerges.remove(key).unwrap_or(ts + 1);
                tbl.row([&[&FmtDate(if stt { started } else { ts })],
                         &[&FmtDur(ts - started)],
                         &[&st.unmerge, &p.ebuild_version()]]);
            },
            Hist::SyncStart { ts } => {
                // Some sync starts have multiple entries in old logs
                sync_start = Some(ts);
            },
            Hist::SyncStop { ts, repo } => {
                if let Some(started) = sync_start.take() {
                    found += 1;
                    tbl.row([&[&FmtDate(if stt { started } else { ts })],
                             &[&FmtDur(ts - started)],
                             &[&st.clr, &"Sync ", &repo]]);
                } else {
                    warn!("Sync stop without a start at {ts}")
                }
            },
        }
        if found >= first {
            break;
        }
    }
    Ok(found > 0)
}

/// Wrapper to extract stats from a list of data points (durations).
struct Times {
    vals: Vec<i64>,
    count: i64,
    tot: i64,
}
impl Times {
    fn new() -> Self {
        Self { vals: vec![], count: 0, tot: 0 }
    }
    /// Digest new data point
    ///
    /// Data points should be inserted in chronological order.
    /// We don't store negative values but we still take them into account.
    fn insert(&mut self, t: i64) {
        self.count += 1;
        if t > 0 {
            self.vals.insert(0, t);
            self.tot += t;
        }
    }
    /// Predict the next data point by looking at past ones
    fn pred(&self, lim: u16, avg: Average) -> i64 {
        if self.vals.is_empty() {
            return -1; // FIXME Return None
        }
        let l = self.vals.len().min(lim as usize);
        match avg {
            // Simple arithmetic mean
            Average::Arith => self.vals.iter().take(l).sum::<i64>() / l as i64,
            // Middle value (or avg of the middle two)
            Average::Median => {
                let mut s: Vec<i64> = self.vals.iter().copied().take(l).collect();
                s.sort_unstable();
                if l % 2 == 0 {
                    (s[(l / 2) - 1] + s[l / 2]) / 2
                } else {
                    s[l / 2]
                }
            },
            // Arithmically weighted arithmetic mean
            // Eg for 4 values the weights are 4,3,2,1 (most recent value first)
            Average::WeightedArith => {
                let (s, n, w) =
                    self.vals
                        .iter()
                        .take(l)
                        .fold((0, l as i64, 0), |(s, n, w), v| (s + v * n, n - 1, w + n));
                debug_assert!(n == 0);
                s / w
            },
            // Arithmically weighted median
            // Eg 3,1,2 -> 3,3,3,1,1,2 -> 1,1,2,2,3,3,3 -> 2
            Average::WeightedMedian => {
                let mut s = Vec::with_capacity((l + 1) * (l / 2 + 1));
                for (n, v) in self.vals.iter().copied().take(l).enumerate() {
                    s.resize(s.len() - n + l, v);
                }
                s.sort_unstable();
                let l = s.len();
                if l % 2 == 0 {
                    (s[(l / 2) - 1] + s[l / 2]) / 2
                } else {
                    s[l / 2]
                }
            },
        }
    }
}

/// Summary display of merge events
///
/// First loop is like cmd_list but we store the merge time for each ebuild instead of printing it.
/// Then we compute the stats per ebuild, and print that.
pub fn cmd_stats(args: &ArgMatches) -> Result<bool, Error> {
    let st = &Styles::from_args(args);
    let show = *args.get_one("show").unwrap();
    let timespan_opt: Option<&Timespan> = args.get_one("group");
    let hist = get_hist(args.get_one::<String>("logfile").unwrap().to_owned(),
                        value_opt(args, "from", parse_date, st.date_offset),
                        value_opt(args, "to", parse_date, st.date_offset),
                        show,
                        args.get_one::<String>("search").map(|s| s.as_str()),
                        args.get_flag("exact"))?;
    let lim = *args.get_one("limit").unwrap();
    let avg = *args.get_one("avg").unwrap();
    let tsname = timespan_opt.map_or("", |timespan| timespan.name());
    let mut tbls = Table::new(st).align_left(0).align_left(1).margin(1, " ");
    tbls.header([tsname, "Repo", "Sync count", "Total time", "Predict time"]);
    let mut tblp = Table::new(st).align_left(0).align_left(1).margin(1, " ");
    tblp.header([tsname,
                 "Package",
                 "Merge count",
                 "Total time",
                 "Predict time",
                 "Unmerge count",
                 "Total time",
                 "Predict time"]);
    let mut tblt = Table::new(st).align_left(0).margin(1, " ");
    tblt.header([tsname,
                 "Merge count",
                 "Total time",
                 "Predict time",
                 "Unmerge count",
                 "Total time",
                 "Predict time"]);
    let mut merge_start: HashMap<String, i64> = HashMap::new();
    let mut unmerge_start: HashMap<String, i64> = HashMap::new();
    let mut pkg_time: BTreeMap<String, (Times, Times)> = BTreeMap::new();
    let mut sync_start: Option<i64> = None;
    let mut sync_time: BTreeMap<String, Times> = BTreeMap::new();
    let mut nextts = 0;
    let mut curts = 0;
    for p in hist {
        if let Some(timespan) = timespan_opt {
            let t = p.ts();
            if nextts == 0 {
                nextts = timespan.next(t, st.date_offset);
                curts = t;
            } else if t > nextts {
                let group = timespan.at(curts, st.date_offset);
                cmd_stats_group(&mut tbls, &mut tblp, &mut tblt, st, lim, avg, show, group,
                                &sync_time, &pkg_time);
                sync_time.clear();
                pkg_time.clear();
                nextts = timespan.next(t, st.date_offset);
                curts = t;
            }
        }
        match p {
            Hist::MergeStart { ts, key, .. } => {
                merge_start.insert(key, ts);
            },
            Hist::MergeStop { ts, ref key, .. } => {
                if let Some(start_ts) = merge_start.remove(key) {
                    let (times, _) = pkg_time.entry(p.ebuild().to_owned())
                                             .or_insert_with(|| (Times::new(), Times::new()));
                    times.insert(ts - start_ts);
                }
            },
            Hist::UnmergeStart { ts, key, .. } => {
                unmerge_start.insert(key, ts);
            },
            Hist::UnmergeStop { ts, ref key, .. } => {
                if let Some(start_ts) = unmerge_start.remove(key) {
                    let (_, times) = pkg_time.entry(p.ebuild().to_owned())
                                             .or_insert_with(|| (Times::new(), Times::new()));
                    times.insert(ts - start_ts);
                }
            },
            Hist::SyncStart { ts } => {
                // Some sync starts have multiple entries in old logs
                sync_start = Some(ts);
            },
            Hist::SyncStop { ts, repo } => {
                if let Some(start_ts) = sync_start.take() {
                    let times = sync_time.entry(repo).or_insert_with(Times::new);
                    times.insert(ts - start_ts);
                } else {
                    warn!("Sync stop without a start at {ts}")
                }
            },
        }
    }
    let group = timespan_opt.map(|timespan| timespan.at(curts, st.date_offset)).unwrap_or_default();
    cmd_stats_group(&mut tbls, &mut tblp, &mut tblt, st, lim, avg, show, group, &sync_time,
                    &pkg_time);
    // Controlled drop to ensure table order and insert blank lines
    let (es, ep, et) = (!tbls.is_empty(), !tblp.is_empty(), !tblt.is_empty());
    drop(tbls);
    if es && ep {
        println!();
    }
    drop(tblp);
    if (es || ep) && et {
        println!();
    }
    drop(tblt);
    Ok(!pkg_time.is_empty() || !sync_time.is_empty())
}

// Reducing the arg count here doesn't seem worth it, for either readability or performance
#[allow(clippy::too_many_arguments)]
fn cmd_stats_group(tbls: &mut Table<5>,
                   tblp: &mut Table<8>,
                   tblt: &mut Table<7>,
                   st: &Styles,
                   lim: u16,
                   avg: Average,
                   show: Show,
                   group: String,
                   sync_time: &BTreeMap<String, Times>,
                   pkg_time: &BTreeMap<String, (Times, Times)>) {
    // Syncs
    if show.sync && !sync_time.is_empty() {
        for (repo, time) in sync_time {
            tbls.row([&[&group],
                      &[repo],
                      &[&st.cnt, &time.count],
                      &[&FmtDur(time.tot)],
                      &[&FmtDur(time.pred(lim, avg))]]);
        }
    }
    // Packages
    if show.pkg && !pkg_time.is_empty() {
        for (pkg, (merge, unmerge)) in pkg_time {
            tblp.row([&[&group],
                      &[&st.pkg, pkg],
                      &[&st.cnt, &merge.count],
                      &[&FmtDur(merge.tot)],
                      &[&FmtDur(merge.pred(lim, avg))],
                      &[&st.cnt, &unmerge.count],
                      &[&FmtDur(unmerge.tot)],
                      &[&FmtDur(unmerge.pred(lim, avg))]]);
        }
    }
    // Totals
    if show.tot && !pkg_time.is_empty() {
        let mut merge_time = 0;
        let mut merge_count = 0;
        let mut unmerge_time = 0;
        let mut unmerge_count = 0;
        for (merge, unmerge) in pkg_time.values() {
            merge_time += merge.tot;
            merge_count += merge.count;
            unmerge_time += unmerge.tot;
            unmerge_count += unmerge.count;
        }
        tblt.row([&[&group],
                  &[&st.cnt, &merge_count],
                  &[&FmtDur(merge_time)],
                  &[&FmtDur(merge_time.checked_div(merge_count).unwrap_or(-1))],
                  &[&st.cnt, &unmerge_count],
                  &[&FmtDur(unmerge_time)],
                  &[&FmtDur(unmerge_time.checked_div(unmerge_count).unwrap_or(-1))]]);
    }
}

/// Predict future merge time
///
/// Very similar to cmd_summary except we want total build time for a list of ebuilds.
pub fn cmd_predict(args: &ArgMatches) -> Result<bool, Error> {
    let st = &Styles::from_args(args);
    let now = epoch_now();
    let show: Show = *args.get_one("show").unwrap();
    let first = *args.get_one("first").unwrap_or(&usize::MAX);
    let last = match args.get_one("last") {
        Some(&n) if show.tot => n + 1,
        Some(&n) => n,
        None => usize::MAX,
    };
    let lim = *args.get_one("limit").unwrap();
    let avg = *args.get_one("avg").unwrap();
    let resume = *args.get_one("resume").unwrap();
    let mut tbl = Table::new(st).align_left(0).align_left(2).margin(2, " ").last(last);
    let tmpdir = args.get_one::<String>("tmpdir").unwrap();

    // Gather and print info about current merge process.
    let mut cms = std::i64::MAX;
    for i in get_all_info(Some("emerge")) {
        cms = std::cmp::min(cms, i.start);
        if show.emerge {
            tbl.row([&[&i], &[&FmtDur(now - i.start)], &[]]);
        }
    }
    if cms == std::i64::MAX
       && atty::is(atty::Stream::Stdin)
       && resume != ResumeKind::Main
       && resume != ResumeKind::Backup
    {
        tbl.row([&[&"No ongoing merge found"], &[], &[]]);
        return Ok(false);
    }

    // Parse emerge log.
    let hist = get_hist(args.get_one::<String>("logfile").unwrap().to_owned(),
                        value_opt(args, "from", parse_date, st.date_offset),
                        value_opt(args, "to", parse_date, st.date_offset),
                        Show { merge: true, ..Show::default() },
                        None,
                        false)?;
    let mut started: BTreeMap<Pkg, i64> = BTreeMap::new();
    let mut times: HashMap<String, Times> = HashMap::new();
    for p in hist {
        match p {
            Hist::MergeStart { ts, .. } => {
                started.insert(Pkg::new(p.ebuild(), p.version()), ts);
            },
            Hist::MergeStop { ts, .. } => {
                if let Some(start_ts) = started.remove(&Pkg::new(p.ebuild(), p.version())) {
                    let timevec = times.entry(p.ebuild().to_string()).or_insert_with(Times::new);
                    timevec.insert(ts - start_ts);
                }
            },
            _ => unreachable!("Should only receive Hist::{{Start,Stop}}"),
        }
    }

    // Build list of pending merges
    let pkgs: Vec<Pkg> = if atty::is(atty::Stream::Stdin) {
        // From resume data + emerge.log after current merge process start time
        let mut r = get_resume(resume);
        for p in started.iter().filter(|&(_, t)| *t > cms).map(|(p, _)| p) {
            if !r.contains(p) {
                r.push(p.clone())
            }
        }
        r
    } else {
        // From portage's stdout
        get_pretend(stdin(), "STDIN")
    };

    // Gather and print per-package and indivudual stats.
    let mut totcount = 0;
    let mut totunknown = 0;
    let mut totpredict = 0;
    let mut totelapsed = 0;
    for p in pkgs {
        totcount += 1;
        // Find the elapsed time, if any (heuristic is that emerge process started before
        // this merge finished, it's not failsafe but IMHO no worse than genlop).
        let elapsed = match started.remove(&p) {
            Some(s) if s > cms => now - s,
            _ => 0,
        };

        // Find the predicted time and adjust counters
        let pred = match times.get(p.ebuild()) {
            Some(tv) => {
                let pred = tv.pred(lim, avg);
                totpredict += pred;
                if elapsed > 0 {
                    totelapsed += elapsed;
                    totpredict -= std::cmp::min(pred, elapsed);
                }
                pred
            },
            None => {
                totunknown += 1;
                -1
            },
        };

        // Done
        if show.merge && totcount <= first {
            if elapsed > 0 {
                let stage = get_buildlog(&p, tmpdir).unwrap_or_default();
                tbl.row([&[&st.pkg, &p.ebuild_version()],
                         &[&FmtDur(pred)],
                         &[&st.clr, &"- ", &FmtDur(elapsed), &st.clr, &stage]]);
            } else {
                tbl.row([&[&st.pkg, &p.ebuild_version()], &[&FmtDur(pred)], &[]]);
            }
        }
    }
    if totcount > 0 {
        if show.tot {
            let mut s: Vec<&dyn Disp> = vec![&"Estimate for ",
                                             &st.cnt,
                                             &totcount,
                                             &st.clr,
                                             if totcount > 1 { &" ebuilds" } else { &" ebuild" }];
            if totunknown > 0 {
                s.extend::<[&dyn Disp; 5]>([&", ", &st.cnt, &totunknown, &st.clr, &" unknown"]);
            }
            let tothidden = totcount.saturating_sub(first.min(last - 1));
            if tothidden > 0 {
                s.extend::<[&dyn Disp; 5]>([&", ", &st.cnt, &tothidden, &st.clr, &" hidden"]);
            }
            let e = FmtDur(totelapsed);
            if totelapsed > 0 {
                s.extend::<[&dyn Disp; 4]>([&", ", &e, &st.clr, &" elapsed"]);
            }
            tbl.row([&s,
                     &[&FmtDur(totpredict), &st.clr],
                     &[&"@ ", &st.dur, &FmtDate(now + totpredict)]]);
        }
    } else {
        tbl.row([&[&"No pretended merge found"], &[], &[]]);
    }
    Ok(totcount > 0)
}

pub fn cmd_accuracy(args: &ArgMatches) -> Result<bool, Error> {
    let st = &Styles::from_args(args);
    let show: Show = *args.get_one("show").unwrap();
    let hist = get_hist(args.get_one::<String>("logfile").unwrap().to_owned(),
                        value_opt(args, "from", parse_date, st.date_offset),
                        value_opt(args, "to", parse_date, st.date_offset),
                        Show { merge: true, ..Show::default() },
                        args.get_one::<String>("search").map(|s| s.as_str()),
                        args.get_flag("exact"))?;
    let last = *args.get_one("last").unwrap_or(&usize::MAX);
    let lim = *args.get_one("limit").unwrap();
    let avg = *args.get_one("avg").unwrap();
    let mut pkg_starts: HashMap<String, i64> = HashMap::new();
    let mut pkg_times: BTreeMap<String, Times> = BTreeMap::new();
    let mut pkg_errs: BTreeMap<String, Vec<f64>> = BTreeMap::new();
    let mut found = false;
    let mut tbl = Table::new(st).align_left(0).align_left(1).last(last);
    tbl.header(["Date", "Package", "Real", "Predicted", "Error"]);
    for p in hist {
        match p {
            Hist::MergeStart { ts, key, .. } => {
                // This'll overwrite any previous entry, if a merge started but never finished
                pkg_starts.insert(key, ts);
            },
            Hist::MergeStop { ts, ref key, .. } => {
                found = true;
                if let Some(start) = pkg_starts.remove(key) {
                    let times = pkg_times.entry(p.ebuild().to_owned()).or_insert_with(Times::new);
                    let real = ts - start;
                    match times.pred(lim, avg) {
                        -1 => {
                            if show.merge {
                                tbl.row([&[&FmtDate(ts)],
                                         &[&st.merge, &p.ebuild_version()],
                                         &[&FmtDur(real)],
                                         &[],
                                         &[]])
                            }
                        },
                        pred => {
                            let err = (pred - real).abs() as f64 * 100.0 / real as f64;
                            if show.merge {
                                tbl.row([&[&FmtDate(ts)],
                                         &[&st.merge, &p.ebuild_version()],
                                         &[&FmtDur(real)],
                                         &[&FmtDur(pred)],
                                         &[&st.cnt, &format!("{err:.1}%")]])
                            }
                            let errs =
                                pkg_errs.entry(p.ebuild().to_owned()).or_insert_with(Vec::new);
                            errs.push(err);
                        },
                    }
                    times.insert(real);
                }
            },
            e => panic!("Unexpected {e:?}"),
        }
    }
    drop(tbl);
    if show.tot {
        let mut tbl = Table::new(st).align_left(0);
        tbl.header(["Package", "Error"]);
        for (p, e) in pkg_errs {
            let avg = e.iter().sum::<f64>() / e.len() as f64;
            tbl.row([&[&st.pkg, &p], &[&st.cnt, &format!("{avg:.1}%")]]);
        }
    }
    Ok(found)
}

pub fn cmd_complete(args: &ArgMatches) -> Result<bool, Error> {
    let shell: clap_complete::Shell = *args.get_one("shell").unwrap();
    let mut cli = cli::build_cli_nocomplete();
    clap_complete::generate(shell, &mut cli, "emlop", &mut std::io::stdout());
    Ok(true)
}


#[cfg(test)]
mod tests {
    #[test]
    fn averages() {
        use super::Times;
        use crate::Average::*;
        for (a, m, wa, wm, lim, vals) in
            [(-1, -1, -1, -1, 10, vec![]),
             (1, 1, 1, 1, 10, vec![1]),
             (12 / 2, 6, 21 / 3, 10, 10, vec![2, 10]),
             (12 / 2, 6, 14 / 3, 2, 10, vec![10, 2]),
             (15 / 3, 4, (1 + 20 + 12) / (1 + 2 + 3), 4, 10, vec![1, 10, 4]),
             (15 / 4, 2, (1 + 20 + 9 + 4) / (1 + 2 + 3 + 4), 2, 10, vec![1, 10, 3, 1]),
             (15 / 4, 2, (1 + 20 + 9 + 4) / (1 + 2 + 3 + 4), 2, 4, vec![999, 1, 10, 3, 1])]
        {
            let mut t = Times::new();
            for &v in vals.iter() {
                t.insert(v);
            }
            assert_eq!(a, t.pred(lim, Arith), "arith {lim} {vals:?}");
            assert_eq!(m, t.pred(lim, Median), "median {lim} {vals:?}");
            assert_eq!(wa, t.pred(lim, WeightedArith), "weighted arith {lim} {vals:?}");
            assert_eq!(wm, t.pred(lim, WeightedMedian), "weighted median {lim} {vals:?}");
        }
    }
}