Skip to content

Commit

Permalink
move storage and api to apps, create base analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarogirona committed May 10, 2024
1 parent 987339f commit 2e56c71
Show file tree
Hide file tree
Showing 42 changed files with 401 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ erl_crash.dump

.env


.DS_store
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule LolAnalytics.Analyzer.BaseAnalyzer do
@callback analyze(:url, path :: String.t()) :: :ok
end
32 changes: 32 additions & 0 deletions apps/lol_analytics/lib/lol_analytics/analyzer/champion_analyzer.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule LolAnalytics.Analyzer.ChampionAnalyzer do
alias Hex.HTTP
@behaviour LolAnalytics.Analyzer.BaseAnalyzer

@doc """
iex> LolAnalytics.Analyzer.ChampionAnalyzer.analyze(:url, "https://na1.api.riotgames.com/lol/match/v4/match/234567890123456789")
:ok
"""
@impl true
@spec analyze(atom(), String.t()) :: :ok
def analyze(:url, path) do
data = HTTPoison.get!(path)
analyze(:data, data.body)
:ok
end

@doc """
iex> LolAnalytics.Analyzer.ChampionAnalyzer.analyze(:url, "http://localhost:9000/ranked/14.9.580.2108/EUW1_6923309745.json")
"""
@impl true
@spec analyze(atom(), any()) :: list(LoLAPI.Model.Participant.t())
def analyze(:data, data) do
decoded = Poison.decode!(data)

%{"info" => %{"participants" => participants}} = decoded

participants
|> Enum.each(fn %{"win" => win, "championId" => champion_id} ->
IO.inspect(%{win: win, champion_id: champion_id})
end)
end
end
4 changes: 3 additions & 1 deletion apps/lol_analytics/lib/lol_analytics/player/player_repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ defmodule LolAnalytics.Player.PlayerRepo do

def get_player(puuid) do
query = from p in PlayerSchema, where: p.puuid == ^puuid
LoLAnalytics.Repo.one(query)

LoLAnalytics.Repo.all(query)
|> List.first()
end

@spec insert_player(String.t(), keyword()) :: %PlayerSchema{}
Expand Down
5 changes: 4 additions & 1 deletion apps/lol_analytics/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ defmodule LoLAnalytics.MixProject do
{:phoenix_pubsub, "~> 2.1"},
{:ecto_sql, "~> 3.10"},
{:postgrex, ">= 0.0.0"},
{:jason, "~> 1.2"}
{:jason, "~> 1.2"},
{:lol_api, in_umbrella: true},
{:httpoison, "~> 2.2"},
{:poison, "~> 5.0"}
]
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule LoLAnalytics.Repo.Migrations.Player do
timestamps()
end

unique_index("player", :puuid)
create index(:player, [:puuid])
end
end
4 changes: 4 additions & 0 deletions apps/lol_api/.formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
26 changes: 26 additions & 0 deletions apps/lol_api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
lol_api-*.tar

# Temporary files, for example, from tests.
/tmp/
21 changes: 21 additions & 0 deletions apps/lol_api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# LoLAPI

**TODO: Add description**

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `lol_api` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[
{:lol_api, "~> 0.1.0"}
]
end
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at <https://hexdocs.pm/lol_api>.

4 changes: 4 additions & 0 deletions apps/lol_api/config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Config

config :lol_api,
riot_api_key: System.get_env("RIOT_API_KEY")
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule Scrapper.Api.AccountApi do
defmodule LoLAPI.AccountApi do
require Logger

@get_puuid_endpoint "https://europe.api.riotgames.com/riot/account/v1/accounts/by-riot-id/%{gameName}/%{tagLine}"

@spec get_puuid(String.t(), String.t()) :: {:ok, String.t()} | {:error, String.t()}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
defmodule Scrapper.Data.Api.MatchApi do
import Logger
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"

@doc """
Get match by id
iex> Scrapper.Data.MatchApi.get_match_by_id("EUW1_6921743825")
iex> LoLAPI.MatchApi.get_match_by_id("EUW1_6921743825")
"""
@spec get_match_by_id(String.t()) :: %Scrapper.Api.Model.MatchResponse{}
@spec get_match_by_id(String.t()) :: %LoLAPI.Model.MatchResponse{}
def get_match_by_id(match_id) do
url = String.replace(@match_base_endpoint, "%{matchid}", match_id)
Logger.info("Making request to #{url}")
Expand All @@ -29,7 +29,7 @@ defmodule Scrapper.Data.Api.MatchApi do
@doc """
Get matches from player
iex> Scrapper.Data.Api.MatchApi.get_matches_from_player "JB6TdEWlKjZwnbgdSzOogYepNfjLPdUh68S8b4kUu4EEZy4R4MMAgv92QMj1XgVjtzHmZVLaOW7mzg"
iex> LoLAPI.MatchApi.get_matches_from_player "JB6TdEWlKjZwnbgdSzOogYepNfjLPdUh68S8b4kUu4EEZy4R4MMAgv92QMj1XgVjtzHmZVLaOW7mzg"
"""
@spec get_matches_from_player(String.t()) :: list(String.t()) | integer()
def get_matches_from_player(puuid) do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Scrapper.Api.Model.Info do
alias Scrapper.Api.Model.Participant
defmodule LoLAPI.Model.Info do
alias LoLAPI.Model.Participant

defstruct endOfGameResult: "",
gameCreation: "",
Expand Down
6 changes: 6 additions & 0 deletions apps/lol_api/lib/api/model/match_response.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule LoLAPI.Model.MatchResponse do
alias LoLAPI.Model.{Info, Metadata}

