Skip to content

Commit 21abe66

Browse files
authored
Merge pull request #20 from GNS-Science/feature/api_based_on_nzshm-model
Feature/api based on nzshm model
2 parents 2fcc08e + 2604a0b commit 21abe66

20 files changed

+1273
-85
lines changed

.github/ISSUE_TEMPLATE.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
* kororaa-graphql-api version:
2+
* Python version:
3+
* Operating System:
4+
5+
### Description
6+
7+
Describe what you were trying to get done.
8+
Tell us what happened, what went wrong, and what you expected to happen.
9+
10+
### What I Did
11+
12+
```
13+
Paste the command(s) you ran and the output.
14+
If there was a crash, please include the traceback here.
15+
```

.github/workflows/deploy-to-aws.yaml

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: deploy-aws-lambda
2+
3+
on:
4+
push:
5+
branches:
6+
- deploy-test
7+
- main
8+
9+
jobs:
10+
call-test-workflow:
11+
# https://docs.github.com/en/actions/using-workflows/reusing-workflows#calling-a-reusable-workflow
12+
uses: ./.github/workflows/run-tests.yml
13+
14+
deploy:
15+
needs: call-test-workflow
16+
runs-on: ubuntu-latest
17+
strategy:
18+
matrix:
19+
node-version: [20]
20+
python-version: ['3.10']
21+
22+
steps:
23+
- uses: actions/checkout@v3
24+
- uses: actions/setup-python@v4
25+
with:
26+
python-version: ${{ matrix.python-version }}
27+
28+
- name: Install and configure Poetry
29+
uses: GNS-Science/install-poetry@main
30+
with:
31+
virtualenvs-create: true
32+
virtualenvs-in-project: true
33+
installer-parallel: true
34+
35+
- name: Use Node.js ${{ matrix.node-version }}
36+
uses: actions/setup-node@v3
37+
with:
38+
node-version: ${{ matrix.node-version }}
39+
check-latest: true
40+
41+
- name: upgrade NPM
42+
run: npm install --location=global npm@latest
43+
44+
# - name: Verify NPM
45+
# run: npm doctor
46+
47+
- name: Install Dependencies
48+
run: npm install
49+
50+
- name: List packages
51+
run: npm ls
52+
53+
- name: Serverless Doctor
54+
run: npm run-script sls_doctor
55+
56+
- name: Configure AWS Credentials
57+
uses: aws-actions/configure-aws-credentials@v1
58+
with:
59+
aws-access-key-id: ${{ env.AWS_ACCESS_KEY_ID }}
60+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
61+
aws-region: ap-southeast-2
62+
63+
- name: Serverless Deploy ....
64+
env:
65+
ENABLE_METRICS: 0
66+
run: |
67+
if [[ "${{github.base_ref}}" == "main" || "${{github.ref}}" == 'refs/heads/main' ]]; then
68+
STAGE=prod npm run-script deploy
69+
else
70+
STAGE=test npm run-script deploy
71+
fi
72+

.github/workflows/dev.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: Dev workflow
2+
3+
on:
4+
pull_request:
5+
branches: [main, deploy-test]
6+
7+
# Allows you to run this workflow manually from the Actions tab
8+
workflow_dispatch:
9+
10+
jobs:
11+
call-test-workflow:
12+
#https://docs.github.com/en/actions/using-workflows/reusing-workflows#calling-a-reusable-workflow
13+
uses: ./.github/workflows/run-tests.yml

