Skip to content

Commit

Permalink
Code for step 3
Browse files Browse the repository at this point in the history
  • Loading branch information
akoutmos committed Aug 21, 2019
1 parent e5cea48 commit 2531ee0
Show file tree
Hide file tree
Showing 7 changed files with 835 additions and 2 deletions.
3 changes: 2 additions & 1 deletion config/dev.exs
Expand Up @@ -7,7 +7,8 @@ config :elixir_monitoring_prom, ElixirMonitoringProm.Repo,
database: "elixir_monitoring_prom_dev",
hostname: "postgres",
show_sensitive_data_on_connection_error: true,
pool_size: 10
pool_size: 10,
types: ElixirMonitoringProm.PostgresTypes

# For development, we disable any cache and enable
# debugging and code reloading.
Expand Down
5 changes: 5 additions & 0 deletions lib/elixir_monitoring_prom/postgres_types.ex
@@ -0,0 +1,5 @@
Postgrex.Types.define(
ElixirMonitoringProm.PostgresTypes,
[Geo.PostGIS.Extension] ++ Ecto.Adapters.Postgres.extensions(),
json: Jason
)
28 changes: 28 additions & 0 deletions lib/elixir_monitoring_prom/zip_codes/zip_code.ex
@@ -0,0 +1,28 @@
defmodule ElixirMonitoringProm.ZipCodes.ZipCode do
@moduledoc """
Schema to define zip code entries
"""

use Ecto.Schema

import Ecto.Changeset

alias __MODULE__

schema "zip_codes" do
field :zip_code, :string
field :city, :string
field :state, :string
field :point, Geo.PostGIS.Geometry
field :timezone, :integer
field :dst, :boolean
end

def changeset(%ZipCode{} = zip_code, attrs \\ %{}) do
all_fields = [:zip_code, :city, :state, :point, :timezone, :dst]

zip_code
|> cast(attrs, all_fields)
|> validate_required(all_fields)
end
end
4 changes: 3 additions & 1 deletion mix.exs
Expand Up @@ -42,7 +42,9 @@ defmodule ElixirMonitoringProm.MixProject do
{:phoenix_live_reload, "~> 1.2", only: :dev},
{:gettext, "~> 0.11"},
{:jason, "~> 1.0"},
{:plug_cowboy, "~> 2.0"}
{:plug_cowboy, "~> 2.0"},
{:geo, "~> 3.1.0"},
{:geo_postgis, "~> 3.1.0"}
]
end

Expand Down
2 changes: 2 additions & 0 deletions mix.lock
Expand Up @@ -7,6 +7,8 @@
"ecto": {:hex, :ecto, "3.1.7", "fa21d06ef56cdc2fdaa62574e8c3ba34a2751d44ea34c30bc65f0728421043e5", [:mix], [{:decimal, "~> 1.6", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
"ecto_sql": {:hex, :ecto_sql, "3.1.6", "1e80e30d16138a729c717f73dcb938590bcdb3a4502f3012414d0cbb261045d8", [:mix], [{:db_connection, "~> 2.0", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.1.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.9.1", [hex: :mariaex, repo: "hexpm", optional: true]}, {:myxql, "~> 0.2.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.14.0 or ~> 0.15.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm"},
"file_system": {:hex, :file_system, "0.2.7", "e6f7f155970975789f26e77b8b8d8ab084c59844d8ecfaf58cbda31c494d14aa", [:mix], [], "hexpm"},
"geo": {:hex, :geo, "3.1.0", "727e005262430d037e870ff364e65d80ca5ca21d5ac8eddd57a1ada72c3f83b0", [:mix], [], "hexpm"},
"geo_postgis": {:hex, :geo_postgis, "3.1.0", "d06c8fa5fd140a52a5c9dab4ad6623a696dd7d99dd791bb361d3f94942442ff9", [:mix], [{:geo, "~> 3.1", [hex: :geo, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:poison, "~> 2.2 or ~> 3.0 or ~> 4.0", [hex: :poison, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.14", [hex: :postgrex, repo: "hexpm", optional: false]}], "hexpm"},
"gettext": {:hex, :gettext, "0.17.0", "abe21542c831887a2b16f4c94556db9c421ab301aee417b7c4fbde7fbdbe01ec", [:mix], [], "hexpm"},
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [:mix], [], "hexpm"},
Expand Down
58 changes: 58 additions & 0 deletions priv/repo/migrations/20190821020059_zip_code_table.exs
@@ -0,0 +1,58 @@
defmodule ElixirMonitoringProm.Repo.Migrations.ZipCodeTable do
use Ecto.Migration

alias ElixirMonitoringProm.ZipCodes.ZipCode

def up do
execute("CREATE EXTENSION IF NOT EXISTS postgis")

create table(:zip_codes) do
add :zip_code, :string, size: 5, null: false
add :city, :string, null: false
add :state, :string, size: 2, null: false
add :timezone, :integer, null: false
add :dst, :boolean, null: false
end

execute("SELECT AddGeometryColumn('zip_codes', 'point', 4326, 'POINT', 2)")
execute("CREATE INDEX zip_code_point_index on zip_codes USING gist (point)")

create unique_index(:zip_codes, [:zip_code])

flush()

"#{__DIR__}/../wa_zip_codes.csv"
|> File.read!()
|> String.split("\n")
|> Enum.filter(fn line -> String.trim(line) != "" end)
|> Enum.map(fn csv_line ->
[zip, city, state, lat, long, tz, dst] =
csv_line
|> String.replace("\"", "")
|> String.replace("\n", "")
|> String.split(",")

city = String.downcase(city)
state = String.downcase(state)

attrs = %{
zip_code: zip,
city: city,
state: state,
point: %Geo.Point{coordinates: {long, lat}, srid: 4326},
timezone: String.to_integer(tz),
dst: (dst == "1" && true) || false
}

ZipCode.changeset(%ZipCode{}, attrs)
end)
|> Enum.each(fn zip_code_changeset ->
ElixirMonitoringProm.Repo.insert(zip_code_changeset, on_conflict: :nothing)
end)
end

def down do
drop(table(:zip_codes))
execute("DROP EXTENSION IF EXISTS postgis")
end
end

0 comments on commit 2531ee0

Please sign in to comment.