diff --git a/apps/shared/lib/gbfs_metadata.ex b/apps/shared/lib/gbfs_metadata.ex index e18fcf9bde..05c68b72dd 100644 --- a/apps/shared/lib/gbfs_metadata.ex +++ b/apps/shared/lib/gbfs_metadata.ex @@ -182,7 +182,7 @@ defmodule Transport.Shared.GBFSMetadata do with {:feed_exists, true} <- {:feed_exists, not is_nil(feed_url)}, {:ok, %HTTPoison.Response{status_code: 200, body: body}} <- http_client().get(feed_url), {:ok, json} <- Jason.decode(body) do - stations = json["data"]["stations"] + stations = Enum.reject(json["data"]["stations"], &unrealistic_station_data?/1) %{ nb_stations: Enum.count(stations), @@ -231,6 +231,24 @@ defmodule Transport.Shared.GBFSMetadata do end end + @doc """ + Is the number of docks or vehicles unrealistic for this station? (more than 500 docks or vehicles). + If yes, used to ignore this station to maintain relevant statistics. + + iex> unrealistic_station_data?(%{"num_vehicles_available" => 1_000}) + true + iex> unrealistic_station_data?(%{"num_docks_available" => 1_000}) + true + iex> unrealistic_station_data?(%{"num_docks_available" => 20, "num_vehicles_available" => 10}) + false + """ + def unrealistic_station_data?(%{} = data) do + data + |> Map.take(["num_vehicles_available", "num_bikes_available", "num_docks_available"]) + |> Map.values() + |> Enum.any?(&(&1 >= 500)) + end + # As of 3.0 defp vehicles_available(%{"num_vehicles_available" => num_vehicles_available}), do: num_vehicles_available # Before 3.0 diff --git a/apps/shared/test/fixtures/gbfs/station_status.2.2.json b/apps/shared/test/fixtures/gbfs/station_status.2.2.json index 2a4c95c20d..e0fdffa8f0 100644 --- a/apps/shared/test/fixtures/gbfs/station_status.2.2.json +++ b/apps/shared/test/fixtures/gbfs/station_status.2.2.json @@ -1,54 +1,84 @@ { - "last_updated": 1609866247, - "ttl": 0, - "version": "2.2", - "data": { - "stations": [ + "last_updated":1609866247, + "ttl":0, + "version":"2.2", + "data":{ + "stations":[ { - "station_id": "station 1", - "is_installed": true, - "is_renting": true, - "is_returning": true, - "last_reported": 1609866125, - "num_docks_available": 3, - "vehicle_docks_available": [{ - "vehicle_type_ids": ["abc123"], - "count": 2 - }, { - "vehicle_type_ids": ["def456"], - "count": 1 - }], - "num_bikes_available": 1, - "vehicle_types_available": [{ - "vehicle_type_id": "abc123", - "count": 1 - }, { - "vehicle_type_id": "def456", - "count": 0 - }] - }, { - "station_id": "station 2", - "is_installed": true, - "is_renting": true, - "is_returning": true, - "last_reported": 1609866106, - "num_docks_available": 8, - "num_docks_disabled": 1, - "vehicle_docks_available": [{ - "vehicle_type_ids": ["abc123"], - "count": 6 - }, { - "vehicle_type_ids": ["def456"], - "count": 2 - }], - "num_bikes_available": 6, - "vehicle_types_available": [{ - "vehicle_type_id": "abc123", - "count": 2 - }, { - "vehicle_type_id": "def456", - "count": 4 - }] + "station_id":"station 1", + "is_installed":true, + "is_renting":true, + "is_returning":true, + "last_reported":1609866125, + "num_docks_available":3, + "vehicle_docks_available":[ + { + "vehicle_type_ids":[ + "abc123" + ], + "count":2 + }, + { + "vehicle_type_ids":[ + "def456" + ], + "count":1 + } + ], + "num_bikes_available":1, + "vehicle_types_available":[ + { + "vehicle_type_id":"abc123", + "count":1 + }, + { + "vehicle_type_id":"def456", + "count":0 + } + ] + }, + { + "station_id":"station 2", + "is_installed":true, + "is_renting":true, + "is_returning":true, + "last_reported":1609866106, + "num_docks_available":8, + "num_docks_disabled":1, + "vehicle_docks_available":[ + { + "vehicle_type_ids":[ + "abc123" + ], + "count":6 + }, + { + "vehicle_type_ids":[ + "def456" + ], + "count":2 + } + ], + "num_bikes_available":6, + "vehicle_types_available":[ + { + "vehicle_type_id":"abc123", + "count":2 + }, + { + "vehicle_type_id":"def456", + "count":4 + } + ] + }, + { + "station_id":"station 3", + "is_installed":true, + "is_renting":true, + "is_returning":true, + "last_reported":1609866106, + "num_docks_available":999999, + "num_bikes_available":555555 } ] }