2020 BranchAllocationPublic ,
2121 BranchProvisioning ,
2222 EntityType ,
23+ OrganizationLimitDefault ,
2324 ProvisioningLog ,
2425 ResourceLimit ,
2526 ResourceLimitsPublic ,
@@ -134,26 +135,6 @@ async def clone_branch_provisioning(session: SessionDep, source: Branch, target:
134135 await session .refresh (target )
135136
136137
137- async def initialize_organization_resource_limits (session : SessionDep , organization : Organization ):
138- result = await session .execute (select (ResourceLimit ).where (ResourceLimit .entity_type == EntityType .system ))
139- system_limits = result .scalars ().all ()
140-
141- with session .no_autoflush :
142- for system_limit in system_limits :
143- await session .merge (
144- ResourceLimit (
145- entity_type = EntityType .org ,
146- org_id = organization .id ,
147- project_id = None ,
148- resource = system_limit .resource ,
149- max_total = system_limit .max_total ,
150- max_per_branch = system_limit .max_per_branch ,
151- )
152- )
153- await session .commit ()
154- await session .refresh (organization )
155-
156-
157138def dict_to_resource_limits (value : Mapping [ResourceType , int | None ]) -> ResourceLimitsPublic :
158139 return ResourceLimitsPublic (
159140 milli_vcpu = value .get (ResourceType .milli_vcpu ),
@@ -320,7 +301,6 @@ async def get_remaining_project_resources(
320301 * ,
321302 exclude_branch_ids : Sequence [Identifier ] | None = None ,
322303) -> ResourceLimitsPublic :
323- system_limits = await get_system_resource_limits (session )
324304 organization_limits = await get_organization_resource_limits (session , organization_id )
325305 project_limits = await get_project_resource_limits (session , project_id )
326306
@@ -337,16 +317,13 @@ async def get_remaining_project_resources(
337317
338318 effective_limits : dict [ResourceType , int ] = {}
339319 for resource_type in ResourceType :
340- system_limit = system_limits .get (resource_type )
341320 organization_limit = organization_limits .get (resource_type )
342321 project_limit = project_limits .get (resource_type )
343322 per_branch_limit = (
344323 project_limit .max_per_branch
345- if project_limit and project_limit . max_per_branch is not None
324+ if project_limit
346325 else organization_limit .max_per_branch
347- if organization_limit and organization_limit .max_per_branch is not None
348- else system_limit .max_per_branch
349- if system_limit and system_limit .max_per_branch is not None
326+ if organization_limit
350327 else None
351328 )
352329
@@ -374,17 +351,6 @@ async def get_remaining_project_resources(
374351 return dict_to_resource_limits (effective_limits )
375352
376353
377- async def get_system_resource_limits (session : SessionDep ) -> dict [ResourceType , ResourceLimit ]:
378- result = await session .execute (
379- select (ResourceLimit ).where (
380- ResourceLimit .entity_type == EntityType .system ,
381- ResourceLimit .org_id .is_ (None ), # type: ignore[union-attr]
382- ResourceLimit .project_id .is_ (None ), # type: ignore[union-attr]
383- )
384- )
385- return _map_resource_limits (list (result .scalars ().all ()))
386-
387-
388354async def get_organization_resource_limits (
389355 session : SessionDep , organization_id : Identifier
390356) -> dict [ResourceType , ResourceLimit ]:
@@ -638,6 +604,18 @@ def from_database(cls, limits: Sequence[ResourceLimit]) -> Self:
638604 per_branch = Resources .from_database (limits , "max_per_branch" ),
639605 )
640606
607+ @classmethod
608+ def from_defaults (cls , defaults : Sequence ["OrganizationLimitDefault" ]) -> Self :
609+ return cls (
610+ total = Resources .from_database (defaults , "max_total" ),
611+ per_branch = Resources .from_database (defaults , "max_per_branch" ),
612+ )
613+
614+ @classmethod
615+ async def organization_defaults (cls , session : AsyncSession ) -> Self :
616+ result = await session .execute (select (OrganizationLimitDefault ))
617+ return cls .from_defaults (list (result .scalars ().all ()))
618+
641619 def to_database (self , entity_type : EntityType ) -> list [ResourceLimit ]:
642620 return [
643621 ResourceLimit (
@@ -651,14 +629,6 @@ def to_database(self, entity_type: EntityType) -> list[ResourceLimit]:
651629 ]
652630
653631
654- async def system_limits (session : AsyncSession ) -> Limits :
655- statement = select (ResourceLimit ).where (
656- col (ResourceLimit .org_id ).is_ (None ), col (ResourceLimit .project_id ).is_ (None )
657- )
658- entries = (await session .exec (statement )).all ()
659- return Limits .from_database (entries )
660-
661-
662632async def organization_limits (organization : Organization ) -> Limits :
663633 return Limits .from_database (await organization .awaitable_attrs .limits )
664634
@@ -683,11 +653,6 @@ def _allocations():
683653 )
684654
685655
686- async def system_allocations (session : AsyncSession ) -> Resources :
687- allocations = (await session .exec (_allocations ())).one ()
688- return Resources (** (allocations ._asdict ()))
689-
690-
691656async def organization_allocations (session : AsyncSession , organization : Organization ) -> Resources :
692657 statement = _allocations ().join (Project ).where (Project .organization_id == organization .id )
693658 allocations = (await session .exec (statement )).one ()
@@ -700,15 +665,8 @@ async def project_allocations(session: AsyncSession, project: Project) -> Resour
700665 return Resources (** (allocations ._asdict ()))
701666
702667
703- async def system_available (session : AsyncSession ) -> Resources :
704- return (await system_limits (session )).total - await system_allocations (session )
705-
706-
707668async def organization_available (session : AsyncSession , organization : Organization ) -> Resources :
708- return Resources .min (
709- (await organization_limits (organization )).total - await organization_allocations (session , organization ),
710- await system_available (session ),
711- )
669+ return (await organization_limits (organization )).total - await organization_allocations (session , organization )
712670
713671
714672async def project_available (session : AsyncSession , project : Project ) -> Resources :
@@ -718,16 +676,13 @@ async def project_available(session: AsyncSession, project: Project) -> Resource
718676 )
719677
720678
721- async def project_branch_maxima (session : AsyncSession , project : Project ) -> Resources :
722- """Minimum per-branch limit across the hierarchy (project > organization > system ).
679+ async def project_branch_maxima (project : Project ) -> Resources :
680+ """Minimum per-branch limit across the hierarchy (project > organization).
723681
724682 Returns None for any field where no per-branch limit has been configured at any level.
725683 """
726684 organization = await project .awaitable_attrs .organization
727685 return Resources .min (
728- Resources .min (
729- (await project_limits (project )).per_branch ,
730- (await organization_limits (organization )).per_branch ,
731- ),
732- (await system_limits (session )).per_branch ,
686+ (await project_limits (project )).per_branch ,
687+ (await organization_limits (organization )).per_branch ,
733688 )
0 commit comments