From a23fd8b10821ddc88b8167f9c1cc171f4096a36c Mon Sep 17 00:00:00 2001 From: Antoine Augusti Date: Fri, 29 Nov 2024 11:17:12 +0100 Subject: [PATCH] GBFSMetadata : lit station_information.is_virtual_station (#4340) --- apps/transport/lib/transport/gbfs_metadata.ex | 27 +++++- .../transport/test/fixture/gbfs/gbfs.2.2.json | 4 + .../transport/test/fixture/gbfs/gbfs.3.0.json | 4 + .../fixture/gbfs/station_information.2.2.json | 19 ++++ .../fixture/gbfs/station_information.3.0.json | 86 +++++++++++++++++++ .../test/fixture/gbfs/station_status.3.0.json | 1 - .../test/transport/gbfs_metadata_test.exs | 2 + 7 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 apps/transport/test/fixture/gbfs/station_information.2.2.json create mode 100644 apps/transport/test/fixture/gbfs/station_information.3.0.json diff --git a/apps/transport/lib/transport/gbfs_metadata.ex b/apps/transport/lib/transport/gbfs_metadata.ex index 701f084344..14064729ae 100644 --- a/apps/transport/lib/transport/gbfs_metadata.ex +++ b/apps/transport/lib/transport/gbfs_metadata.ex @@ -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)}, @@ -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(), @@ -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) diff --git a/apps/transport/test/fixture/gbfs/gbfs.2.2.json b/apps/transport/test/fixture/gbfs/gbfs.2.2.json index 59c6fe7a19..dfbbe7bf68 100644 --- a/apps/transport/test/fixture/gbfs/gbfs.2.2.json +++ b/apps/transport/test/fixture/gbfs/gbfs.2.2.json @@ -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" diff --git a/apps/transport/test/fixture/gbfs/gbfs.3.0.json b/apps/transport/test/fixture/gbfs/gbfs.3.0.json index eb2bfeb958..6683e790a7 100644 --- a/apps/transport/test/fixture/gbfs/gbfs.3.0.json +++ b/apps/transport/test/fixture/gbfs/gbfs.3.0.json @@ -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" diff --git a/apps/transport/test/fixture/gbfs/station_information.2.2.json b/apps/transport/test/fixture/gbfs/station_information.2.2.json new file mode 100644 index 0000000000..c3b03bd595 --- /dev/null +++ b/apps/transport/test/fixture/gbfs/station_information.2.2.json @@ -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 + } + } + ] + } +} diff --git a/apps/transport/test/fixture/gbfs/station_information.3.0.json b/apps/transport/test/fixture/gbfs/station_information.3.0.json new file mode 100644 index 0000000000..6d5880f6ae --- /dev/null +++ b/apps/transport/test/fixture/gbfs/station_information.3.0.json @@ -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 + } + + ] + } + ] + } +} diff --git a/apps/transport/test/fixture/gbfs/station_status.3.0.json b/apps/transport/test/fixture/gbfs/station_status.3.0.json index 86f4cc9c1f..0e12581866 100644 --- a/apps/transport/test/fixture/gbfs/station_status.3.0.json +++ b/apps/transport/test/fixture/gbfs/station_status.3.0.json @@ -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":[ diff --git a/apps/transport/test/transport/gbfs_metadata_test.exs b/apps/transport/test/transport/gbfs_metadata_test.exs index 024091eb8f..48feb910cf 100644 --- a/apps/transport/test/transport/gbfs_metadata_test.exs +++ b/apps/transport/test/transport/gbfs_metadata_test.exs @@ -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 @@ -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