Skip to content

Commit

Permalink
Setup OTP Evaluation (#2081)
Browse files Browse the repository at this point in the history
* livebook working

* write to tmp dir

* use name for bus

* cleanup

* remove file

* change puts to inspect

* readme

* add dates

* rename file for consistency

* ...
  • Loading branch information
anthonyshull authored Jun 24, 2024
1 parent 0f01196 commit 46ebf9e
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 1 deletion.
5 changes: 5 additions & 0 deletions livebooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# USING LIVEBOOK

1. [Download, install, and run Livebook](https://livebook.dev/#install)
2. [Start the app and connect to it](https://fly.io/docs/elixir/advanced-guides/interesting-things-with-livebook/#connect-to-your-project)
3. Click "Open" at the top right to open the relevant Livebook
4 changes: 4 additions & 0 deletions livebooks/datetimes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- 2024-06-11T08:00
- 2024-06-12T12:00
- 2024-06-13T16:00
96 changes: 96 additions & 0 deletions livebooks/evaluate-open-trip-planner.livemd
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Evaluate Open Trip Planner

## Section

```elixir
defmodule EvaluateOpenTripPlanner.Helpers do
@moduledoc false

def attach_itineraries_to_plan(%{"from" => from, "to" => to} = plan, datetime) do
itineraries =
plan(from, to, depart_at: datetime)
|> Kernel.elem(1)
|> itineraries_to_strings()

plan
|> Map.put("datetime", Timex.format!(datetime, "%FT%H:%M", :strftime))
|> Map.put("itineraries", itineraries)
end

defp address_to_named_position(address) do
address
|> LocationService.geocode()
|> Kernel.elem(1)
|> List.first()
|> TripPlan.NamedPosition.new()
end

defp itineraries_to_strings(itineraries) when is_binary(itineraries) do
[itineraries]
end

defp itineraries_to_strings(itineraries) when is_list(itineraries) do
Enum.map(itineraries, &itinerary_to_string/1)
end

defp itinerary_to_string(itinerary) do
Enum.map(itinerary.legs, &leg_to_string/1)
end

defp leg_to_string(%{description: "WALK"} = leg) do
"#{leg.description} FROM #{leg.from.name} TO #{leg.to.name}"
end

defp leg_to_string(%{description: "BUS"} = leg) do
"TAKE #{leg.name} #{leg.description} FROM #{leg.from.name} TO #{leg.to.name}"
end

defp leg_to_string(leg) do
"TAKE #{leg.long_name} #{leg.description} FROM #{leg.from.name} TO #{leg.to.name}"
end

defp plan(from, to, opts) do
TripPlan.Api.OpenTripPlanner.plan(
address_to_named_position(from),
address_to_named_position(to),
opts
)
end
end

alias EvaluateOpenTripPlanner.Helpers
```

```elixir
Application.start(:yamerl)

write_path =
System.tmp_dir!()
|> Path.join("itineraries.yml")
|> IO.inspect()

datetimes =
File.cwd!()
|> Path.join("/livebooks/datetimes.yml")
|> YamlElixir.read_from_file!()
|> Enum.map(fn datetime ->
datetime
|> Timex.parse!("%Y-%m-%dT%H:%M", :strftime)
|> Timex.to_datetime("America/New_York")
end)

plans =
File.cwd!()
|> Path.join("/livebooks/plans.yml")
|> YamlElixir.read_from_file!()
|> Enum.map(fn plan ->
Enum.map(datetimes, fn datetime ->
Helpers.attach_itineraries_to_plan(plan, datetime)
end)
end)
|> List.flatten()

yaml = Ymlr.document!(plans)

File.write!(write_path, yaml)
```
13 changes: 13 additions & 0 deletions livebooks/plans.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
- name: Origin between two subway lines
from: 115 College Ave, Somerville, MA 02144
to: Tremont St & Park Street &, Winter St, Boston, MA 02108
- name: Origin along a bus route & walkable to station
from: 7 Walk Hill St, Jamaica Plain, MA 02130
to: 393 Massachusetts Ave, Boston, MA 02118
- name: Parallel subway & commuter rail
from: 186 Canal St, Boston, MA 02114
to: Commercial St &, Pleasant St, Malden, MA 02148
- name: Walking-only itineraries
from: 700 Atlantic Ave, Boston, MA 02110
to: 630 Washington St, Boston, MA 02111
4 changes: 3 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ defmodule DotCom.Mixfile do
{:timex, "3.1.24"},
{:unrooted_polytree, "0.1.1"},
{:uuid, "1.1.8"},
{:wallaby, "0.30.9", [runtime: false, only: [:test, :dev]]}
{:wallaby, "0.30.9", [runtime: false, only: [:test, :dev]]},
{:yaml_elixir, "2.9.0", only: [:dev]},
{:ymlr, "5.1.3", only: [:dev]}
]
end
end
3 changes: 3 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,7 @@
"vector": {:hex, :vector, "1.1.0", "0789b5e00e9c551d8d5880acab9a8f44ed46690d083af397018bf0c7f30c1092", [:mix], [], "hexpm", "48b0a800ec88e55b12c689b09100e4c9ba41ea1befb459221c085a4e70040696"},
"wallaby": {:hex, :wallaby, "0.30.9", "51d60682092c3c428c63b656b818e2258202b9f9a31ec37230659647ae20325b", [:mix], [{:ecto_sql, ">= 3.0.0", [hex: :ecto_sql, repo: "hexpm", optional: true]}, {:httpoison, "~> 0.12 or ~> 1.0 or ~> 2.0", [hex: :httpoison, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:phoenix_ecto, ">= 3.0.0", [hex: :phoenix_ecto, repo: "hexpm", optional: true]}, {:web_driver_client, "~> 0.2.0", [hex: :web_driver_client, repo: "hexpm", optional: false]}], "hexpm", "62e3ccb89068b231b50ed046219022020516d44f443eebef93a19db4be95b808"},
"web_driver_client": {:hex, :web_driver_client, "0.2.0", "63b76cd9eb3b0716ec5467a0f8bead73d3d9612e63f7560d21357f03ad86e31a", [:mix], [{:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:tesla, "~> 1.3", [hex: :tesla, repo: "hexpm", optional: false]}], "hexpm", "83cc6092bc3e74926d1c8455f0ce927d5d1d36707b74d9a65e38c084aab0350f"},
"yamerl": {:hex, :yamerl, "0.10.0", "4ff81fee2f1f6a46f1700c0d880b24d193ddb74bd14ef42cb0bcf46e81ef2f8e", [:rebar3], [], "hexpm", "346adb2963f1051dc837a2364e4acf6eb7d80097c0f53cbdc3046ec8ec4b4e6e"},
"yaml_elixir": {:hex, :yaml_elixir, "2.9.0", "9a256da867b37b8d2c1ffd5d9de373a4fda77a32a45b452f1708508ba7bbcb53", [:mix], [{:yamerl, "~> 0.10", [hex: :yamerl, repo: "hexpm", optional: false]}], "hexpm", "0cb0e7d4c56f5e99a6253ed1a670ed0e39c13fc45a6da054033928607ac08dfc"},
"ymlr": {:hex, :ymlr, "5.1.3", "a8061add5a378e20272a31905be70209a5680fdbe0ad51f40cb1af4bdd0a010b", [:mix], [], "hexpm", "8663444fa85101a117887c170204d4c5a2182567e5f84767f0071cf15f2efb1e"},
}

0 comments on commit 46ebf9e

Please sign in to comment.