Skip to content

Commit 3117421

Browse files
authored
Merge pull request #6 from KWB-R/feature/admin-page
add QMRA models for the admin page
2 parents 69e1fe0 + 0cfcd2b commit 3117421

34 files changed

Lines changed: 805 additions & 320 deletions

.github/workflows/ci.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ on:
55
push:
66
branches:
77
- main
8+
pull_request:
9+
branches:
10+
- main
811

912
jobs:
1013
test:
@@ -25,13 +28,15 @@ jobs:
2528
environment: dev
2629
secrets: inherit
2730
build-prod:
31+
if: github.ref_name == 'main'
2832
needs: test
2933
name: build-prod
3034
uses: ./.github/workflows/build.yaml
3135
with:
3236
environment: prod
3337
secrets: inherit
3438
deploy-prod:
39+
if: github.ref_name == 'main'
3540
needs:
3641
- build-prod
3742
- deploy-dev

.github/workflows/deploy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
username: ${{ secrets.DEPLOY_USER }}
2626
key: ${{ secrets.DEPLOY_SERVER_SSH_KEY }}
2727
script: |
28-
cd ${{ secrets.DEPLOY_PATH }} && git pull
28+
cd ${{ secrets.DEPLOY_PATH }} && git pull && git checkout ${{ github.head_ref || github.ref_name }}
2929
microk8s ctr image import img.tar && rm img.tar
3030
cd infra/helm
3131
microk8s helm upgrade --install -f ./qmra/${{ inputs.environment }}.values.yaml qmra ./qmra -n qmra --set app_secret_key.value=${{ secrets.APP_SECRET_KEY }},image.tag=${{ env.sha_short }}

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ venv/
44
.idea
55
.static
66
qmra.db
7+
default_qmra_data.db
78
qmra-prod.db
89
dump*
910
prod-migrations/
10-
*.tar
11+
*.tar
12+
.vscode

infra/helm/qmra/dev.values.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ sqlite:
1717
mount_path: /var/lib/qmra/qmra.db
1818
hostpath: /var/lib/qmra/qmra.db
1919

20+
qmra_default:
21+
mount_path: /var/lib/qmra/default_qmra_data.db
22+
hostpath: /var/lib/qmra/default_qmra_data.db
23+
2024
static:
2125
mount_path: /var/cache/qmra/static
2226
hostpath: /var/cache/qmra/static

infra/helm/qmra/prod.values.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ sqlite:
1717
mount_path: /var/lib/qmra/qmra.db
1818
hostpath: /var/lib/qmra/qmra.db
1919

20+
qmra_default:
21+
mount_path: /var/lib/qmra/default_qmra_data.db
22+
hostpath: /var/lib/qmra/default_qmra_data.db
23+
2024
static:
2125
mount_path: /var/cache/qmra/static
2226
hostpath: /var/cache/qmra/static

infra/helm/qmra/templates/configmap.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ data:
77
DEBUG: "{{ .Values.debug }}"
88
DOMAIN_NAME: {{ .Values.domain }}
99
SQLITE_PATH: {{ .Values.sqlite.mount_path }}
10+
DEFAULT_QMRA_PATH: {{ .Values.qmra_default.mount_path }}
1011
STATIC_ROOT: {{ .Values.static.mount_path }}
1112
---
1213
apiVersion: v1

infra/helm/qmra/templates/deployment.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ spec:
3737
- name: sqlite
3838
mountPath: {{ .Values.sqlite.mount_path }}
3939
command: [ python, manage.py, migrate ]
40+
- name: migrate-qmra-default
41+
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
42+
envFrom:
43+
- configMapRef:
44+
name: {{ .Values.configmap_name }}
45+
volumeMounts:
46+
- name: qmra-default
47+
mountPath: {{ .Values.qmra_default.mount_path }}
48+
command: [ python, manage.py, migrate, --database, qmra ]
4049
containers:
4150
- name: {{ .Chart.Name }}
4251
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
@@ -68,12 +77,17 @@ spec:
6877
volumeMounts:
6978
- name: sqlite
7079
mountPath: {{ .Values.sqlite.mount_path }}
80+
- name: qmra-default
81+
mountPath: {{ .Values.qmra_default.mount_path }}
7182
- name: static
7283
mountPath: {{ .Values.static.mount_path }}
7384
volumes:
7485
- name: sqlite
7586
persistentVolumeClaim:
7687
claimName: {{ include "app.fullname" . }}-sqlite-file-pvc
88+
- name: qmra-default
89+
persistentVolumeClaim:
90+
claimName: {{ include "app.fullname" . }}-qmra-default-file-pvc
7791
- name: static
7892
persistentVolumeClaim:
7993
claimName: {{ include "app.fullname" . }}-static-files-pvc

