async def post(self) -> None: """Handle POST /Bundles/actions/bulk_create.""" req = json_decode(self.request.body) if 'bundles' not in req: raise tornado.web.HTTPError(400, reason="missing bundles field") if not isinstance(req['bundles'], list): raise tornado.web.HTTPError(400, reason="bundles field is not a list") if not req['bundles']: raise tornado.web.HTTPError(400, reason="bundles field is empty") for xfer_bundle in req["bundles"]: right_now = now() # https://www.youtube.com/watch?v=BQkFEG_iZUA xfer_bundle["uuid"] = unique_id() xfer_bundle["create_timestamp"] = right_now xfer_bundle["update_timestamp"] = right_now xfer_bundle["work_priority_timestamp"] = right_now xfer_bundle["claimed"] = False logging.debug( f"MONGO-START: db.Bundles.insert_many(documents={req['bundles']})") ret = await self.db.Bundles.insert_many(documents=req["bundles"]) logging.debug("MONGO-END: db.Bundles.insert_many(documents)") create_count = len(ret.inserted_ids) uuids = [] for x in req["bundles"]: uuid = x["uuid"] uuids.append(uuid) logging.info(f"created Bundle {uuid}") self.set_status(201) self.write({'bundles': uuids, 'count': create_count})
async def post(self) -> None: """Handle POST /Bundles/actions/bulk_update.""" req = json_decode(self.request.body) if 'update' not in req: raise tornado.web.HTTPError(400, reason="missing update field") if not isinstance(req['update'], dict): raise tornado.web.HTTPError(400, reason="update field is not an object") if 'bundles' not in req: raise tornado.web.HTTPError(400, reason="missing bundles field") if not isinstance(req['bundles'], list): raise tornado.web.HTTPError(400, reason="bundles field is not a list") if not req['bundles']: raise tornado.web.HTTPError(400, reason="bundles field is empty") results = [] for uuid in req["bundles"]: query = {"uuid": uuid} update_doc = {"$set": req["update"]} logging.debug( f"MONGO-START: db.Bundles.update_one(filter={query}, update={update_doc})" ) ret = await self.db.Bundles.update_one(filter=query, update=update_doc) logging.debug("MONGO-END: db.Bundles.update_one(filter, update)") if ret.modified_count > 0: logging.info(f"updated Bundle {uuid}") results.append(uuid) self.write({'bundles': results, 'count': len(results)})
async def post(self) -> None: """Handle a new fruit.""" assert tracing_tools.get_current_span().parent.span_id # type: ignore[attr-defined] body = json_decode(self.request.body) logging.info("body: %r", body) self.fruit[body["name"]] = body self.write({})
async def post(self) -> None: """Handle POST /Bundles/actions/pop.""" dest = self.get_argument('dest', default=None) source = self.get_argument('source', default=None) status = self.get_argument('status') if (not dest) and (not source): raise tornado.web.HTTPError( 400, reason="missing source and dest fields") pop_body = json_decode(self.request.body) if 'claimant' not in pop_body: raise tornado.web.HTTPError(400, reason="missing claimant field") claimant = pop_body["claimant"] # find and claim a bundle for the specified source sdb = self.db.Bundles find_query = { "status": status, "claimed": False, } if dest: find_query["dest"] = dest if source: find_query["source"] = source right_now = now() # https://www.youtube.com/watch?v=WaSy8yy-mr8 update_doc = { "$set": { "update_timestamp": right_now, "claimed": True, "claimant": claimant, "claim_timestamp": right_now, } } logging.debug( f"MONGO-START: db.Bundles.find_one_and_update(filter={find_query}, update={update_doc}, projection={REMOVE_ID}, sort={FIRST_IN_FIRST_OUT}, return_document={AFTER})" ) bundle = await sdb.find_one_and_update(filter=find_query, update=update_doc, projection=REMOVE_ID, sort=FIRST_IN_FIRST_OUT, return_document=AFTER) logging.debug( "MONGO-END: db.Bundles.find_one_and_update(filter, update, projection, sort, return_document)" ) # return what we found to the caller if not bundle: logging.info( f"Unclaimed Bundle with source {source} and status {status} does not exist." ) else: logging.info(f"Bundle {bundle['uuid']} claimed by {claimant}") self.write({'bundle': bundle})
async def patch(self, bundle_id: str) -> None: """Handle PATCH /Bundles/{uuid}.""" req = json_decode(self.request.body) if 'uuid' in req and req['uuid'] != bundle_id: raise tornado.web.HTTPError(400, reason="bad request") query = {"uuid": bundle_id} update_doc = {"$set": req} logging.debug(f"MONGO-START: db.Bundles.find_one_and_update(filter={query}, update={update_doc}, projection={REMOVE_ID}, return_document={AFTER})") ret = await self.db.Bundles.find_one_and_update(filter=query, update=update_doc, projection=REMOVE_ID, return_document=AFTER) logging.debug("MONGO-END: db.Bundles.find_one_and_update(filter, update, projection, return_document)") if not ret: raise tornado.web.HTTPError(404, reason="not found") logging.info(f"patched Bundle {bundle_id} with {req}") self.write(ret)
async def post(self) -> None: """Handle POST /TransferRequests/actions/pop.""" source = self.get_argument('source') pop_body = json_decode(self.request.body) if 'claimant' not in pop_body: raise tornado.web.HTTPError(400, reason="missing claimant field") claimant = pop_body["claimant"] # find and claim a transfer request for the specified source sdtr = self.db.TransferRequests find_query = { "source": source, "status": "unclaimed", } right_now = now() # https://www.youtube.com/watch?v=nRGCZh5A8T4 update_doc = { "$set": { "status": "processing", "update_timestamp": right_now, "claimed": True, "claimant": claimant, "claim_timestamp": right_now, } } logging.debug( f"MONGO-START: db.TransferRequests.find_one_and_update(filter={find_query}, update={update_doc}, projection={REMOVE_ID}, sort={FIRST_IN_FIRST_OUT}, return_document={AFTER})" ) tr = await sdtr.find_one_and_update(filter=find_query, update=update_doc, projection=REMOVE_ID, sort=FIRST_IN_FIRST_OUT, return_document=AFTER) logging.debug( "MONGO-END: db.TransferRequests.find_one_and_update(filter, update, projection, sort, return_document)" ) # return what we found to the caller if not tr: logging.info( f"Unclaimed TransferRequest with source {source} does not exist." ) else: logging.info(f"TransferRequest {tr['uuid']} claimed by {claimant}") self.write({'transfer_request': tr})
async def patch(self, component: str) -> None: """Update the detailed status of a component.""" req = json_decode(self.request.body) sds = self.db.Status name = list(req.keys())[0] query = {"component": component, "name": name} status_doc = req[name] status_doc["name"] = name status_doc["component"] = component update_doc = {"$set": status_doc} logging.debug(f"MONGO-START: db.Status.update_one(filter={query}, update={update_doc}, upsert=True)") ret = await sds.update_one(filter=query, update=update_doc, upsert=True) logging.debug("MONGO-END: db.Status.update_one(filter, update, upsert)") if (ret.modified_count) or (ret.upserted_id): logging.info(f"PATCH /status/{component} with {req}") else: logging.error(f"Unable to PATCH /status/{component} with {req}") self.write({})
async def post(self) -> None: """Handle POST /TransferRequests.""" req = json_decode(self.request.body) if 'source' not in req: raise tornado.web.HTTPError(400, reason="missing source field") if 'dest' not in req: raise tornado.web.HTTPError(400, reason="missing dest field") if 'path' not in req: raise tornado.web.HTTPError(400, reason="missing path field") if not isinstance(req['source'], str): raise tornado.web.HTTPError(400, reason="source field is not a string") if not isinstance(req['dest'], str): raise tornado.web.HTTPError(400, reason="dest field is not a string") if not isinstance(req['path'], str): raise tornado.web.HTTPError(400, reason="path field is not a string") if not req['source']: raise tornado.web.HTTPError(400, reason="source field is empty") if not req['dest']: raise tornado.web.HTTPError(400, reason="dest field is empty") if not req['path']: raise tornado.web.HTTPError(400, reason="path field is empty") right_now = now() # https://www.youtube.com/watch?v=He0p5I0b8j8 req['type'] = "TransferRequest" req['uuid'] = unique_id() req['status'] = "unclaimed" req['create_timestamp'] = right_now req['update_timestamp'] = right_now req['work_priority_timestamp'] = right_now req['claimed'] = False logging.debug( f"MONGO-START: db.TransferRequests.insert_one(document={req}") await self.db.TransferRequests.insert_one(document=req) logging.debug("MONGO-END: db.TransferRequests.insert_one(document)") logging.info(f"created TransferRequest {req['uuid']}") self.set_status(201) self.write({'TransferRequest': req['uuid']})
async def post(self): """Handle a new fruit""" body = json_decode(self.request.body) logging.info('body: %r', body) self.fruit[body['name']] = body self.write({})
def response2(req: PreparedRequest, ctx: object) -> bytes: # pylint: disable=W0613 assert req.body is not None _ = json_decode(req.body) return json_encode(result2).encode("utf-8")