Skip to content

Commit

Permalink
fix: Move simple/advanced date input toggle to a separate, earlier ce…
Browse files Browse the repository at this point in the history
…ll to take advantage of Livebook's staling feature and not have to use Kino.listen
  • Loading branch information
jzimbel-mbta committed Aug 29, 2024
1 parent 803d0dc commit 932d081
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 74 deletions.
85 changes: 48 additions & 37 deletions reports/glides_terminal_departure_accuracy.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ Cells that generate inputs / outputs are marked with a "👉"—you can skip to

## Setup

```elixir
use_advanced_date? = Kino.Shorts.read_checkbox("Use advanced date/time input?")
Kino.nothing()
```

**Simple:** Choose a date. The report will analyze the full service day on that date.

**Advanced:** Choose a start date+time and end date+time. The report will analyze within that window only.

```elixir
env_input =
Kino.Input.select("Environment", [
Expand All @@ -69,23 +78,43 @@ env_input =
{"-sandbox", "sandbox"}
])

# DateTimes are cached in the process dictionary to prevent the input's default
# value from changing (and the input from resetting to that default) every time this cell re-runs.
process_get = fn key, default_fn ->
case Process.get(key, nil) do
nil ->
value = default_fn.()
Process.put(key, {:value, value})
value
{:value, value} ->
value
end
end


today_date =
DateTime.utc_now()
|> DateTime.shift_zone!("America/New_York")
|> DateTime.to_date()
process_get.(:today_date, fn ->
DateTime.utc_now()
|> DateTime.shift_zone!("America/New_York")
|> DateTime.to_date()
end)

yesterday_date = Date.add(today_date, -1)
yesterday_date = process_get.(:yesterday_date, fn -> Date.add(today_date, -1) end)

# Default setting: Analyze a full service day, starting at 4am yesterday (Eastern) and ending at 4am today (Eastern).
yesterday_start_time =
DateTime.new!(yesterday_date, ~T[04:00:00], "America/New_York")
|> DateTime.shift_zone!("Etc/UTC")
|> DateTime.to_naive()
process_get.(:yesterday_start_time, fn ->
DateTime.new!(yesterday_date, ~T[04:00:00], "America/New_York")
|> DateTime.shift_zone!("Etc/UTC")
|> DateTime.to_naive()
end)

today_end_time =
DateTime.new!(today_date, ~T[03:59:59], "America/New_York")
|> DateTime.shift_zone!("Etc/UTC")
|> DateTime.to_naive()
process_get.(:today_end_time, fn ->
DateTime.new!(today_date, ~T[03:59:59], "America/New_York")
|> DateTime.shift_zone!("Etc/UTC")
|> DateTime.to_naive()
end)

# Simple date input
date_input = Kino.Input.date("Date", default: yesterday_date, max: today_date)
Expand All @@ -106,11 +135,6 @@ sample_rate_input =
samples_per_minute_input =
Kino.Input.number("Take (?) samples per minute - leave blank for ALL", default: 1)

use_advanced_date_input =
Kino.Input.select("Use advanced date/time input?", nil: "", false: "No", true: "Yes")

frame = Kino.Frame.new(placeholder: false)

content_map = %{
true:
Kino.Layout.grid(
Expand Down Expand Up @@ -164,38 +188,25 @@ doc_table_map =
""")}
end)

update_frame = fn
nil ->
Kino.Frame.clear(frame)

use_advanced_date ->
Kino.Frame.clear(frame)
Kino.Frame.append(frame, content_map[use_advanced_date])
Kino.Frame.append(frame, doc_table_map[use_advanced_date])
end

# Update the form content whenever the "Use advanced input?" toggle changes.
Kino.listen(use_advanced_date_input, fn %{value: value} -> update_frame.(value) end)
frame = Kino.Frame.new(placeholder: false)
Kino.Frame.clear(frame)
Kino.Frame.append(frame, content_map[use_advanced_date?])
Kino.Frame.append(frame, doc_table_map[use_advanced_date?])

Kino.render(use_advanced_date_input)
frame
```

Read inputs.

