filtering by roles
Some checks are pending
ci / docker (push) Waiting to run

This commit is contained in:
Álvaro 2024-05-31 02:40:03 +02:00
parent 22b79f5376
commit 77b292e47e
6 changed files with 111 additions and 95 deletions

View File

@ -6,7 +6,6 @@ defmodule LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameRepo do
alias LolAnalytics.Dimensions.Champion.ChampionRepo
alias LolAnalytics.Dimensions.Match.MatchRepo
alias LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameSchema
alias LolAnalytics.Facts.ChampionPlayedGame
alias LoLAnalytics.Repo
def insert(attrs) do
@ -21,13 +20,13 @@ defmodule LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameRepo do
Repo.all(ChampionPlayedGameSchema)
end
def get_win_rates2 do
query2 =
def get_win_rates do
query =
from m in ChampionPlayedGameSchema,
join: c in ChampionSchema,
on: c.champion_id == m.champion_id,
select: %{
wins: count(m.is_win),
wins: fragment("count(CASE WHEN ? THEN 1 END)", m.is_win),
win_rate:
fragment(
"
@ -37,41 +36,13 @@ defmodule LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameRepo do
),
id: m.champion_id,
name: c.name,
image: c.image
image: c.image,
team_position: m.team_position,
total_games: count("*")
},
group_by: [m.champion_id, c.image, c.name]
group_by: [m.champion_id, c.image, c.name, m.team_position]
Repo.all(query2)
end
def get_win_rates() do
query = """
SELECT
(cast(count(CASE WHEN is_win THEN 1 END) as float) / cast(count(*) as float)) * 100.0 as win_rate,
count(CASE WHEN is_win THEN 1 END) as games_won,
count(*) as total_games,
champion_id
FROM fact_champion_played_game
GROUP BY champion_id
ORDER BY win_rate desc;
"""
case Ecto.Adapters.SQL.query(Repo, query, []) do
{:ok, %Postgrex.Result{rows: rows}} ->
rows
|> Enum.map(fn [win_rate, wins, games, champion_id] ->
%{
win_rate: win_rate,
wins: wins,
games: games,
champion_id: champion_id
}
end)
{:error, _exception} ->
:error
end
Repo.all(query)
end
def get_win_rates_by_roles() do

View File

@ -11,7 +11,7 @@
<script defer phx-track-static type="text/javascript" src={~p"/assets/app.js"}>
</script>
</head>
<body class="bg-white antialiased">
<body class="bg-slate-700 antialiased">
<%= @inner_content %>
</body>
</html>

View File

@ -0,0 +1,3 @@
defmodule LolAnalyticsWeb.ChampionLive.Champion do
defstruct [:id, :win_rate, :image, :name, :team_position, :wins, :total_games]
end

View File

