Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In worker, set enum value for archive type #335

Merged
merged 9 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ jobs:
run: |
bazel --bazelrc=.github/workflows/ci.bazelrc --bazelrc=.bazelrc run --build_tag_filters=manual --stamp --embed_label $(git rev-parse HEAD) //ark_nova_stats/api:api_image_image_dockerhub
bazel --bazelrc=.github/workflows/ci.bazelrc --bazelrc=.bazelrc run --build_tag_filters=manual --stamp --embed_label $(git rev-parse HEAD) //ark_nova_stats/frontend:production_cross_platform_image_dockerhub
bazel --bazelrc=.github/workflows/ci.bazelrc --bazelrc=.bazelrc run --build_tag_filters=manual --stamp --embed_label $(git rev-parse HEAD) //ark_nova_stats/worker:worker_image_dockerhub
deploy_ark_nova_stats:
needs: push_ark_nova_stats
if: ${{ github.ref == 'refs/heads/main' }}
Expand All @@ -68,6 +69,8 @@ jobs:
working-directory: ark_nova_stats/api
- run: flyctl deploy
working-directory: ark_nova_stats/frontend
- run: flyctl deploy
working-directory: ark_nova_stats/worker
push_fitbit_challenges:
needs: build
if: ${{ github.ref == 'refs/heads/main' }}
Expand Down
1 change: 1 addition & 0 deletions ark_nova_stats/api/gql/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ py_library(
deps = [
"//ark_nova_stats:models_py",
"//ark_nova_stats/api/gql/types:game_log",
"@py_deps//flask",
"@py_deps//graphql_core",
],
)
3 changes: 2 additions & 1 deletion ark_nova_stats/api/gql/schema.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import flask
from graphql import GraphQLObjectType, GraphQLSchema

