Compare commits

...

55 Commits

Author SHA1 Message Date
alvaro eb5e1cf41d remove logs
ci / docker (push) Waiting to run
2024-06-17 00:37:58 +02:00
alvaro 3e443617e9 champion detail win rate chart 2024-06-17 00:37:33 +02:00
alvaro 648dacdd1a load champion detail asynchronously
ci / docker (push) Waiting to run
2024-06-16 19:32:23 +02:00
alvaro 7a613ac191 champion picked items by patch
ci / docker (push) Waiting to run
2024-06-16 16:04:38 +02:00
alvaro 1414439ceb Update "wins / total games" text size
ci / docker (push) Waiting to run
2024-06-16 15:05:23 +02:00
alvaro 036c0cdd5b add patch to query
ci / docker (push) Waiting to run
2024-06-16 14:52:03 +02:00
alvaro ccac1fec15 Filter by patch
ci / docker (push) Waiting to run
2024-06-16 12:53:26 +02:00
alvaro bba1913d65 fix 2024-06-15 23:25:38 +02:00
alvaro d24dc4726b Log errors on fact processors 2024-06-15 21:24:08 +02:00
alvaro 0c0c7f1230 Update facts processors error handling
ci / docker (push) Waiting to run
2024-06-15 19:06:12 +02:00
alvaro 1c06ffb8a8 fix ChampionPlayedGame Repo insert
ci / docker (push) Waiting to run
2024-06-15 17:40:46 +02:00
alvaro 8cfc39e381 add all facts
ci / docker (push) Has been cancelled
2024-06-14 05:00:19 +02:00
alvaro 6d89ed657c spawn tasks for processing matches
ci / docker (push) Waiting to run
2024-06-14 04:58:40 +02:00
alvaro 3e3017f31a request only rankeds
ci / docker (push) Waiting to run
2024-06-14 03:36:26 +02:00
alvaro 37dcd67bdc .
ci / docker (push) Waiting to run
2024-06-14 03:19:41 +02:00
alvaro ffb31bf2b1 aaaa
ci / docker (push) Waiting to run
2024-06-14 03:16:08 +02:00
alvaro 4c8f7fd11d fixfix
ci / docker (push) Waiting to run
2024-06-14 03:15:04 +02:00
alvaro d07e604cbe fix
ci / docker (push) Waiting to run
2024-06-14 03:10:38 +02:00
alvaro 75ec6a49e9 directly save ranked to ranked bucket 2024-06-14 03:01:06 +02:00
alvaro c3de834f57 update tooltip 2024-06-14 02:57:55 +02:00
alvaro a5cb6dc299 display boots section in champion detail 2024-06-14 02:56:34 +02:00
alvaro 56af93b14e pass team_position to get_win_rates_by_patch 2024-06-14 02:54:22 +02:00
alvaro 45f72cb836 remove inspect 2024-06-14 02:54:03 +02:00
alvaro 656edf30a7 fix summoner spells team position
ci / docker (push) Waiting to run
2024-06-14 01:18:36 +02:00
alvaro aafb805194 Display picked items by champion
ci / docker (push) Waiting to run
2024-06-14 01:08:23 +02:00
alvaro bd8148f3e2 update summoner_spells component
ci / docker (push) Waiting to run
2024-06-13 21:57:37 +02:00
alvaro 4823e68700 process champion picked item
ci / docker (push) Waiting to run
2024-06-13 21:03:59 +02:00
Álvaro Girona Arias 4371701ca9 wip process champion picked item
ci / docker (push) Waiting to run
2024-06-13 18:37:46 +02:00
Álvaro Girona Arias 60bf927c67 fix typo ChhampionPickedItem
ci / docker (push) Waiting to run
2024-06-13 15:41:03 +02:00
Álvaro Girona Arias f79f0db862 create behaviour for fact processor
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
alvaro 0987290dca update pool size and concurrency default value for facts runner
ci / docker (push) Successful in 4m19s
2024-06-09 14:15:37 +02:00
alvaro 4e9d98afe2 Increase default concurrency for facts processing
ci / docker (push) Successful in 3m53s
2024-06-09 13:56:05 +02:00
alvaro ce825b75b1 Add patch number to champion played match
ci / docker (push) Has been cancelled
2024-06-09 13:53:43 +02:00
alvaro 0c72f96e16 fix default role filter
ci / docker (push) Failing after 4m27s
2024-06-09 13:24:24 +02:00
alvaro fb4fe9d487 update role filters overflow
ci / docker (push) Successful in 4m10s
2024-06-09 13:15:03 +02:00
alvaro b5e58c4c25 make champions the root route, change filters overflow to auto
ci / docker (push) Has been cancelled
2024-06-09 13:10:52 +02:00
alvaro b611b296c3 Improve responsiveness
ci / docker (push) Successful in 4m13s
2024-06-09 12:52:54 +02:00
alvaro 16437b5deb filter out champ roles with less than 100 games
ci / docker (push) Successful in 4m48s
2024-06-09 03:38:13 +02:00
alvaro 33bcbece88 fix role filtering
ci / docker (push) Successful in 4m7s
2024-06-09 03:25:31 +02:00
alvaro 74a086c855 update champion card
ci / docker (push) Successful in 4m17s
2024-06-09 02:48:45 +02:00
alvaro 192748bb1d create loader component and load champs asynchronously 2024-06-09 02:48:29 +02:00
alvaro d9dee3af7b refactor champions, position filters, champion card...
ci / docker (push) Successful in 3m41s
2024-06-08 23:16:53 +02:00
alvaro 2b51aace6e fix ChampionPickedSummonerSpell.Repo insert
ci / docker (push) Successful in 5m20s
2024-06-07 02:04:28 +02:00
alvaro 3ef7861f22 insert_or_update for champion picked summoner spell fact
ci / docker (push) Successful in 5m7s
2024-06-07 01:57:34 +02:00
alvaro 968c2634b7 add metadata to summoner spell 2024-06-07 01:42:45 +02:00
alvaro 6dd8eea3d3 Fix warnings, remove analyzers and create fact for champion played game
ci / docker (push) Successful in 7m38s
2024-06-06 20:20:45 +02:00
Álvaro Girona Arias fe3d040978 wip champion picked summoner spell
ci / docker (push) Successful in 3m45s
2024-06-06 19:03:30 +02:00
alvaro 9b955641d0 remote inspects
ci / docker (push) Successful in 3m43s
2024-05-31 02:40:46 +02:00
alvaro 77b292e47e filtering by roles
ci / docker (push) Waiting to run
2024-05-31 02:40:03 +02:00
alvaro 22b79f5376 Rendering champions with win rates
ci / docker (push) Waiting to run
2024-05-31 00:45:22 +02:00
Álvaro Girona Arias 520c234a94 created schema, repo... for analytic tables
ci / docker (push) Waiting to run
2024-05-30 17:48:35 +02:00
Álvaro Girona Arias dacb9ad8fc add docker-compose 2024-05-30 17:47:33 +02:00
alvaro 6053cfcdac wip analytics tables
ci / docker (push) Waiting to run
2024-05-29 15:34:27 +02:00
alvaro 5ce7ce0542 classify matches with stream
ci / docker (push) Waiting to run
2024-05-28 19:29:45 +02:00
99 changed files with 2383 additions and 156 deletions
@@ -1,3 +0,0 @@
defmodule LolAnalytics.Analyzer do
@callback analyze(:url, path :: String.t()) :: :ok
end
@@ -1,50 +0,0 @@
defmodule LolAnalytics.Analyzer.ChampionAnalyzer do
alias Hex.HTTP
@behaviour LolAnalytics.Analyzer
def analyze_all_matches do
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
@spec analyze(atom(), String.t()) :: :ok
def analyze(:url, path) do
data = HTTPoison.get!(path)
analyze(:data, data.body)
:ok
end
@impl true
@spec analyze(atom(), any()) :: list(LoLAPI.Model.Participant.t())
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
LolAnalytics.ChampionWinRate.ChampionWinRateRepo.add_champion_win_rate(
participant.championId,
version,
participant.teamPosition,
participant.win
)
end
end)
end
defp extract_game_version(game_data) do
game_data.info.gameVersion
|> String.split(".")
|> Enum.take(2)
|> Enum.join(".")
end
end
@@ -10,7 +10,8 @@ defmodule LoLAnalytics.Application do
children = [
LoLAnalytics.Repo,
{DNSCluster, query: Application.get_env(:lol_analytics, :dns_cluster_query) || :ignore},
{Phoenix.PubSub, name: LoLAnalytics.PubSub}
{Phoenix.PubSub, name: LoLAnalytics.PubSub},
{Task.Supervisor, name: LoLAnalytics.TaskSupervisor}
# Start a worker by calling: LoLAnalytics.Worker.start_link(arg)
# {LoLAnalytics.Worker, arg}
]
@@ -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
@@ -0,0 +1,41 @@
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"
def update_metadata() do
{:ok, %{"data" => data}} = get_champions()
data
|> Enum.each(&save_metadata/1)
end
defp get_champions() do
with {:ok, resp} <- HTTPoison.get(@champions_data_url),
data <- Poison.decode(resp.body) do
data
else
{:error, reason} -> {:error, reason}
end
end
defp save_metadata({_champion, info}) do
%{
"image" => %{
"full" => full_image
},
"name" => name,
"key" => key_string
} = info
attrs = %{
image: full_image,
name: name
}
{champion_id, _} = Integer.parse(key_string)
ChampionRepo.update(champion_id, attrs)
info
end
end
@@ -0,0 +1,29 @@
defmodule LolAnalytics.Dimensions.Champion.ChampionRepo do
alias LoLAnalytics.Repo
alias LolAnalytics.Dimensions.Champion.ChampionSchema
@spec get_or_create(String.t()) :: struct()
def get_or_create(champion_id) do
champion = Repo.get_by(ChampionSchema, champion_id: champion_id)
case champion do
nil ->
changeset = ChampionSchema.changeset(%ChampionSchema{}, %{champion_id: champion_id})
Repo.insert(changeset)
champion ->
champion
end
end
def update(champion_id, attrs) do
get_or_create(champion_id)
|> ChampionSchema.changeset(attrs)
|> Repo.update()
end
@spec list_champions() :: any()
def list_champions() do
Repo.all(ChampionSchema)
end
end
@@ -0,0 +1,17 @@
defmodule LolAnalytics.Dimensions.Champion.ChampionSchema do
use Ecto.Schema
import Ecto.Changeset
schema "dim_champion" do
field :champion_id, :integer
field :name, :string
field :image, :string
timestamps()
end
def changeset(champion = %__MODULE__{}, attrs \\ %{}) do
champion
|> cast(attrs, [:champion_id, :name, :image])
|> validate_required([:champion_id])
end
end
@@ -0,0 +1,25 @@
defmodule LolAnalytics.Dimensions.Item.ItemMetadata do
alias LolAnalytics.Dimensions.Item.ItemRepo
alias LolAnalytics.Dimensions.Champion.ChampionRepo
@items_data_url "https://ddragon.leagueoflegends.com/cdn/14.11.1/data/en_US/item.json"
def update_metadata() do
data = get_items()
data
|> Enum.each(&save_metadata/1)
end
defp get_items() do
with {:ok, resp} <- HTTPoison.get(@items_data_url),
%{"data" => data} <- Poison.decode!(resp.body) do
data
else
_ -> {:error, :get_items_error}
end
end
defp save_metadata({item_id, metadata}) do
ItemRepo.update(item_id, %{metadata: metadata})
end
end
@@ -0,0 +1,30 @@
defmodule LolAnalytics.Dimensions.Item.ItemRepo do
alias LolAnalytics.Dimensions.Item.ItemSchema
alias LoLAnalytics.Repo
import Ecto.Query
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)
item ->
item
end
end
def list_items() do
Repo.all(ItemSchema)
end
@spec update(item_id :: String.t(), attrs :: map()) :: any()
def update(item_id, attrs) do
get_or_create(item_id)
|> ItemSchema.changeset(attrs)
|> Repo.update()
end
end
@@ -0,0 +1,18 @@
defmodule LolAnalytics.Dimensions.Item.ItemSchema do
use Ecto.Schema
import Ecto.Changeset
@args [:item_id, :metadata]
schema "dim_item" do
field :item_id, :integer
field :metadata, :map
timestamps()
end
def changeset(item = %__MODULE__{}, attrs \\ %{}) do
item
|> cast(attrs, @args)
|> validate_required(@args)
end
end
@@ -0,0 +1,30 @@
defmodule LolAnalytics.Dimensions.Match.MatchRepo do
alias LolAnalytics.Dimensions.Match.MatchSchema
alias LoLAnalytics.Repo
import Ecto.Query
@spec get_or_create(String.t()) :: %MatchSchema{}
def get_or_create(match_id) do
query = from m in MatchSchema, where: m.match_id == ^match_id
match = Repo.one(query)
case match do
nil ->
match_changeset =
MatchSchema.changeset(
%MatchSchema{},
%{match_id: match_id}
)
Repo.insert(match_changeset)
match ->
match
end
end
def list_matches() do
Repo.all(MatchSchema)
end
end
@@ -0,0 +1,15 @@
defmodule LolAnalytics.Dimensions.Match.MatchSchema do
use Ecto.Schema
import Ecto.Changeset
schema "dim_match" do
field :match_id, :string
timestamps()
end
def changeset(match = %__MODULE__{}, attrs \\ %{}) do
match
|> cast(attrs, [:match_id])
|> validate_required([:match_id])
end
end
@@ -0,0 +1,28 @@
defmodule LolAnalytics.Dimensions.Patch.PatchRepo do
alias LolAnalytics.Dimensions.Patch.PatchSchema
alias LoLAnalytics.Repo
import Ecto.Query
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(
%PatchSchema{},
%{patch_number: patch_number}
)
Repo.insert(patch_changeset)
patch ->
patch
end
end
def list_patches() do
Repo.all(PatchSchema)
end
end
@@ -0,0 +1,16 @@
defmodule LolAnalytics.Dimensions.Patch.PatchSchema do
use Ecto.Schema
import Ecto.Changeset
schema "dim_patch" do
field :patch_number, :string
timestamps()
end
def changeset(patch = %__MODULE__{}, attrs \\ %{}) do
patch
|> cast(attrs, [:patch_number])
|> validate_required([:patch_number])
|> unique_constraint([:patch_number])
end
end
@@ -0,0 +1,28 @@
defmodule LolAnalytics.Dimensions.Player.PlayerRepo do
import Ecto.Query
alias LolAnalytics.Dimensions.Player.PlayerSchema
alias LoLAnalytics.Repo
def get_or_create(puuid) do
query = from p in PlayerSchema, where: p.puuid == ^puuid
case Repo.one(query) do
nil ->
player_changeset =
PlayerSchema.changeset(
%PlayerSchema{},
%{puuid: puuid}
)
Repo.insert(player_changeset)
player ->
player
end
end
def list_players() do
Repo.all(PlayerSchema)
end
end
@@ -0,0 +1,16 @@
defmodule LolAnalytics.Dimensions.Player.PlayerSchema do
use Ecto.Schema
import Ecto.Changeset
schema "dim_player" do
field :puuid, :string
timestamps()
end
def changeset(player = %__MODULE__{}, attrs \\ %{}) do
player
|> cast(attrs, [:puuid])
|> validate_required([:puuid])
|> unique_constraint([:puuid])
end
end
@@ -0,0 +1,22 @@
defmodule LolAnalytics.Dimensions.SummonerSpell.SummonerSpellMetadata do
alias LolAnalytics.Dimensions.SummonerSpell.SummonerSpellRepo
@spells_url "https://ddragon.leagueoflegends.com/cdn/14.11.1/data/en_US/summoner.json"
def update_metadata() do
get_spells()
|> Enum.each(&save_metadata/1)
end
defp get_spells() do
case HTTPoison.get(@spells_url) do
{:ok, resp} ->
Poison.decode!(resp.body)["data"]
end
end
defp save_metadata({name, metadata}) do
%{"key" => spell_id} = metadata
SummonerSpellRepo.update(spell_id, %{metadata: metadata})
end
end
@@ -0,0 +1,37 @@
defmodule LolAnalytics.Dimensions.SummonerSpell.SummonerSpellRepo do
import Ecto.Query
alias LolAnalytics.Dimensions.SummonerSpell.SummonerSpellSchema
alias LoLAnalytics.Repo
@spec get_or_create(String.t()) :: any()
def get_or_create(spell_id) do
query = from s in SummonerSpellSchema, where: s.spell_id == ^spell_id
spell = Repo.one(query)
case spell do
nil ->
spell_changeset =
SummonerSpellSchema.changeset(
%SummonerSpellSchema{},
%{spell_id: spell_id}
)
Repo.insert(spell_changeset)
spell ->
spell
end
end
@spec update(spell_id :: String.t(), attrs :: map()) :: any()
def update(spell_id, attrs) do
get_or_create(spell_id)
|> SummonerSpellSchema.changeset(attrs)
|> Repo.update()
end
def list_spells() do
Repo.all(SummonerSpellSchema)
end
end
@@ -0,0 +1,16 @@
defmodule LolAnalytics.Dimensions.SummonerSpell.SummonerSpellSchema do
use Ecto.Schema
import Ecto.Changeset
schema "dim_summoner_spell" do
field :spell_id, :integer
field :metadata, :map
timestamps()
end
def changeset(summoner_spell = %__MODULE__{}, attrs) do
summoner_spell
|> cast(attrs, [:spell_id, :metadata])
|> validate_required([:spell_id])
end
end
@@ -0,0 +1,60 @@
defmodule LolAnalytics.Facts.ChampionPickedItem.FactProcessor do
alias LolAnalytics.Facts.ChampionPickedItem.Repo
require Logger
@behaviour LolAnalytics.Facts.FactBehaviour
@doc """
iex> LolAnalytics.Facts.ChampionPickedItem.FactProcessor.process_game_at_url("http://192.168.1.55:9000/ranked/14.3.558.106/EUW1_6803789466.json")
"""
@impl true
def process_game_at_url(url) do
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
HTTPoison.get(url),
{:ok, decoded_match} <- Poison.decode(body, as: %LoLAPI.Model.MatchResponse{}) do
process_game_data(decoded_match)
else
_ ->
Logger.error("Could not process data from #{url} for ChampionPickedItem")
{:error, "Could not process data from #{url}"}
end
end
defp process_game_data(decoded_match) do
participants = decoded_match.info.participants
version = extract_game_version(decoded_match)
Logger.info("Processing ChampionPickedItem for match #{decoded_match.metadata.matchId}")
participants
|> Enum.each(fn participant = %LoLAPI.Model.Participant{} ->
if participant.teamPosition != "" do
[:item0, :item1, :item2, :item3, :item4, :item5, :item6]
|> Enum.with_index()
|> Enum.each(fn {item_key, index} ->
item_key = Map.get(participant, item_key)
Repo.insert(%{
champion_id: participant.championId,
match_id: decoded_match.metadata.matchId,
is_win: participant.win,
item_id: item_key,
slot_number: index,
game_length_seconds: decoded_match.info.gameDuration,
queue_id: decoded_match.info.queueId,
puuid: participant.puuid,
team_position: participant.teamPosition,
patch_number: version
})
end)
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,90 @@
defmodule LolAnalytics.Facts.ChampionPickedItem.Repo do
import Ecto.Query
alias LolAnalytics.Dimensions.Patch.PatchRepo
alias LolAnalytics.Dimensions.Item.ItemSchema
alias LolAnalytics.Dimensions.Champion.ChampionSchema
alias LolAnalytics.Facts.ChampionPickedItem.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
_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)
_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(), String.t()) :: list()
def get_champion_picked_items(champion_id, team_position, patch_number) do
query =
from f in Schema,
where:
f.champion_id == ^champion_id and
f.team_position == ^team_position and
f.item_id != 0 and f.patch_number == ^patch_number,
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
),
metadata: i.metadata,
item_id: i.item_id,
champion_id: c.champion_id,
team_position: f.team_position,
total_games: count("*")
},
group_by: [
i.metadata,
i.item_id,
c.champion_id,
f.team_position
]
Repo.all(query)
end
end
@@ -0,0 +1,34 @@
defmodule LolAnalytics.Facts.ChampionPickedItem.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
@@ -0,0 +1,67 @@
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
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
HTTPoison.get(url),
{:ok, decoded_match} <- Poison.decode(body, as: %LoLAPI.Model.MatchResponse{}) do
process_game_data(decoded_match)
else
_ ->
Logger.error("Could not process data from #{url} for ChampionPickedSummonerSpell")
{:error, "Could not process data from #{url}"}
end
end
defp process_game_data(decoded_match) do
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,
is_win: participant.win,
summoner_spell_id: participant.summoner1Id,
game_length_seconds: decoded_match.info.gameDuration,
queue_id: decoded_match.info.queueId,
puuid: participant.puuid,
team_position: participant.teamPosition,
patch_number: version
}
attrs_spell_2 = %{
champion_id: participant.championId,
match_id: decoded_match.metadata.matchId,
is_win: participant.win,
summoner_spell_id: participant.summoner2Id,
game_length_seconds: decoded_match.info.gameDuration,
queue_id: decoded_match.info.queueId,
puuid: participant.puuid,
team_position: participant.teamPosition,
patch_number: version
}
ChampionPickedSummonerSpell.Repo.insert(attrs_spell_1)
ChampionPickedSummonerSpell.Repo.insert(attrs_spell_2)
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,81 @@
defmodule LolAnalytics.Facts.ChampionPickedSummonerSpell.Repo do
import Ecto.Query
alias LolAnalytics.Dimensions.SummonerSpell.SummonerSpellSchema
alias LolAnalytics.Dimensions.SummonerSpell.SummonerSpellRepo
alias LolAnalytics.Dimensions.Champion.ChampionSchema
alias LolAnalytics.Facts.ChampionPickedSummonerSpell.Schema
alias LolAnalytics.Dimensions.Player.PlayerRepo
alias LolAnalytics.Dimensions.Champion.ChampionRepo
alias LolAnalytics.Dimensions.Match.MatchRepo
alias LoLAnalytics.Repo
@type insert_attrs :: %{
match_id: String.t(),
champion_id: String.t(),
puuid: String.t(),
summoner_spell_id: :integer
}
@spec insert(insert_attrs()) :: any()
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)
_spell = SummonerSpellRepo.get_or_create(attrs.summoner_spell_id)
prev =
from(f in Schema,
where:
f.match_id == ^attrs.match_id and
f.champion_id == ^attrs.champion_id and
f.summoner_spell_id ==
^attrs.summoner_spell_id
)
|> Repo.one()
Schema.changeset(prev || %Schema{}, attrs)
|> Repo.insert_or_update()
end
def get_champion_picked_summoners(champion_id, team_position \\ "MIDDLE") 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: s in SummonerSpellSchema,
on: s.spell_id == f.summoner_spell_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
),
spell_id: f.summoner_spell_id,
metadata: s.metadata,
champion_id: c.champion_id,
team_position: f.team_position,
total_games: count("*")
},
group_by: [
f.champion_id,
f.summoner_spell_id,
s.metadata,
c.champion_id,
f.team_position
]
Repo.all(query)
end
def list_facts() do
Repo.all(Schema)
end
end
@@ -0,0 +1,34 @@
defmodule LolAnalytics.Facts.ChampionPickedSummonerSpell.Schema do
use Ecto.Schema
import Ecto.Changeset
@params [
:champion_id,
:summoner_spell_id,
:match_id,
:is_win,
:game_length_seconds,
:queue_id,
:team_position,
:puuid
]
schema "fact_champion_picked_summoner_spell" do
field :champion_id, :integer
field :summoner_spell_id, :integer
field :match_id, :string
field :is_win, :boolean
field :game_length_seconds, :integer
field :queue_id, :integer
field :team_position, :string
field :puuid, :string
end
def changeset(fact = %__MODULE__{}, attrs \\ %{}) do
fact
|> cast(attrs, @params)
|> validate_required(@params)
|> unique_constraint([:puuid, :match_id, :summoner_spell_id])
end
end
@@ -0,0 +1,138 @@
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
alias LolAnalytics.Dimensions.Match.MatchRepo
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)
_patch = PatchRepo.get_or_create(attrs.patch_number)
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.insert_or_update(changeset)
end
def list_played_matches() do
Repo.all(Schema)
end
def get_win_rates(opts \\ []) do
team_position = Keyword.get(opts, :team_position)
patch = Keyword.get(opts, :patch_number, "14.12")
case {team_position, patch} do
{nil, nil} ->
get_win_rates()
{"all", patch} ->
get_win_rates_for_patch(patch)
{nil, patch} ->
get_win_rates_for_patch(patch)
{team_position, patch} ->
get_win_rates_for_patch_and_team_position(patch, team_position)
end
end
@spec champion_win_rates_by_patch(String.t(), String.t()) :: [map()]
def champion_win_rates_by_patch(champion_id, team_position) 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("*")
},
group_by: [m.champion_id, c.image, c.name, m.patch_number, m.team_position],
where: m.team_position == ^team_position and m.champion_id == ^champion_id,
having: count("*") > 50
Repo.all(query)
end
defp get_win_rates_for_patch(patch_number) 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("*")
},
group_by: [m.champion_id, c.image, c.name, m.team_position, m.patch_number],
having: m.patch_number == ^patch_number and count("*") > 100
Repo.all(query)
end
defp get_win_rates_for_patch_and_team_position(patch_number, team_position) 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("*")
},
group_by: [m.champion_id, c.image, c.name, m.team_position, m.patch_number],
having:
m.team_position == ^team_position and m.patch_number == ^patch_number and
count("*") > 100
Repo.all(query)
end
end
@@ -0,0 +1,35 @@
defmodule LolAnalytics.Facts.ChampionPlayedGame.Schema do
use Ecto.Schema
import Ecto.Changeset
@casting_attrs [
:champion_id,
:match_id,
:patch_number,
:is_win,
:game_length_seconds,
:team_position,
:puuid,
:queue_id
]
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
field :puuid, :string
field :queue_id, :integer
timestamps()
end
def changeset(fact = %__MODULE__{}, attrs \\ %{}) do
fact
|> cast(attrs, @casting_attrs)
|> validate_required(@casting_attrs)
|> unique_constraint([:id, :champion_id, :queue_id])
end
end
@@ -0,0 +1,51 @@
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(url) do
with {:ok, %HTTPoison.Response{status_code: 200, body: body}} <-
HTTPoison.get(url),
{:ok, decoded_match} <- Poison.decode(body, as: %LoLAPI.Model.MatchResponse{}) do
process_game_data(decoded_match)
else
_ ->
Logger.error("Could not process data from #{url} for ChampionPlayedGame")
{:error, "Could not process data from #{url}"}
end
end
def process_game_data(decoded_match) 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
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,3 @@
defmodule LolAnalytics.Facts.FactBehaviour do
@callback process_game_at_url(String.t()) :: any()
end
@@ -0,0 +1,40 @@
defmodule LolAnalytics.Facts.FactsRunner do
alias LolAnalytics.Facts
def analyze_by_patch(patch) do
Storage.MatchStorage.S3MatchStorage.stream_files("ranked", patch: patch)
|> peach(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_all_matches do
Storage.MatchStorage.S3MatchStorage.stream_files("ranked")
|> peach(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,
&Facts.ChampionPlayedGame.FactProcessor.process_game_at_url/1,
&Facts.ChampionPickedItem.FactProcessor.process_game_at_url/1
]
end
def peach(enum, fun, concurrency \\ System.schedulers_online(), 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
@@ -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
@@ -0,0 +1,12 @@
defmodule LolAnalytics.MatchesProcessor do
use GenServer
@doc """
iex> LolAnalytics.MatchesProcessor.process_for_patch "14.12.593.5894"
"""
def process_for_patch(patch) do
Task.Supervisor.async(LoLAnalytics.TaskSupervisor, fn ->
LolAnalytics.Facts.FactsRunner.analyze_by_patch(patch)
end)
end
end
+1
View File
@@ -42,6 +42,7 @@ defmodule LoLAnalytics.MixProject do
{:postgrex, ">= 0.0.0"},
{:jason, "~> 1.2"},
{:lol_api, in_umbrella: true},
{:storage, in_umbrella: true},
{:httpoison, "~> 2.2"},
{:poison, "~> 5.0"}
]
@@ -0,0 +1,62 @@
defmodule LoLAnalytics.Repo.Migrations.AnalyticsTables do
use Ecto.Migration
def change do
create table("dim_champion") do
add :champion_id, :integer, primary_key: true, null: false
timestamps()
end
create index("dim_champion", [:champion_id], unique: true)
create table("dim_item") do
add :item_id, :integer, primary_key: true, null: false
timestamps()
end
create index("dim_item", [:item_id], unique: true)
create table("dim_match") do
add :match_id, :string, primary_key: true, null: false
timestamps()
end
create index("dim_match", [:match_id], unique: true)
create table("dim_patch") do
add :patch_number, :string, primary_key: true, null: false
timestamps()
end
create index("dim_patch", [:patch_number], unique: true)
create table("dim_player") do
add :puuid, :string, primary_key: true, null: false
timestamps()
end
create index("dim_player", [:puuid], unique: true)
create table("dim_summoner_spell") do
add :spell_id, :integer, primary_key: true, null: false
timestamps()
end
create index("dim_summoner_spell", [:spell_id], unique: true)
create table("fact_champion_played_game") do
add :champion_id, references("dim_champion", column: :champion_id, type: :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)
timestamps()
end
create index("fact_champion_played_game", [:id, :champion_id, :queue_id])
create index("fact_champion_played_game", [:puuid, :match_id], unique: true)
end
end
@@ -0,0 +1,10 @@
defmodule LoLAnalytics.Repo.Migrations.ChampionDimName do
use Ecto.Migration
def change do
alter table("dim_champion") do
add :name, :string
add :image, :string
end
end
end
@@ -0,0 +1,31 @@
defmodule LoLAnalytics.Repo.Migrations.SummonerSpellWinRate do
use Ecto.Migration
def change do
create table("fact_champion_picked_summoner_spell") do
add :champion_id, references("dim_champion", column: :champion_id, type: :integer)
add :summoner_spell_id, references("dim_summoner_spell", column: :spell_id, type: :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 :team_position, :string
add :puuid, references("dim_player", column: :puuid, type: :string)
add :patch_number, references("dim_patch", column: :patch_number, type: :string)
end
create index(
"fact_champion_picked_summoner_spell",
[:puuid, :match_id, :summoner_spell_id],
unique: true
)
create index("fact_champion_picked_summoner_spell", [
:is_win,
:team_position,
:queue_id,
:summoner_spell_id,
:patch_number
])
end
end
@@ -0,0 +1,9 @@
defmodule LoLAnalytics.Repo.Migrations.SummonerSpellMetadata do
use Ecto.Migration
def change do
alter table("dim_summoner_spell") do
add :metadata, :map
end
end
end
@@ -0,0 +1,11 @@
defmodule LoLAnalytics.Repo.Migrations.SummonerSpellMetadataIndex do
use Ecto.Migration
def up do
execute("CREATE INDEX dim_summoner_spell_metadata ON dim_summoner_spell USING GIN(metadata)")
end
def down do
execute("DROP INDEX dim_summoner_spell_metadata")
end
end
@@ -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
@@ -0,0 +1,9 @@
defmodule LoLAnalytics.Repo.Migrations.ItemMetadata do
use Ecto.Migration
def change do
alter table("dim_item") do
add :metadata, :map
end
end
end
@@ -0,0 +1,11 @@
defmodule LoLAnalytics.Repo.Migrations.ItemMetadataIndex do
use Ecto.Migration
def up do
execute("CREATE INDEX dim_item_metadata ON dim_item USING GIN(metadata)")
end
def down do
execute("DROP INDEX dim_item_metadata")
end
end
+13
View File
@@ -3,3 +3,16 @@
@import "tailwindcss/utilities";
/* This file is for your main application CSS */
.tooltip {
visibility: hidden;
position: absolute;
}
.has-tooltip:hover .tooltip {
visibility: visible;
z-index: 100;
border: 1px solid #333;
background-color: whitesmoke;
border-radius: 12px;
}
+5 -1
View File
@@ -21,11 +21,15 @@ import "phoenix_html"
import { Socket } from "phoenix"
import { LiveSocket } from "phoenix_live_view"
import topbar from "../vendor/topbar"
import { ChampionWinRate } from "./hooks/champion_win_rate_patch"
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let liveSocket = new LiveSocket("/live", Socket, {
longPollFallbackMs: 2500,
params: {_csrf_token: csrfToken}
params: { _csrf_token: csrfToken },
hooks: {
ChampionWinRate,
}
})
// Show progress bar on live navigation and form submits
@@ -0,0 +1,32 @@
import Chart from "chart.js/auto"
const ChampionWinRate = {
mounted() {
this.handleEvent("win-rate", ({ winRates }) => {
patches = winRates.map((winRate) => {
return winRate.patch_number
})
winRateValues = winRates.map((winRate) => winRate.win_rate)
setInterval(() => {
const data = {
labels: patches,
datasets: [{
label: 'Win rate',
data: winRateValues,
fill: false,
borderColor: 'rgb(75, 192, 192)',
tension: 0.1
}]
};
this.chart = new Chart(document.getElementById("win-rate"), {
type: 'line',
data: data
})
this.chart.canvas.parentNode.style.height = '250px';
this.chart.canvas.parentNode.style.width = '400px';
}, 1000)
});
}
}
export { ChampionWinRate };
+28
View File
@@ -0,0 +1,28 @@
{
"name": "assets",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"dependencies": {
"chart.js": "^4.4.3"
}
},
"node_modules/@kurkle/color": {
"version": "0.3.2",
"resolved": "https://registry.npmjs.org/@kurkle/color/-/color-0.3.2.tgz",
"integrity": "sha512-fuscdXJ9G1qb7W8VdHi+IwRqij3lBkosAm4ydQtEmbY58OzHXqQhvlxqEkoz0yssNVn38bcpRWgA9PP+OGoisw=="
},
"node_modules/chart.js": {
"version": "4.4.3",
"resolved": "https://registry.npmjs.org/chart.js/-/chart.js-4.4.3.tgz",
"integrity": "sha512-qK1gkGSRYcJzqrrzdR6a+I0vQ4/R+SoODXyAjscQ/4mzuNzySaMCd+hyVxitSY1+L2fjPD1Gbn+ibNqRmwQeLw==",
"dependencies": {
"@kurkle/color": "^0.3.0"
},
"engines": {
"pnpm": ">=8"
}
}
}
}
@@ -0,0 +1,5 @@
{
"dependencies": {
"chart.js": "^4.4.3"
}
}
@@ -0,0 +1,23 @@
defmodule LolAnalyticsWeb.ChampionComponents.ChampionAvatar do
use Phoenix.Component
attr :id, :integer, required: true
attr :image, :string, required: true
attr :name, :string, required: true
attr :width, :integer, default: 100
attr :height, :integer, default: 100
def champion_avatar(assigns) do
~H"""
<style>
.champion-avatar {
width: 100px;
height: 100px;
}
</style>
<div class="flex flex-col w-40">
<img src={@image} class="champion-avatar" alt="champion-icon" />
</div>
"""
end
end
@@ -0,0 +1,39 @@
defmodule LolAnalyticsWeb.ChampionComponents.ChampionCard.Props do
defstruct [:id, :win_rate, :image, :name, :team_position, :patch_number, :wins, :total_games]
end
defmodule LolAnalyticsWeb.ChampionComponents.ChampionCard do
use Phoenix.Component
alias LolAnalyticsWeb.ChampionComponents.ChampionCard.Props
attr :props, Props, default: %Props{}
def champion_card(assigns) do
~H"""
<.link patch={"/champions/#{@props.id}?team-position=#{@props.team_position}&patch=#{@props.patch_number}"}>
<div class="flex flex-col rounded-xl bg-clip-border overflow-hidden bg-gray-200">
<div class="flex flex-col flex-col-reverse">
<div class="flex w-auto px-4 py-1 opacity-80 gap-2 absolute z-10 align-bottom bg-black">
<img src={team_position_image(@props.team_position)} class="w-5 h-5" />
<h3 class="text-white"><%= @props.name %></h3>
</div>
<img src={"https://ddragon.leagueoflegends.com/cdn/14.11.1/img/champion/#{@props.image}"} />
</div>
<div class="py-1" />
<div class="pl-2">
<h3>Win rate: <%= @props.win_rate %></h3>
<p class="text-xs">Wins: <%= @props.wins %> / <%= @props.total_games %></p>
</div>
<div class="py-1" />
</div>
</.link>
"""
end
defp team_position_image("BOTTOM"), do: "/images/lanes/bot.png"
defp team_position_image("MIDDLE"), do: "/images/lanes/mid.png"
defp team_position_image("TOP"), do: "/images/lanes/top.png"
defp team_position_image("JUNGLE"), do: "/images/lanes/jungle.png"
defp team_position_image("UTILITY"), do: "/images/lanes/utility.png"
end
@@ -0,0 +1,23 @@
defmodule LolAnalyticsWeb.ChampionComponents.Items do
use Phoenix.Component
def items(assigns) do
image = ""
~H"""
<div class="flex flex-wrap flex-wrap gap-4">
<%= for item <- assigns.items do %>
<div class="has-tooltip">
<div clas="flex flex-col gap-1 p-4">
<img src={item.image} />
<%!-- <p><%= item.name %></p> --%>
<p><%= item.win_rate %>%</p>
<p class="text-xs"><%= item.wins %>/<%= item.total_games %></p>
</div>
<span class="tooltip -mt-8 py-2 px-4 rounded-xl"><%= item.name %></span>
</div>
<% end %>
</div>
"""
end
end
@@ -0,0 +1,23 @@
defmodule LolAnalyticsWeb.ChampionComponents.SummonerSpells do
alias LolAnalyticsWeb.ChampionComponents.SummonerSpells.Props
use Phoenix.Component
def summoner_spells(assigns) do
~H"""
<div class="flex flex-wrap flex-wrap gap-4">
<%= for spell <- assigns.spells do %>
<div class="has-tooltip">
<div clas="flex flex-col gap-1">
<img src={spell.image} />
<p><%= spell.win_rate %>%</p>
<p class="text-xs"><%= spell.wins %>/<%= spell.total_games %></p>
</div>
<div class="tooltip -my-8 px-4 py-2 rounded-xl">
<p><%= spell.name %></p>
</div>
</div>
<% end %>
</div>
"""
end
end
@@ -315,8 +315,8 @@ defmodule LoLAnalyticsWeb.CoreComponents do
type="checkbox"
id={@id}
name={@name}
value="true"
checked={@checked}
value="false"
checked={false}
class="rounded border-zinc-300 text-zinc-900 focus:ring-0"
{@rest}
/>
@@ -2,25 +2,16 @@
<div class="flex items-center justify-between border-b border-zinc-100 py-3 text-sm">
<div class="flex items-center gap-4">
<a href="/">
<img src={~p"/images/logo.svg"} width="36" />
LoL Analytics
</a>
<p class="bg-brand/5 text-brand rounded-full px-2 font-medium leading-6">
v<%= Application.spec(:phoenix, :vsn) %>
</p>
</div>
<div class="flex items-center gap-4 font-semibold leading-6 text-zinc-900">
<a href="https://twitter.com/elixirphoenix" class="hover:text-zinc-700">
@elixirphoenix
</a>
<a href="https://github.com/phoenixframework/phoenix" class="hover:text-zinc-700">
GitHub
</a>
<a
href="https://hexdocs.pm/phoenix/overview.html"
class="rounded-lg bg-zinc-100 px-2 py-1 hover:bg-zinc-200/80"
>
Get Started <span aria-hidden="true">&rarr;</span>
</a>
<.link patch="/champions">
Champions
</.link>
</div>
</div>
</header>
@@ -1,5 +1,6 @@
<!DOCTYPE html>
<html lang="en" class="[scrollbar-gutter:stable]">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
@@ -11,7 +12,9 @@
<script defer phx-track-static type="text/javascript" src={~p"/assets/app.js"}>
</script>
</head>
<body class="bg-white antialiased">
<%= @inner_content %>
</body>
</html>
@@ -0,0 +1,28 @@
defmodule LolAnalyticsWeb.Loader do
use Phoenix.Component
def loader(assigns) do
~H"""
<div role="status" class="flex flex-col mx-auto w-max justify-center items-center gap-4">
<span class="text-3xl">Loading</span>
<svg
aria-hidden="true"
class="w-16 h-16 text-gray-300 animate-spin dark:text-gray-300 fill-orange-600"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span class="sr-only">Loading...</span>
</div>
"""
end
end
@@ -0,0 +1,56 @@
defmodule LolAnalyticsWeb.PatchSelector do
use LoLAnalyticsWeb, :live_component
def mount(socket) do
patches =
LolAnalytics.Dimensions.Patch.PatchRepo.list_patches()
|> Enum.sort(fn %{patch_number: p1}, %{patch_number: p2} ->
[_, minor_1] = String.split(p1, ".") |> Enum.map(&String.to_integer/1)
[_, minor_2] = String.split(p2, ".") |> Enum.map(&String.to_integer/1)
p1 > p2 && minor_1 > minor_2
end)
patch_numbers = Enum.map(patches, & &1.patch_number)
[last_patch | _] = patch_numbers
send(self(), %{patch: last_patch})
socket =
assign(socket, :patch_numbers, patch_numbers)
|> assign(:patch_form, to_form(%{"selected_patch" => last_patch}))
{:ok, socket}
end
@impl true
def handle_event("selected_patch", %{"patch" => patch} = unsigned_params, socket) do
send(self(), %{patch: patch})
{:noreply, assign(socket, :selected_patch, patch)}
end
def render(assigns) do
~H"""
<div phx-feedback-for={@id}>
<.form for={@patch_form} phx-change="validate" phx-target={@myself} phx-submit="save">
<div class="flex gap-4">
<p class="my-auto">Patch</p>
<select
phx-change="selected_patch"
id="patch"
name="patch"
class="block w-full rounded-md border border-gray-300 bg-white shadow-sm focus:border-zinc-400 focus:ring-0 sm:text-sm"
>
<%= for patch <- @patch_numbers do %>
<option key={patch} phx-click="select-patch" name={patch} value={patch}>
<%= patch %>
</option>
<% end %>
</select>
</div>
</.form>
</div>
"""
end
end
@@ -0,0 +1,3 @@
defmodule LolAnalyticsWeb.ChampionLive.ChampionSummary do
defstruct [:id, :win_rate, :image, :name, :team_position, :wins, :total_games]
end
@@ -0,0 +1,57 @@
defmodule LolAnalyticsWeb.ChampionLive.Components.ChampionFilters do
use LoLAnalyticsWeb, :live_component
@roles_definition [
%{title: "All", value: "all"},
%{title: "Top", value: "TOP"},
%{title: "Jungle", value: "JUNGLE"},
%{title: "Mid", value: "MIDDLE"},
%{title: "Bot", value: "BOTTOM"},
%{title: "Support", value: "UTILITY"}
]
def on_role_selected(role) do
send_update(LolAnalyticsWeb.ChampionLive, role)
end
def mount(socket) do
socket =
assign(socket, :roles, @roles_definition)
{:ok, socket}
end
attr :selectedrole, :string, required: true
attr :roles, :list, default: []
def render(assigns) do
selected_class =
"px-8 py-2 flex flex-row gap-2 align-middle rounded-full border-gray-200 border border-orange-600 cursor-pointer"
~H"""
<div>
<div class="flex flex-row overflow-x-scroll sm:overflow-x-auto justify-between">
<%= for role <- @roles do %>
<%= if (assigns.selectedrole == role.value) do %>
<div phx-click="filter" phx-value-role={role.title} class={selected_class}>
<p><%= role.title %></p>
</div>
<% else %>
<div
phx-click="filter"
phx-value-role={role.value}
class="px-8 py-2 flex flex-row gap-2 align-middle cursor-pointer"
>
<p><%= role.title %></p>
</div>
<% end %>
<% end %>
</div>
</div>
"""
end
end
defmodule LolAnalyticsWeb.ChampionLive.Components.ChampionFilters.EventHandler do
@callback role_selected() :: none()
end
@@ -0,0 +1,129 @@
defmodule LoLAnalyticsWeb.ChampionLive.Index do
use LoLAnalyticsWeb, :live_view
import LolAnalyticsWeb.ChampionComponents.ChampionCard
import LolAnalyticsWeb.Loader
import Phoenix.VerifiedRoutes
alias LolAnalyticsWeb.ChampionLive.Mapper
alias LolAnalyticsWeb.ChampionLive.Components.ChampionFilters
alias LolAnalyticsWeb.PatchSelector
@behaviour LolAnalyticsWeb.ChampionFilters.EventHandler
@impl true
def mount(params, _session, socket) do
role = params["role"] || "all"
patch = params["patch"] || "14.12"
socket =
socket
|> assign(:selected_role, role)
|> assign(:selected_patch, patch)
|> assign(:champions, %{status: :loading})
|> load_champs(role, patch)
{:ok, socket}
end
def handle_params(params, _uri, socket) do
role = params["role"] || "all"
patch = params["patch"] || "14.12"
{
:noreply,
assign(socket, selected_role: role)
|> assign(selected_patch: patch)
}
end
@impl true
def handle_event("filter", %{"role" => selected_role} = params, socket) do
query = Map.merge(params, %{patch: socket.assigns.selected_patch || "14.12"})
{:reply, %{},
socket
|> push_patch(to: ~p"/champions?#{query}")
|> assign(:champions, %{status: :loading})
|> load_champs(selected_role, socket.assigns.selected_patch)
|> assign(:selected_role, selected_role)}
end
def handle_info(%{patch: patch}, socket) do
selected_role = socket.assigns.selected_role
query_params = %{role: selected_role, patch: patch}
socket =
assign(socket, :champions, %{status: :loading})
|> assign(:selected_patch, patch)
|> push_patch(to: ~p"/champions?#{query_params}")
|> load_champs(selected_role, patch)
{:noreply, socket}
end
defp load_champs(socket, selected_role, "all") do
socket
|> start_async(
:get_champs,
fn ->
LolAnalytics.Facts.ChampionPlayedGame.Repo.get_win_rates(team_position: selected_role)
|> Mapper.map_champs()
|> Enum.sort(&(&1.win_rate >= &2.win_rate))
end
)
end
defp load_champs(socket, selected_role, patch) do
socket
|> start_async(
:get_champs,
fn ->
LolAnalytics.Facts.ChampionPlayedGame.Repo.get_win_rates(
team_position: selected_role,
patch_number: patch
)
|> Mapper.map_champs()
|> Enum.sort(&(&1.win_rate >= &2.win_rate))
end
)
end
def handle_async(:get_champs, {:ok, champs}, socket) do
{:noreply, assign(socket, :champions, %{status: :data, data: champs})}
end
def render_champions(assigns) do
case assigns.champions do
%{status: :loading} ->
~H"""
<.loader />
"""
%{status: :data, data: champions} ->
~H"""
<div id="champions" class="grid grid-cols-2 sm:grid-cols-4 gap-4">
<%= for champion <- champions do %>
<.champion_card props={champion} />
<% end %>
</div>
"""
end
end
defp filter_role(role) do
fn champ ->
champ.team_position == role || role == "all"
end
end
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Champions")
end
end
@@ -0,0 +1,18 @@
<.header>
Champions
</.header>
<div class="px-2">
<h1 class="text-l font-semibold">Filters</h1>
<div class="flex flex-col py-2 gap-2">
<p>Roles</p>
<.live_component module={ChampionFilters} id="role-filters" selectedrole={@selected_role || "all" } />
</div>
<.live_component module={PatchSelector} id="patch-selector" />
<div class="h-4"></div>
<.render_champions champions={@champions} />
</div>
@@ -0,0 +1,13 @@
defmodule LolAnalyticsWeb.ChampionLive.Mapper do
alias LolAnalyticsWeb.ChampionLive.ChampionSummary
def map_champs(champs) do
champs
|> Enum.map(fn champ ->
%{
champ
| win_rate: :erlang.float_to_binary(champ.win_rate, decimals: 2)
}
end)
end
end
@@ -0,0 +1,164 @@
defmodule LoLAnalyticsWeb.ChampionLive.Show do
use LoLAnalyticsWeb, :live_view
import LolAnalyticsWeb.ChampionComponents.SummonerSpells
import LolAnalyticsWeb.ChampionComponents.ChampionAvatar
import LolAnalyticsWeb.ChampionComponents.Items
import LolAnalyticsWeb.Loader
alias LolAnalyticsWeb.ChampionComponents.SummonerSpells.ShowMapper
@impl true
def mount(_params, _session, socket) do
{:ok, socket}
end
@impl true
def handle_params(%{"id" => id, "team-position" => team_position, "patch" => patch}, _, socket) do
{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:champion, load_champion_info(id))
|> load_win_rates(id, team_position)
|> load_summoner_spells(id, team_position)
|> load_items(id, team_position, patch)}
end
def load_win_rates(socket, champion_id, team_position) do
socket
|> start_async(:get_win_rates, fn ->
LolAnalytics.Facts.ChampionPlayedGame.Repo.champion_win_rates_by_patch(
champion_id,
team_position
)
|> Enum.sort(fn p1, p2 ->
[_, minor_1] = String.split(p1.patch_number, ".") |> Enum.map(&String.to_integer/1)
[_, minor_2] = String.split(p2.patch_number, ".") |> Enum.map(&String.to_integer/1)
p1 < p2 && minor_1 < minor_2
end)
end)
end
defp load_summoner_spells(socket, champion_id, team_position) do
socket
|> assign(:summoner_spells, %{status: :loading})
|> start_async(:get_summoners, fn ->
LolAnalytics.Facts.ChampionPickedSummonerSpell.Repo.get_champion_picked_summoners(
champion_id,
team_position
)
|> ShowMapper.map_spells()
end)
end
defp load_items(socket, champion_id, team_position, patch) do
socket
|> assign(:items, %{status: :loading})
|> assign(:boots, %{status: :loading})
|> start_async(
:get_items,
fn ->
items =
LolAnalytics.Facts.ChampionPickedItem.Repo.get_champion_picked_items(
champion_id,
team_position,
patch
)
popular_items = items |> ShowMapper.map_items() |> Enum.take(30)
boots = items |> ShowMapper.extract_boots()
%{boots: boots, popular: popular_items}
end
)
end
def handle_async(:get_win_rates, {:ok, result}, socket) do
{:noreply, push_event(socket, "win-rate", %{winRates: result})}
end
def handle_async(:get_items, {:ok, %{popular: popular, boots: boots}} = result, socket) do
socket =
socket
|> assign(:items, %{
status: :data,
data: %{
popular: popular,
boots: boots
}
})
{:noreply, socket}
end
def handle_async(:get_summoners, {:ok, summoner_spells}, socket) do
socket =
assign(socket, :summoner_spells, %{
status: :data,
data: summoner_spells
})
{:noreply, socket}
end
defp load_champion_info(champion_id) do
LolAnalytics.Dimensions.Champion.ChampionRepo.get_or_create(champion_id)
|> ShowMapper.map_champion()
end
defp page_title(:show), do: "Show Champion"
defp page_title(:edit), do: "Edit Champion"
def render_summoner_spells(assigns) do
end
def render_items(assigns) do
case assigns.items do
%{status: :loading} ->
~H"""
<.loader />
"""
%{status: :data, data: data} ->
~H"""
<%= if Enum.count(data.boots) > 0 do %>
<h2 class="text-2xl">Items</h2>
<h2 class="text-xl">Boots</h2>
<div class="my-2" />
<.items items={data.boots} />
<div class="my-4" />
<% end %>
<h2 class="text-xl">Popular items</h2>
<div class="my-2" />
<.items items={data.popular} />
"""
end
end
def render_summoners(assigns) do
case assigns.summoner_pells do
%{status: :loading} ->
~H"""
<.loader />
"""
%{status: :data, data: data} ->
~H"""
<h2 class="text-2xl">Summoner spells</h2>
<div class="my-2" />
<.summoner_spells spells={data} />
"""
end
end
end
@@ -0,0 +1,26 @@
<style>
.win-rate {
height: 250px;
}
</style>
<.header>
<h1 class="text-4xl">
<%= @champion.name %>
</h1>
</.header>
<div class="my-6" />
<div class="flex flex-row gap-4">
<.champion_avatar class="w-20" id={@champion.id} name={@champion.name}
image={"https://ddragon.leagueoflegends.com/cdn/14.11.1/img/champion/#{@champion.image}"} />
<canvas class="w-full win-rate" height="250" class="win-rate" id="win-rate" phx-hook="ChampionWinRate" />
</div>
<div class="my-4" />
<.render_summoners summoner_pells={@summoner_spells} />
<div class="my-4" />
<.render_items items={@items} />
@@ -0,0 +1,60 @@
defmodule LolAnalyticsWeb.ChampionComponents.SummonerSpells.ShowMapper do
alias LolAnalyticsWeb.ChampionComponents.SummonerSpells.SummonerSpell
@spec map_champion(%LolAnalytics.Dimensions.Champion.ChampionSchema{}) :: map()
def map_champion(champion) do
%{
id: champion.champion_id,
name: champion.name,
image: champion.image
}
end
def map_spells(items) do
items
|> Enum.map(&map_spell/1)
|> Enum.sort(&(&1.total_games > &2.total_games))
end
def map_spell(spell) do
image = spell.metadata["image"]["full"]
%{
id: spell["id"],
win_rate: :erlang.float_to_binary(spell.win_rate, decimals: 2),
total_games: spell.total_games,
image: "https://ddragon.leagueoflegends.com/cdn/14.11.1/img/spell/#{image}",
name: spell.metadata["name"],
wins: spell.wins
}
end
def map_items(items) do
items
|> Enum.map(&map_item/1)
|> Enum.sort(&(&1.total_games > &2.total_games))
end
def map_item(item) do
image = item.metadata["image"]["full"]
%{
id: item["id"],
win_rate: :erlang.float_to_binary(item.win_rate, decimals: 2),
total_games: item.total_games,
image: "https://ddragon.leagueoflegends.com/cdn/14.11.1/img/item/#{image}",
name: item.metadata["name"],
wins: item.wins
}
end
def extract_boots(items) do
items
|> Enum.filter(fn item ->
tags = item.metadata["tags"]
tags |> Enum.any?(&String.equivalent?(&1, "Boots"))
end)
|> Enum.map(&map_item/1)
|> Enum.sort(&(&1.total_games > &2.total_games))
end
end
@@ -0,0 +1,27 @@
defmodule LoLAnalyticsWeb.RoleLive.FormComponent do
use LoLAnalyticsWeb, :live_component
@impl true
def render(assigns) do
~H"""
<div>
<.header>
<%= @title %>
<:subtitle>Use this form to manage role records in your database.</:subtitle>
</.header>
<.simple_form
for={@form}
id="role-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
>
<:actions>
<.button phx-disable-with="Saving...">Save Role</.button>
</:actions>
</.simple_form>
</div>
"""
end
end
@@ -0,0 +1,34 @@
defmodule LoLAnalyticsWeb.RoleLive.Index do
use LoLAnalyticsWeb, :live_view
@roles ["ALL", "TOP", "MIDDLE", "JUNGLE", "UTILITY", "BOTTOM"]
@impl true
def mount(_params, _session, socket) do
{:ok, stream(socket, :roles, @roles)}
end
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Roles")
|> assign(:role, nil)
end
@impl true
def handle_info({LoLAnalyticsWeb.RoleLive.FormComponent, {:saved, role}}, socket) do
{:noreply, stream_insert(socket, :roles, role)}
end
# @impl true
# def handle_event("delete", %{"id" => id}, socket) do
# role = Accounts.get_role!(id)
# {:ok, _} = Accounts.delete_role(role)
# {:noreply, stream_delete(socket, :roles, role)}
# end
end
@@ -0,0 +1,4 @@
<.header>
Listing Roles
<:actions></:actions>
</.header>
@@ -0,0 +1,18 @@
defmodule LoLAnalyticsWeb.RoleLive.Show do
use LoLAnalyticsWeb, :live_view
@impl true
def mount(_params, _session, socket) do
{:ok, socket}
end
@impl true
def handle_params(%{"id" => _id}, _, socket) do
{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))}
end
defp page_title(:show), do: "Show Role"
defp page_title(:edit), do: "Edit Role"
end
@@ -0,0 +1,5 @@
<.header>
Role <%= @role.id %>
<:subtitle>This is a role record from your database.</:subtitle>
<:actions></:actions>
</.header>
@@ -17,7 +17,13 @@ defmodule LoLAnalyticsWeb.Router do
scope "/", LoLAnalyticsWeb do
pipe_through :browser
get "/", PageController, :home
live "/", ChampionLive.Index, :index
live "/champions", ChampionLive.Index, :index
live "/champions/new", ChampionLive.Index, :new
live "/champions/:id/edit", ChampionLive.Index, :edit
live "/champions/:id", ChampionLive.Show, :show
live "/champions/:id/show/edit", ChampionLive.Show, :edit
end
# Other scopes may use custom stacks.
Binary file not shown.