defstruct metadata: %Metadata{},
info: %Info{}
end
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
defmodule Scrapper.Api.Model.Metadata do
defmodule LoLAPI.Model.Metadata do
defstruct [:dataVersion, :matchId, :participants]
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Scrapper.Api.Model.Participant do
defmodule LoLAPI.Model.Participant do
# Enum.map(participant, fn {k,_v} -> ":#{k}" end) |> Enum.join(", ")
defstruct [
:onMyWayPings,
Expand Down
18 changes: 18 additions & 0 deletions apps/lol_api/lib/lol_api.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule LoLAPI do
@moduledoc """
Documentation for `LoLAPI`.
"""

@doc """
Hello world.
## Examples
iex> LoLAPI.hello()
:world
"""
def hello do
:world
end
end
35 changes: 35 additions & 0 deletions apps/lol_api/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule LoLAPI.MixProject do
use Mix.Project

def project do
[
app: :lol_api,
version: "0.1.0",
build_path: "../../_build",
config_path: "../../config/config.exs",
deps_path: "../../deps",
lockfile: "../../mix.lock",
elixir: "~> 1.16",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:httpoison, "~> 2.2"},
{:poison, "~> 5.0"}
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"},
# {:sibling_app_in_umbrella, in_umbrella: true}
]
end
end
8 changes: 8 additions & 0 deletions apps/lol_api/test/lo_lapi_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule LoLAPITest do
use ExUnit.Case
doctest LoLAPI

test "greets the world" do
assert LoLAPI.hello() == :world
end
end
1 change: 1 addition & 0 deletions apps/lol_api/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExUnit.start()
6 changes: 0 additions & 6 deletions apps/scrapper/lib/scrapper/api/model/match_response.ex

This file was deleted.

31 changes: 31 additions & 0 deletions apps/scrapper/lib/scrapper/match_classifier.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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_matches()
total_matches = Enum.count(matches)

matches
|> Enum.with_index(fn match, index -> {match, index} end)
|> Scrapper.Parallel.peach(fn {match, index} ->
%{key: json_file} = match
[key | _] = String.split(json_file, ".")
Logger.info("Match at #{index} of #{total_matches} is classified")
response = HTTPoison.get!("http://localhost:9000/matches/#{key}.json", [], timeout: 5000)
%{"info" => %{"gameVersion" => gameVersion}} = Poison.decode!(response.body)
Storage.MatchStorage.S3MatchStorage.store_match(key, response.body, "ranked", gameVersion)
match
end)
end

# pass functions, not data

def classify_match_by_queue(_) do
end
end
7 changes: 7 additions & 0 deletions apps/scrapper/lib/scrapper/parallel/Parallel.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Scrapper.Parallel do
def peach(enum, fun, concurrency \\ 10, 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
10 changes: 6 additions & 4 deletions apps/scrapper/lib/scrapper/processor/match_processor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule Scrapper.Processor.MatchProcessor do
]},
concurrency: 1,
rate_limiting: [
interval: 1000 * 1,
interval: 333 * 1,
allowed_messages: 1
]
],
Expand All @@ -36,15 +36,15 @@ defmodule Scrapper.Processor.MatchProcessor do
def handle_message(_, message = %Broadway.Message{}, _) do
match_id = message.data

resp = Scrapper.Data.Api.MatchApi.get_match_by_id(match_id)
resp = LoLAPI.MatchApi.get_match_by_id(match_id)
process_resp(resp, match_id)

message
end

def process_resp({:ok, raw_match}, match_id) do
decoded_match = Poison.decode!(raw_match, as: %Scrapper.Api.Model.MatchResponse{})
match_url = Scrapper.Storage.S3MatchStorage.store_match(match_id, raw_match)
decoded_match = Poison.decode!(raw_match, as: %LoLAPI.Model.MatchResponse{})
match_url = Storage.MatchStorage.S3MatchStorage.store_match(match_id, raw_match)
match = LolAnalytics.Match.MatchRepo.get_match(match_id)

case match do
Expand All @@ -59,6 +59,8 @@ defmodule Scrapper.Processor.MatchProcessor do
end

decoded_match.metadata.participants
|> Enum.shuffle()
|> Enum.take(2)
|> Enum.each(fn participant_puuid ->
Scrapper.Queue.PlayerQueue.queue_puuid(participant_puuid)
end)
Expand Down
2 changes: 1 addition & 1 deletion apps/scrapper/lib/scrapper/processor/player_processor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule Scrapper.Processor.PlayerProcessor do
update_player_processed(player)
end

match_history = Scrapper.Data.Api.MatchApi.get_matches_from_player(puuid)
match_history = LoLAPI.MatchApi.get_matches_from_player(puuid)

case match_history do
{:ok, matches} ->
Expand Down
4 changes: 0 additions & 4 deletions apps/scrapper/lib/scrapper/storage/match_storage.ex

This file was deleted.

9 changes: 3 additions & 6 deletions apps/scrapper/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@ defmodule Scrapper.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:httpoison, "~> 2.2"},
{:poison, "~> 5.0"},
{:ex_aws, "~> 2.1"},
{:ex_aws_s3, "~> 2.5.3"},
{:hackney, "~> 1.9"},
{:sweet_xml, "~> 0.6"},
{:broadway_rabbitmq, "~> 0.7"},
{:amqp, "~> 3.3"},
{:lol_analytics, in_umbrella: true}
{:lol_analytics, in_umbrella: true},
{:lol_api, in_umbrella: true},
{:storage, in_umbrella: true}
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"},
# {:sibling_app_in_umbrella, in_umbrella: true}
Expand Down
4 changes: 4 additions & 0 deletions apps/storage/.formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
Loading

0 comments on commit 2e56c71

Please sign in to comment.