Skip to content

Commit

Permalink
Merge branch 'master' into irve-dataframe
Browse files Browse the repository at this point in the history
  • Loading branch information
thbar authored Nov 29, 2024
2 parents 7f0cef1 + df9ec60 commit a35805c
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 7 deletions.
6 changes: 3 additions & 3 deletions apps/transport/client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1997,9 +1997,9 @@ cosmiconfig@^8.2.0:
path-type "^4.0.0"

cross-spawn@^7.0.2, cross-spawn@^7.0.3:
version "7.0.3"
resolved "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz"
integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==
version "7.0.6"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.6.tgz#8a58fe78f00dcd70c370451759dfbfaf03e8ee9f"
integrity sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==
dependencies:
path-key "^3.1.0"
shebang-command "^2.0.0"
Expand Down
27 changes: 24 additions & 3 deletions apps/transport/lib/transport/gbfs_metadata.ex
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,13 @@ defmodule Transport.GBFSMetadata do
end

def stats(%{"data" => _data} = payload) do
stations_statistics(payload)
station_status_statistics(payload)
|> Map.merge(station_information_statistics(payload))
|> Map.merge(vehicle_statistics(payload))
|> Map.merge(%{version: 2})
end

def stations_statistics(%{"data" => _data} = payload) do
def station_status_statistics(%{"data" => _data} = payload) do
feed_url = feed_url_by_name(payload, :station_status)

with {:feed_exists, true} <- {:feed_exists, not is_nil(feed_url)},
Expand All @@ -249,7 +250,6 @@ defmodule Transport.GBFSMetadata do
nb_installed_stations: Enum.count(stations, & &1["is_installed"]),
nb_renting_stations: Enum.count(stations, & &1["is_renting"]),
nb_returning_stations: Enum.count(stations, & &1["is_returning"]),
nb_virtual_stations: Enum.count(stations, & &1["is_virtual_station"]),
nb_docks_available: stations |> Enum.map(& &1["num_docks_available"]) |> non_nil_sum(),
nb_docks_disabled: stations |> Enum.map(& &1["num_docks_disabled"]) |> non_nil_sum(),
nb_vehicles_available_stations: stations |> Enum.map(&vehicles_available/1) |> non_nil_sum(),
Expand All @@ -265,6 +265,27 @@ defmodule Transport.GBFSMetadata do
end
end

def station_information_statistics(%{"data" => _data} = payload) do
feed_url = feed_url_by_name(payload, :station_information)

with {:feed_exists, true} <- {:feed_exists, not is_nil(feed_url)},
{:ok, %HTTPoison.Response{status_code: 200, body: body}} <- cached_http_get(feed_url),
{:ok, json} <- Jason.decode(body) do
stations = json["data"]["stations"]

%{
nb_virtual_stations: Enum.count(stations, & &1["is_virtual_station"])
}
else
{:feed_exists, false} ->
%{}

e ->
Logger.error("Cannot get GBFS station_information details: #{inspect(e)}")
%{}
end
end

def vehicle_statistics(%{"data" => _data} = payload) do
feed_url = feed_url_by_name(payload, :vehicle_status)

Expand Down
4 changes: 4 additions & 0 deletions apps/transport/test/fixture/gbfs/gbfs.2.2.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"name": "station_status",
"url": "https://example.com/gbfs/station_status"
},
{
"name": "station_information",
"url": "https://example.com/gbfs/station_information"
},
{
"name": "free_bike_status",
"url": "https://example.com/gbfs/free_bike_status"
Expand Down
4 changes: 4 additions & 0 deletions apps/transport/test/fixture/gbfs/gbfs.3.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
"name": "station_status",
"url": "https://example.com/gbfs/station_status"
},
{
"name": "station_information",
"url": "https://example.com/gbfs/station_information"
},
{
"name": "vehicle_status",
"url": "https://example.com/gbfs/vehicle_status"
Expand Down
19 changes: 19 additions & 0 deletions apps/transport/test/fixture/gbfs/station_information.2.2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"last_updated": 1609866247,
"ttl": 0,
"version": "2.2",
"data": {
"stations": [
{
"station_id": "pga",
"name": "Parking garage A",
"lat": 12.345678,
"lon": 45.678901,
"vehicle_type_capacity": {
"abc123": 7,
"def456": 9
}
}
]
}
}
86 changes: 86 additions & 0 deletions apps/transport/test/fixture/gbfs/station_information.3.0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"last_updated": "2023-07-17T13:34:13+02:00",
"ttl": 0,
"version": "3.0",
"data": {
"stations": [
{
"station_id": "pga",
"name": [
{
"text": "Parking garage A",
"language": "en"
}
],
"lat": 12.345678,
"lon": 45.678901,
"station_opening_hours": "Su-Th 05:00-22:00; Fr-Sa 05:00-01:00",
"parking_type": "underground_parking",
"parking_hoop": false,
"contact_phone": "+33109874321",
"is_charging_station": true,
"vehicle_docks_capacity": [
{
"vehicle_type_ids": ["abc123"],
"count": 7
}
]
},
{
"station_id": "station12",
"name": [
{
"text": "SE Belmont & SE 10th",
"language": "en"
}
],
"lat": 45.516445,
"lon": -122.655775,
"is_valet_station": false,
"is_virtual_station": true,
"is_charging_station": false,
"station_area": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-122.655775,
45.516445
],
[
-122.655705,
45.516445
],
[
-122.655705,
45.516495
],
[
-122.655775,
45.516495
],
[
-122.655775,
45.516445
]
]
]
]
},
"capacity": 16,
"vehicle_types_capacity": [
{
"vehicle_type_ids": ["abc123", "def456"],
"count": 15
},
{
"vehicle_type_ids": ["def456"],
"count": 1
}

]
}
]
}
}
1 change: 0 additions & 1 deletion apps/transport/test/fixture/gbfs/station_status.3.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"last_reported":"2023-07-17T13:34:13+02:00",
"num_docks_available":8,
"num_docks_disabled":1,
"is_virtual_station":true,
"vehicle_docks_available":[
{
"vehicle_type_ids":[
Expand Down
2 changes: 2 additions & 0 deletions apps/transport/test/transport/gbfs_metadata_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ defmodule Transport.GBFSMetadataTest do
describe "stats" do
test "3.0 feed" do
setup_response("https://example.com/gbfs/station_status", fixture_content("station_status.3.0"))
setup_response("https://example.com/gbfs/station_information", fixture_content("station_information.3.0"))
setup_response("https://example.com/gbfs/vehicle_status", fixture_content("vehicle_status.3.0"))

# `version` is bumped when keys change
Expand Down Expand Up @@ -644,6 +645,7 @@ defmodule Transport.GBFSMetadataTest do

test "2.2 feed" do
setup_response("https://example.com/gbfs/station_status", fixture_content("station_status.2.2"))
setup_response("https://example.com/gbfs/station_information", fixture_content("station_information.2.2"))
setup_response("https://example.com/gbfs/free_bike_status", fixture_content("free_bike_status.2.2"))

# Values may not make sense: responses have been taken from the GBFS spec
Expand Down

0 comments on commit a35805c

Please sign in to comment.