Fix warnings, remove analyzers and create fact for champion played game
ci / docker (push) Successful in 7m38s

This commit is contained in:
2024-06-06 20:20:45 +02:00
parent fe3d040978
commit 6dd8eea3d3
26 changed files with 92 additions and 402 deletions
@@ -1,3 +0,0 @@
defmodule LolAnalytics.Analyzer do
@callback analyze(:url, path :: String.t()) :: :ok
end
@@ -1,56 +0,0 @@
defmodule LolAnalytics.Analyzer.ChampionAnalyzer do
alias LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameSchema
@behaviour LolAnalytics.Analyzer
def analyze_all_matches do
Storage.MatchStorage.S3MatchStorage.stream_files("ranked")
|> Enum.each(fn %{key: path} ->
LolAnalytics.Analyzer.ChampionAnalyzer.analyze(:url, "http://192.168.1.55:9000/ranked/#{path}")
end)
# Storage.MatchStorage.S3MatchStorage.list_files("ranked")
# |> Enum.map(& &1.key)
# |> Enum.each(fn path ->
# LolAnalytics.Analyzer.ChampionAnalyzer.analyze(:url, "http://localhost:9000/ranked/#{path}")
# end)
end
@doc """
iex> LolAnalytics.Analyzer.ChampionAnalyzer.analyze(:url, "http://localhost:9000/ranked/14.9.580.2108/EUW1_6923309745.json")
"""
@impl true
def analyze(:url, path) do
data = HTTPoison.get!(path)
analyze(:data, data.body)
end
@impl true
def analyze(:data, data) do
decoded_match = Poison.decode!(data, as: %LoLAPI.Model.MatchResponse{})
participants = decoded_match.info.participants
version = extract_game_version(decoded_match)
participants
|> Enum.each(fn participant = %LoLAPI.Model.Participant{} ->
if participant.teamPosition != "" do
attrs = %{
champion_id: participant.championId,
match_id: decoded_match.metadata.matchId,
is_win: participant.win,
game_length_seconds: decoded_match.info.gameDuration,
queue_id: decoded_match.info.queueId,
puuid: participant.puuid,
team_position: participant.teamPosition
}
LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameRepo.insert(attrs)
end
end)
end
defp extract_game_version(game_data) do
game_data.info.gameVersion
|> String.split(".")
|> Enum.take(2)
|> Enum.join(".")
end
end
@@ -46,7 +46,7 @@ defmodule LolAnalytics.ChampionWinRate.ChampionWinRateRepo do
Repo.all(ChampionWinRateSchema)
end
def get_champion_win_rate(champion_id, patch) do
def get_champion_win_rate(champion_id, _patch) do
champion_query =
from cwr in LolAnalytics.ChampionWinRate.ChampionWinRateSchema,
where: cwr.champion_id == ^champion_id
@@ -2,8 +2,6 @@ defmodule LolAnalytics.Dimensions.Champion.ChampionMetadata do
alias LolAnalytics.Dimensions.Champion.ChampionRepo
@champions_data_url "https://ddragon.leagueoflegends.com/cdn/14.11.1/data/en_US/champion.json"
use GenServer
def update_metadata() do
{:ok, %{"data" => data}} = get_champions()
@@ -20,7 +18,7 @@ defmodule LolAnalytics.Dimensions.Champion.ChampionMetadata do
end
end
defp save_metadata({champion, info}) do
defp save_metadata({_champion, info}) do
%{
"image" => %{
"full" => full_image
@@ -1,6 +1,4 @@
defmodule LolAnalytics.Dimensions.Champion.ChampionRepo do
import Ecto.Query
alias LoLAnalytics.Repo
alias LolAnalytics.Dimensions.Champion.ChampionSchema
@@ -1,28 +1,28 @@
defmodule LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameRepo do
defmodule LolAnalytics.Facts.ChampionPlayedGame.Repo do
import Ecto.Query
alias LolAnalytics.Dimensions.Champion.ChampionSchema
alias LolAnalytics.Dimensions.Player.PlayerRepo
alias LolAnalytics.Dimensions.Champion.ChampionRepo
alias LolAnalytics.Dimensions.Match.MatchRepo
alias LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameSchema
alias LolAnalytics.Facts.ChampionPlayedGame.Schema
alias LoLAnalytics.Repo
def insert(attrs) do
_match = MatchRepo.get_or_create(attrs.match_id)
_champion = ChampionRepo.get_or_create(attrs.champion_id)
_player = PlayerRepo.get_or_create(attrs.puuid)
changeset = ChampionPlayedGameSchema.changeset(%ChampionPlayedGameSchema{}, attrs)
changeset = Schema.changeset(%Schema{}, attrs)
Repo.insert(changeset)
end
def list_played_matches() do
Repo.all(ChampionPlayedGameSchema)
Repo.all(Schema)
end
def get_win_rates do
query =
from m in ChampionPlayedGameSchema,
from m in Schema,
join: c in ChampionSchema,
on: c.champion_id == m.champion_id,
select: %{
@@ -1,4 +1,4 @@
defmodule LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameSchema do
defmodule LolAnalytics.Facts.ChampionPlayedGame.Schema do
use Ecto.Schema
import Ecto.Changeset
@@ -0,0 +1,37 @@
defmodule LolAnalytics.Facts.ChampionPlayedGame.FactProcessor do
def process_game_at_url(path) do
data = HTTPoison.get!(path)
process_game_data(data.body)
end
def process_game_data(data) do
decoded_match = Poison.decode!(data, as: %LoLAPI.Model.MatchResponse{})
participants = decoded_match.info.participants
version = extract_game_version(decoded_match)
participants
|> Enum.each(fn participant = %LoLAPI.Model.Participant{} ->
if participant.teamPosition != "" do
attrs = %{
champion_id: participant.championId,
match_id: decoded_match.metadata.matchId,
is_win: participant.win,
game_length_seconds: decoded_match.info.gameDuration,
queue_id: decoded_match.info.queueId,
puuid: participant.puuid,
team_position: participant.teamPosition,
patch_number: version
}
LolAnalytics.Facts.ChampionPlayedGame.Repo.insert(attrs)
end
end)
end
defp extract_game_version(game_data) do
game_data.info.gameVersion
|> String.split(".")
|> Enum.take(2)
|> Enum.join(".")
end
end
@@ -0,0 +1,22 @@
defmodule LolAnalytics.Facts.FactsRunner do
alias LolAnalytics.Facts
def analyze_all_matches do
Storage.MatchStorage.S3MatchStorage.stream_files("ranked")
|> Enum.each(fn %{key: path} ->
get_facts()
|> Enum.each(fn fact_runner ->
apply(fact_runner, ["http://192.168.1.55:9000/ranked/#{path}"])
end)
end)
end
def analyze_match() do
end
def get_facts() do
[
&Facts.ChampionPickedSummonerSpell.FactProcessor.process_game_at_url/1
]
end
end
@@ -13,7 +13,7 @@ defmodule LolAnalytics.Match.MatchRepo do
LoLAnalytics.Repo.one(query)
end
@spec get_match(String.t()) :: %LolAnalytics.Match.MatchSchema{}
@spec get_match(String.t()) :: %LolAnalytics.Match.MatchSchema{} | nil
def get_match(match_id) do
query = from m in MatchSchema, where: m.match_id == ^match_id