Compare commits

..

2 Commits

Author SHA1 Message Date
Álvaro Girona Arias
f79f0db862 create behaviour for fact processor
Some checks are pending
ci / docker (push) Waiting to run
2024-06-13 15:36:57 +02:00
Álvaro Girona Arias
7e30db3864 Create fact for champion picked item 2024-06-13 15:36:05 +02:00
9 changed files with 171 additions and 12 deletions

View File

@ -2,10 +2,12 @@ defmodule LolAnalytics.Dimensions.Item.ItemRepo do
alias LolAnalytics.Dimensions.Item.ItemSchema
alias LoLAnalytics.Repo
def get_or_create(item_id) do
item = Repo.get(ItemSchema, item_id: item_id)
import Ecto.Query
case item do
def get_or_create(item_id) do
query = from i in ItemSchema, where: i.item_id == ^item_id
case Repo.one(query) do
nil ->
item_changeset = ItemSchema.changeset(%ItemSchema{}, %{item_id: item_id})
Repo.insert(item_changeset)

View File

@ -0,0 +1,2 @@
defmodule LolAnalytics.Facts.ChhampionPickedItem.FactProcessor do
end

View File

@ -0,0 +1,87 @@
defmodule LolAnalytics.Facts.ChampionPickedItem.Repo do
import Ecto.Query
alias LolAnalytics.Dimensions.Item.ItemSchema
alias LolAnalytics.Dimensions.Champion.ChampionSchema
alias LolAnalytics.Facts.ChhampionPickedItem.Schema
alias LolAnalytics.Dimensions.Item.ItemRepo
alias LolAnalytics.Dimensions.Player.PlayerRepo
alias LolAnalytics.Dimensions.Champion.ChampionRepo
alias LolAnalytics.Dimensions.Match.MatchRepo
alias LoLAnalytics.Repo
@doc """
Inserts a new entry for the campion_picked_item fact.
Example:
iex> item_picked = %{champion_id: 90, item_id: 1, match_id: "123", patch_number: "14.9", puuid: "1223", slot_number: 1, queue_id: 420, team_position: "JUNGLE", slot_number: 1, is_win: true}
iex> LolAnalytics.Facts.ChampionPickedItem.Repo.insert(item_picked)
"""
@spec insert(%{
:champion_id => integer(),
:item_id => integer(),
:match_id => binary(),
:patch_number => String.t(),
:puuid => String.t(),
:is_win => boolean(),
:queue_id => integer(),
:team_position => String.t(),
:slot_number => integer()
}) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
def insert(attrs) do
IO.inspect(attrs)
_match = MatchRepo.get_or_create(attrs.match_id)
_champion = ChampionRepo.get_or_create(attrs.champion_id)
_player = PlayerRepo.get_or_create(attrs.puuid)
_patch = PlayerRepo.get_or_create(attrs.patch_number)
_item_id = ItemRepo.get_or_create(attrs.item_id)
prev =
from(f in Schema,
where:
f.match_id == ^attrs.match_id and
f.puuid == ^attrs.puuid and
f.item_id == ^attrs.item_id and
f.slot_number == ^attrs.slot_number
)
|> Repo.one()
Schema.changeset(prev || %Schema{}, attrs)
|> Repo.insert_or_update()
end
@spec get_champion_picked_items(String.t(), String.t()) :: list()
def get_champion_picked_items(champion_id, team_position) do
query =
from f in Schema,
where:
f.champion_id == ^champion_id and
f.team_position == ^team_position,
join: c in ChampionSchema,
on: c.champion_id == f.champion_id,
join: i in ItemSchema,
on: i.item_id == f.item_id,
select: %{
wins: fragment("count(CASE WHEN ? THEN 1 END)", f.is_win),
win_rate:
fragment(
"
((cast(count(CASE WHEN ? THEN 1 END) as float) / cast(count(*) as float)) * 100.0
)",
f.is_win
),
item_id: i.item_id,
champion_id: c.champion_id,
team_position: f.team_position,
total_games: count("*")
},
group_by: [
i.item_id,
c.champion_id,
f.team_position
]
Repo.all(query)
end
end

View File

