Skip to content

Commit

Permalink
Pull log parser out of API (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaldengeki authored Jul 20, 2024
1 parent 19aa928 commit a024281
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 58 deletions.
1 change: 1 addition & 0 deletions ark_nova_stats/api/gql/types/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ py_library(
deps = [
"//ark_nova_stats:config_py",
"//ark_nova_stats:models_py",
"//ark_nova_stats/bga_log_parser:game_log",
"@py_deps//flask",
"@py_deps//graphql_core",
],
Expand Down
5 changes: 3 additions & 2 deletions ark_nova_stats/api/gql/types/game_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
GraphQLString,
)

from ark_nova_stats.bga_log_parser.game_log import GameLog as ParsedGameLog
from ark_nova_stats.config import app, db
from ark_nova_stats.models import GameLog, GameLogContainerJSON
from ark_nova_stats.models import GameLog


def game_log_fields() -> dict[str, GraphQLField]:
Expand Down Expand Up @@ -62,7 +63,7 @@ def submit_game_logs(
args: dict[str, Any],
) -> GameLog:
json_logs = json.loads(args["logs"])
parsed_logs = GameLogContainerJSON(**json_logs)
parsed_logs = ParsedGameLog(**json_logs)

# For now, just store the literal log string.
log = GameLog(log=args["logs"])
Expand Down
7 changes: 1 addition & 6 deletions ark_nova_stats/api/tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,11 @@ py_library(
],
)

filegroup(
name = "fixture_files",
srcs = glob(["fixtures/*"]),
)

py_test(
name = "game_log_test",
srcs = ["game_log_test.py"],
data = [
":fixture_files",
"//ark_nova_stats/bga_log_parser:fixtures",
],
deps = [
":fixtures",
Expand Down
2 changes: 1 addition & 1 deletion ark_nova_stats/api/tests/game_log_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
def test_submit_game_logs(client: FlaskClient) -> None:
r = Runfiles.Create()
sample_game_fixture = r.Rlocation(
"_main/ark_nova_stats/api/tests/fixtures/sample_game.log.json"
"_main/ark_nova_stats/bga_log_parser/fixtures/sample_game.log.json"
)
with open(sample_game_fixture, "r") as sample_game_logfile:
game_log = sample_game_logfile.read().strip()
Expand Down
24 changes: 24 additions & 0 deletions ark_nova_stats/bga_log_parser/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load("@rules_python//python:defs.bzl", "py_library", "py_test")

filegroup(
name = "fixtures",
srcs = glob(["fixtures/*"]),
visibility = ["//ark_nova_stats:__subpackages__"],
)

py_library(
name = "game_log",
srcs = ["game_log.py"],
visibility = ["//:__subpackages__"],
)

py_test(
name = "game_log_test",
srcs = ["game_log_test.py"],
data = [":fixtures"],
deps = [
":game_log",
"@py_deps//pytest",
"@rules_python//python/runfiles",
],
)
Empty file.
54 changes: 54 additions & 0 deletions ark_nova_stats/bga_log_parser/game_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from dataclasses import dataclass
from typing import Optional


@dataclass
class GameLogEntryData:
uid: str
type: str
log: str
args: dict
lock_uuid: Optional[str] = None
synchro: Optional[int] = None
h: Optional[str] = None


@dataclass
class GameLogEntry:
channel: str
table_id: str
packet_id: str
packet_type: str
move_id: str
time: str
data: list[GameLogEntryData]

def __post_init__(self):
self.data = [GameLogEntryData(**x) for x in self.data] # type: ignore


@dataclass
class GameLogPlayer:
id: int
color: str
name: str
avatar: str


@dataclass
class GameLogData:
logs: list[GameLogEntry]
players: list[GameLogPlayer]

def __post_init__(self):
self.players = [GameLogPlayer(**x) for x in self.players] # type: ignore
self.logs = [GameLogEntry(**x) for x in self.logs] # type: ignore


@dataclass
class GameLog:
status: int
data: GameLogData

def __post_init__(self):
self.data = GameLogData(**self.data) # type: ignore
23 changes: 23 additions & 0 deletions ark_nova_stats/bga_log_parser/game_log_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import json
import sys

import pytest
from python.runfiles import Runfiles

from ark_nova_stats.bga_log_parser.game_log import GameLog


class TestGameLog:
def test_parses_sample_game(self):
r = Runfiles.Create()
sample_game_fixture = r.Rlocation(
"_main/ark_nova_stats/bga_log_parser/fixtures/sample_game.log.json"
)
with open(sample_game_fixture, "r") as sample_game_logfile:
game_log = json.loads(sample_game_logfile.read().strip())

x = GameLog(**game_log)


if __name__ == "__main__":
sys.exit(pytest.main([__file__] + sys.argv[1:]))
49 changes: 0 additions & 49 deletions ark_nova_stats/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,6 @@
from ark_nova_stats.config import db


@dataclasses.dataclass
class GameLogDataLogDataJSON:
uid: str
type: str
log: str
synchro: int
args: dict


@dataclasses.dataclass
class GameLogDataLogJSON:
channel: str
table_id: str
packet_id: str
packet_type: str
move_id: str
time: str
data: list[GameLogDataLogDataJSON]

def __post_init__(self):
self.data = [GameLogDataLogDataJSON(**x) for x in self.data] # type: ignore


@dataclasses.dataclass
class GameLogDataPlayerJSON:
id: int
color: str
name: str
avatar: str


@dataclasses.dataclass
class GameLogDataJSON:
logs: list
players: list[GameLogDataPlayerJSON]

def __post_init__(self):
self.players = [GameLogDataPlayerJSON(**x) for x in self.players] # type: ignore


@dataclasses.dataclass
class GameLogContainerJSON:
status: int
data: GameLogDataJSON

def __post_init__(self):
self.data = GameLogDataJSON(**self.data) # type: ignore


class GameLog(db.Model):
__tablename__ = "game_logs"

Expand Down

0 comments on commit a024281

Please sign in to comment.