from ark_nova_stats.api.gql.types.game_log import (
Expand All @@ -10,7 +11,7 @@
from ark_nova_stats.models import GameLog, User


def Schema(app):
def Schema(app: flask.Flask):
return GraphQLSchema(
query=GraphQLObjectType(
name="Query",
Expand Down
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 @@ -9,5 +9,6 @@ py_library(
"//ark_nova_stats:models_py",
"//ark_nova_stats/bga_log_parser:game_log",
"@py_deps//graphql_core",
"@py_deps//sqlalchemy",
],
)
46 changes: 46 additions & 0 deletions ark_nova_stats/api/gql/types/game_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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 as GameLogModel
from ark_nova_stats.models import GameLogArchiveType
from ark_nova_stats.models import User as UserModel


Expand Down Expand Up @@ -238,3 +239,48 @@ def stats_field(
args={},
resolve=lambda root, info, **args: fetch_stats(game_log_model, user_model),
)


def game_log_archive_fields() -> dict[str, GraphQLField]:
archive_types = set(t.name for t in GameLogArchiveType)
return {
"id": GraphQLField(
GraphQLNonNull(GraphQLInt),
description="ID of game log archive.",
),
"archiveType": GraphQLField(
GraphQLNonNull(GraphQLString),
description=f"Type of game logs. Possible values are: {', '.join(archive_types)}.",
),
"url": GraphQLField(
GraphQLNonNull(GraphQLString),
description=f"URL of archive.",
),
"sizeBytes": GraphQLField(
GraphQLNonNull(GraphQLInt),
description=f"Size of archive, in bytes.",
),
"numGameLogs": GraphQLField(
GraphQLNonNull(GraphQLInt),
description=f"Number of game logs in this archive.",
),
"numUsers": GraphQLField(
GraphQLNonNull(GraphQLInt),
description=f"Number of users in this archive.",
),
"maxGameLog": GraphQLField(
GraphQLNonNull(GraphQLInt),
description=f"The game log with the highest BGA table ID.",
),
"createdAt": GraphQLField(
GraphQLInt,
description="UNIX timestamp for when this archive was created.",
),
}


game_log_archive_type = GraphQLObjectType(
"GameLogArchive",
description="An archive of game logs.",
fields=game_log_archive_fields,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""alter-game-log-archives-type-field-enum

Revision ID: fef90e3dfd97
Revises: 89684eb58df8
Create Date: 2024-08-03 18:31:10.362460

"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "fef90e3dfd97"
down_revision = "89684eb58df8"
branch_labels = None
depends_on = None


def upgrade():
op.drop_column("game_log_archives", "archive_type")
op.add_column(
"game_log_archives",
sa.Column(
"archive_type",
sa.Integer,
nullable=False,
),
)


def downgrade():
op.drop_column("game_log_archives", "archive_type")
op.add_column(
"game_log_archives",
sa.Column(
"archive_type",
sa.UnicodeText,
nullable=False,
),
)
10 changes: 9 additions & 1 deletion ark_nova_stats/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import enum

from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
Expand Down Expand Up @@ -92,9 +93,16 @@ class User(db.Model): # type: ignore
)


class GameLogArchiveType(enum.Enum):
GAME_LOG_ARCHIVE_TYPE_UNKNOWN = 0
RAW_BGA_JSONL = 1


class GameLogArchive(db.Model): # type: ignore
__tablename__ = "game_log_archives"

id: Mapped[int] = mapped_column(primary_key=True)
archive_type: Mapped[str]
archive_type: Mapped[int]
url: Mapped[str]
size_bytes: Mapped[int]
num_game_logs: Mapped[int]
Expand Down
2 changes: 2 additions & 0 deletions ark_nova_stats/worker/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ py_binary(
visibility = ["//:__subpackages__"],
deps = [
"//ark_nova_stats:config_py",
"//ark_nova_stats:models_py",
"@py_deps//boto3",
"@py_deps//sqlalchemy",
],
)

Expand Down
29 changes: 15 additions & 14 deletions ark_nova_stats/worker/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
from sqlalchemy import desc

from ark_nova_stats.config import app, db
from ark_nova_stats.models import GameLog, GameLogArchive, User
from ark_nova_stats.models import GameLog, GameLogArchive, GameLogArchiveType, User

max_delay = 10
max_delay = 12 * 60 * 60

logging.basicConfig(
format="[%(asctime)s][%(levelname)s] %(message)s", level=logging.WARNING
format="[%(asctime)s][%(levelname)s] %(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)

Expand All @@ -29,19 +29,20 @@ def archive_logs_to_tigris(
last_archive: GameLogArchive = GameLogArchive.query.order_by(
desc(GameLogArchive.created_at)
).first()
time_since_last_archive = datetime.datetime.now() - last_archive.created_at
if last_archive is not None and time_since_last_archive < min_interval:
logger.debug(
f"Last archive was uploaded at {last_archive.created_at}, which was {time_since_last_archive} ago; skipping."
)
return None
if last_archive is not None:
time_since_last_archive = datetime.datetime.now() - last_archive.created_at
if time_since_last_archive < min_interval:
logger.debug(
f"Last archive was uploaded at {last_archive.created_at}, which was {time_since_last_archive} ago; skipping."
)
return None

# Retrieve all the game logs so we can serialize them.
all_logs: list[GameLog] = GameLog.query.all()
users: set[str] = set()
last_game_log: Optional[GameLog] = None
log_list = []
archive_type = "raw_bga_jsonl"
archive_type = GameLogArchiveType.RAW_BGA_JSONL

# Assemble a list of the game logs and compress them using gzip.
for game_log in all_logs:
Expand All @@ -55,17 +56,17 @@ def archive_logs_to_tigris(
compressed_jsonl = gzip.compress("\n".join(log_list).encode("utf-8"))
size_bytes = len(compressed_jsonl)
filename = (
archive_type
archive_type.name
+ "_"
+ datetime.datetime.now(tz=datetime.timezone.utc).strftime("%Y_%M_%d")
+ datetime.datetime.now(tz=datetime.timezone.utc).strftime("%Y_%m_%d")
+ ".gz"
)

# Upload the compressed gzip jsonl to Tigris.
tigris_client.upload_fileobj(
io.BytesIO(compressed_jsonl),
os.getenv("BUCKET_NAME"),
archive_type + "/" + filename,
archive_type.name + "/" + filename,
)
url = f"{os.getenv('TIGRIS_CUSTOM_DOMAIN_HOST')}/{filename}"
logger.info(f"Uploaded game log archive at: {url} with size: {size_bytes}")
Expand All @@ -78,7 +79,7 @@ def archive_logs_to_tigris(

new_archive = GameLogArchive(
url=url,
archive_type=archive_type,
archive_type=archive_type.value,
size_bytes=size_bytes,
num_game_logs=len(all_logs),
num_users=len(users),
Expand Down
2 changes: 1 addition & 1 deletion ark_nova_stats/worker/fly.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# fly.toml app configuration file generated for worker-red-log-3531 on 2024-07-31T21:33:15-04:00
# fly.toml app configuration file generated for ark-nova-stats-worker on 2024-08-03T19:09:45-04:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#
Expand Down