Skip to content

Commit b8ca5d6

Browse files
committed
merge quota set & add methods into one quota update method
1 parent 856ef6e commit b8ca5d6

File tree

1 file changed

+47
-92
lines changed

1 file changed

+47
-92
lines changed

backend/btrixcloud/orgs.py

Lines changed: 47 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
Dict,
2020
Callable,
2121
List,
22+
Literal,
2223
AsyncGenerator,
2324
Any,
2425
)
@@ -611,71 +612,16 @@ async def update_proxies(self, org: Organization, proxies: OrgProxies) -> None:
611612
)
612613

613614
async def update_quotas(
614-
self, org: Organization, quotas: OrgQuotasIn, context: str | None = None
615+
self,
616+
org: Organization,
617+
quotas: OrgQuotasIn,
618+
mode: Literal["set", "add"],
619+
context: str | None = None,
615620
) -> None:
616621
"""update organization quotas"""
617622

618-
previous_extra_mins = (
619-
org.quotas.extraExecMinutes
620-
if (org.quotas and org.quotas.extraExecMinutes)
621-
else 0
622-
)
623-
previous_gifted_mins = (
624-
org.quotas.giftedExecMinutes
625-
if (org.quotas and org.quotas.giftedExecMinutes)
626-
else 0
627-
)
628-
629623
quotas.context = None
630624

