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
use std::collections::BTreeSet;
use std::fs::File;
use std::io::Write;

use anyhow::Result;

use abstutil::prettyprint_usize;
use geom::{Distance, Duration, Polygon, Pt2D};
use map_gui::tools::PopupMsg;
use sim::TripMode;
use widgetry::{
    Choice, Color, CompareTimes, DrawWithTooltips, EventCtx, GeomBatch, GfxCtx, Line, Outcome,
    Panel, State, Text, TextExt, Toggle, Widget,
};

use crate::app::{App, Transition};
use crate::common::color_for_mode;
use crate::sandbox::dashboards::DashTab;

pub struct TripSummaries {
    panel: Panel,
}

impl TripSummaries {
    pub fn new(ctx: &mut EventCtx, app: &App, filter: Filter) -> Box<dyn State<App>> {
        let mut filters = vec!["Filters".text_widget(ctx)];
        for mode in TripMode::all() {
            filters.push(Toggle::colored_checkbox(
                ctx,
                mode.ongoing_verb(),
                color_for_mode(app, mode),
                filter.modes.contains(&mode),
            ));
        }
        filters.push(Widget::dropdown(
            ctx,
            "filter",
            filter.changes_pct,
            vec![
                Choice::new("any change", None),
                Choice::new("at least 1% change", Some(0.01)),
                Choice::new("at least 10% change", Some(0.1)),
                Choice::new("at least 50% change", Some(0.5)),
            ],
        ));
        filters.push(
            ctx.style()
                .btn_plain
                .text("Export to CSV")
                .build_def(ctx)
                .align_bottom(),
        );

        Box::new(TripSummaries {
            panel: Panel::new(Widget::col(vec![
                DashTab::TripSummaries.picker(ctx, app),
                Widget::row(vec![
                    Widget::col(filters).section(ctx),
                    Widget::col(vec![
                        summary_boxes(ctx, app, &filter),
                        Widget::row(vec![
                            contingency_table(ctx, app, &filter).bg(ctx.style().section_bg),
                            scatter_plot(ctx, app, &filter).bg(ctx.style().section_bg),
                        ])
                        .evenly_spaced(),
                    ]),
                ]),
            ]))
            .exact_size_percent(90, 90)
            .build(ctx),
        })
    }
}

impl State<App> for TripSummaries {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        match self.panel.event(ctx) {
            Outcome::Clicked(x) => match x.as_ref() {
                "Export to CSV" => {
                    return Transition::Push(match export_times(app) {
                        Ok(path) => PopupMsg::new(
                            ctx,
                            "Data exported",
                            vec![format!("Data exported to {}", path)],
                        ),
                        Err(err) => PopupMsg::new(ctx, "Export failed", vec![err.to_string()]),
                    });
                }
                "close" => {
                    return Transition::Pop;
                }
                _ => unreachable!(),
            },
            Outcome::Changed => {
                if let Some(t) = DashTab::TripSummaries.transition(ctx, app, &self.panel) {
                    return t;
                }

                let mut filter = Filter {
                    changes_pct: self.panel.dropdown_value("filter"),
                    modes: BTreeSet::new(),
                };
                for m in TripMode::all() {
                    if self.panel.is_checked(m.ongoing_verb()) {
                        filter.modes.insert(m);
                    }
                }
                Transition::Replace(TripSummaries::new(ctx, app, filter))
            }
            _ => Transition::Keep,
        }
    }

    fn draw(&self, g: &mut GfxCtx, _app: &App) {
        self.panel.draw(g);
    }
}