.github/workflows/release.yml

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Publish package on main branch if it's tagged with 'v*'
2+
3+
name: release & publish workflow
4+
5+
# Controls when the action will run.
6+
on:
7+
# Triggers the workflow on push events but only for the master branch
8+
push:
9+
tags:
10+
- 'v*'
11+
12+
# Allows you to run this workflow manually from the Actions tab
13+
workflow_dispatch:
14+
15+
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
16+
jobs:
17+
# This workflow contains a single job called "release"
18+
release:
19+
name: Create Release
20+
runs-on: ubuntu-20.04
21+
22+
strategy:
23+
matrix:
24+
python-versions: [3.8]
25+
26+
# Steps represent a sequence of tasks that will be executed as part of the job
27+
steps:
28+
- name: Get version from tag
29+
id: tag_name
30+
run: |
31+
echo ::set-output name=current_version::${GITHUB_REF#refs/tags/v}
32+
shell: bash
33+
34+
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
35+
- uses: actions/checkout@v2
36+
37+
- name: Get Changelog Entry
38+
id: changelog_reader
39+
uses: mindsers/changelog-reader-action@v2
40+
with:
41+
validation_depth: 10
42+
version: ${{ steps.tag_name.outputs.current_version }}
43+
path: ./CHANGELOG.md
44+
45+
- uses: actions/setup-python@v2
46+
with:
47+
python-version: ${{ matrix.python-versions }}
48+
49+
- name: Install dependencies
50+
run: |
51+
python -m pip install --upgrade pip
52+
pip install poetry
53+
54+
- name: build documentation
55+
run: |
56+
poetry install -E doc
57+
poetry run mkdocs build
58+
59+
- name: publish documentation
60+
uses: peaceiris/actions-gh-pages@v3
61+
with:
62+
github_token: ${{ secrets.GITHUB_TOKEN }}
63+
publish_dir: ./site
64+
65+
# - name: Build wheels and source tarball
66+
# run: >-
67+
# poetry build
68+
69+
# - name: show temporary files
70+
# run: >-
71+
# ls -l
72+
73+
# - name: create github release
74+
# id: create_release
75+
# uses: softprops/action-gh-release@v1
76+
# env:
77+
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
78+
# with:
79+
# body: ${{ steps.changelog_reader.outputs.changes }}
80+
# files: dist/*.whl
81+
# draft: false
82+
# prerelease: false
83+
84+
# - name: publish to PyPI
85+
# uses: pypa/gh-action-pypi-publish@release/v1
86+
# with:
87+
# user: __token__
88+
# password: ${{ secrets.PYPI_API_TOKEN }}
89+
# skip_existing: true

.github/workflows/run-tests.yml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Run Tests
2+
3+
#https://docs.github.com/en/actions/using-workflows/reusing-workflows#calling-a-reusable-workflow
4+
5+
on:
6+
workflow_call:
7+
8+
jobs:
9+
run_unit_tests:
10+
runs-on: ubuntu-latest
11+
timeout-minutes: 10
12+
13+
strategy:
14+
matrix:
15+
python-versions: ['3.10']
16+
os: [ubuntu-latest]
17+
18+
steps:
19+
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
20+
- uses: actions/checkout@v3
21+
- uses: actions/setup-python@v4
22+
with:
23+
python-version: ${{ matrix.python-versions }}
24+
25+
- name: Install and configure Poetry
26+
uses: GNS-Science/install-poetry@main
27+
with:
28+
virtualenvs-create: true
29+
virtualenvs-in-project: true
30+
installer-parallel: true
31+
32+
- name: Install dependencies w Poetry
33+
run: |
34+
poetry install --no-interaction --no-root --with dev
35+
poetry add tox-gh-actions
36+
37+
- name: test with tox (uses tox-gh-actions to select correct environment)
38+
run:
39+
poetry run tox
40+
41+
- name: list files
42+
run: ls -l .
43+
44+
- uses: codecov/codecov-action@v3
45+
with:
46+
fail_ci_if_error: false
47+
files: coverage.xml

CHANGELOG.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
# Changelog
22

33

4-
## [0.2.0] 2024-05-30
4+
## [0.2.0] - 2024-05-30
5+
### Changed
56
- Complete reset, no more django
67
- all previous code is mothballed
8+
- flake8 config
9+
- new about and version resolvers
10+
11+
### Added
12+
- Node resolver support for NshmModel
13+
- get_model resolver
14+
- get_models resolver
15+
- source logic tree models and resolvers
716

817
## [0.1.3] - 2023-09-04
9-
## Added
18+
### Added
1019
- new about and version resolvers
1120

1221
## [0.1.2] - 2023-09-04
13-
## Changed
22+
### Changed
1423
- configure static_url correctly for both local & AWS
1524

1625
## [0.1.1] - 2023-09-01
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""Main module."""
2+
3+
import logging
4+
import logging.config
5+
import os
6+
7+
import yaml
8+
from flask import Flask
9+
from flask_cors import CORS
10+
from flask_graphql import GraphQLView
11+
12+
# from nshm_model_graphql_api.library_version_check import log_library_info
13+
from nshm_model_graphql_api.schema import schema_root
14+
15+
LOGGING_CFG = os.getenv("LOGGING_CFG", "nshm_model_graphql_api/logging_aws.yaml")
16+
logger = logging.getLogger(__name__)
17+
18+
# TESTING = os.getenv('TESTING', False)
19+
# if not TESTING:
20+
# # because in testing, this screws up moto mocking
21+
# log_library_info(['botocore', 'boto3', 'fiona'])
22+
23+
24+
def create_app():
25+
"""Function that creates our Flask application."""
26+
app = Flask(__name__)
27+
CORS(app)
28+
29+
# app.before_first_request(migrate)
30+
31+
app.add_url_rule(
32+
"/graphql",
33+
view_func=GraphQLView.as_view(
34+
"graphql",
35+
schema=schema_root,
36+
graphiql=True,
37+
),
38+
)
39+
40+
"""
41+
Setup logging configuration
42+
ref https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/
43+
"""
44+
if os.path.exists(LOGGING_CFG):
45+
with open(LOGGING_CFG, "rt") as f:
46+
config = yaml.safe_load(f.read())
47+
logging.config.dictConfig(config)
48+
else:
49+
print("Warning, no logging config found, using basicConfig(INFO)")
50+
logging.basicConfig(level=logging.INFO)
51+
52+
logger.debug("DEBUG logging enabled")
53+
logger.info("INFO logging enabled")
54+
logger.warning("WARN logging enabled")
55+
logger.error("ERROR logging enabled")
56+
57+
return app
58+
59+
60+
# pragma: no cover
61+
app = create_app()
62+
63+
64+
if __name__ == "__main__":
65+
app.run()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""API schema package."""
2+
3+
from .schema_root import schema_root # noqa: F401
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""Define graphene model for nzshm_model class."""
2+
3+
import logging
4+
from typing import Iterator, Optional
5+
6+
import graphene
7+
import nzshm_model as nm
8+
from graphene import relay
9+
10+
from .nshm_model_sources_schema import SourceLogicTree
11+
12+
log = logging.getLogger(__name__)
13+
14+
15+
class NshmModel(graphene.ObjectType):
16+
"""A custom Node representing an entire model."""
17+
18+
class Meta:
19+
interfaces = (relay.Node,)
20+
21+
version = graphene.String()
22+
title = graphene.String()
23+
source_logic_tree = graphene.Field(SourceLogicTree)
24+
25+
def resolve_id(self, info):
26+
return self.version
27+
28+
@staticmethod
29+
def resolve_source_logic_tree(root, info, **kwargs):
30+
log.info(f"resolve_source_logic_tree root: {root} kwargs: {kwargs}")
31+
return SourceLogicTree(
32+
model_version=root.version
33+
) # , branch_sets=get_branch_sets(slt))
34+
35+
@classmethod
36+
def get_node(cls, info, version: str):
37+
return get_nshm_model(version)
38+
39+
40+
def get_nshm_models() -> Iterator[NshmModel]:
41+
for version in nm.all_model_versions():
42+
yield NshmModel(version=version)
43+
44+
45+
def get_nshm_model(version: Optional[str] = None) -> Optional[NshmModel]:
46+
# model = nm.get_model_version(version)
47+
model = nm.get_model_version(version) if version else nm.get_model_version()
48+
return NshmModel(version=model.version, title=model.title) if model else None
49+
50+
51+
def get_current_model_version() -> str:
52+
return nm.CURRENT_VERSION

0 commit comments

Comments
 (0)