Skip to content

Commit

Permalink
Add Ark Nova stats skeleton (#241)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
shaldengeki and pre-commit-ci[bot] authored Jul 15, 2024
1 parent df67166 commit 7fb575a
Show file tree
Hide file tree
Showing 38 changed files with 724 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
build --announce_rc
build --disk_cache=$HOME/.cache/bazel
build --repository_cache=$HOME/.cache/bazel-repo
build --build_tag_filters=-manual

test --test_output=errors
test --test_tag_filters=-manual
27 changes: 27 additions & 0 deletions ark_nova_stats/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
load("@rules_python//python:defs.bzl", "py_library", "py_test")

py_library(
name = "models_py",
srcs = ["models.py"],
visibility = ["//ark_nova_stats:__subpackages__"],
deps = [
":config_py",
"@py_deps//requests",
"@py_deps//sqlalchemy",
],
)

py_library(
name = "config_py",
srcs = ["config.py"],
visibility = ["//ark_nova_stats:__subpackages__"],
deps = [
"//base:flask_app_py",
],
)

py_test(
name = "models_test",
srcs = ["models_test.py"],
deps = [":models_py"],
)
Empty file added ark_nova_stats/__init__.py
Empty file.
23 changes: 23 additions & 0 deletions ark_nova_stats/api/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@rules_python//python:defs.bzl", "py_library")
load("//tools/build_rules:api_image.bzl", "api_image")

py_library(
name = "app",
srcs = ["app.py"],
visibility = ["//:__subpackages__"],
deps = [
"//ark_nova_stats:config_py",
"//ark_nova_stats:models_py",
"//ark_nova_stats/api/gql:schema",
"@py_deps//flask",
"@py_deps//graphql_server",
],
)

api_image(
name = "api_image",
app_package = "ark_nova_stats.api.app",
docker_hub_repository = "docker.io/shaldengeki/ark-nova-stats-api",
repo_tags = ["shaldengeki/ark-nova-stats-api:latest"],
deps = [":app"],
)
Empty file added ark_nova_stats/api/__init__.py
Empty file.
15 changes: 15 additions & 0 deletions ark_nova_stats/api/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import datetime
from datetime import timezone
from typing import Optional

from flask import abort, redirect, request, session
from graphql_server.flask import GraphQLView # type: ignore

from ark_nova_stats import models
from ark_nova_stats.api.gql import schema
from ark_nova_stats.config import app, db

app.add_url_rule(
"/graphql",
view_func=GraphQLView.as_view("graphql", schema=schema.Schema(app), graphiql=True),
)
12 changes: 12 additions & 0 deletions ark_nova_stats/api/gql/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@rules_python//python:defs.bzl", "py_library")

py_library(
name = "schema",
srcs = ["schema.py"],
visibility = ["//ark_nova_stats/api:__subpackages__"],
deps = [
"//ark_nova_stats:models_py",
"//ark_nova_stats/api/gql/types:example_model",
"@py_deps//graphql_core",
],
)
Empty file.
15 changes: 15 additions & 0 deletions ark_nova_stats/api/gql/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from graphql import GraphQLObjectType, GraphQLSchema

from ark_nova_stats.api.gql.types.example_model import example_model_field
from ark_nova_stats.models import ExampleModel


def Schema(app):
return GraphQLSchema(
query=GraphQLObjectType(
name="Query",
fields={
"testModel": example_model_field(ExampleModel),
},
),
)
13 changes: 13 additions & 0 deletions ark_nova_stats/api/gql/types/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@rules_python//python:defs.bzl", "py_library")

py_library(
name = "example_model",
srcs = ["example_model.py"],
visibility = ["//:__subpackages__"],
deps = [
"//ark_nova_stats:config_py",
"//ark_nova_stats:models_py",
"@py_deps//flask",
"@py_deps//graphql_core",
],
)
Empty file.
54 changes: 54 additions & 0 deletions ark_nova_stats/api/gql/types/example_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from typing import Any, Optional, Type

from flask import Flask
from graphql import (
GraphQLArgument,
GraphQLBoolean,
GraphQLField,
GraphQLFloat,
GraphQLInt,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
)

from ark_nova_stats.config import app, db
from ark_nova_stats.models import ExampleModel


def example_model_fields() -> dict[str, GraphQLField]:
return {
"id": GraphQLField(
GraphQLNonNull(GraphQLInt),
description="The id of the example model.",
),
}


example_model_type = GraphQLObjectType(
"ExampleModel",
description="A example model.",
fields=example_model_fields,
)


def fetch_example_model(
example_model: Type[ExampleModel], params: dict[str, Any]
) -> Optional[ExampleModel]:
return (example_model.query.filter(example_model.id == params["id"])).first()


example_model_filters: dict[str, GraphQLArgument] = {
"id": GraphQLArgument(
GraphQLNonNull(GraphQLInt),
description="ID of the example model.",
),
}


def example_model_field(example_model: type[ExampleModel]) -> GraphQLField:
return GraphQLField(
example_model_type,
args=example_model_filters,
resolve=lambda root, info, **args: fetch_example_model(example_model, args),
)
76 changes: 76 additions & 0 deletions ark_nova_stats/api/migrations/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
load("@py_deps//:requirements.bzl", "requirement")
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
load("//tools/build_rules:cross_platform_image.bzl", "cross_platform_image")
load("//tools/build_rules:py_layer.bzl", "py_oci_image")

py_library(
name = "migrate_lib",
srcs = glob(["**/*.py"]), # keep
imports = [".."],
visibility = ["//:__subpackages__"],
deps = [
requirement("alembic"),
requirement("Flask"),
],
)

py_binary(
name = "binary",
srcs = glob(["**/*.py"]), # keep
data = ["alembic.ini"],
imports = [".."],
main = "__main__.py",
visibility = ["//:__subpackages__"],
deps = [
"//ark_nova_stats:config_py",
"//scripts:wait_for_postgres", # keep
"@py_deps//flask_migrate",
"@rules_python//python/runfiles",
],
)

py_oci_image(
name = "base_image",
base = "@python3_image",
binary = ":binary",
cmd = [
"/ark_nova_stats/api/migrations/binary.runfiles/_main/scripts/wait_for_postgres",
"/ark_nova_stats/api/migrations/binary",
],
env = {
"FLASK_APP": "app.py",
"FLASK_DEBUG": "True",
"API_PORT": "5000",
"FRONTEND_PROTOCOL": "http",
"FRONTEND_HOST": "frontend",
"FRONTEND_PORT": "5001",
"DB_HOST": "pg",
"DB_USERNAME": "admin",
"DB_PASSWORD": "development",
"DATABASE_NAME": "api_development",
"FITBIT_CLIENT_ID": "testing",
"FITBIT_CLIENT_SECRET": "testing",
"FITBIT_VERIFICATION_CODE": "testing",
"FLASK_SECRET_KEY": "testing",
},
)

# $ bazel run //ark_nova_stats/api/migrations:image_tarball
# $ docker run --rm shaldengeki/ark-nova-stats-api-migrations:latest
cross_platform_image(
name = "image",
image = ":base_image",
repo_tags = ["shaldengeki/ark-nova-stats-api-migrations:latest"],
repository = "docker.io/shaldengeki/ark-nova-stats-api-migrations",
visibility = ["//ark_nova_stats/api/migrations:__subpackages__"],
)

py_library(
name = "env",
srcs = ["env.py"],
visibility = ["//:__subpackages__"],
deps = [
"@py_deps//alembic",
"@py_deps//flask",
],
)
1 change: 1 addition & 0 deletions ark_nova_stats/api/migrations/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Single-database configuration for Flask.
15 changes: 15 additions & 0 deletions ark_nova_stats/api/migrations/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import shutil

from flask_migrate import upgrade
from python.runfiles import Runfiles

from ark_nova_stats.config import app

if __name__ == "__main__":
# Copy the alembic.ini.
r = Runfiles.Create()
alembic_ini_src = r.Rlocation("_main/ark_nova_stats/api/migrations/alembic.ini")
shutil.copyfile(alembic_ini_src, "/ark_nova_stats/api/migrations/alembic.ini")

with app.app_context():
upgrade(directory="/ark_nova_stats/api/migrations")
51 changes: 51 additions & 0 deletions ark_nova_stats/api/migrations/alembic.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# A generic, single database configuration.

[alembic]
# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s

# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false

script_location = .

# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic,flask_migrate

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console
qualname =

[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine

[logger_alembic]
level = INFO
handlers =
qualname = alembic

[logger_flask_migrate]
level = INFO
handlers =
qualname = flask_migrate

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
Loading

0 comments on commit 7fb575a

Please sign in to comment.