Skip to content

Feature/api based on nzshm model #20

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

Merged
merged 20 commits into from
Jun 4, 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
15 changes: 15 additions & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
* kororaa-graphql-api version:
* Python version:
* Operating System:

### Description

Describe what you were trying to get done.
Tell us what happened, what went wrong, and what you expected to happen.

### What I Did

```
Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.
```
72 changes: 72 additions & 0 deletions .github/workflows/deploy-to-aws.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: deploy-aws-lambda

on:
push:
branches:
- deploy-test
- main

jobs:
call-test-workflow:
# https://docs.github.com/en/actions/using-workflows/reusing-workflows#calling-a-reusable-workflow
uses: ./.github/workflows/run-tests.yml

deploy:
needs: call-test-workflow
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20]
python-version: ['3.10']

steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install and configure Poetry
uses: GNS-Science/install-poetry@main
with:
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
check-latest: true

- name: upgrade NPM
run: npm install --location=global npm@latest

# - name: Verify NPM
# run: npm doctor

- name: Install Dependencies
run: npm install

- name: List packages
run: npm ls

- name: Serverless Doctor
run: npm run-script sls_doctor

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ env.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-southeast-2

- name: Serverless Deploy ....
env:
ENABLE_METRICS: 0
run: |
if [[ "${{github.base_ref}}" == "main" || "${{github.ref}}" == 'refs/heads/main' ]]; then
STAGE=prod npm run-script deploy
else
STAGE=test npm run-script deploy
fi

13 changes: 13 additions & 0 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Dev workflow

on:
pull_request:
branches: [main, deploy-test]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

jobs:
call-test-workflow:
#https://docs.github.com/en/actions/using-workflows/reusing-workflows#calling-a-reusable-workflow
uses: ./.github/workflows/run-tests.yml
89 changes: 89 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Publish package on main branch if it's tagged with 'v*'

name: release & publish workflow

# Controls when the action will run.
on:
# Triggers the workflow on push events but only for the master branch
push:
tags:
- 'v*'

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "release"
release:
name: Create Release
runs-on: ubuntu-20.04

strategy:
matrix:
python-versions: [3.8]

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- name: Get version from tag
id: tag_name
run: |
echo ::set-output name=current_version::${GITHUB_REF#refs/tags/v}
shell: bash

# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

- name: Get Changelog Entry
id: changelog_reader
uses: mindsers/changelog-reader-action@v2
with:
validation_depth: 10
version: ${{ steps.tag_name.outputs.current_version }}
path: ./CHANGELOG.md

- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-versions }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install poetry

- name: build documentation
run: |
poetry install -E doc
poetry run mkdocs build

- name: publish documentation
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./site

# - name: Build wheels and source tarball
# run: >-
# poetry build

# - name: show temporary files
# run: >-
# ls -l

# - name: create github release
# id: create_release
# uses: softprops/action-gh-release@v1
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# with:
# body: ${{ steps.changelog_reader.outputs.changes }}
# files: dist/*.whl
# draft: false
# prerelease: false

# - name: publish to PyPI
# uses: pypa/gh-action-pypi-publish@release/v1
# with:
# user: __token__
# password: ${{ secrets.PYPI_API_TOKEN }}
# skip_existing: true
47 changes: 47 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Run Tests

#https://docs.github.com/en/actions/using-workflows/reusing-workflows#calling-a-reusable-workflow

on:
workflow_call:

jobs:
run_unit_tests:
runs-on: ubuntu-latest
timeout-minutes: 10

strategy:
matrix:
python-versions: ['3.10']
os: [ubuntu-latest]

steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-versions }}

- name: Install and configure Poetry
uses: GNS-Science/install-poetry@main
with:
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true

- name: Install dependencies w Poetry
run: |
poetry install --no-interaction --no-root --with dev
poetry add tox-gh-actions

- name: test with tox (uses tox-gh-actions to select correct environment)
run:
poetry run tox