After

Width:  |  Height:  |  Size: 152 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

@@ -0,0 +1,5 @@
# See https://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file
#
# To ban all spiders from the entire site uncomment the next two lines:
# User-agent: *
# Disallow: /
Binary file not shown.
@@ -0,0 +1,108 @@
defmodule LoLAnalyticsWeb.ChampionLiveTest do
use LoLAnalyticsWeb.ConnCase
import Phoenix.LiveViewTest
import LoLAnalytics.AccountsFixtures
@create_attrs %{}
@update_attrs %{}
@invalid_attrs %{}
defp create_champion(_) do
champion = champion_fixture()
%{champion: champion}
end
describe "Index" do
setup [:create_champion]
test "lists all champions", %{conn: conn} do
{:ok, _index_live, html} = live(conn, ~p"/champions")
assert html =~ "Listing Champions"
end
test "saves new champion", %{conn: conn} do
{:ok, index_live, _html} = live(conn, ~p"/champions")
assert index_live |> element("a", "New Champion") |> render_click() =~
"New Champion"
assert_patch(index_live, ~p"/champions/new")
assert index_live
|> form("#champion-form", champion: @invalid_attrs)
|> render_change() =~ "can&#39;t be blank"
assert index_live
|> form("#champion-form", champion: @create_attrs)
|> render_submit()
assert_patch(index_live, ~p"/champions")
html = render(index_live)
assert html =~ "Champion created successfully"
end
test "updates champion in listing", %{conn: conn, champion: champion} do
{:ok, index_live, _html} = live(conn, ~p"/champions")
assert index_live |> element("#champions-#{champion.id} a", "Edit") |> render_click() =~
"Edit Champion"
assert_patch(index_live, ~p"/champions/#{champion}/edit")
assert index_live
|> form("#champion-form", champion: @invalid_attrs)
|> render_change() =~ "can&#39;t be blank"
assert index_live
|> form("#champion-form", champion: @update_attrs)
|> render_submit()
assert_patch(index_live, ~p"/champions")
html = render(index_live)
assert html =~ "Champion updated successfully"
end
test "deletes champion in listing", %{conn: conn, champion: champion} do
{:ok, index_live, _html} = live(conn, ~p"/champions")
assert index_live |> element("#champions-#{champion.id} a", "Delete") |> render_click()
refute has_element?(index_live, "#champions-#{champion.id}")
end
end
describe "Show" do
setup [:create_champion]
test "displays champion", %{conn: conn, champion: champion} do
{:ok, _show_live, html} = live(conn, ~p"/champions/#{champion}")
assert html =~ "Show Champion"
end
test "updates champion within modal", %{conn: conn, champion: champion} do
{:ok, show_live, _html} = live(conn, ~p"/champions/#{champion}")
assert show_live |> element("a", "Edit") |> render_click() =~
"Edit Champion"
assert_patch(show_live, ~p"/champions/#{champion}/show/edit")
assert show_live
|> form("#champion-form", champion: @invalid_attrs)
|> render_change() =~ "can&#39;t be blank"
assert show_live
|> form("#champion-form", champion: @update_attrs)
|> render_submit()
assert_patch(show_live, ~p"/champions/#{champion}")
html = render(show_live)
assert html =~ "Champion updated successfully"
end
end
end
@@ -0,0 +1,108 @@
defmodule LoLAnalyticsWeb.RoleLiveTest do
use LoLAnalyticsWeb.ConnCase
import Phoenix.LiveViewTest
import LoLAnalytics.AccountsFixtures
@create_attrs %{}
@update_attrs %{}
@invalid_attrs %{}
defp create_role(_) do
role = role_fixture()
%{role: role}
end
describe "Index" do
setup [:create_role]
test "lists all roles", %{conn: conn} do
{:ok, _index_live, html} = live(conn, ~p"/roles")
assert html =~ "Listing Roles"
end
test "saves new role", %{conn: conn} do
{:ok, index_live, _html} = live(conn, ~p"/roles")
assert index_live |> element("a", "New Role") |> render_click() =~
"New Role"
assert_patch(index_live, ~p"/roles/new")
assert index_live
|> form("#role-form", role: @invalid_attrs)
|> render_change() =~ "can&#39;t be blank"
assert index_live
|> form("#role-form", role: @create_attrs)
|> render_submit()
assert_patch(index_live, ~p"/roles")
html = render(index_live)
assert html =~ "Role created successfully"
end
test "updates role in listing", %{conn: conn, role: role} do
{:ok, index_live, _html} = live(conn, ~p"/roles")
assert index_live |> element("#roles-#{role.id} a", "Edit") |> render_click() =~
"Edit Role"
assert_patch(index_live, ~p"/roles/#{role}/edit")
assert index_live
|> form("#role-form", role: @invalid_attrs)
|> render_change() =~ "can&#39;t be blank"
assert index_live
|> form("#role-form", role: @update_attrs)
|> render_submit()
assert_patch(index_live, ~p"/roles")
html = render(index_live)
assert html =~ "Role updated successfully"
end
test "deletes role in listing", %{conn: conn, role: role} do
{:ok, index_live, _html} = live(conn, ~p"/roles")
assert index_live |> element("#roles-#{role.id} a", "Delete") |> render_click()
refute has_element?(index_live, "#roles-#{role.id}")
end
end
describe "Show" do
setup [:create_role]
test "displays role", %{conn: conn, role: role} do
{:ok, _show_live, html} = live(conn, ~p"/roles/#{role}")
assert html =~ "Show Role"
end
test "updates role within modal", %{conn: conn, role: role} do
{:ok, show_live, _html} = live(conn, ~p"/roles/#{role}")
assert show_live |> element("a", "Edit") |> render_click() =~
"Edit Role"
assert_patch(show_live, ~p"/roles/#{role}/show/edit")
assert show_live
|> form("#role-form", role: @invalid_attrs)
|> render_change() =~ "can&#39;t be blank"
assert show_live
|> form("#role-form", role: @update_attrs)
|> render_submit()
assert_patch(show_live, ~p"/roles/#{role}")
html = render(show_live)
assert html =~ "Role updated successfully"
end
end
end
+1 -1
View File
@@ -18,7 +18,7 @@ defmodule LoLAPI.AccountApi do
200 ->
{:ok, Poison.decode(response.body)}
code ->
_code ->
Logger.error("Error getting puuid from player #{name} \##{tag}")
{:err, response.status_code}
end
+1 -1
View File
@@ -1,7 +1,7 @@
defmodule LoLAPI.MatchApi do
require Logger
@match_base_endpoint "https://europe.api.riotgames.com/lol/match/v5/matches/%{matchid}"
@puuid_matches_base_endpoint "https://europe.api.riotgames.com/lol/match/v5/matches/by-puuid/%{puuid}/ids"
@puuid_matches_base_endpoint "https://europe.api.riotgames.com/lol/match/v5/matches/by-puuid/%{puuid}/ids?queue=420"
@doc """
Get match by id
+6 -19
View File
@@ -1,19 +1,9 @@
defmodule Scrapper.MatchClassifier do
require Logger
@spec classify_match(%LoLAPI.Model.MatchResponse{}) :: nil
def classify_match(match = %LoLAPI.Model.MatchResponse{}) do
classify_match_by_queue(match.info.queueId)
end
@spec classify_match_by_queue(String.t()) :: nil
def classify_match_by_queue("420") do
matches = Storage.MatchStorage.S3MatchStorage.list_files("matches")
total_matches = Enum.count(matches)
matches
|> Enum.with_index(fn match, index -> {match, index} end)
|> Scrapper.Parallel.peach(fn {match, index} ->
def stream_classify_matches_by_queue(queue \\ 420, bucket \\ "ranked") do
Storage.MatchStorage.S3MatchStorage.stream_files("matches")
|> Stream.each(fn match ->
%{key: json_file} = match
[key | _] = String.split(json_file, ".")
@@ -25,15 +15,12 @@ defmodule Scrapper.MatchClassifier do
%{"info" => %{"gameVersion" => gameVersion, "queueId" => queueId}} =
Poison.decode!(response.body)
if queueId == 420 do
Storage.MatchStorage.S3MatchStorage.store_match(key, response.body, "ranked", gameVersion)
Logger.info("Match at #{index} of #{total_matches} is classified")
if queueId == queue do
Storage.MatchStorage.S3MatchStorage.store_match(key, response.body, bucket, gameVersion)
Logger.info("Match #{key} processed")
end
match
end)
end
def classify_match_by_queue(_) do
end
end
@@ -1,4 +1,5 @@
defmodule Scrapper.Processor.MatchProcessor do
require Logger
use Broadway
def start_link(_opts) do
@@ -44,7 +45,23 @@ defmodule Scrapper.Processor.MatchProcessor do
def process_resp({:ok, raw_match}, match_id) do
decoded_match = Poison.decode!(raw_match, as: %LoLAPI.Model.MatchResponse{})
match_url = Storage.MatchStorage.S3MatchStorage.store_match(match_id, raw_match, "matches")
match_url =
case decoded_match.info.queueId do
420 ->
Logger.info("#{match_id} #{decoded_match.info.gameVersion}")
Storage.MatchStorage.S3MatchStorage.store_match(
match_id,
raw_match,
"ranked",
"#{decoded_match.info.gameVersion}"
)
_queue_id ->
Storage.MatchStorage.S3MatchStorage.store_match(match_id, raw_match, "matches")
end
match = LolAnalytics.Match.MatchRepo.get_match(match_id)
case match do
@@ -66,6 +83,6 @@ defmodule Scrapper.Processor.MatchProcessor do
end)
end
def process_resp({:err, code}, match_id) do
def process_resp({:err, _code}, _match_id) do
end
end
@@ -1,5 +1,4 @@
defmodule Scrapper.Processor.PlayerProcessor do
alias Calendar.ISO
use Broadway
def start_link(_opts) do
@@ -19,7 +19,7 @@ defmodule Scrapper.Queue.MatchQueue do
LolAnalytics.Match.MatchRepo.get_match(match_id)
|> case do
nil -> GenServer.call(__MODULE__, {:queue_match, match_id})
match -> :already_processed
_match -> :already_processed
end
end
@@ -1,6 +1,13 @@
defmodule Storage.MatchStorage do
@callback stream_files(String.t()) :: Enumerable.t()
@callback get_match(String.t()) :: {:ok, Scrapper.Data.Match.t()} | {:error, :not_found}
@callback save_match(String.t(), Scrapper.Data.Match.t()) :: :ok
@callback list_matches() :: [map()]
@callback store_match(match_id :: String.t(), match :: map(), path :: String.t()) :: String.t()
@callback store_match(match_id :: String.t(), match_data :: String.t(), path :: String.t()) ::
String.t()
@callback store_match(
match_id :: String.t(),
match_data :: String.t(),
bucket :: String.t(),
path :: String.t()
) ::
String.t()
end
@@ -2,43 +2,16 @@ defmodule Storage.MatchStorage.S3MatchStorage do
require Logger
@behaviour Storage.MatchStorage
def get_match(match_id) do
""
end
@doc """
Lists all files at the given path.
iex > Storage.MatchStorage.S3MatchStorage.list_files("matches")
"""
@impl true
def list_files(path) do
{:ok, %{:body => %{:contents => contents, next_continuation_token: next_continuation_token}}} =
ExAws.S3.list_objects_v2(path)
|> ExAws.request()
if next_continuation_token do
list_files(path, contents, next_continuation_token)
# |> Enum.map(fn %{key: key} -> key end)
else
contents
# |> Enum.map(fn %{key: key} -> key end)
end
def get_match(_match_id) do
end
@spec list_files(String.t(), list(String.t()), String.t()) :: list(String.t())
defp list_files(path, acc, continuation_token) do
resp =
{:ok,
%{:body => %{:contents => contents, next_continuation_token: next_continuation_token}}} =
ExAws.S3.list_objects_v2(path, continuation_token: continuation_token)
|> ExAws.request()
@impl true
def stream_files(path, opts \\ []) do
patch = Keyword.get(opts, :patch, "")
if next_continuation_token == "" do
acc ++ contents
else
list_files(path, acc ++ contents, next_continuation_token)
end
ExAws.S3.list_objects_v2(path, prefix: patch)
|> ExAws.stream!()
end
@doc """
+1 -1
View File
@@ -8,7 +8,7 @@ import Config
config :lol_analytics_web, LoLAnalyticsWeb.Endpoint,
server: true,
http: [ip: {0, 0, 0, 0}, port: 4000],
url: [host: "lol-analytics.kaizer.cloud", port: 80],
url: [host: "localhost", port: 80],
cache_static_manifest: "priv/static/cache_manifest.json"
# Do not print debug messages in production
+1 -1
View File
@@ -6,7 +6,7 @@ import Config
# and secrets from environment variables or elsewhere. Do not define
# any compile-time configuration in here, as it won't be applied.
# The block below contains prod specific runtime configuration.
if config_env() == :prod do
if config_env() == :prod || true do
database_url =
System.get_env("DATABASE_URL") ||
raise """
+1 -1
View File
@@ -11,7 +11,7 @@ config :lol_analytics, LoLAnalytics.Repo,
hostname: "localhost",
database: "lol_analytics_test#{System.get_env("MIX_TEST_PARTITION")}",
pool: Ecto.Adapters.SQL.Sandbox,
pool_size: System.schedulers_online() * 2
pool_size: System.schedulers_online() * 4
# We don't run a server during test. If one is required,
# you can enable the server option below.
+36
View File
@@ -0,0 +1,36 @@
version: '3.8'
services:
db:
image: postgres:latest
volumes:
- postgres-data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
ports:
- "5432:5432"
rabbitmq:
image: rabbitmq:3-management-alpine
container_name: 'rabbitmq'
ports:
- "15671:15671"
- "15672:15672"
- "15691:15691"
- "15692:15692"
- "25672:25672"
- "4639:4639"
- "5671:5671"
- "5672:5672"
volumes:
- ~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/
- ~/.docker-conf/rabbitmq/log/:/var/log/rabbitmq
networks:
- rabbitmq_go_net
volumes:
postgres-data:
networks:
rabbitmq_go_net:
driver: bridge
+22
View File
@@ -0,0 +1,22 @@
```
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;
```