created schema, repo... for analytic tables
Some checks are pending
ci / docker (push) Waiting to run
Some checks are pending
ci / docker (push) Waiting to run
This commit is contained in:
parent
dacb9ad8fc
commit
520c234a94
@ -1,13 +1,19 @@
|
|||||||
defmodule LolAnalytics.Analyzer.ChampionAnalyzer do
|
defmodule LolAnalytics.Analyzer.ChampionAnalyzer do
|
||||||
alias Hex.HTTP
|
alias LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameSchema
|
||||||
@behaviour LolAnalytics.Analyzer
|
@behaviour LolAnalytics.Analyzer
|
||||||
|
|
||||||
def analyze_all_matches do
|
def analyze_all_matches do
|
||||||
Storage.MatchStorage.S3MatchStorage.list_files("ranked")
|
Storage.MatchStorage.S3MatchStorage.stream_files("ranked")
|
||||||
|> Enum.map(& &1.key)
|
|> Enum.each(fn %{key: path} ->
|
||||||
|> Enum.each(fn path ->
|
IO.inspect(path)
|
||||||
LolAnalytics.Analyzer.ChampionAnalyzer.analyze(:url, "http://localhost:9000/ranked/#{path}")
|
LolAnalytics.Analyzer.ChampionAnalyzer.analyze(:url, "http://192.168.1.55:9000/ranked/#{path}")
|
||||||
end)
|
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
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@ -30,12 +36,16 @@ defmodule LolAnalytics.Analyzer.ChampionAnalyzer do
|
|||||||
participants
|
participants
|
||||||
|> Enum.each(fn participant = %LoLAPI.Model.Participant{} ->
|
|> Enum.each(fn participant = %LoLAPI.Model.Participant{} ->
|
||||||
if participant.teamPosition != "" do
|
if participant.teamPosition != "" do
|
||||||
LolAnalytics.ChampionWinRate.ChampionWinRateRepo.add_champion_win_rate(
|
attrs = %{
|
||||||
participant.championId,
|
champion_id: participant.championId,
|
||||||
version,
|
match_id: decoded_match.metadata.matchId,
|
||||||
participant.teamPosition,
|
is_win: participant.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)
|
end)
|
||||||
end
|
end
|
||||||
|
@ -0,0 +1,24 @@
|
|||||||
|
defmodule LolAnalytics.Dimensions.Champion.ChampionRepo do
|
||||||
|
import Ecto.Query
|
||||||
|
|
||||||
|
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 list_champions() do
|
||||||
|
Repo.all(ChampionSchema)
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,15 @@
|
|||||||
|
defmodule LolAnalytics.Dimensions.Champion.ChampionSchema do
|
||||||
|
use Ecto.Schema
|
||||||
|
import Ecto.Changeset
|
||||||
|
|
||||||
|
schema "dim_champion" do
|
||||||
|
field :champion_id, :integer
|
||||||
|
timestamps()
|
||||||
|
end
|
||||||
|
|
||||||
|
def changeset(champion = %__MODULE__{}, attrs \\ %{}) do
|
||||||
|
champion
|
||||||
|
|> cast(attrs, [:champion_id])
|
||||||
|
|> validate_required([:champion_id])
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,21 @@
|
|||||||
|
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)
|
||||||
|
|
||||||
|
case item 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
|
||||||
|
end
|
@ -0,0 +1,15 @@
|
|||||||
|
defmodule LolAnalytics.Dimensions.Item.ItemSchema do
|
||||||
|
use Ecto.Schema
|
||||||
|
import Ecto.Changeset
|
||||||
|
|
||||||
|
schema "dim_item" do
|
||||||
|
field :item_id, :integer
|
||||||
|
timestamps()
|
||||||
|
end
|
||||||
|
|
||||||
|
def changeset(item = %__MODULE__{}, attrs \\ %{}) do
|
||||||
|
item
|
||||||
|
|> cast(attrs, [:item_id])
|
||||||
|
|> validate_required([:item_id])
|
||||||
|
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,26 @@
|
|||||||
|
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)
|
||||||
|
|
||||||
|
case patch 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,15 @@
|
|||||||
|
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])
|
||||||
|
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
|
||||||
|
player = Repo.one(query)
|
||||||
|
|
||||||
|
case player 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,15 @@
|
|||||||
|
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])
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,26 @@
|
|||||||
|
defmodule LolAnalytics.Dimensions.SummonerSpell.SummonerSpellRepo do
|
||||||
|
alias LolAnalytics.Dimensions.SummonerSpell.SummonerSpellSchema
|
||||||
|
alias LoLAnalytics.Repo
|
||||||
|
|
||||||
|
def get_or_create(spell_id) do
|
||||||
|
spell = Repo.get(SummonerSpellSchema, spell_id: spell_id)
|
||||||
|
|
||||||
|
case spell do
|
||||||
|
nil ->
|
||||||
|
spell_changeset =
|
||||||
|
SummonerSpellSchema.changeset(
|
||||||
|
%SummonerSpellSchema{},
|
||||||
|
%{spell_id: spell_id}
|
||||||
|
)
|
||||||
|
|
||||||
|
Repo.insert(spell_changeset)
|
||||||
|
|
||||||
|
spell ->
|
||||||
|
spell
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def list_spells() do
|
||||||
|
Repo.all(SummonerSpellSchema)
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,15 @@
|
|||||||
|
defmodule LolAnalytics.Dimensions.SummonerSpell.SummonerSpellSchema do
|
||||||
|
use Ecto.Schema
|
||||||
|
import Ecto.Changeset
|
||||||
|
|
||||||
|
schema "dim_summoner_spell" do
|
||||||
|
field :spell_id, :integer
|
||||||
|
timestamps()
|
||||||
|
end
|
||||||
|
|
||||||
|
def changeset(summoner_spell = %__MODULE__{}, attrs) do
|
||||||
|
summoner_spell
|
||||||
|
|> cast(attrs, [:spell_id])
|
||||||
|
|> validate_required([:spell_id])
|
||||||
|
end
|
||||||
|
end
|
@ -1,3 +1,27 @@
|
|||||||
defmodule LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameRepo do
|
defmodule LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameRepo do
|
||||||
|
import Ecto.Query
|
||||||
|
|
||||||
|
alias LolAnalytics.Dimensions.Player.PlayerRepo
|
||||||
|
alias LolAnalytics.Dimensions.Champion.ChampionRepo
|
||||||
|
alias LolAnalytics.Dimensions.Match.MatchRepo
|
||||||
|
alias LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameSchema
|
||||||
|
alias LolAnalytics.Facts.ChampionPlayedGame
|
||||||
|
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)
|
||||||
|
IO.puts(">>>>")
|
||||||
|
IO.inspect(attrs)
|
||||||
|
changeset = ChampionPlayedGameSchema.changeset(%ChampionPlayedGameSchema{}, attrs)
|
||||||
|
|
||||||
|
IO.inspect(changeset)
|
||||||
|
Repo.insert(changeset)
|
||||||
|
# Repo.insert(match)
|
||||||
|
end
|
||||||
|
|
||||||
|
def list_played_matches() do
|
||||||
|
Repo.all(ChampionPlayedGameSchema)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,11 +1,33 @@
|
|||||||
defmodule LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameSchema do
|
defmodule LolAnalytics.Facts.ChampionPlayedGame.ChampionPlayedGameSchema do
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
|
|
||||||
|
import Ecto.Changeset
|
||||||
|
|
||||||
|
@casting_attrs [
|
||||||
|
:champion_id,
|
||||||
|
:match_id,
|
||||||
|
:is_win,
|
||||||
|
:game_length_seconds,
|
||||||
|
:team_position,
|
||||||
|
:puuid,
|
||||||
|
:queue_id
|
||||||
|
]
|
||||||
|
|
||||||
schema "fact_champion_played_game" do
|
schema "fact_champion_played_game" do
|
||||||
field :champion_id, :integer
|
field :champion_id, :integer
|
||||||
field :match_id, :integer
|
field :match_id, :string
|
||||||
field :is_win, :boolean
|
field :is_win, :boolean
|
||||||
field :game_length_seconds, :integer
|
field :game_length_seconds, :integer
|
||||||
|
field :team_position, :string
|
||||||
|
field :puuid, :string
|
||||||
field :queue_id, :integer
|
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
|
||||||
end
|
end
|
||||||
|
@ -42,6 +42,7 @@ defmodule LoLAnalytics.MixProject do
|
|||||||
{:postgrex, ">= 0.0.0"},
|
{:postgrex, ">= 0.0.0"},
|
||||||
{:jason, "~> 1.2"},
|
{:jason, "~> 1.2"},
|
||||||
{:lol_api, in_umbrella: true},
|
{:lol_api, in_umbrella: true},
|
||||||
|
{:storage, in_umbrella: true},
|
||||||
{:httpoison, "~> 2.2"},
|
{:httpoison, "~> 2.2"},
|
||||||
{:poison, "~> 5.0"}
|
{:poison, "~> 5.0"}
|
||||||
]
|
]
|
||||||
|
@ -2,50 +2,61 @@ defmodule LoLAnalytics.Repo.Migrations.AnalyticsTables do
|
|||||||
use Ecto.Migration
|
use Ecto.Migration
|
||||||
|
|
||||||
def change do
|
def change do
|
||||||
create table("dom_champion") do
|
create table("dim_champion") do
|
||||||
add :champion_id, :integer, primary_key: true
|
add :champion_id, :integer, primary_key: true, null: false
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
create table("dom_match") do
|
create index("dim_champion", [:champion_id], unique: true)
|
||||||
add :match_id, :integer, primary_key: true
|
|
||||||
|
create table("dim_item") do
|
||||||
|
add :item_id, :integer, primary_key: true, null: false
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
create index("dom_match", [:match_id], unique: true)
|
create index("dim_item", [:item_id], unique: true)
|
||||||
|
|
||||||
create table("dom_patch") do
|
create table("dim_match") do
|
||||||
add :patch_number, :string, primary_key: true
|
add :match_id, :string, primary_key: true, null: false
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
create index("dom_patch", [:patch_number], unique: true)
|
create index("dim_match", [:match_id], unique: true)
|
||||||
|
|
||||||
create table("dom_item") do
|
create table("dim_patch") do
|
||||||
add :item_id, :integer, primary_key: true
|
add :patch_number, :string, primary_key: true, null: false
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
create index("dom_item", [:item_id], unique: true)
|
create index("dim_patch", [:patch_number], unique: true)
|
||||||
|
|
||||||
create table("dom_summoner_spell") do
|
create table("dim_player") do
|
||||||
add :spell_id, :integer, primary_key: true
|
add :puuid, :string, primary_key: true, null: false
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
create index("dom_summoner_spell", [:spell_id], unique: true)
|
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
|
create table("fact_champion_played_game") do
|
||||||
add :champion_id, references("dom_champion", with: [champion_id: :champion_id]),
|
add :champion_id, references("dim_champion", column: :champion_id, type: :integer)
|
||||||
primary_key: true
|
add :match_id, references("dim_match", column: :match_id, type: :string)
|
||||||
|
|
||||||
add :match_id, references("dom_match", with: [match_id: :match_id])
|
|
||||||
add :is_win, :boolean
|
add :is_win, :boolean
|
||||||
add :game_length_seconds, :integer
|
add :game_length_seconds, :integer
|
||||||
add :queue_id, :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()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
create index("fact_champion_played_game", [:id, :champion_id, :queue_id])
|
create index("fact_champion_played_game", [:id, :champion_id, :queue_id])
|
||||||
|
create index("fact_champion_played_game", [:puuid, :match_id], unique: true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
10
queries.md
Normal file
10
queries.md
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
```
|
||||||
|
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;
|
||||||
|
```
|
Loading…
x
Reference in New Issue
Block a user