@ -1,46 +1,95 @@
defmodule LoLAnalyticsWeb.ChampionLive.Index do
alias ElixirLS.LanguageServer.Providers.Completion.Reducers.Struct
alias LolAnalyticsWeb.ChampionLive.Mapper
use LoLAnalyticsWeb, :live_view
defstruct id: "", win_rate: 0, wins: 0, image: "", name: ""
@roles ["all", "TOP", "JUNGLE", "MIDDLE", "BOTTOM", "UTILITY"]
@roles [
%{title: "All", value: "all"},
%{title: "Top", value: "TOP"},
%{title: "Jungle", value: "JUNGLE"},
%{title: "Mid", value: "MIDDLE"},
%{title: "Bot", value: "BOTTOM"},
%{title: "Support", value: "UTILITY"}
]
@impl true
def mount(_params, _session, socket) do
champs = LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameRepo.get_win_rates2()
champs = LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameRepo.get_win_rates()
mapped =
champs
|> Enum.map(fn champ ->
Kernel.struct!(%__MODULE__{}, champ)
end)
|> Mapper.map_champs()
|> Enum.sort(&(&1.win_rate >= &2.win_rate))
{:ok,
stream(
socket,
roles =
@roles
|> Enum.reduce(%{}, fn role, acc ->
Map.merge(acc, %{"#{role.value}" => false})
end)
|> Map.merge(%{"all" => true})
form =
Map.merge(
%{"name" => ""},
roles
)
socket =
socket
|> stream(
:champions,
mapped
)
|> assign(:form, to_form(%{"name" => ""}))}
|> assign(:form, to_form(form))
|> assign(:roles, @roles)
{:ok, socket}
end
@impl true
def handle_event("filter", params, socket) do
%{"name" => query_name} = params
%{
"name" => query_name,
"all" => all,
"TOP" => top,
"JUNGLE" => jungle,
"MIDDLE" => mid,
"BOTTOM" => bot,
"UTILITY" => utility
} = params
IO.inspect(params)
filter =
if all == "true" do
nil
else
%{
"TOP" => top == "true",
"JUNGLE" => jungle == "true",
"MIDDLE" => mid == "true",
"BOTTOM" => bot == "true",
"UTILITY" => utility == "true"
}
|> Enum.filter(fn {_k, v} -> v end)
|> Enum.map(fn {k, _v} -> k end)
end
IO.puts(">>>>> 3")
IO.inspect(filter)
champs =
LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameRepo.get_win_rates2()
LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameRepo.get_win_rates()
|> Enum.filter(fn %{name: name} ->
String.downcase(name) |> String.contains?(query_name)
end)
|> Enum.map(fn champ ->
Kernel.struct!(%__MODULE__{}, champ)
|> Enum.filter(fn champ ->
if filter != nil do
Enum.any?(filter, fn f -> f == champ.team_position end)
end
end)
|> Mapper.map_champs()
|> Enum.sort(&(&1.win_rate >= &2.win_rate))
IO.inspect(champs)
{:noreply,
stream(
socket,
@ -49,8 +98,8 @@ defmodule LoLAnalyticsWeb.ChampionLive.Index do
)}
end
def handle_event("filter_by", %{"name" => name}, socket) do
{:noreply, assign(socket, %{filter_by: name})}
def handle_event("filter", unsigned_params, socket) do
{:noreply, socket}
end
@impl true
@ -69,12 +118,4 @@ defmodule LoLAnalyticsWeb.ChampionLive.Index do
def handle_info({LoLAnalyticsWeb.ChampionLive.FormComponent, {:saved, champion}}, socket) do
{:noreply, stream_insert(socket, :champions, champion)}
end
# @impl true
# def handle_event("delete", %{"id" => id}, socket) do
# champion = Accounts.get_champion!(id)
# {:ok, _} = Accounts.delete_champion(champion)
# {:noreply, stream_delete(socket, :champions, champion)}
# end
end

View File

@ -4,42 +4,30 @@
<h1>Champions</h1>
<.form for={@form} phx-change="filter" phx-submit="save">
<div class="flex flex-col gap-2">
<.input type="text" field={@form["name"]} />
<.input type="checkbox" field={@form["all_roles"]} />
<div class="flex flex-col justify-between">
<div class="flex flex-row gap-2">
<.input type="checkbox" field={@form["top_role"]} />
<p>Top</p>
<div class="flex flex-row justify-between">
<%= for role <- @roles do %>
<div class="flex flex-row gap-2 align-middle">
<.input type="checkbox" field={@form[role.value]} />
<p><%= role.title %></p>
</div>
<div class="flex flex-row">
<.input type="checkbox" field={@form["middle_role"]} />
<p>Middle</p>
</div>
<div class="flex flex-row">
<.input type="checkbox" field={@form["jungle_role"]} />
<p>Jungle</p>
</div>
<div class="flex flex-row">
<.input type="checkbox" field={@form["bottom_role"]} />
<p>Bottom</p>
</div>
<div class="flex flex-row">
<.input type="checkbox" field={@form["utility_role"]} />
<p>Utility</p>
<% end %>
</div>
</div>
<button>Save</button>
<%!-- <button>Save</button> --%>
</.form>
<div id="champions" class="grid grid-cols-4 gap-4">
<%= for {_, champion} <- @streams.champions do %>
<div class="flex flex-col">
<div class="flex flex-col max-w-sm bg-white border border-gray-200 rounded-lg shadow hover:bg-gray-100 dark:bg-gray-800 dark:border-gray-700 dark:hover:bg-gray-700">
<img src={"https://ddragon.leagueoflegends.com/cdn/14.11.1/img/champion/#{champion.image}"} />
<div class="flex-auto flex-col">
<div class="flex-auto flex-col p-4 ">
<p><%= champion.name %></p>
<p><%= champion.id %></p>
<p><%= champion.wins %> / <%= champion.total_games %></p>
<p><%= champion.win_rate %>%</p>
<p><%= champion.team_position %></p>
</div>
</div>

View File

@ -0,0 +1,13 @@
defmodule LolAnalyticsWeb.ChampionLive.Mapper do
alias LolAnalyticsWeb.ChampionLive.Champion
def map_champs(champs) do
champs
|> Enum.map(fn champ ->
%{
Kernel.struct!(%Champion{}, champ)
| win_rate: :erlang.float_to_binary(champ.win_rate, decimals: 2)
}
end)
end
end