Create fact for champion picked item
This commit is contained in:
parent
0987290dca
commit
7e30db3864
@ -2,10 +2,12 @@ defmodule LolAnalytics.Dimensions.Item.ItemRepo do
|
|||||||
alias LolAnalytics.Dimensions.Item.ItemSchema
|
alias LolAnalytics.Dimensions.Item.ItemSchema
|
||||||
alias LoLAnalytics.Repo
|
alias LoLAnalytics.Repo
|
||||||
|
|
||||||
def get_or_create(item_id) do
|
import Ecto.Query
|
||||||
item = Repo.get(ItemSchema, item_id: item_id)
|
|
||||||
|
|
||||||
case item do
|
def get_or_create(item_id) do
|
||||||
|
query = from i in ItemSchema, where: i.item_id == ^item_id
|
||||||
|
|
||||||
|
case Repo.one(query) do
|
||||||
nil ->
|
nil ->
|
||||||
item_changeset = ItemSchema.changeset(%ItemSchema{}, %{item_id: item_id})
|
item_changeset = ItemSchema.changeset(%ItemSchema{}, %{item_id: item_id})
|
||||||
Repo.insert(item_changeset)
|
Repo.insert(item_changeset)
|
||||||
|
@ -0,0 +1,2 @@
|
|||||||
|
defmodule LolAnalytics.Facts.ChhampionPickedItem.FactProcessor do
|
||||||
|
end
|
@ -0,0 +1,87 @@
|
|||||||
|
defmodule LolAnalytics.Facts.ChampionPickedItem.Repo do
|
||||||
|
import Ecto.Query
|
||||||
|
|
||||||
|
alias LolAnalytics.Dimensions.Item.ItemSchema
|
||||||
|
alias LolAnalytics.Dimensions.Champion.ChampionSchema
|
||||||
|
alias LolAnalytics.Facts.ChhampionPickedItem.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
|
||||||
|
IO.inspect(attrs)
|
||||||
|
_match = MatchRepo.get_or_create(attrs.match_id)
|
||||||
|
_champion = ChampionRepo.get_or_create(attrs.champion_id)
|
||||||
|
_player = PlayerRepo.get_or_create(attrs.puuid)
|
||||||
|
_patch = PlayerRepo.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()) :: list()
|
||||||
|
def get_champion_picked_items(champion_id, team_position) 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: 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
|
||||||
|
),
|
||||||
|
item_id: i.item_id,
|
||||||
|
champion_id: c.champion_id,
|
||||||
|
team_position: f.team_position,
|
||||||
|
total_games: count("*")
|
||||||
|
},
|
||||||
|
group_by: [
|
||||||
|
i.item_id,
|
||||||
|
c.champion_id,
|
||||||
|
f.team_position
|
||||||
|
]
|
||||||
|
|
||||||
|
Repo.all(query)
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,34 @@
|
|||||||
|
defmodule LolAnalytics.Facts.ChhampionPickedItem.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,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
|
Loading…
x
Reference in New Issue
Block a user