Compare commits
No commits in common. "3993f97de195018109cff9ccaae0e9bfb5801e48" and "8ed22bebf0ffbfd8030daf736e8490ef1355f492" have entirely different histories.
3993f97de1
...
8ed22bebf0
@ -0,0 +1,56 @@
|
||||
defmodule LolAnalytics.ChampionWinRate.ChampionWinRateRepo do
|
||||
import Ecto.Query
|
||||
alias LolAnalytics.ChampionWinRate.ChampionWinRateSchema
|
||||
alias LoLAnalytics.Repo
|
||||
|
||||
@spec add_champion_win_rate(
|
||||
champion_id :: String.t(),
|
||||
patch :: String.t(),
|
||||
position :: String.t(),
|
||||
win? :: boolean
|
||||
) :: {:ok, ChampionWinRateSchema.t()} | {:error, Ecto.Changeset.t()}
|
||||
def add_champion_win_rate(champion_id, patch, position, win?) do
|
||||
Repo.transaction(fn ->
|
||||
champion_query =
|
||||
from cwr in LolAnalytics.ChampionWinRate.ChampionWinRateSchema,
|
||||
where: cwr.champion_id == ^champion_id and cwr.position == ^position,
|
||||
lock: "FOR UPDATE"
|
||||
|
||||
champion_data = Repo.one(champion_query)
|
||||
|
||||
case champion_data do
|
||||
nil ->
|
||||
ChampionWinRateSchema.changeset(%ChampionWinRateSchema{}, %{
|
||||
champion_id: champion_id,
|
||||
patch: patch,
|
||||
total_games: 1,
|
||||
position: position,
|
||||
total_wins: if(win?, do: 1, else: 0)
|
||||
})
|
||||
|> Repo.insert!()
|
||||
|
||||
_ ->
|
||||
total_games = champion_data.total_games + 1
|
||||
total_wins = champion_data.total_wins + if win?, do: 1, else: 0
|
||||
|
||||
ChampionWinRateSchema.changeset(champion_data, %{
|
||||
total_games: total_games,
|
||||
total_wins: total_wins
|
||||
})
|
||||
|> Repo.update!()
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
def list_win_rates() do
|
||||
Repo.all(ChampionWinRateSchema)
|
||||
end
|
||||
|
||||
def get_champion_win_rate(champion_id, _patch) do
|
||||
champion_query =
|
||||
from cwr in LolAnalytics.ChampionWinRate.ChampionWinRateSchema,
|
||||
where: cwr.champion_id == ^champion_id
|
||||
|
||||
Repo.one(champion_query)
|
||||
end
|
||||
end
|
@ -0,0 +1,20 @@
|
||||
defmodule LolAnalytics.ChampionWinRate.ChampionWinRateSchema do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
schema "champion_win_rate" do
|
||||
field :champion_id, :integer
|
||||
field :total_games, :integer
|
||||
field :patch, :string
|
||||
field :position, :string
|
||||
field :total_wins, :integer
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
def changeset(%__MODULE__{} = champion_win_rate, attrs) do
|
||||
champion_win_rate
|
||||
|> cast(attrs, [:champion_id, :total_games, :patch, :total_wins, :position])
|
||||
|> validate_required([:champion_id, :total_games, :patch, :total_wins, :position])
|
||||
end
|
||||
end
|
@ -2,20 +2,14 @@ defmodule LolAnalytics.Dimensions.Player.PlayerSchema do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
@attrs [:puuid, :last_processed_at]
|
||||
|
||||
schema "dim_player" do
|
||||
field :puuid, :string
|
||||
|
||||
field :last_processed_at, :utc_datetime,
|
||||
default: DateTime.utc_now() |> DateTime.truncate(:second)
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
def changeset(player = %__MODULE__{}, attrs \\ %{}) do
|
||||
player
|
||||
|> cast(attrs, @attrs)
|
||||
|> cast(attrs, [:puuid])
|
||||
|> validate_required([:puuid])
|
||||
|> unique_constraint([:puuid])
|
||||
end
|
||||
|
@ -36,6 +36,13 @@ defmodule LolAnalytics.Facts.ChampionPickedItem.Repo do
|
||||
_patch = PatchRepo.get_or_create(attrs.patch_number)
|
||||
_item_id = ItemRepo.get_or_create(attrs.item_id)
|
||||
|
||||
_match =
|
||||
MatchRepo.get_or_create(%{
|
||||
match_id: attrs.match_id,
|
||||
patch_number: attrs.patch_number,
|
||||
queue_id: attrs.queue_id
|
||||
})
|
||||
|
||||
prev =
|
||||
from(f in Schema,
|
||||
where:
|
||||
|
@ -27,6 +27,13 @@ defmodule LolAnalytics.Facts.ChampionPickedSummonerSpell.Repo do
|
||||
_spell = SummonerSpellRepo.get_or_create(attrs.summoner_spell_id)
|
||||
_patch = PatchRepo.get_or_create(attrs.patch_number)
|
||||
|
||||
_match =
|
||||
MatchRepo.get_or_create(%{
|
||||
match_id: attrs.match_id,
|
||||
patch_number: attrs.patch_number,
|
||||
queue_id: attrs.queue_id
|
||||
})
|
||||
|
||||
prev =
|
||||
from(f in Schema,
|
||||
where:
|
||||
|
@ -5,6 +5,7 @@ defmodule LolAnalytics.Facts.ChampionPlayedGame.Repo do
|
||||
alias LolAnalytics.Dimensions.Champion.ChampionSchema
|
||||
alias LolAnalytics.Dimensions.Player.PlayerRepo
|
||||
alias LolAnalytics.Dimensions.Champion.ChampionRepo
|
||||
alias LolAnalytics.Dimensions.Match.MatchRepo
|
||||
alias LolAnalytics.Facts.ChampionPlayedGame.Schema
|
||||
alias LoLAnalytics.Repo
|
||||
|
||||
@ -13,6 +14,13 @@ defmodule LolAnalytics.Facts.ChampionPlayedGame.Repo do
|
||||
_player = PlayerRepo.get_or_create(attrs.puuid)
|
||||
_patch = PatchRepo.get_or_create(attrs.patch_number)
|
||||
|
||||
_match =
|
||||
MatchRepo.get_or_create(%{
|
||||
match_id: attrs.match_id,
|
||||
patch_number: attrs.patch_number,
|
||||
queue_id: attrs.queue_id
|
||||
})
|
||||
|
||||
prev =
|
||||
from(f in Schema,
|
||||
where:
|
||||
|
@ -1,9 +0,0 @@
|
||||
defmodule LoLAnalytics.Repo.Migrations.DimPlayerLastProcessed do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
alter table "dim_player" do
|
||||
add :last_processed_at, :utc_datetime
|
||||
end
|
||||
end
|
||||
end
|
@ -3,18 +3,10 @@ import Chart from "chart.js/auto"
|
||||
const ChampionWinRate = {
|
||||
mounted() {
|
||||
this.handleEvent("win-rate", ({ winRates }) => {
|
||||
this.sortedWinRates = winRates.sort((a, b) => {
|
||||
let [p1Major, p1Minor] = a.patch_number.split(".").map(Number);
|
||||
let [p2Major, p2Minor] = b.patch_number.split(".").map(Number);
|
||||
|
||||
|
||||
if (p1Major > p2Major || (p1Major == p2Major && p1Minor > p2Minor)) return 1;
|
||||
return -1
|
||||
})
|
||||
this.patches = this.sortedWinRates.map((winRate) => {
|
||||
this.patches = winRates.map((winRate) => {
|
||||
return winRate.patch_number
|
||||
})
|
||||
this.winRateValues = this.sortedWinRates.map((winRate) => winRate.win_rate)
|
||||
this.winRateValues = winRates.map((winRate) => winRate.win_rate)
|
||||
// TODO: it breaks on liveview updates, should apply a better fix...
|
||||
setInterval(() => {
|
||||
const data = {
|
||||
|
22
queries.md
Normal file
22
queries.md
Normal file
@ -0,0 +1,22 @@
|
||||
```
|
||||
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;
|
||||
```
|
||||
|
||||
```
|
||||
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,
|
||||
team_position
|
||||
FROM fact_champion_played_game
|
||||
GROUP BY champion_id, team_position
|
||||
ORDER BY win_rate desc;
|
||||
```
|
Loading…
x
Reference in New Issue
Block a user