-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from martide/master
Bump elixir/erlang and dependencies & add timezone to airports
- Loading branch information
Showing
13 changed files
with
99,610 additions
and
14,171 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
elixir 1.13.0-otp-24 | ||
erlang 24.1.7 | ||
# https://github.com/elixir-lang/elixir/releases | ||
|
||
elixir 1.17.2-otp-27 | ||
|
||
# https://github.com/erlang/otp/releases | ||
|
||
erlang 27.0.1 |
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 |
---|---|---|
@@ -1,56 +1,27 @@ | ||
NimbleCSV.define(AirportsParser, separator: ",", escape: "\"") | ||
|
||
defmodule Airports do | ||
@moduledoc """ | ||
Airports main API | ||
""" | ||
@airports [__DIR__, "../priv", "airports.csv"] | ||
|> Path.join() | ||
|> File.stream!([], :line) | ||
|> AirportsParser.parse_stream(skip_headers: false) | ||
|> Stream.map(fn line -> | ||
[ | ||
_, | ||
ident, | ||
type, | ||
name, | ||
latitude, | ||
longitude, | ||
elevation_ft, | ||
continent, | ||
iso_country, | ||
iso_region, | ||
municipality, | ||
scheduled_service, | ||
gps_code, | ||
iata_code, | ||
local_code, | ||
home_link, | ||
wikipedia_link, | ||
keywords | ||
] = line | ||
alias Airports.Airport | ||
|
||
%Airports.Airport{ | ||
ident: ident, | ||
type: type, | ||
name: name, | ||
latitude: latitude, | ||
longitude: longitude, | ||
elevation_ft: elevation_ft, | ||
continent: continent, | ||
iso_country: iso_country, | ||
iso_region: iso_region, | ||
municipality: municipality, | ||
scheduled_service: scheduled_service, | ||
gps_code: gps_code, | ||
iata_code: iata_code, | ||
local_code: local_code, | ||
home_link: home_link, | ||
wikipedia_link: wikipedia_link, | ||
keywords: keywords | ||
} | ||
end) | ||
|> Enum.to_list() | ||
@source_path Airports.Loader.source_path() | ||
@airports if File.exists?(@source_path), do: Airports.Loader.run(), else: [] | ||
|
||
@spec all() :: [Airport.t()] | ||
def all, do: @airports | ||
|
||
@spec get_airport(String.t()) :: Airport.t() | nil | ||
def get_airport(iata_code) do | ||
Enum.find(all(), &(&1.iata_code == iata_code)) | ||
end | ||
|
||
@spec get_timezone(String.t()) :: String.t() | nil | ||
def get_timezone(iata_code) do | ||
iata_code | ||
|> get_airport() | ||
|> case do | ||
nil -> nil | ||
airport -> airport.timezone_id | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
NimbleCSV.define(AirportsParser, separator: ",", escape: "\"") | ||
|
||
defmodule Airports.Loader do | ||
@moduledoc """ | ||
Load airports from a CSV file | ||
""" | ||
alias Airports.Airport | ||
# Tag as external_resource to ensure this module recompiles when this file changed | ||
@external_resource Path.join([:code.priv_dir(:airports), "airports_with_tz.csv"]) | ||
|
||
def source_path, do: @external_resource | ||
|
||
def run do | ||
source_path() | ||
|> File.stream!() | ||
|> AirportsParser.parse_stream(skip_headers: true) | ||
|> Stream.map(fn line -> | ||
[ | ||
_, | ||
ident, | ||
type, | ||
name, | ||
latitude, | ||
longitude, | ||
elevation_ft, | ||
continent, | ||
iso_country, | ||
iso_region, | ||
municipality, | ||
scheduled_service, | ||
gps_code, | ||
iata_code, | ||
local_code, | ||
home_link, | ||
wikipedia_link, | ||
keywords, | ||
timezone_id | ||
] = line | ||
|
||
%Airport{ | ||
ident: ident, | ||
type: type, | ||
name: name, | ||
latitude: latitude, | ||
longitude: longitude, | ||
elevation_ft: elevation_ft, | ||
continent: continent, | ||
iso_country: iso_country, | ||
iso_region: iso_region, | ||
municipality: municipality, | ||
scheduled_service: scheduled_service, | ||
gps_code: gps_code, | ||
iata_code: iata_code, | ||
local_code: local_code, | ||
home_link: home_link, | ||
wikipedia_link: wikipedia_link, | ||
keywords: keywords, | ||
timezone_id: timezone_id | ||
} | ||
end) | ||
|> Enum.to_list() | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
defmodule Mix.Tasks.Airports.Update do | ||
@moduledoc """ | ||
Updates the latest timezone data of each airport and save it on the timezone_id column of airports.csv | ||
""" | ||
|
||
@shortdoc "Update airports with timezone data" | ||
|
||
@source_file "airports.csv" | ||
@destination_path Path.join([:code.priv_dir(:airports), "airports_with_tz.csv"]) | ||
|
||
use Mix.Task | ||
require Logger | ||
|
||
def run(_args) do | ||
restart_backends() | ||
Process.put(:lines, 0) | ||
|
||
stream = | ||
[:code.priv_dir(:airports), @source_file] | ||
|> Path.join() | ||
|> File.stream!() | ||
|
||
total_lines = Enum.count(stream) | ||
|
||
Mix.shell().info("Loading airports from #{@source_file}...") | ||
|
||
stream | ||
|> AirportsParser.parse_stream(skip_headers: false) | ||
|> Stream.map(fn | ||
["id" | _] = headers -> | ||
display_progress(total_lines) | ||
headers ++ ["timezone_id"] | ||
|
||
line -> | ||
[ | ||
_, | ||
_ident, | ||
_type, | ||
_name, | ||
latitude, | ||
longitude, | ||
_elevation_ft, | ||
_continent, | ||
_iso_country, | ||
_iso_region, | ||
_municipality, | ||
_scheduled_service, | ||
_gps_code, | ||
_iata_code, | ||
_local_code, | ||
_home_link, | ||
_wikipedia_link, | ||
_keywords | ||
] = line | ||
|
||
timezone_id = get_timezone(longitude, latitude) | ||
display_progress(total_lines) | ||
line ++ [timezone_id] | ||
end) | ||
|> AirportsParser.dump_to_iodata() | ||
|> then(&File.write!(@destination_path, &1)) | ||
|
||
Mix.shell().info("\nSuccessfully saved airports data!") | ||
end | ||
|
||
defp display_progress(total_lines) do | ||
Process.put(:lines, Process.get(:lines) + 1) | ||
IO.write("\rUpdating timezone data #{div(Process.get(:lines) * 100, total_lines)}%...") | ||
end | ||
|
||
defp get_timezone(long, lat) do | ||
with {long, _} <- Float.parse(long), | ||
{lat, _} <- Float.parse(lat), | ||
{:ok, tz} <- TzWorld.timezone_at({long, lat}) do | ||
tz | ||
else | ||
_ -> nil | ||
end | ||
end | ||
|
||
defp restart_backends do | ||
GenServer.stop(TzWorld.Backend.Memory) | ||
GenServer.stop(TzWorld.Backend.Dets) | ||
TzWorld.Backend.Memory.start_link() | ||
TzWorld.Backend.Dets.start_link() | ||
end | ||
end |
Oops, something went wrong.