fn summary_boxes(ctx: &mut EventCtx, app: &App, filter: &Filter) -> Widget {
    if app.has_prebaked().is_none() {
        return Widget::nothing();
    }

    let mut num_same = 0;
    let mut num_faster = 0;
    let mut num_slower = 0;
    let mut sum_faster = Duration::ZERO;
    let mut sum_slower = Duration::ZERO;
    for (_, b, a, mode) in app
        .primary
        .sim
        .get_analytics()
        .both_finished_trips(app.primary.sim.time(), app.prebaked())
    {
        if !filter.modes.contains(&mode) {
            continue;
        }
        let same = if let Some(pct) = filter.changes_pct {
            pct_diff(a, b) <= pct
        } else {
            a == b
        };

        if same {
            num_same += 1;
        } else if a < b {
            num_faster += 1;
            sum_faster += b - a;
        } else {
            num_slower += 1;
            sum_slower += a - b;
        }
    }
    let num_total = (num_faster + num_slower + num_same) as f64;

    Widget::row(vec![
        Text::from_multiline(vec![
            Line(format!("Faster Trips: {}", prettyprint_usize(num_faster))).big_heading_plain(),
            Line(format!(
                "{:.2}% of finished trips",
                100.0 * (num_faster as f64) / num_total
            ))
            .small(),
            Line(format!(
                "Average {} faster per trip",
                if num_faster == 0 {
                    Duration::ZERO
                } else {
                    sum_faster / (num_faster as f64)
                }
            ))
            .small(),
            Line(format!("Saved {} in total", sum_faster)).small(),
        ])
        .into_widget(ctx)
        .container()
        .padding(20)
        .bg(Color::hex("#72CE36").alpha(0.5))
        .outline(ctx.style().section_outline),
        Text::from_multiline(vec![
            Line(format!("Slower Trips: {}", prettyprint_usize(num_slower))).big_heading_plain(),
            Line(format!(
                "{:.2}% of finished trips",
                100.0 * (num_slower as f64) / num_total
            ))
            .small(),
            Line(format!(
                "Average {} slower per trip",
                if num_slower == 0 {
                    Duration::ZERO
                } else {
                    sum_slower / (num_slower as f64)
                }
            ))
            .small(),
            Line(format!("Lost {} in total", sum_slower)).small(),
        ])
        .into_widget(ctx)
        .container()
        .padding(20)
        .bg(Color::hex("#EB3223").alpha(0.5))
        .outline(ctx.style().section_outline),
        Text::from_multiline(vec![
            Line(format!("Unchanged: {}", prettyprint_usize(num_same))).big_heading_plain(),
            Line(format!(
                "{:.2}% of finished trips",
                100.0 * (num_same as f64) / num_total
            ))
            .small(),
        ])
        .into_widget(ctx)
        .container()
        .padding(20)
        .bg(Color::hex("#F4DA22").alpha(0.5))
        .outline(ctx.style().section_outline),
    ])
    .evenly_spaced()
}

fn scatter_plot(ctx: &mut EventCtx, app: &App, filter: &Filter) -> Widget {
    if app.has_prebaked().is_none() {
        return Widget::nothing();
    }

    let points = filter.get_trips(app);
    if points.is_empty() {
        return Widget::nothing();
    }

    Widget::col(vec![
        Line("Trip time before and after")
            .small_heading()
            .into_widget(ctx),
        CompareTimes::new(
            ctx,
            format!(
                "Trip time before \"{}\"",
                app.primary.map.get_edits().edits_name
            ),
            format!(
                "Trip time after \"{}\"",
                app.primary.map.get_edits().edits_name
            ),
            points,
        ),
    ])
    .padding(16)
    .outline(ctx.style().section_outline)
}

