-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Live.Admin.TripPlanFeedback): click to download the data (#1932)
* fix(DotcomWeb.Router): properly define the root layout * feat(DotcomWeb.Live.Admin): new admin page * feat(TripPlan.FeedbackCSV): turn feedback into table * feat(Live.Admin.TripPlanFeedback): click to download the data * fix(TripPlanController): set modes default value
- Loading branch information
1 parent
c7bdf47
commit fff1e8a
Showing
16 changed files
with
954 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
defmodule DotcomWeb.TripPlan.FeedbackCSV do | ||
@moduledoc """ | ||
Handle formatting feedback into a spreadsheet-friendly format. | ||
""" | ||
|
||
alias TripPlan.PersonalDetail.Step | ||
|
||
@headers [ | ||
"generated_time", | ||
"itinerary_index", | ||
"feedback_vote", | ||
"feedback_long", | ||
"mode_subway", | ||
"mode_commuter_rail", | ||
"mode_bus", | ||
"mode_ferry", | ||
"query_wheelchair", | ||
"query_time_type", | ||
"query_date_time", | ||
"query_from", | ||
"query_to", | ||
"itinerary_0_accessible", | ||
"itinerary_0_tag", | ||
"itinerary_0_start_stop", | ||
"itinerary_0_legs", | ||
"itinerary_1_accessible", | ||
"itinerary_1_tag", | ||
"itinerary_1_start_stop", | ||
"itinerary_1_legs", | ||
"itinerary_2_accessible", | ||
"itinerary_2_tag", | ||
"itinerary_2_start_stop", | ||
"itinerary_2_legs", | ||
"itinerary_3_accessible", | ||
"itinerary_3_tag", | ||
"itinerary_3_start_stop", | ||
"itinerary_3_legs", | ||
"itinerary_4_accessible", | ||
"itinerary_4_tag", | ||
"itinerary_4_start_stop", | ||
"itinerary_4_legs" | ||
] | ||
|
||
def rows(data) do | ||
data | ||
|> Enum.map(&format_all/1) | ||
|> CSV.encode(headers: @headers) | ||
|> Enum.to_list() | ||
end | ||
|
||
def format_all(data) do | ||
data_map = | ||
data | ||
|> Enum.reduce(%{}, fn | ||
{key, value}, acc when key in ["feedback_vote", "feedback_long", "itinerary_index"] -> | ||
Map.put(acc, key, value) | ||
|
||
{"generated_time", time_string}, acc -> | ||
Map.put(acc, "generated_time", format_time(time_string)) | ||
|
||
{"modes", modes}, acc -> | ||
modes = Enum.map(modes, fn {k, v} -> {"mode_#{k}", v} end) |> Enum.into(%{}) | ||
Map.merge(acc, modes) | ||
|
||
{"query", | ||
%{ | ||
"from" => from, | ||
"to" => to, | ||
"date_time" => date_time, | ||
"time_type" => time_type, | ||
"wheelchair" => wheelchair, | ||
"itineraries" => itineraries | ||
}}, | ||
acc -> | ||
acc | ||
|> Map.merge(%{ | ||
"query_from" => place_description(from), | ||
"query_to" => place_description(to), | ||
"query_date_time" => format_time(date_time), | ||
"query_time_type" => time_type, | ||
"query_wheelchair" => wheelchair | ||
}) | ||
|> Map.merge(mapped_itineraries(itineraries)) | ||
|
||
_, acc -> | ||
acc | ||
end) | ||
|
||
# sometimes fields are empty or not populated, so seed them here | ||
@headers | ||
|> Enum.map(&{&1, ""}) | ||
|> Map.new() | ||
|> Map.merge(data_map) | ||
end | ||
|
||
defp place_description(%{"name" => name, "stop_id" => stop_id}) when not is_nil(stop_id), | ||
do: name <> " (id: #{stop_id})" | ||
|
||
defp place_description(%{"name" => name, "latitude" => lat, "longitude" => lon}), | ||
do: "#{name} (#{lat}, #{lon})" | ||
|
||
defp format_time(t) do | ||
with {:ok, dt} <- Timex.parse(t, "{ISO:Extended}"), | ||
{:ok, formatted} <- Timex.format(dt, "{ISOdate} {kitchen}") do | ||
formatted | ||
end | ||
end | ||
|
||
defp mapped_itineraries(itineraries) do | ||
itineraries | ||
|> Enum.with_index() | ||
|> Enum.map(fn {%{ | ||
"tag" => tag, | ||
"accessible?" => accessible, | ||
"stop" => stop, | ||
"start" => start, | ||
"legs" => legs | ||
}, index} -> | ||
prefix = "itinerary_#{index}_" | ||
|
||
%{ | ||
(prefix <> "tag") => tag, | ||
(prefix <> "accessible") => accessible, | ||
(prefix <> "start_stop") => "#{format_time(start)}, #{format_time(stop)}", | ||
(prefix <> "legs") => Enum.map(legs, &mapped_leg/1) |> Enum.join(";\n") | ||
} | ||
end) | ||
|> Enum.reduce(&Map.merge/2) | ||
end | ||
|
||
defp mapped_leg(%{"from" => from, "to" => to, "mode" => mode}) do | ||
"#{place_description(from)} to #{place_description(to)} via #{mode_description(mode)}" | ||
end | ||
|
||
defp mode_description(%{"route_id" => route_id, "trip_id" => trip_id}), | ||
do: route_id <> " route on trip " <> trip_id | ||
|
||
defp mode_description(%{"steps" => steps, "distance" => distance}) do | ||
step_description = | ||
Enum.map(steps, fn %{"relative_direction" => direction, "street_name" => street_name} -> | ||
relative_direction = String.to_atom(direction) | ||
|
||
[ | ||
Step.human_relative_direction(relative_direction), | ||
Step.human_relative_preposition(relative_direction), | ||
street_name | ||
] | ||
|> Enum.join(" ") | ||
end) | ||
|> Enum.join(";\n\t") | ||
|
||
"walking #{distance} meters:\n\t#{step_description}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule DotcomWeb.Live.Admin do | ||
use DotcomWeb, :live_view | ||
|
||
def mount(_params, _session, socket) do | ||
{:ok, | ||
assign( | ||
socket, | ||
:admin_features, | ||
[ | ||
%{ | ||
url: Helpers.live_path(socket, DotcomWeb.Live.Admin.TripPlanFeedback), | ||
title: "Trip Planner Feedback", | ||
description: "Find and download the latest comments and votes." | ||
} | ||
] | ||
)} | ||
end | ||
|
||
def render(assigns) do | ||
~H""" | ||
<section style="display: grid; gap: .5rem; grid-template-columns: 1fr 1fr 1fr"> | ||
<%= for feature <- @admin_features do %> | ||
<%= link to: feature.url, class: "btn btn-secondary", style: "white-space: inherit;" do %> | ||
<header class="h3 mt-0"><%= feature.title %></header> | ||
<p class="mb-0"><%= feature.description %></p> | ||
<% end %> | ||
<% end %> | ||
<div class="btn btn-secondary disabled" style="white-space: inherit;"> | ||
<header class="h3 mt-0">???</header> | ||
<p class="mb-0">Your idea here (just send a message to <code>@thecristen</code>)</p> | ||
</div> | ||
</section> | ||
""" | ||
end | ||
end |
Oops, something went wrong.