Compare commits
2 Commits
8ed22bebf0
...
3993f97de1
Author | SHA1 | Date | |
---|---|---|---|
3993f97de1 | |||
f0a7fc1303 |
@ -1,56 +0,0 @@
|
|||||||
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
|
|
@ -1,20 +0,0 @@
|
|||||||
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,14 +2,20 @@ defmodule LolAnalytics.Dimensions.Player.PlayerSchema do
|
|||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
|
||||||
|
@attrs [:puuid, :last_processed_at]
|
||||||
|
|
||||||
schema "dim_player" do
|
schema "dim_player" do
|
||||||
field :puuid, :string
|
field :puuid, :string
|
||||||
|
|
||||||
|
field :last_processed_at, :utc_datetime,
|
||||||
|
default: DateTime.utc_now() |> DateTime.truncate(:second)
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
def changeset(player = %__MODULE__{}, attrs \\ %{}) do
|
def changeset(player = %__MODULE__{}, attrs \\ %{}) do
|
||||||
player
|
player
|
||||||
|> cast(attrs, [:puuid])
|
|> cast(attrs, @attrs)
|
||||||
|> validate_required([:puuid])
|
|> validate_required([:puuid])
|
||||||
|> unique_constraint([:puuid])
|
|> unique_constraint([:puuid])
|
||||||
end
|
end
|
||||||
|
@ -36,13 +36,6 @@ defmodule LolAnalytics.Facts.ChampionPickedItem.Repo do
|
|||||||
_patch = PatchRepo.get_or_create(attrs.patch_number)
|
_patch = PatchRepo.get_or_create(attrs.patch_number)
|
||||||
_item_id = ItemRepo.get_or_create(attrs.item_id)
|
_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 =
|
prev =
|
||||||
from(f in Schema,
|
from(f in Schema,
|
||||||
where:
|
where:
|
||||||
|
@ -27,13 +27,6 @@ defmodule LolAnalytics.Facts.ChampionPickedSummonerSpell.Repo do
|
|||||||
_spell = SummonerSpellRepo.get_or_create(attrs.summoner_spell_id)
|
_spell = SummonerSpellRepo.get_or_create(attrs.summoner_spell_id)
|
||||||
_patch = PatchRepo.get_or_create(attrs.patch_number)
|
_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 =
|
prev =
|
||||||
from(f in Schema,
|
from(f in Schema,
|
||||||
where:
|
where:
|
||||||
|
@ -5,7 +5,6 @@ defmodule LolAnalytics.Facts.ChampionPlayedGame.Repo do
|
|||||||
alias LolAnalytics.Dimensions.Champion.ChampionSchema
|
alias LolAnalytics.Dimensions.Champion.ChampionSchema
|
||||||
alias LolAnalytics.Dimensions.Player.PlayerRepo
|
alias LolAnalytics.Dimensions.Player.PlayerRepo
|
||||||
alias LolAnalytics.Dimensions.Champion.ChampionRepo
|
alias LolAnalytics.Dimensions.Champion.ChampionRepo
|
||||||
alias LolAnalytics.Dimensions.Match.MatchRepo
|
|
||||||
alias LolAnalytics.Facts.ChampionPlayedGame.Schema
|
alias LolAnalytics.Facts.ChampionPlayedGame.Schema
|
||||||
alias LoLAnalytics.Repo
|
alias LoLAnalytics.Repo
|
||||||
|
|
||||||
@ -14,13 +13,6 @@ defmodule LolAnalytics.Facts.ChampionPlayedGame.Repo do
|
|||||||
_player = PlayerRepo.get_or_create(attrs.puuid)
|
_player = PlayerRepo.get_or_create(attrs.puuid)
|
||||||
_patch = PatchRepo.get_or_create(attrs.patch_number)
|
_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 =
|
prev =
|
||||||
from(f in Schema,
|
from(f in Schema,
|
||||||
where:
|
where:
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
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,10 +3,18 @@ import Chart from "chart.js/auto"
|
|||||||
const ChampionWinRate = {
|
const ChampionWinRate = {
|
||||||
mounted() {
|
mounted() {
|
||||||
this.handleEvent("win-rate", ({ winRates }) => {
|
this.handleEvent("win-rate", ({ winRates }) => {
|
||||||
this.patches = winRates.map((winRate) => {
|
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) => {
|
||||||
return winRate.patch_number
|
return winRate.patch_number
|
||||||
})
|
})
|
||||||
this.winRateValues = winRates.map((winRate) => winRate.win_rate)
|
this.winRateValues = this.sortedWinRates.map((winRate) => winRate.win_rate)
|
||||||
// TODO: it breaks on liveview updates, should apply a better fix...
|
// TODO: it breaks on liveview updates, should apply a better fix...
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
const data = {
|
const data = {
|
||||||
|
22
queries.md
22
queries.md
@ -1,22 +0,0 @@
|
|||||||
```
|
|
||||||
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