fn contingency_table(ctx: &mut EventCtx, app: &App, filter: &Filter) -> Widget {
    if app.has_prebaked().is_none() {
        return Widget::nothing();
    }

    let total_width = 500.0;
    let total_height = 300.0;

    let points = filter.get_trips(app);
    if points.is_empty() {
        return Widget::nothing();
    }
    let num_buckets = 10;
    let (_, endpts) = points
        .iter()
        .map(|(b, a)| a.max(b))
        .max()
        .unwrap()
        .make_intervals_for_max(num_buckets);

    let mut batch = GeomBatch::new();
    batch.autocrop_dims = false;

    // Draw the X axis, time before changes in buckets.
    for (idx, mins) in endpts.iter().enumerate() {
        batch.append(
            Text::from(Line(mins.to_string()).secondary())
                .render(ctx)
                .centered_on(Pt2D::new(
                    (idx as f64) / (num_buckets as f64) * total_width,
                    total_height / 2.0,
                )),
        );
    }
    // TODO Position this better
    if false {
        batch.append(
            Text::from_multiline(vec![
                Line("trip").secondary(),
                Line("time").secondary(),
                Line("after").secondary(),
            ])
            .render(ctx)
            .translate(total_width, total_height / 2.0),
        );
    }

    // Now measure savings and losses per bucket.
    let mut savings_per_bucket: Vec<(Duration, usize)> = std::iter::repeat((Duration::ZERO, 0))
        .take(num_buckets)
        .collect();
    let mut losses_per_bucket: Vec<(Duration, usize)> = std::iter::repeat((Duration::ZERO, 0))
        .take(num_buckets)
        .collect();
    for (b, a) in points {
        let before_mins = b.num_minutes_rounded_up();
        let raw_idx = endpts.iter().rev().position(|x| before_mins >= *x).unwrap();
        let mut idx = endpts.len() - 1 - raw_idx;
        // Careful. We might be exactly the max...
        if idx == endpts.len() - 1 {
            idx -= 1;
        }

        if a > b {
            losses_per_bucket[idx].0 += a - b;
            losses_per_bucket[idx].1 += 1;
        } else if a < b {
            savings_per_bucket[idx].0 += b - a;
            savings_per_bucket[idx].1 += 1;
        }
    }
    let max_y = losses_per_bucket
        .iter()
        .max()
        .unwrap()
        .0
        .max(savings_per_bucket.iter().max().unwrap().0);

    // Draw the bars!
    let bar_width = total_width / (num_buckets as f64);
    let max_bar_height = (total_height - ctx.default_line_height()) / 2.0;
    let mut outlines = Vec::new();
    let mut tooltips = Vec::new();
    let mut x1 = 0.0;
    let mut idx = 0;
    for ((total_savings, num_savings), (total_loss, num_loss)) in savings_per_bucket
        .into_iter()
        .zip(losses_per_bucket.into_iter())
    {
        if num_savings > 0 {
            let height = (total_savings / max_y) * max_bar_height;
            let rect = Polygon::rectangle(bar_width, height).translate(x1, max_bar_height - height);
            if let Ok(o) = rect.to_outline(Distance::meters(1.5)) {
                outlines.push(o);
            }
            batch.push(Color::GREEN, rect.clone());
            tooltips.push((
                rect,
                Text::from_multiline(vec![
                    Line(format!(
                        "{} trips between {} and {} minutes",
                        prettyprint_usize(num_savings),
                        endpts[idx],
                        endpts[idx + 1]
                    )),
                    Line(format!("Saved {} in total", total_savings)).fg(Color::hex("#72CE36")),
                ]),
            ));
        }
        if num_loss > 0 {
            let height = (total_loss / max_y) * max_bar_height;
            let rect =
                Polygon::rectangle(bar_width, height).translate(x1, total_height - max_bar_height);
            if let Ok(o) = rect.to_outline(Distance::meters(1.5)) {
                outlines.push(o);
            }
            batch.push(Color::RED, rect.clone());
            tooltips.push((
                rect,
                Text::from_multiline(vec![
                    Line(format!(
                        "{} trips between {} and {} minutes",
                        prettyprint_usize(num_loss),
                        endpts[idx],
                        endpts[idx + 1]
                    )),
                    Line(format!("Lost {} in total", total_loss)).fg(Color::hex("#EB3223")),
                ]),
            ));
        }
        x1 += bar_width;
        idx += 1;
    }
    batch.extend(Color::BLACK, outlines);

    Widget::col(vec![
        Text::from_multiline(vec![
            Line("Number of slower/faster trips").small_heading(),
            Line("by ranges of trip time (after)").small_heading(),
        ])
        .into_widget(ctx),
        Line("number of trips (faster)")
            .secondary()
            .into_widget(ctx),
        DrawWithTooltips::new(ctx, batch, tooltips, Box::new(|_| GeomBatch::new())),
        Line("number of trips (slower)")
            .secondary()
            .into_widget(ctx),
    ])
    .padding(16)
    .outline(ctx.style().section_outline)
}

pub struct Filter {
    changes_pct: Option<f64>,
    modes: BTreeSet<TripMode>,
}

impl Filter {
    pub fn new() -> Filter {
        Filter {
            changes_pct: None,
            modes: TripMode::all().into_iter().collect(),
        }
    }

    fn get_trips(&self, app: &App) -> Vec<(Duration, Duration)> {
        let mut points = Vec::new();
        for (_, b, a, mode) in app
            .primary
            .sim
            .get_analytics()
            .both_finished_trips(app.primary.sim.time(), app.prebaked())
        {
            if self.modes.contains(&mode)
                && self
                    .changes_pct
                    .map(|pct| pct_diff(a, b) > pct)
                    .unwrap_or(true)
            {
                points.push((b, a));
            }
        }
        points
    }
}

fn pct_diff(a: Duration, b: Duration) -> f64 {
    if a >= b {
        (a / b) - 1.0
    } else {
        (b / a) - 1.0
    }
}

fn export_times(app: &App) -> Result<String> {
    let path = format!(
        "trip_times_{}_{}.csv",
        app.primary.map.get_name().as_filename(),
        app.primary.sim.time().as_filename()
    );
    let mut f = File::create(&path)?;
    writeln!(f, "id,mode,seconds_before,seconds_after")?;
    for (id, b, a, mode) in app
        .primary
        .sim
        .get_analytics()
        .both_finished_trips(app.primary.sim.time(), app.prebaked())
    {
        writeln!(
            f,
            "{},{:?},{},{}",
            id.0,
            mode,
            b.inner_seconds(),
            a.inner_seconds()
        )?;
    }
    Ok(path)
}