|
87 | 87 | Scatter,
|
88 | 88 | Scatters,
|
89 | 89 | SearchTableRequest,
|
| 90 | + KindUsage, |
90 | 91 | )
|
91 | 92 | from fixbackend.logging_context import set_cloud_account_id, set_fix_cloud_account_id, set_workspace_id
|
92 | 93 | from fixbackend.types import Redis
|
@@ -860,35 +861,29 @@ async def overall_score() -> Tuple[int, int]:
|
860 | 861 |
|
861 | 862 | return await self.cache.call(compute_inventory_info, key=str(dba.workspace_id))(duration)
|
862 | 863 |
|
863 |
| - async def descendant_summary( |
864 |
| - self, dba: GraphDatabaseAccess, kind: Literal["account", "region", "zone"] |
865 |
| - ) -> Dict[str, Dict[str, int]]: |
| 864 | + async def descendant_summary(self, dba: GraphDatabaseAccess) -> Dict[str, KindUsage]: |
866 | 865 |
|
867 |
| - async def compute_descendant_summary(on: str) -> Dict[str, Dict[str, int]]: |
868 |
| - result: Dict[str, Dict[str, int]] = defaultdict(lambda: defaultdict(int)) |
869 |
| - async with self.client.search(dba, f"is({on}) and /metadata.descendant_count>0") as response: |
| 866 | + async def compute_descendant_summary() -> Dict[str, KindUsage]: |
| 867 | + account_usage: Dict[str, Dict[str, int]] = defaultdict(lambda: defaultdict(int)) |
| 868 | + region_usage: Dict[str, Dict[str, int]] = defaultdict(lambda: defaultdict(int)) |
| 869 | + async with self.client.search(dba, "is(account) and /metadata.descendant_count>0") as response: |
870 | 870 | async for acc in response:
|
871 |
| - if level_name := value_in_path(acc, "reported.id"): |
| 871 | + if account_id := value_in_path(acc, "reported.id"): |
872 | 872 | descendant_summary = value_in_path(acc, "metadata.descendant_summary") or {}
|
873 | 873 | for descendant_kind, count in descendant_summary.items():
|
874 |
| - result[level_name][descendant_kind] += count |
875 |
| - return result |
876 |
| - |
877 |
| - return await self.cache.call(compute_descendant_summary, key=str(dba.workspace_id))(kind) |
878 |
| - |
879 |
| - async def descendant_count_by( |
880 |
| - self, dba: GraphDatabaseAccess, kind: Literal["account", "region", "zone"] |
881 |
| - ) -> Dict[str, int]: |
882 |
| - |
883 |
| - async def compute_descendant_count_by(on: str) -> Dict[str, int]: |
884 |
| - counter: Dict[str, Dict[str, int]] = defaultdict(lambda: defaultdict(int)) |
885 |
| - async with self.client.search(dba, f"is({on}) and /metadata.descendant_count>0") as response: |
| 874 | + account_usage[descendant_kind][account_id] += count |
| 875 | + async with self.client.search(dba, "is(region) and /metadata.descendant_count>0") as response: |
886 | 876 | async for acc in response:
|
887 |
| - if level_name := value_in_path(acc, "reported.id"): |
| 877 | + if region_id := value_in_path(acc, "reported.id"): |
888 | 878 | descendant_summary = value_in_path(acc, "metadata.descendant_summary") or {}
|
889 |
| - for descendant_kind in descendant_summary: |
890 |
| - counter[descendant_kind][level_name] = 1 |
891 |
| - |
892 |
| - return {k: len(v) for k, v in counter.items()} |
893 |
| - |
894 |
| - return await self.cache.call(compute_descendant_count_by, key=str(dba.workspace_id))(kind) |
| 879 | + for descendant_kind, count in descendant_summary.items(): |
| 880 | + region_usage[descendant_kind][region_id] += count |
| 881 | + kind_usage: Dict[str, KindUsage] = defaultdict(KindUsage) |
| 882 | + for kind, accounts in account_usage.items(): |
| 883 | + kind_usage[kind].accounts = len(accounts) |
| 884 | + kind_usage[kind].resources = sum(accounts.values()) |
| 885 | + for kind, regions in region_usage.items(): |
| 886 | + kind_usage[kind].regions = len(regions) |
| 887 | + return kind_usage |
| 888 | + |
| 889 | + return await self.cache.call(compute_descendant_summary, key=str(dba.workspace_id))() |
0 commit comments