-
Notifications
You must be signed in to change notification settings - Fork 5
/
workflows.py
105 lines (88 loc) · 3.64 KB
/
workflows.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
import os
from typing import Any, List
import pymongo
from fastapi import APIRouter, Depends, HTTPException
from motor.motor_asyncio import AsyncIOMotorDatabase
from pymongo.database import Database as MongoDatabase
from pymongo.errors import BulkWriteError
from starlette import status
from nmdc_runtime.api.core.util import raise404_if_none
from nmdc_runtime.api.db.mongo import get_mongo_db, activity_collection_names
from nmdc_runtime.api.models.capability import Capability
from nmdc_runtime.api.models.object_type import ObjectType
from nmdc_runtime.api.models.site import Site, get_current_client_site
from nmdc_runtime.api.models.workflow import Workflow
from nmdc_runtime.site.resources import MongoDB
from nmdc_runtime.util import validate_json
router = APIRouter()
@router.get("/workflows", response_model=List[Workflow])
def list_workflows(
mdb: pymongo.database.Database = Depends(get_mongo_db),
):
return list(mdb.workflows.find())
@router.get("/workflows/{workflow_id}", response_model=Workflow)
def get_workflow(
workflow_id: str,
mdb: pymongo.database.Database = Depends(get_mongo_db),
):
return raise404_if_none(mdb.workflows.find_one({"id": workflow_id}))
@router.get("/workflows/{workflow_id}/object_types", response_model=List[ObjectType])
def list_workflow_object_types(
workflow_id: str, mdb: pymongo.database.Database = Depends(get_mongo_db)
):
object_type_ids = [
doc["object_type_id"] for doc in mdb.triggers.find({"workflow_id": workflow_id})
]
return list(mdb.object_types.find({"id": {"$in": object_type_ids}}))
@router.get("/workflows/{workflow_id}/capabilities", response_model=List[Capability])
def list_workflow_capabilities(
workflow_id: str, mdb: pymongo.database.Database = Depends(get_mongo_db)
):
doc = raise404_if_none(mdb.workflows.find_one({"id": workflow_id}))
return list(mdb.capabilities.find({"id": {"$in": doc.get("capability_ids", [])}}))
# TODO: Create activity.py in ../models
@router.post("/workflows/activities")
async def post_activity(
activity_set: dict[str, Any],
site: Site = Depends(get_current_client_site),
mdb: MongoDatabase = Depends(get_mongo_db),
):
"""
Please migrate all workflows from `v1/workflows/activities` to this endpoint.
-------
Post activity set to database and claim job.
Parameters
-------
activity_set: dict[str,Any]
Set of activities for specific workflows.
Returns
-------
dict[str,str]
"""
_ = site # must be authenticated
try:
# verify activities in activity_set are nmdc-schema compliant
for collection_name in activity_set:
if collection_name not in activity_collection_names(mdb):
raise ValueError("keys must be nmdc-schema activity collection names`")
# validate request JSON
rv = validate_json(activity_set, mdb)
if rv["result"] == "errors":
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=str(rv),
)
# create mongodb instance for dagster
mongo_resource = MongoDB(
host=os.getenv("MONGO_HOST"),
dbname=os.getenv("MONGO_DBNAME"),
username=os.getenv("MONGO_USERNAME"),
password=os.getenv("MONGO_PASSWORD"),
)
mongo_resource.add_docs(activity_set, validate=False, replace=True)
# TODO: Update return value to List[Activity]
return {"message": "jobs accepted"}
except BulkWriteError as e:
raise HTTPException(status_code=409, detail=str(e))
except ValueError as e:
raise HTTPException(status_code=409, detail=str(e))