Skip to content

Commit 9efadef

Browse files
committed
Code for step 2
1 parent 6c0fe57 commit 9efadef

8 files changed

Lines changed: 132 additions & 13 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
defmodule AutoFinderLivedashboard.UsedCars.UsedCar do
2+
use Ecto.Schema
3+
4+
import Ecto.Changeset
5+
6+
@fields ~w(make model year mileage price)a
7+
8+
@derive {Jason.Encoder, only: @fields}
9+
10+
schema "used_cars" do
11+
field :make, :string
12+
field :model, :string
13+
field :year, :integer
14+
field :mileage, :integer
15+
field :price, :integer
16+
17+
timestamps()
18+
end
19+
20+
def changeset(used_car, attrs \\ %{}) do
21+
used_car
22+
|> cast(attrs, @fields)
23+
|> validate_required(@fields)
24+
end
25+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
defmodule AutoFinderLivedashboard.UsedCars do
2+
import Ecto.Query
3+
4+
alias AutoFinderLivedashboard.{Repo, UsedCars.UsedCar}
5+
6+
@event_name [:auto_finder_livedashboard, :query]
7+
8+
def get_used_cars(query_params) do
9+
base_query = from(used_car in UsedCar)
10+
11+
query_params
12+
|> Enum.reduce(base_query, &handle_query_param/2)
13+
|> Repo.all()
14+
end
15+
16+
defp handle_query_param({"make", make}, acc_query) do
17+
:telemetry.execute(@event_name, %{count: 1}, %{filter: "make"})
18+
from used_car in acc_query, where: ilike(used_car.make, ^make)
19+
end
20+
21+
defp handle_query_param({"model", model}, acc_query) do
22+
:telemetry.execute(@event_name, %{count: 1}, %{filter: "model"})
23+
from used_car in acc_query, where: ilike(used_car.model, ^model)
24+
end
25+
26+
defp handle_query_param({"min_year", min_year}, acc_query) do
27+
:telemetry.execute(@event_name, %{count: 1}, %{filter: "min_year"})
28+
from used_car in acc_query, where: used_car.year >= ^min_year
29+
end
30+
31+
defp handle_query_param({"max_price", max_price}, acc_query) do
32+
:telemetry.execute(@event_name, %{count: 1}, %{filter: "max_price"})
33+
from used_car in acc_query, where: used_car.price <= ^max_price
34+
end
35+
36+
defp handle_query_param({"max_mileage", max_mileage}, acc_query) do
37+
:telemetry.execute(@event_name, %{count: 1}, %{filter: "max_mileage"})
38+
from used_car in acc_query, where: used_car.mileage <= ^max_mileage
39+
end
40+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defmodule AutoFinderLivedashboardWeb.UsedCarController do
2+
use AutoFinderLivedashboardWeb, :controller
3+
4+
alias AutoFinderLivedashboard.UsedCars
5+
6+
require Logger
7+
8+
def index(conn, params) do
9+
results = UsedCars.get_used_cars(params)
10+
11+
json(conn, results)
12+
end
13+
end

lib/auto_finder_livedashboard_web/router.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ defmodule AutoFinderLivedashboardWeb.Router do
1313
plug :accepts, ["json"]
1414
end
1515

16+
scope "/api", AutoFinderLivedashboardWeb do
17+
pipe_through :api
18+
19+
get "/used_cars", UsedCarController, :index
20+
end
21+
1622
scope "/", AutoFinderLivedashboardWeb do
1723
pipe_through :browser
1824

lib/auto_finder_livedashboard_web/telemetry.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ defmodule AutoFinderLivedashboardWeb.Telemetry do
3939
summary("vm.memory.total", unit: {:byte, :kilobyte}),
4040
summary("vm.total_run_queue_lengths.total"),
4141
summary("vm.total_run_queue_lengths.cpu"),
42-
summary("vm.total_run_queue_lengths.io")
42+
summary("vm.total_run_queue_lengths.io"),
43+
44+
# Application metrics
45+
counter("auto_finder_livedashboard.query.count", tags: [:filter])
4346
]
4447
end
4548

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ defmodule AutoFinderLivedashboard.MixProject do
2020
def application do
2121
[
2222
mod: {AutoFinderLivedashboard.Application, []},
23-
extra_applications: [:logger, :runtime_tools]
23+
extra_applications: [:logger, :runtime_tools, :os_mon]
2424
]
2525
end
2626

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defmodule AutoFinderLivedashboard.Repo.Migrations.UsedCars do
2+
use Ecto.Migration
3+
4+
def change do
5+
create table(:used_cars) do
6+
add :make, :string
7+
add :model, :string
8+
add :year, :integer
9+
add :mileage, :integer
10+
add :price, :integer
11+
12+
timestamps()
13+
end
14+
end
15+
end

priv/repo/seeds.exs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
1-
# Script for populating the database. You can run it as:
2-
#
3-
# mix run priv/repo/seeds.exs
4-
#
5-
# Inside the script, you can read and write to any of your
6-
# repositories directly:
7-
#
8-
# AutoFinderLivedashboard.Repo.insert!(%AutoFinderLivedashboard.SomeSchema{})
9-
#
10-
# We recommend using the bang functions (`insert!`, `update!`
11-
# and so on) as they will fail if something goes wrong.
1+
alias AutoFinderLivedashboard.{Repo, UsedCars.UsedCar}
2+
3+
car_selection = [
4+
{"Acura", ~w(ILX TLX RLX RDX MDX NSX), 15_000..35_000},
5+
{"Honda", ~w(Accord Civic CR-V Odyssey Passport), 10_000..25_000},
6+
{"Nissan", ~w(GT-R 370Z Titan Leaf Sentra), 25_000..50_000},
7+
{"Mazda", ~w(MX-5 CX-3 CX5 CX-9), 15_000..25_000},
8+
{"Chevrolet", ~w(Camaro Corvette Colorado Silverado), 25_000..50_000},
9+
{"Ford", ~w(Escape Explorer Mustang Focus), 15_000..25_000},
10+
{"Audi", ~w(A4 Q3 A6 Q7 R8 S3 S4 RS5), 20_000..50_000},
11+
{"BMW", ~w(M2 M3 M5 X4 X7), 20_000..50_000},
12+
{"Subaru", ~w(Impreza Legacy Forester BRZ WRX), 15_000..25_000},
13+
{"Porsche", ~w(Taycan Panamera MAcan Cayenne Carrera Cayman), 40_000..70_000},
14+
{"Ferrari", ~w(812 F8 488 GTC4 Portofino), 150_000..250_000}
15+
]
16+
17+
1..1_000
18+
|> Enum.each(fn _ ->
19+
{make, models, price_range} = Enum.random(car_selection)
20+
model = Enum.random(models)
21+
price = Enum.random(price_range)
22+
year = Enum.random(2015..2020)
23+
mileage = Enum.random(10_000..60_000)
24+
25+
%UsedCar{}
26+
|> UsedCar.changeset(%{make: make, model: model, price: price, year: year, mileage: mileage})
27+
|> Repo.insert!()
28+
end)

0 commit comments

Comments
 (0)