add metadata to summoner spell

This commit is contained in:
2024-06-07 01:42:45 +02:00
parent 6dd8eea3d3
commit 968c2634b7
15 changed files with 143 additions and 19 deletions
@@ -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
@@ -4,6 +4,7 @@ defmodule LolAnalytics.Dimensions.SummonerSpell.SummonerSpellRepo do
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)
@@ -23,6 +24,13 @@ defmodule LolAnalytics.Dimensions.SummonerSpell.SummonerSpellRepo do
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
@@ -4,12 +4,13 @@ defmodule LolAnalytics.Dimensions.SummonerSpell.SummonerSpellSchema do
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])
|> cast(attrs, [:spell_id, :metadata])
|> validate_required([:spell_id])
end
end
@@ -1,6 +1,7 @@
defmodule LolAnalytics.Facts.ChampionPickedSummonerSpell.Repo do
import Ecto.Query
alias LolAnalytics.Dimensions.SummonerSpell.SummonerSpellSchema
alias LolAnalytics.Dimensions.SummonerSpell.SummonerSpellRepo
alias LolAnalytics.Dimensions.Champion.ChampionSchema
@@ -13,11 +14,11 @@ defmodule LolAnalytics.Facts.ChampionPickedSummonerSpell.Repo do
alias LoLAnalytics.Repo
@type insert_attrs :: %{
match_id: String.t(),
champion_id: String.t(),
puuid: String.t(),
summoner_spell_id: :integer
}
match_id: String.t(),
champion_id: String.t(),
puuid: String.t(),
summoner_spell_id: :integer
}
@spec insert(insert_attrs()) :: any()
def insert(attrs) do
@@ -33,11 +34,17 @@ defmodule LolAnalytics.Facts.ChampionPickedSummonerSpell.Repo do
|> IO.inspect()
end
@spec get_champion_spells_by_win_rate(String.t()) :: list()
def get_champion_spells_by_win_rate(championId) do
end
def get_champion_picked_summoners() do
query =
from f in Schema,
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:
@@ -48,13 +55,23 @@ defmodule LolAnalytics.Facts.ChampionPickedSummonerSpell.Repo do
f.is_win
),
id: f.champion_id,
spell_id: f.spell_id,
name: c.name,
spell_id: f.summoner_spell_id,
metadata: s.metadata,
champion_name: c.name,
champion_id: c.champion_id,
image: c.image,
team_position: f.team_position,
total_games: count("*")
},
group_by: [f.champion_id, f.spell_id, c.image, c.name, f.team_position]
group_by: [
f.champion_id,
f.summoner_spell_id,
s.metadata,
c.image,
c.name,
c.champion_id,
f.team_position
]
Repo.all(query)
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