perf(access): batch tenant summary counts
This commit is contained in:
@@ -42,6 +42,69 @@ class SqlAccessAdministration(AccessAdministration):
|
||||
"active_api_keys": int(active_api_keys),
|
||||
}
|
||||
|
||||
def tenant_counts_many(
|
||||
self,
|
||||
session: object,
|
||||
tenant_ids: Sequence[str],
|
||||
) -> Mapping[str, Mapping[str, int]]:
|
||||
ids = tuple(dict.fromkeys(str(tenant_id) for tenant_id in tenant_ids if tenant_id))
|
||||
if not ids:
|
||||
return {}
|
||||
db = _session(session)
|
||||
counts: dict[str, dict[str, int]] = {
|
||||
tenant_id: {
|
||||
"users": 0,
|
||||
"active_users": 0,
|
||||
"groups": 0,
|
||||
"api_keys": 0,
|
||||
"active_api_keys": 0,
|
||||
}
|
||||
for tenant_id in ids
|
||||
}
|
||||
user_rows = (
|
||||
db.query(
|
||||
User.tenant_id,
|
||||
func.count(User.id),
|
||||
func.coalesce(
|
||||
func.sum(case((User.is_active.is_(True), 1), else_=0)),
|
||||
0,
|
||||
),
|
||||
)
|
||||
.filter(User.tenant_id.in_(ids))
|
||||
.group_by(User.tenant_id)
|
||||
.all()
|
||||
)
|
||||
for tenant_id, users, active_users in user_rows:
|
||||
counts[tenant_id]["users"] = int(users)
|
||||
counts[tenant_id]["active_users"] = int(active_users)
|
||||
|
||||
group_rows = (
|
||||
db.query(Group.tenant_id, func.count(Group.id))
|
||||
.filter(Group.tenant_id.in_(ids))
|
||||
.group_by(Group.tenant_id)
|
||||
.all()
|
||||
)
|
||||
for tenant_id, groups in group_rows:
|
||||
counts[tenant_id]["groups"] = int(groups)
|
||||
|
||||
api_key_rows = (
|
||||
db.query(
|
||||
ApiKey.tenant_id,
|
||||
func.count(ApiKey.id),
|
||||
func.coalesce(
|
||||
func.sum(case((ApiKey.revoked_at.is_(None), 1), else_=0)),
|
||||
0,
|
||||
),
|
||||
)
|
||||
.filter(ApiKey.tenant_id.in_(ids))
|
||||
.group_by(ApiKey.tenant_id)
|
||||
.all()
|
||||
)
|
||||
for tenant_id, api_keys, active_api_keys in api_key_rows:
|
||||
counts[tenant_id]["api_keys"] = int(api_keys)
|
||||
counts[tenant_id]["active_api_keys"] = int(active_api_keys)
|
||||
return counts
|
||||
|
||||
def system_account_count(self, session: object) -> int:
|
||||
db = _session(session)
|
||||
return db.query(Account).count()
|
||||
|
||||
Reference in New Issue
Block a user