Add patch number to champion played match
Some checks failed
ci / docker (push) Has been cancelled

This commit is contained in:
Álvaro 2024-06-09 13:53:43 +02:00
parent 0c72f96e16
commit ce825b75b1
7 changed files with 69 additions and 11 deletions

View File

@ -2,10 +2,12 @@ defmodule LolAnalytics.Dimensions.Patch.PatchRepo do
alias LolAnalytics.Dimensions.Patch.PatchSchema
alias LoLAnalytics.Repo
def get_or_create(patch_number) do
patch = Repo.get(PatchSchema, patch_number: patch_number)
import Ecto.Query
case patch do
def get_or_create(patch_number) do
query = from p in PatchSchema, where: p.patch_number == ^patch_number
case Repo.one(query) do
nil ->
patch_changeset =
PatchSchema.changeset(

View File

@ -1,5 +1,7 @@
defmodule LolAnalytics.Facts.ChampionPickedSummonerSpell.FactProcessor do
alias LolAnalytics.Facts.ChampionPickedSummonerSpell
require Logger
alias LolAnalytics.Facts.ChampionPickedSummonerSpell
def process_game_at_url(url) do
data = HTTPoison.get!(url)
@ -11,10 +13,11 @@ alias LolAnalytics.Facts.ChampionPickedSummonerSpell
participants = decoded_match.info.participants
version = extract_game_version(decoded_match)
Logger.info("Processing ChampionPickedSummoner for match #{decoded_match.metadata.matchId}")
participants
|> Enum.each(fn participant = %LoLAPI.Model.Participant{} ->
if participant.teamPosition != "" do
attrs_spell_1 = %{
champion_id: participant.championId,
match_id: decoded_match.metadata.matchId,
@ -26,6 +29,7 @@ alias LolAnalytics.Facts.ChampionPickedSummonerSpell
team_position: participant.teamPosition,
patch_number: version
}
attrs_spell_2 = %{
champion_id: participant.championId,
match_id: decoded_match.metadata.matchId,
@ -37,6 +41,7 @@ alias LolAnalytics.Facts.ChampionPickedSummonerSpell
team_position: participant.teamPosition,
patch_number: version
}
ChampionPickedSummonerSpell.Repo.insert(attrs_spell_1)
ChampionPickedSummonerSpell.Repo.insert(attrs_spell_2)
end

View File

@ -39,10 +39,7 @@ defmodule LolAnalytics.Facts.ChampionPickedSummonerSpell.Repo do
changeset = Schema.changeset(prev || %Schema{}, attrs)
IO.inspect(attrs)
Repo.insert_or_update(changeset)
|> IO.inspect()
end
def get_champion_picked_summoners(championId, team_position \\ "MIDDLE") do

View File

@ -1,6 +1,7 @@
defmodule LolAnalytics.Facts.ChampionPlayedGame.Repo do
import Ecto.Query
alias LolAnalytics.Dimensions.Patch.PatchRepo
alias LolAnalytics.Dimensions.Champion.ChampionSchema
alias LolAnalytics.Dimensions.Player.PlayerRepo
alias LolAnalytics.Dimensions.Champion.ChampionRepo
@ -12,8 +13,20 @@ defmodule LolAnalytics.Facts.ChampionPlayedGame.Repo do
_match = MatchRepo.get_or_create(attrs.match_id)
_champion = ChampionRepo.get_or_create(attrs.champion_id)
_player = PlayerRepo.get_or_create(attrs.puuid)
_patch = PatchRepo.get_or_create(attrs.patch_number)
changeset = Schema.changeset(%Schema{}, attrs)
Repo.insert(changeset)
prev =
from(f in Schema,
where:
f.match_id == ^attrs.match_id and
f.champion_id == ^attrs.champion_id and f.queue_id == ^attrs.queue_id
)
|> Repo.one()
changeset = Schema.changeset(prev || %Schema{}, attrs)
Repo.update(changeset)
end
def list_played_matches() do
@ -46,6 +59,34 @@ defmodule LolAnalytics.Facts.ChampionPlayedGame.Repo do
Repo.all(query)
end
def get_win_rates_by_patch(champion_id) do
query =
from m in Schema,
join: c in ChampionSchema,
on: c.champion_id == m.champion_id,
select: %{
wins: fragment("count(CASE WHEN ? THEN 1 END)", m.is_win),
win_rate:
fragment(
"
((cast(count(CASE WHEN ? THEN 1 END) as float) / cast(count(*) as float)) * 100.0
)",
m.is_win
),
id: m.champion_id,
patch_number: m.patch_number,
name: c.name,
image: c.image,
team_position: m.team_position,
total_games: count("*")
},
where: c.champion_id == ^champion_id,
group_by: [m.champion_id, c.image, c.name, m.team_position, m.patch_number],
having: count("*") > 100
Repo.all(query)
end
def get_win_rates_by_roles() do
end
end

View File

@ -6,6 +6,7 @@ defmodule LolAnalytics.Facts.ChampionPlayedGame.Schema do
@casting_attrs [
:champion_id,
:match_id,
:patch_number,
:is_win,
:game_length_seconds,
:team_position,
@ -16,6 +17,7 @@ defmodule LolAnalytics.Facts.ChampionPlayedGame.Schema do
schema "fact_champion_played_game" do
field :champion_id, :integer
field :match_id, :string
field :patch_number, :string
field :is_win, :boolean
field :game_length_seconds, :integer
field :team_position, :string

View File

@ -1,4 +1,6 @@
defmodule LolAnalytics.Facts.ChampionPlayedGame.FactProcessor do
require Logger
def process_game_at_url(path) do
data = HTTPoison.get!(path)
process_game_data(data.body)
@ -9,6 +11,8 @@ defmodule LolAnalytics.Facts.ChampionPlayedGame.FactProcessor do
participants = decoded_match.info.participants
version = extract_game_version(decoded_match)
Logger.info("Processing ChampionPlayedMatch for #{decoded_match.metadata.matchId}")
participants
|> Enum.each(fn participant = %LoLAPI.Model.Participant{} ->
if participant.teamPosition != "" do

View File

@ -3,7 +3,7 @@ defmodule LolAnalytics.Facts.FactsRunner do
def analyze_all_matches do
Storage.MatchStorage.S3MatchStorage.stream_files("ranked")
|> Enum.each(fn %{key: path} ->
|> peach(fn %{key: path} ->
get_facts()
|> Enum.each(fn fact_runner ->
apply(fact_runner, ["http://192.168.1.55:9000/ranked/#{path}"])
@ -16,7 +16,14 @@ defmodule LolAnalytics.Facts.FactsRunner do
def get_facts() do
[
&Facts.ChampionPickedSummonerSpell.FactProcessor.process_game_at_url/1
&Facts.ChampionPickedSummonerSpell.FactProcessor.process_game_at_url/1,
&Facts.ChampionPlayedGame.FactProcessor.process_game_at_url/1
]
end
def peach(enum, fun, concurrency \\ 10, timeout \\ :infinity) do
Task.async_stream(enum, &fun.(&1), max_concurrency: concurrency, timeout: timeout)
|> Stream.each(fn {:ok, val} -> val end)
|> Enum.to_list()
end
end