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
use geom::Duration;
use sim::{TripEndpoint, TripID, TripPhaseType};
use widgetry::table::{Col, Filter, Table};
use widgetry::{
    EventCtx, Filler, GeomBatch, GfxCtx, Line, Outcome, Panel, State, Text, Toggle, Widget,
};

use crate::app::{App, Transition};
use crate::sandbox::dashboards::generic_trip_table::{open_trip_transition, preview_trip};
use crate::sandbox::dashboards::DashTab;

// TODO Compare all of these things before/after

pub struct ParkingOverhead {
    tab: DashTab,
    table: Table<App, Entry, Filters>,
    panel: Panel,
}

impl ParkingOverhead {
    pub fn new(ctx: &mut EventCtx, app: &App) -> Box<dyn State<App>> {
        let table = make_table(app);
        let col = Widget::col(vec![
            DashTab::ParkingOverhead.picker(ctx, app),
            Widget::col(vec![
                Widget::row(vec![
                    Text::from_multiline(vec![
                        Line(
                            "Trips taken by car also include time to walk between the building \
                             and parking spot, as well as the time to find parking.",
                        ),
                        Line("Overhead is 1 - driving time / total time"),
                        Line("Ideally, overhead is 0% -- the entire trip is just spent driving."),
                        Line(""),
                        Line("High overhead could mean:"),
                        Line(
                            "- the car burned more resources and caused more traffic looking for \
                             parking",
                        ),
                        Line(
                            "- somebody with impaired movement had to walk far to reach their \
                             vehicle",
                        ),
                        Line("- the person was inconvenienced"),
                        Line(""),
                        Line(
                            "Note: Trips beginning/ending outside the map have an artifically \
                             high overhead,",
                        ),
                        Line("since the time spent driving off-map isn't shown here."),
                    ])
                    .into_widget(ctx),
                    Filler::square_width(ctx, 0.15).named("preview"),
                ])
                .evenly_spaced(),
                table.render(ctx, app),
            ])
            .section(ctx),
        ]);

        let panel = Panel::new(col).exact_size_percent(90, 90).build(ctx);

        Box::new(Self {
            tab: DashTab::ParkingOverhead,
            table,
            panel,
        })
    }
}

impl State<App> for ParkingOverhead {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        match self.panel.event(ctx) {
            Outcome::Clicked(x) => {
                if self.table.clicked(&x) {
                    self.table.replace_render(ctx, app, &mut self.panel);
                } else if let Ok(idx) = x.parse::<usize>() {
                    return open_trip_transition(app, idx);
                } else if x == "close" {
                    return Transition::Pop;
                } else {
                    unreachable!()
                }
            }
            Outcome::Changed(_) => {
                if let Some(t) = self.tab.transition(ctx, app, &self.panel) {
                    return t;
                }

                self.table.panel_changed(&self.panel);
                self.table.replace_render(ctx, app, &mut self.panel);
            }
            _ => {}
        }

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        self.panel.draw(g);
        preview_trip(g, app, &self.panel, GeomBatch::new());
    }
}

struct Entry {
    trip: TripID,
    total_duration: Duration,
    driving_duration: Duration,
    parking_duration: Duration,
    walking_duration: Duration,
    percent_overhead: usize,
    starts_off_map: bool,
    ends_off_map: bool,
}

struct Filters {
    starts_off_map: bool,
    ends_off_map: bool,
}

fn produce_raw_data(app: &App) -> Vec<Entry> {
    // Gather raw data
    let mut data = Vec::new();
    for (id, phases) in app.primary.sim.get_analytics().get_all_trip_phases() {
        let trip = app.primary.sim.trip_info(id);
        let starts_off_map = match trip.start {
            TripEndpoint::Border(_) => true,
            _ => false,
        };
        let ends_off_map = match trip.end {
            TripEndpoint::Border(_) => true,
            _ => false,
        };

        let mut total_duration = Duration::ZERO;
        let mut driving_duration = Duration::ZERO;
        let mut parking_duration = Duration::ZERO;
        let mut walking_duration = Duration::ZERO;
        let mut ok = true;
        for p in phases {
            if let Some(t2) = p.end_time {
                let dt = t2 - p.start_time;
                total_duration += dt;
                match p.phase_type {
                    TripPhaseType::Driving => {
                        driving_duration += dt;
                    }
                    TripPhaseType::Walking => {
                        walking_duration += dt;
                    }
                    TripPhaseType::Parking => {
                        parking_duration += dt;
                    }
                    _ => {}
                }
            } else {
                ok = false;
                break;
            }
        }
        if !ok || driving_duration == Duration::ZERO {
            continue;
        }

        data.push(Entry {
            trip: id,
            total_duration,
            driving_duration,
            parking_duration,
            walking_duration,
            percent_overhead: (100.0 * (1.0 - (driving_duration / total_duration))) as usize,
            starts_off_map,
            ends_off_map,
        });
    }
    data
}

fn make_table(app: &App) -> Table<App, Entry, Filters> {
    let filter: Filter<App, Entry, Filters> = Filter {
        state: Filters {
            starts_off_map: true,
            ends_off_map: true,
        },
        to_controls: Box::new(move |ctx, _, state| {
            Widget::row(vec![
                Toggle::switch(ctx, "starting off-map", None, state.starts_off_map),
                Toggle::switch(ctx, "ending off-map", None, state.ends_off_map),
            ])
        }),
        from_controls: Box::new(|panel| Filters {
            starts_off_map: panel.is_checked("starting off-map"),
            ends_off_map: panel.is_checked("ending off-map"),
        }),
        apply: Box::new(|state, x, _| {
            if !state.starts_off_map && x.starts_off_map {
                return false;
            }
            if !state.ends_off_map && x.ends_off_map {
                return false;
            }
            true
        }),
    };

    let mut table = Table::new(
        "parking_overhead",
        produce_raw_data(app),
        Box::new(|x| x.trip.0.to_string()),
        "Percent overhead",
        filter,
    );
    table.static_col("Trip ID", Box::new(|x| x.trip.0.to_string()));
    table.column(
        "Total duration",
        Box::new(|ctx, app, x| Text::from(x.total_duration.to_string(&app.opts.units)).render(ctx)),
        Col::Sortable(Box::new(|rows| rows.sort_by_key(|x| x.total_duration))),
    );
    table.column(
        "Driving duration",
        Box::new(|ctx, app, x| {
            Text::from(x.driving_duration.to_string(&app.opts.units)).render(ctx)
        }),
        Col::Sortable(Box::new(|rows| rows.sort_by_key(|x| x.driving_duration))),
    );
    table.column(
        "Parking duration",
        Box::new(|ctx, app, x| {
            Text::from(x.parking_duration.to_string(&app.opts.units)).render(ctx)
        }),
        Col::Sortable(Box::new(|rows| rows.sort_by_key(|x| x.parking_duration))),
    );
    table.column(
        "Walking duration",
        Box::new(|ctx, app, x| {
            Text::from(x.walking_duration.to_string(&app.opts.units)).render(ctx)
        }),
        Col::Sortable(Box::new(|rows| rows.sort_by_key(|x| x.walking_duration))),
    );
    table.column(
        "Percent overhead",
        Box::new(|ctx, _, x| Text::from(format!("{}%", x.percent_overhead)).render(ctx)),
        Col::Sortable(Box::new(|rows| rows.sort_by_key(|x| x.percent_overhead))),
    );

    table
}