```elixir
# Some manual validation:
if is_nil(Kino.Input.read(use_advanced_date_input)) do
Kino.interrupt!(:error, ~s|Please choose an option in the "Use advanced date/time input?" dropdown.|)
end

sample_count = Kino.Input.read(samples_per_minute_input)

if is_integer(sample_count) and sample_count <= 0 do
if is_number(sample_count) and trunc(sample_count) <= 0 do
Kino.interrupt!(:error, "Samples per minute must be either blank or a positive integer.")
end

if Kino.Input.read(use_advanced_date_input) do
if use_advanced_date? do
start_dt = Kino.Input.read(start_date_input)
end_dt = Kino.Input.read(end_date_input)

Expand Down Expand Up @@ -228,11 +239,11 @@ end

# Inputs are valid. Drop them into a settings struct.
loader_settings =
if Kino.Input.read(use_advanced_date_input) do
if use_advanced_date? do
GlidesReport.Settings.Load.new(
Kino.Input.read(env_input),
Kino.Input.read(start_date_input) |> DateTime.from_naive!("Etc/UTC"),
Kino.Input.read(end_date_input) |> DateTime.from_naive!("Etc/UTC"),
Kino.Input.read(end_date_input) |> DateTime.from_naive!("Etc/UTC"),
Kino.Input.read(sample_rate_input),
Kino.Input.read(samples_per_minute_input)
)
Expand Down Expand Up @@ -589,4 +600,4 @@ VegaLite.new(width: 500, height: 300, title: "Percent accurate by bucket size")
|> VegaLite.encode_field(:y, "Accuracy (% of total predictions)", type: :quantitative)
```

<!-- livebook:{"offset":20496,"stamp":{"token":"XCP.-EXEMhs5k9Gi_mbKx8oXARcJZasMGsfSE8Rd-SjV-BpOlr5qKkoFFkLN7WMMR-aoIzpoqCPXgNUJnhb6tAKAHt0wSrpFxRQX2xuKH6KqDJu7CSFDic0DnbFpNjkOxgAPuUvD1GzcFo7YCOLg2nJAiOgHI5-ewA","version":2}} -->
<!-- livebook:{"offset":20788,"stamp":{"token":"XCP.v-Vmiri-1qFf7hWBFEpvcszBFXQZQX1vPvzAn98g7DRUwZmh3rjuiTxseJggpriZzee8HKpf2-TX3FGHpXyrk_0oJaFHlsMiBbWm6YsrmoiuqRnfTQfT1Zk-Neb7r-WQ0wxJb8VWMe9eKfus2zjEdFKx3wf6ew","version":2}} -->
87 changes: 50 additions & 37 deletions reports/glides_terminal_missed_extraneous_predictions.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ Cells that generate inputs / outputs are marked with a "👉"—you can skip to

## Setup

```elixir
use_advanced_date? = Kino.Shorts.read_checkbox("Use advanced date/time input?")
Kino.nothing()
```

**Simple:** Choose a date. The report will analyze the full service day on that date.

**Advanced:** Choose a start date+time and end date+time. The report will analyze within that window only.

<!-- livebook:{"break_markdown":true} -->

### 👉 What data do you want to load?