- name: list files
run: ls -l .

- uses: codecov/codecov-action@v3
with:
fail_ci_if_error: false
files: coverage.xml
15 changes: 12 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
# Changelog


## [0.2.0] 2024-05-30
## [0.2.0] - 2024-05-30
### Changed
- Complete reset, no more django
- all previous code is mothballed
- flake8 config
- new about and version resolvers

### Added
- Node resolver support for NshmModel
- get_model resolver
- get_models resolver
- source logic tree models and resolvers

## [0.1.3] - 2023-09-04
## Added
### Added
- new about and version resolvers

## [0.1.2] - 2023-09-04
## Changed
### Changed
- configure static_url correctly for both local & AWS

## [0.1.1] - 2023-09-01
Expand Down
65 changes: 65 additions & 0 deletions nshm_model_graphql_api/nshm_model_graphql_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""Main module."""

import logging
import logging.config
import os

import yaml
from flask import Flask
from flask_cors import CORS
from flask_graphql import GraphQLView

# from nshm_model_graphql_api.library_version_check import log_library_info
from nshm_model_graphql_api.schema import schema_root

LOGGING_CFG = os.getenv("LOGGING_CFG", "nshm_model_graphql_api/logging_aws.yaml")
logger = logging.getLogger(__name__)

# TESTING = os.getenv('TESTING', False)
# if not TESTING:
# # because in testing, this screws up moto mocking
# log_library_info(['botocore', 'boto3', 'fiona'])


def create_app():
"""Function that creates our Flask application."""
app = Flask(__name__)
CORS(app)

# app.before_first_request(migrate)

app.add_url_rule(
"/graphql",
view_func=GraphQLView.as_view(
"graphql",
schema=schema_root,
graphiql=True,
),
)

"""
Setup logging configuration
ref https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/
"""
if os.path.exists(LOGGING_CFG):
with open(LOGGING_CFG, "rt") as f:
config = yaml.safe_load(f.read())
logging.config.dictConfig(config)
else:
print("Warning, no logging config found, using basicConfig(INFO)")
logging.basicConfig(level=logging.INFO)

logger.debug("DEBUG logging enabled")
logger.info("INFO logging enabled")
logger.warning("WARN logging enabled")
logger.error("ERROR logging enabled")

return app


# pragma: no cover
app = create_app()


if __name__ == "__main__":
app.run()
3 changes: 3 additions & 0 deletions nshm_model_graphql_api/schema/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""API schema package."""

from .schema_root import schema_root # noqa: F401
52 changes: 52 additions & 0 deletions nshm_model_graphql_api/schema/nshm_model_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Define graphene model for nzshm_model class."""

import logging
from typing import Iterator, Optional

import graphene
import nzshm_model as nm
from graphene import relay

from .nshm_model_sources_schema import SourceLogicTree

log = logging.getLogger(__name__)


class NshmModel(graphene.ObjectType):
"""A custom Node representing an entire model."""

class Meta:
interfaces = (relay.Node,)

version = graphene.String()
title = graphene.String()
source_logic_tree = graphene.Field(SourceLogicTree)

def resolve_id(self, info):
return self.version

@staticmethod
def resolve_source_logic_tree(root, info, **kwargs):
log.info(f"resolve_source_logic_tree root: {root} kwargs: {kwargs}")
return SourceLogicTree(
model_version=root.version
) # , branch_sets=get_branch_sets(slt))

@classmethod
def get_node(cls, info, version: str):
return get_nshm_model(version)


def get_nshm_models() -> Iterator[NshmModel]:
for version in nm.all_model_versions():
yield NshmModel(version=version)


def get_nshm_model(version: Optional[str] = None) -> Optional[NshmModel]:
# model = nm.get_model_version(version)
model = nm.get_model_version(version) if version else nm.get_model_version()
return NshmModel(version=model.version, title=model.title) if model else None


def get_current_model_version() -> str:
return nm.CURRENT_VERSION
Loading
Loading