631-
update = quotas.dict(
632-
exclude_unset=True, exclude_defaults=True, exclude_none=True
633-
)
634-
635-
await self.orgs.find_one_and_update(
636-
{"_id": org.id},
637-
{
638-
"$set": {
639-
"quotas": update,
640-
},
641-
"$push": {
642-
"quotaUpdates": OrgQuotaUpdate(
643-
update=OrgQuotas(**update), modified=dt_now(), context=context
644-
).model_dump()
645-
},
646-
},
647-
)
648-
649-
# Inc org available fields for extra/gifted execution time as needed
650-
if quotas.extraExecMinutes is not None:
651-
extra_secs_diff = (quotas.extraExecMinutes - previous_extra_mins) * 60
652-
if org.extraExecSecondsAvailable + extra_secs_diff <= 0:
653-
await self.orgs.find_one_and_update(
654-
{"_id": org.id},
655-
{"$set": {"extraExecSecondsAvailable": 0}},
656-
)
657-
else:
658-
await self.orgs.find_one_and_update(
659-
{"_id": org.id},
660-
{"$inc": {"extraExecSecondsAvailable": extra_secs_diff}},
661-
)
662-
663-
if quotas.giftedExecMinutes is not None:
664-
gifted_secs_diff = (quotas.giftedExecMinutes - previous_gifted_mins) * 60
665-
if org.giftedExecSecondsAvailable + gifted_secs_diff <= 0:
666-
await self.orgs.find_one_and_update(
667-
{"_id": org.id},
668-
{"$set": {"giftedExecSecondsAvailable": 0}},
669-
)
670-
else:
671-
await self.orgs.find_one_and_update(
672-
{"_id": org.id},
673-
{"$inc": {"giftedExecSecondsAvailable": gifted_secs_diff}},
674-
)
675-
676-
async def add_to_org_quotas(
677-
self, org: Organization, quotas: OrgQuotasIn, context: str | None
678-
):
679625
previous_extra_mins = (
680626
org.quotas.extraExecMinutes
681627
if (org.quotas and org.quotas.extraExecMinutes)
@@ -687,56 +633,65 @@ async def add_to_org_quotas(
687633
else 0
688634
)
689635

690-
update: dict[str, Any] = {
691-
"$inc": {},
692-
}
636+
if mode == "add":
637+
increment_update: dict[str, Any] = {
638+
"$inc": {},
639+
}
693640

694-
for field, value in quotas.model_dump(exclude_unset=True).items():
695-
if field == "context" or value is None:
696-
continue
697-
inc = max(value, -org.quotas.model_dump().get(field, 0))
698-
update["$inc"][f"quotas.{field}"] = inc
641+
for field, value in quotas.model_dump(
642+
exclude_unset=True, exclude_defaults=True, exclude_none=True
643+
).items():
644+
if field == "context" or value is None:
645+
continue
646+
inc = max(value, -org.quotas.model_dump().get(field, 0))
647+
increment_update["$inc"][f"quotas.{field}"] = inc
699648

700-
updated_org = await self.orgs.find_one_and_update(
701-
{"_id": org.id},
702-
update,
703-
projection={"quotas": True},
704-
return_document=ReturnDocument.AFTER,
705-
)
706-
updated_quotas = OrgQuotas(**updated_org["quotas"])
649+
updated_org = await self.orgs.find_one_and_update(
650+
{"_id": org.id},
651+
increment_update,
652+
projection={"quotas": True},
653+
return_document=ReturnDocument.AFTER,
654+
)
655+
quotas = OrgQuotasIn(**updated_org["quotas"])
707656

708-
quotaUpdate: dict[str, dict[str, dict[str, Any] | int]] = {
657+
update: dict[str, dict[str, dict[str, Any] | int]] = {
709658
"$push": {
710659
"quotaUpdates": OrgQuotaUpdate(
711660
modified=dt_now(),
712-
update=updated_quotas,
661+
update=OrgQuotas(
662+
**quotas.model_dump(
663+
exclude_unset=True, exclude_defaults=True, exclude_none=True
664+
)
665+
),
713666
context=context,
714667
).model_dump()
715668
},
716669
"$inc": {},
717670
"$set": {},
718671
}
719672

673+
if mode == "set":
674+
increment_update = quotas.model_dump(
675+
exclude_unset=True, exclude_defaults=True, exclude_none=True
676+
)
677+
update["$set"]["quotas"] = increment_update
678+
720679
# Inc org available fields for extra/gifted execution time as needed
721-
if updated_quotas.extraExecMinutes is not None:
722-
extra_secs_diff = (
723-
updated_quotas.extraExecMinutes - previous_extra_mins
724-
) * 60
680+
if quotas.extraExecMinutes is not None:
681+
extra_secs_diff = (quotas.extraExecMinutes - previous_extra_mins) * 60
725682
if org.extraExecSecondsAvailable + extra_secs_diff <= 0:
726-
quotaUpdate["$set"]["extraExecSecondsAvailable"] = 0
683+
update["$set"]["extraExecSecondsAvailable"] = 0
727684
else:
728-
quotaUpdate["$inc"]["extraExecSecondsAvailable"] = extra_secs_diff
685+
update["$inc"]["extraExecSecondsAvailable"] = extra_secs_diff
729686

730-
if updated_quotas.giftedExecMinutes is not None:
731-
gifted_secs_diff = (
732-
updated_quotas.giftedExecMinutes - previous_gifted_mins
733-
) * 60
687+
if quotas.giftedExecMinutes is not None:
688+
gifted_secs_diff = (quotas.giftedExecMinutes - previous_gifted_mins) * 60
734689
if org.giftedExecSecondsAvailable + gifted_secs_diff <= 0:
735-
quotaUpdate["$set"]["giftedExecSecondsAvailable"] = 0
690+
update["$set"]["giftedExecSecondsAvailable"] = 0
736691
else:
737-
quotaUpdate["$inc"]["giftedExecSecondsAvailable"] = gifted_secs_diff
692+
update["$inc"]["giftedExecSecondsAvailable"] = gifted_secs_diff
738693

739-
await self.orgs.find_one_and_update({"_id": org.id}, quotaUpdate)
694+
await self.orgs.find_one_and_update({"_id": org.id}, update)
740695

741696
async def update_event_webhook_urls(
742697
self, org: Organization, urls: OrgWebhookUrls
@@ -1751,7 +1706,7 @@ async def update_quotas(
17511706
if not user.is_superuser:
17521707
raise HTTPException(status_code=403, detail="Not Allowed")
17531708

1754-
await ops.update_quotas(org, quotas, quotas.context)
1709+
await ops.update_quotas(org, quotas, mode="set", context=quotas.context)
17551710

17561711
return {"updated": True}
17571712

@@ -1762,7 +1717,7 @@ async def update_quotas_add(
17621717
quotas: OrgQuotasIn,
17631718
org: Organization = Depends(org_superuser_or_shared_secret_dep),
17641719
):
1765-
await ops.add_to_org_quotas(org, quotas, quotas.context)
1720+
await ops.update_quotas(org, quotas, mode="add", context=quotas.context)
17661721

17671722
return {"updated": True}
17681723

0 commit comments

Comments
 (0)