add last_processed_at column to dim_player
Some checks failed
ci / docker (push) Failing after 3m58s

This commit is contained in:
Álvaro 2024-06-27 09:30:00 +02:00
parent f0a7fc1303
commit 3993f97de1
8 changed files with 16 additions and 121 deletions

View File

@ -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

View File

@ -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

View File

@ -2,14 +2,20 @@ 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, [:puuid])
|> cast(attrs, @attrs)
|> validate_required([:puuid])
|> unique_constraint([:puuid])
end

View File

@ -36,13 +36,6 @@ 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:

View File

@ -27,13 +27,6 @@ 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:

View File

@ -5,7 +5,6 @@ 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
@ -14,13 +13,6 @@ 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:

View File

@ -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

View File

@ -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;
```