Skip to content

Commit

Permalink
feat(main.py): Implement /nodes/set endpoint
Browse files Browse the repository at this point in the history
Implement batch "field to value" bulk set to improve speed of some
batch operations.
This way we can update field on multiple nodes to particular value
at once.

Signed-off-by: Denys Fedoryshchenko <[email protected]>
  • Loading branch information
nuclearcat committed Dec 5, 2024
1 parent 22febf3 commit d982b4a
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
UserGroup,
)
from .metrics import Metrics
from pydantic import BaseModel

Check warning on line 62 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

third party import "from pydantic import BaseModel" should be placed before "from .auth import Authentication"


@asynccontextmanager
Expand Down Expand Up @@ -770,6 +771,53 @@ async def put_node(node_id: str, node: Node,
await db.create(evhist)
return obj

class NodeUpdateRequest(BaseModel):

Check warning on line 774 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

Missing class docstring

Check warning on line 774 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

expected 2 blank lines, found 1
nodes: List[str]
field: str
value: str

@app.put('/batch/nodeset', response_model=int)

Check warning on line 779 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

expected 2 blank lines, found 1
async def put_nodes_set(data: NodeUpdateRequest,
user: str = Depends(authorize_user)):
"""
Set a field to a value for multiple nodes
TBD: Make db.bulkupdate to update multiple nodes in one go
"""
metrics.add('http_requests_total', 1)
updated = 0
nodes = data.nodes
field = data.field
value = data.value
for node_id in nodes:
node_from_id = await db.find_by_id(Node, node_id)
if not node_from_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Node not found with id: {node_id}"
)
# verify ownership
if not user.username == node_from_id.owner:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthorized to complete the operation"
)
# right now we support only field:
# processed_by_kcidb_bridge, also value should be boolean
if field == 'processed_by_kcidb_bridge':
if value == 'true' or value == 'True':

Check warning on line 807 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

Consider merging these comparisons with "in" to "value in ('true', 'True')"
value = True
elif value == 'false' or value == 'False':

Check warning on line 809 in api/main.py

View workflow job for this annotation

GitHub Actions / Lint

Consider merging these comparisons with "in" to "value in ('false', 'False')"
value = False
setattr(node_from_id, field, value)
await db.update(node_from_id)
updated += 1
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Field not supported"
)
return updated


async def _set_node_ownership_recursively(user: User, hierarchy: Hierarchy,
submitter: str, treeid: str):
Expand Down

0 comments on commit d982b4a

Please sign in to comment.