```elixir
Expand All @@ -70,23 +81,43 @@ env_input =
{"-sandbox", "sandbox"}
])

# DateTimes are cached in the process dictionary to prevent the input's default
# value from changing (and the input from resetting to that default) every time this cell re-runs.
process_get = fn key, default_fn ->
case Process.get(key, nil) do
nil ->
value = default_fn.()
Process.put(key, {:value, value})
value
{:value, value} ->
value
end
end


today_date =
DateTime.utc_now()
|> DateTime.shift_zone!("America/New_York")
|> DateTime.to_date()
process_get.(:today_date, fn ->
DateTime.utc_now()
|> DateTime.shift_zone!("America/New_York")
|> DateTime.to_date()
end)

yesterday_date = Date.add(today_date, -1)
yesterday_date = process_get.(:yesterday_date, fn -> Date.add(today_date, -1) end)

# Default setting: Analyze a full service day, starting at 4am yesterday (Eastern) and ending at 4am today (Eastern).
yesterday_start_time =
DateTime.new!(yesterday_date, ~T[04:00:00], "America/New_York")
|> DateTime.shift_zone!("Etc/UTC")
|> DateTime.to_naive()
process_get.(:yesterday_start_time, fn ->
DateTime.new!(yesterday_date, ~T[04:00:00], "America/New_York")
|> DateTime.shift_zone!("Etc/UTC")
|> DateTime.to_naive()
end)

today_end_time =
DateTime.new!(today_date, ~T[03:59:59], "America/New_York")
|> DateTime.shift_zone!("Etc/UTC")
|> DateTime.to_naive()
process_get.(:today_end_time, fn ->
DateTime.new!(today_date, ~T[03:59:59], "America/New_York")
|> DateTime.shift_zone!("Etc/UTC")
|> DateTime.to_naive()
end)

# Simple date input
date_input = Kino.Input.date("Date", default: yesterday_date, max: today_date)
Expand All @@ -107,11 +138,6 @@ sample_rate_input =
samples_per_minute_input =
Kino.Input.number("Take (?) samples per minute - leave blank for ALL", default: 1)

use_advanced_date_input =
Kino.Input.select("Use advanced date/time input?", nil: "", false: "No", true: "Yes")

frame = Kino.Frame.new(placeholder: false)

content_map = %{
true:
Kino.Layout.grid(
Expand Down Expand Up @@ -165,38 +191,25 @@ doc_table_map =
""")}
end)

update_frame = fn
nil ->
Kino.Frame.clear(frame)

use_advanced_date ->
Kino.Frame.clear(frame)
Kino.Frame.append(frame, content_map[use_advanced_date])
Kino.Frame.append(frame, doc_table_map[use_advanced_date])
end

# Update the form content whenever the "Use advanced input?" toggle changes.
Kino.listen(use_advanced_date_input, fn %{value: value} -> update_frame.(value) end)
frame = Kino.Frame.new(placeholder: false)
Kino.Frame.clear(frame)
Kino.Frame.append(frame, content_map[use_advanced_date?])
Kino.Frame.append(frame, doc_table_map[use_advanced_date?])

Kino.render(use_advanced_date_input)
frame
```

Read inputs.

```elixir
# Some manual validation:
if is_nil(Kino.Input.read(use_advanced_date_input)) do
Kino.interrupt!(:error, ~s|Please choose an option in the "Use advanced date/time input?" dropdown.|)
end

sample_count = Kino.Input.read(samples_per_minute_input)

if is_integer(sample_count) and sample_count <= 0 do
if is_number(sample_count) and trunc(sample_count) <= 0 do
Kino.interrupt!(:error, "Samples per minute must be either blank or a positive integer.")
end

if Kino.Input.read(use_advanced_date_input) do
if use_advanced_date? do
start_dt = Kino.Input.read(start_date_input)
end_dt = Kino.Input.read(end_date_input)

Expand Down Expand Up @@ -229,11 +242,11 @@ end

# Inputs are valid. Drop them into a settings struct.
loader_settings =
if Kino.Input.read(use_advanced_date_input) do
if use_advanced_date? do
GlidesReport.Settings.Load.new(
Kino.Input.read(env_input),
Kino.Input.read(start_date_input) |> DateTime.from_naive!("Etc/UTC"),
Kino.Input.read(end_date_input) |> DateTime.from_naive!("Etc/UTC"),
Kino.Input.read(end_date_input) |> DateTime.from_naive!("Etc/UTC"),
Kino.Input.read(sample_rate_input),
Kino.Input.read(samples_per_minute_input)
)
Expand Down Expand Up @@ -556,4 +569,4 @@ Kino.Download.new(
Kino.DataTable.new(trips_table, name: trips_table_name)
```

<!-- livebook:{"offset":19072,"stamp":{"token":"XCP.aq9STmMpkaP4Y6YvvQoTHCyZGPWFv6CytxecLkjkeR1dEKo7Qw9itfMmpTOtvBWkeJsQ9w0rvDfSX5s_WgXK4EENx3_Ps0nkGBGHb2filReMKUL8L7COxK4w1A8UXTKZZW37-ILIkyplLaJesJE0lTFlM1mLRg","version":2}} -->
<!-- livebook:{"offset":19407,"stamp":{"token":"XCP.0bc84sDOs5zFXN5CwioIeyNlJgq8iWyBmTe574h5aUXCdsggE9YZVu9e2PY8pXpmkR8dAJL95hzDVG1MqEm6nEsYC4JFDE0J9V_OhNxRqfQpvLV-ylIt1h9-E-w42XMA2pUxbUtbAIyN3tWLx1KvxZgOnUD_wQ","version":2}} -->

0 comments on commit 932d081

Please sign in to comment.