Skip to content

Commit

Permalink
Code for step 2
Browse files Browse the repository at this point in the history
  • Loading branch information
akoutmos committed May 2, 2020
1 parent 6c0fe57 commit 9efadef
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 13 deletions.
25 changes: 25 additions & 0 deletions lib/auto_finder_livedashboard/used_cars/used_car.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule AutoFinderLivedashboard.UsedCars.UsedCar do
use Ecto.Schema

import Ecto.Changeset

@fields ~w(make model year mileage price)a

@derive {Jason.Encoder, only: @fields}

schema "used_cars" do
field :make, :string
field :model, :string
field :year, :integer
field :mileage, :integer
field :price, :integer

timestamps()
end

def changeset(used_car, attrs \\ %{}) do
used_car
|> cast(attrs, @fields)
|> validate_required(@fields)
end
end
40 changes: 40 additions & 0 deletions lib/auto_finder_livedashboard/used_cars/used_cars.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
defmodule AutoFinderLivedashboard.UsedCars do
import Ecto.Query

alias AutoFinderLivedashboard.{Repo, UsedCars.UsedCar}

@event_name [:auto_finder_livedashboard, :query]

def get_used_cars(query_params) do
base_query = from(used_car in UsedCar)

query_params
|> Enum.reduce(base_query, &handle_query_param/2)
|> Repo.all()
end

defp handle_query_param({"make", make}, acc_query) do
:telemetry.execute(@event_name, %{count: 1}, %{filter: "make"})
from used_car in acc_query, where: ilike(used_car.make, ^make)
end

defp handle_query_param({"model", model}, acc_query) do
:telemetry.execute(@event_name, %{count: 1}, %{filter: "model"})
from used_car in acc_query, where: ilike(used_car.model, ^model)
end

defp handle_query_param({"min_year", min_year}, acc_query) do
:telemetry.execute(@event_name, %{count: 1}, %{filter: "min_year"})
from used_car in acc_query, where: used_car.year >= ^min_year
end

defp handle_query_param({"max_price", max_price}, acc_query) do
:telemetry.execute(@event_name, %{count: 1}, %{filter: "max_price"})
from used_car in acc_query, where: used_car.price <= ^max_price
end

defp handle_query_param({"max_mileage", max_mileage}, acc_query) do
:telemetry.execute(@event_name, %{count: 1}, %{filter: "max_mileage"})
from used_car in acc_query, where: used_car.mileage <= ^max_mileage
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule AutoFinderLivedashboardWeb.UsedCarController do
use AutoFinderLivedashboardWeb, :controller

alias AutoFinderLivedashboard.UsedCars

require Logger

def index(conn, params) do
results = UsedCars.get_used_cars(params)

json(conn, results)
end
end
6 changes: 6 additions & 0 deletions lib/auto_finder_livedashboard_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ defmodule AutoFinderLivedashboardWeb.Router do
plug :accepts, ["json"]
end

scope "/api", AutoFinderLivedashboardWeb do
pipe_through :api

get "/used_cars", UsedCarController, :index
end

scope "/", AutoFinderLivedashboardWeb do
pipe_through :browser

Expand Down
5 changes: 4 additions & 1 deletion lib/auto_finder_livedashboard_web/telemetry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ defmodule AutoFinderLivedashboardWeb.Telemetry do
summary("vm.memory.total", unit: {:byte, :kilobyte}),
summary("vm.total_run_queue_lengths.total"),
summary("vm.total_run_queue_lengths.cpu"),
summary("vm.total_run_queue_lengths.io")
summary("vm.total_run_queue_lengths.io"),

# Application metrics
counter("auto_finder_livedashboard.query.count", tags: [:filter])
]
end

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule AutoFinderLivedashboard.MixProject do
def application do
[
mod: {AutoFinderLivedashboard.Application, []},
extra_applications: [:logger, :runtime_tools]
extra_applications: [:logger, :runtime_tools, :os_mon]
]
end

Expand Down
15 changes: 15 additions & 0 deletions priv/repo/migrations/20200502172701_used_cars.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule AutoFinderLivedashboard.Repo.Migrations.UsedCars do
use Ecto.Migration

def change do
create table(:used_cars) do
add :make, :string
add :model, :string
add :year, :integer
add :mileage, :integer
add :price, :integer

timestamps()
end
end
end
39 changes: 28 additions & 11 deletions priv/repo/seeds.exs
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
# Script for populating the database. You can run it as:
#
# mix run priv/repo/seeds.exs
#
# Inside the script, you can read and write to any of your
# repositories directly:
#
# AutoFinderLivedashboard.Repo.insert!(%AutoFinderLivedashboard.SomeSchema{})
#
# We recommend using the bang functions (`insert!`, `update!`
# and so on) as they will fail if something goes wrong.
alias AutoFinderLivedashboard.{Repo, UsedCars.UsedCar}

car_selection = [
{"Acura", ~w(ILX TLX RLX RDX MDX NSX), 15_000..35_000},
{"Honda", ~w(Accord Civic CR-V Odyssey Passport), 10_000..25_000},
{"Nissan", ~w(GT-R 370Z Titan Leaf Sentra), 25_000..50_000},
{"Mazda", ~w(MX-5 CX-3 CX5 CX-9), 15_000..25_000},
{"Chevrolet", ~w(Camaro Corvette Colorado Silverado), 25_000..50_000},
{"Ford", ~w(Escape Explorer Mustang Focus), 15_000..25_000},
{"Audi", ~w(A4 Q3 A6 Q7 R8 S3 S4 RS5), 20_000..50_000},
{"BMW", ~w(M2 M3 M5 X4 X7), 20_000..50_000},
{"Subaru", ~w(Impreza Legacy Forester BRZ WRX), 15_000..25_000},
{"Porsche", ~w(Taycan Panamera MAcan Cayenne Carrera Cayman), 40_000..70_000},
{"Ferrari", ~w(812 F8 488 GTC4 Portofino), 150_000..250_000}
]

1..1_000
|> Enum.each(fn _ ->
{make, models, price_range} = Enum.random(car_selection)
model = Enum.random(models)
price = Enum.random(price_range)
year = Enum.random(2015..2020)
mileage = Enum.random(10_000..60_000)

%UsedCar{}
|> UsedCar.changeset(%{make: make, model: model, price: price, year: year, mileage: mileage})
|> Repo.insert!()
end)

0 comments on commit 9efadef

Please sign in to comment.