infra/helm/qmra/templates/volumes.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ spec:
3131
type: FileOrCreate
3232
---
3333
apiVersion: v1
34+
kind: PersistentVolume
35+
metadata:
36+
name: {{ include "app.fullname" . }}-qmra-default-file-pv
37+
spec:
38+
capacity:
39+
storage: 2Gi
40+
volumeMode: Filesystem
41+
accessModes:
42+
- ReadWriteMany
43+
persistentVolumeReclaimPolicy: Retain
44+
storageClassName: microk8s-hostpath
45+
hostPath:
46+
path: {{ .Values.qmra_default.hostpath }}
47+
type: FileOrCreate
48+
---
49+
apiVersion: v1
3450
kind: PersistentVolumeClaim
3551
metadata:
3652
name: {{ include "app.fullname" . }}-static-files-pvc
@@ -53,6 +69,20 @@ spec:
5369
- ReadWriteMany
5470
volumeMode: Filesystem
5571
volumeName: {{ include "app.fullname" . }}-sqlite-file-pv
72+
resources:
73+
requests:
74+
storage: 1Gi
75+
storageClassName: microk8s-hostpath
76+
---
77+
apiVersion: v1
78+
kind: PersistentVolumeClaim
79+
metadata:
80+
name: {{ include "app.fullname" . }}-qmra-default-file-pvc
81+
spec:
82+
accessModes:
83+
- ReadWriteMany
84+
volumeMode: Filesystem
85+
volumeName: {{ include "app.fullname" . }}-qmra-default-file-pv
5686
resources:
5787
requests:
5888
storage: 1Gi

qmra/management/commands/collect_static_default_entities.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ def get_default_inflows():
3030
inflows = pd.merge(inflows, sources, left_on="source_id", right_on="id", how="left").rename(columns={"name": "source_name"})
3131
inflows = pd.merge(inflows, pathogens, left_on="pathogen_id", right_on="id", how="left").rename(columns={"name": "pathogen_name"})
3232
inflows = inflows[inflows.pathogen_id.isin((3, 32, 34))]
33-
return inflows.loc[:, ["source_name", "pathogen_name", "min", "max", "ReferenceID"]]
33+
inflows["id"] = list(range(len(inflows.index)))
34+
return inflows.loc[:, ["id", "source_name", "pathogen_name", "min", "max", "ReferenceID"]]
3435

3536

3637
def get_default_treatments():
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import json
2+
from django.core.management.base import BaseCommand
3+
from qmra.risk_assessment.qmra_models import QMRAReference, QMRAReferences, QMRASource, \
4+
QMRASources, QMRAPathogen, QMRAPathogens, QMRAInflow, QMRAInflows, QMRATreatment, \
5+
QMRATreatments, QMRAExposure, QMRAExposures
6+
7+
8+
def save_as_json(data, destination: str):
9+
with open(destination, "w") as f:
10+
json.dump(data, f)
11+
12+
13+
class Command(BaseCommand):
14+
help = "export the default data of qmra to json files for serving them as statics"
15+
16+
# def add_arguments(self, parser):
17+
# parser.add_argument('--format', type=str, help="'json' (default) or 'csv' ", default="json")
18+
19+
def handle(self, *args, **options):
20+
save_as_json(
21+
{src.name: src.to_dict() for src in QMRASource.objects.all()},
22+
QMRASources.source
23+
)
24+
save_as_json(
25+
{pathogen.name: pathogen.to_dict() for pathogen in QMRAPathogen.objects.all()},
26+
QMRAPathogens.source
27+
)
28+
save_as_json(
29+
{src.name: [inflow.to_dict() for inflow in QMRAInflow.objects.filter(source__name=src.name).all()]
30+
for src in QMRASource.objects.all()},
31+
QMRAInflows.source
32+
)
33+
save_as_json(
34+
{t.name: t.to_dict() for t in QMRATreatment.objects.all()},
35+
QMRATreatments.source
36+
)
37+
save_as_json(
38+
{e.name: e.to_dict() for e in QMRAExposure.objects.all()},
39+
QMRAExposures.source
40+
)
41+
save_as_json(
42+
{str(ref.pk): ref.to_dict() for ref in QMRAReference.objects.all()},
43+
QMRAReferences.source
44+
)
45+
46+
47+
if __name__ == '__main__':
48+
Command().handle()

0 commit comments

Comments
 (0)