Newer
Older
"""
API server for querying the local MongoDB and posting
data to it.
To be used by the front end.
"""
import os
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pymongo import MongoClient
from model import Model, GTModel, WorkflowModel
import gt
import workflows
import releases
import runs
origins = [
'*'
]
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_methods=['*'],
allow_headers=['*'],
)
USERNAME = os.getenv('USERNAME')
PASSWORD = os.getenv('PASSWORD')
CLIENT = MongoClient(f'mongodb://{USERNAME}:{PASSWORD}@quiver-mongodb-1:27017/results?authSource=results')
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
DB = CLIENT.results
COLL = DB.quiver
@app.get('/api/gt')
def api_get_all_gt() -> list:
"""
Returns information about all available Ground Truth
curated by OCR-D for QuiVer
"""
return gt.get_all_gt(COLL)
@app.get('/api/gt/{gt_id}')
def api_get_gt(gt_id: str) -> list:
"""
Returns information about one available Ground Truth
curated by OCR-D for QuiVer
"""
return gt.get_gt(COLL, gt_id)
@app.post('/api/gt')
def api_post_new_gt(gt_model: GTModel) -> str:
"""
Posts information about a new Ground Truth dataset to the database.
Args:
- gt_model (GTModel): information about the GT
Return
- str: True if POST was successful
"""
return gt.post_new_gt(COLL, gt_model)
@app.get('/api/workflows')
def get_all_workflows() -> list:
"""
Returns information about all available workflows
curated by OCR-D for QuiVer
"""
return workflows.get_all_workflows(COLL)
@app.get('/api/workflows/{wf_id}')
def get_workflows(wf_id: str) -> list:
"""
Returns information about one available workflow
curated by OCR-D for QuiVer
"""
return workflows.get_workflows(COLL, wf_id)
@app.post('/api/workflows')
def post_new_workflow(workflow: WorkflowModel) -> str:
"""
Posts information about a new workflow to the database.
Args:
- workflow (WorkflowModel): information about the workflow
Return
- bool: True if POST was successful
"""
return workflows.post_new_workflow(COLL, workflow)
@app.get('/api/runs')
def get_all_runs() -> list:
"""
Returns all evalutation results for all Quiver workspaces.
"""
return runs.get_all_runs(COLL)
@app.get('/api/runs/latest')
def get_all_latest_runs() -> list:
"""
Returns all evalutation results for all Quiver workspaces.
"""
return runs.get_all_latest_runs(COLL)
@app.get('/api/runs/{gt_id}')
def get_all_runs_by_gt(gt_id: str,
start_date: str | None = None,
end_date: str | None = None) -> list:
"""
Returns evalutation results for all Quiver workspaces with a
given GT
Args:
- gt_id (id): The ID of the GT data used for a run
"""
return runs.get_all_runs_by_gt(COLL, gt_id, start_date, end_date)
@app.get('/api/runs/{gt_id}/latest')
def get_latest_runs_per_gt(gt_id: str) -> list:
"""
Returns evalutation results for the latest Quiver workspace with a
given GT
Args:
- gt_id (id): The ID of the GT data used for a run
"""
return runs.get_latest_runs_per_gt(COLL, gt_id)
@app.get('/api/runs/{gt_id}/{workflow_id}')
def get_all_runs_by_gt_and_wf(workflow_id: str,
gt_id: str,
start_date: str | None = None,
end_date: str | None = None) -> list:
"""
Returns evalutation results for all Quiver workspaces with a
given workflow and GT
Args:
- workflow_id (str): The ID of the workflow used for a run
- gt_id (id): The ID of the GT data used for a run
"""
return runs.get_all_runs_by_gt_and_wf(COLL, workflow_id, gt_id, start_date, end_date)
@app.get('/api/runs/{gt_id}/{workflow_id}/latest')
def get_latest_runs(workflow_id: str,
gt_id: str) -> list:
"""
Returns evalutation results for the latest Quiver workspace with a
given workflow and GT
Args:
- workflow_id (str): The ID of the workflow used for a run
- gt_id (id): The ID of the GT data used for a run
"""
return runs.get_latest_runs(COLL, workflow_id, gt_id)
@app.post("/api/runs")
def post_new_result(data: Model):
"""
Posts information about a new evaluation workspace to the database.
Args:
- data (Model): information about the evaluation workspace
Return
- bool: True if POST was successful
"""
return runs.post_new_result(COLL, data)
@app.get("/api/releases")
def get_releases():
"""
Returns a list of all releases for which Quiver provides data.
"""
return releases.get_all_releases(COLL)