perf(access): batch tenant summary counts
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy import create_engine, event
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from govoplan_access.backend.administration import SqlAccessAdministration
|
||||
from govoplan_access.backend.api.v1.admin_common import (
|
||||
_accounts_by_user_id,
|
||||
_group_member_ids_by_group_id,
|
||||
@@ -13,7 +15,7 @@ from govoplan_access.backend.api.v1.admin_common import (
|
||||
_roles_by_user_id,
|
||||
_tenant_role_assignment_counts,
|
||||
)
|
||||
from govoplan_access.backend.db.models import Account, Group, GroupRoleAssignment, Role, User, UserGroupMembership, UserRoleAssignment
|
||||
from govoplan_access.backend.db.models import Account, ApiKey, Group, GroupRoleAssignment, Role, User, UserGroupMembership, UserRoleAssignment
|
||||
from govoplan_core.db.base import Base
|
||||
|
||||
|
||||
@@ -56,6 +58,92 @@ class AdminBatchHelperTests(unittest.TestCase):
|
||||
self.assertEqual([item.id for item in roles_by_user[user.id]], [role.id])
|
||||
self.assertEqual(role_counts, {role.id: (1, 1)})
|
||||
|
||||
def test_tenant_counts_many_uses_three_grouped_queries(self) -> None:
|
||||
accounts = [
|
||||
Account(
|
||||
id=f"account-{index}",
|
||||
email=f"user-{index}@example.test",
|
||||
normalized_email=f"user-{index}@example.test",
|
||||
)
|
||||
for index in range(3)
|
||||
]
|
||||
users = [
|
||||
User(
|
||||
id="user-1",
|
||||
tenant_id="tenant-1",
|
||||
account_id=accounts[0].id,
|
||||
email=accounts[0].email,
|
||||
),
|
||||
User(
|
||||
id="user-2",
|
||||
tenant_id="tenant-1",
|
||||
account_id=accounts[1].id,
|
||||
email=accounts[1].email,
|
||||
is_active=False,
|
||||
),
|
||||
User(
|
||||
id="user-3",
|
||||
tenant_id="tenant-2",
|
||||
account_id=accounts[2].id,
|
||||
email=accounts[2].email,
|
||||
),
|
||||
]
|
||||
self.session.add_all(
|
||||
[
|
||||
*accounts,
|
||||
*users,
|
||||
Group(id="group-1", tenant_id="tenant-1", slug="one", name="One"),
|
||||
Group(id="group-2", tenant_id="tenant-2", slug="two", name="Two"),
|
||||
ApiKey(
|
||||
id="key-1",
|
||||
tenant_id="tenant-1",
|
||||
user_id="user-1",
|
||||
name="Active",
|
||||
prefix="active",
|
||||
key_hash="hash-1",
|
||||
),
|
||||
ApiKey(
|
||||
id="key-2",
|
||||
tenant_id="tenant-1",
|
||||
user_id="user-2",
|
||||
name="Revoked",
|
||||
prefix="revoked",
|
||||
key_hash="hash-2",
|
||||
revoked_at=datetime.now(UTC),
|
||||
),
|
||||
]
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
query_count = 0
|
||||
|
||||
def count_query(*_args: object) -> None:
|
||||
nonlocal query_count
|
||||
query_count += 1
|
||||
|
||||
event.listen(self.engine, "before_cursor_execute", count_query)
|
||||
try:
|
||||
counts = SqlAccessAdministration().tenant_counts_many(
|
||||
self.session,
|
||||
["tenant-1", "tenant-2", "tenant-empty"],
|
||||
)
|
||||
finally:
|
||||
event.remove(self.engine, "before_cursor_execute", count_query)
|
||||
|
||||
self.assertEqual(3, query_count)
|
||||
self.assertEqual(
|
||||
{
|
||||
"users": 2,
|
||||
"active_users": 1,
|
||||
"groups": 1,
|
||||
"api_keys": 2,
|
||||
"active_api_keys": 1,
|
||||
},
|
||||
counts["tenant-1"],
|
||||
)
|
||||
self.assertEqual(1, counts["tenant-2"]["users"])
|
||||
self.assertEqual(0, counts["tenant-empty"]["users"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user