@ -0,0 +1,34 @@
defmodule LolAnalytics.Facts.ChhampionPickedItem.Schema do
use Ecto.Schema
import Ecto.Changeset
@casting_args [
:champion_id,
:item_id,
:match_id,
:is_win,
:queue_id,
:patch_number,
:team_position,
:puuid,
:slot_number
]
schema "fact_champion_picked_item" do
field :champion_id, :integer
field :item_id, :integer
field :match_id, :string
field :is_win, :boolean
field :queue_id, :integer
field :patch_number, :string
field :team_position, :string
field :puuid, :string
field :slot_number, :integer
end
def changeset(fact, attrs) do
fact
|> cast(attrs, @casting_args)
|> validate_required(@casting_args)
end
end

View File

@ -1,8 +1,12 @@
defmodule LolAnalytics.Facts.ChampionPickedSummonerSpell.FactProcessor do
@behaviour LolAnalytics.Facts.FactBehaviour
require Logger
alias LolAnalytics.Facts.ChampionPickedSummonerSpell
@impl true
@spec process_game_at_url(String.t()) :: any()
def process_game_at_url(url) do
data = HTTPoison.get!(url)
process_game_data(data.body)

View File

@ -37,15 +37,14 @@ defmodule LolAnalytics.Facts.ChampionPickedSummonerSpell.Repo do
)
|> Repo.one()
changeset = Schema.changeset(prev || %Schema{}, attrs)
Repo.insert_or_update(changeset)
Schema.changeset(prev || %Schema{}, attrs)
|> Repo.insert_or_update()
end
def get_champion_picked_summoners(championId, team_position \\ "MIDDLE") do
def get_champion_picked_summoners(champion_id, team_position \\ "MIDDLE") do
query =
from f in Schema,
where: f.champion_id == ^championId and f.team_position == ^team_position,
where: f.champion_id == ^champion_id and f.team_position == ^team_position,
join: c in ChampionSchema,
on: c.champion_id == f.champion_id,
join: s in SummonerSpellSchema,
@ -59,10 +58,8 @@ defmodule LolAnalytics.Facts.ChampionPickedSummonerSpell.Repo do
)",
f.is_win
),
id: f.champion_id,
spell_id: f.summoner_spell_id,
metadata: s.metadata,
champion_name: c.name,
champion_id: c.champion_id,
team_position: f.team_position,
total_games: count("*")
@ -71,8 +68,6 @@ defmodule LolAnalytics.Facts.ChampionPickedSummonerSpell.Repo do
f.champion_id,
f.summoner_spell_id,
s.metadata,
c.image,
c.name,
c.champion_id,
f.team_position
]

View File

@ -1,6 +1,10 @@
defmodule LolAnalytics.Facts.ChampionPlayedGame.FactProcessor do
require Logger
@behaviour LolAnalytics.Facts.FactBehaviour
@impl true
@spec process_game_at_url(String.t()) :: none()
def process_game_at_url(path) do
data = HTTPoison.get!(path)
process_game_data(data.body)

View File

@ -0,0 +1,3 @@
defmodule LolAnalytics.Facts.FactBehaviour do
@callback process_game_at_url(String.t()) :: any()
end

View File

@ -0,0 +1,28 @@
defmodule LoLAnalytics.Repo.Migrations.ChampionPickedItem do
use Ecto.Migration
def change do
create table("fact_champion_picked_item") do
add :champion_id, references("dim_champion", column: :champion_id, type: :integer)
add :item_id, references("dim_item", column: :item_id, type: :integer)
add :slot_number, :integer
add :match_id, references("dim_match", column: :match_id, type: :string)
add :is_win, :boolean
add :game_length_seconds, :integer
add :queue_id, :integer
add :patch_number, references("dim_patch", column: :patch_number, type: :string)
add :team_position, :string
add :puuid, references("dim_player", column: :puuid, type: :string)
end
create index("fact_champion_picked_item", [:champion_id])
create index("fact_champion_picked_item", [:puuid, :match_id, :slot_number, :item_id], unique: true)
create index("fact_champion_picked_item", [
:item_id,
:queue_id,
:is_win,
:patch_number,
:team_position
])
end
end