Skip to content

Commit 1f33f54

Browse files
authored
[feat] Provide one summary for all kinds (#628)
1 parent 8e49b7d commit 1f33f54

File tree

3 files changed

+33
-37
lines changed

3 files changed

+33
-37
lines changed

fixbackend/inventory/inventory_router.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
SearchTableRequest,
3838
TimeseriesRequest,
3939
Scatters,
40+
KindUsage,
4041
)
4142
from fixbackend.streaming_response import streaming_response, StreamOnSuccessResponse
4243
from fixbackend.workspaces.dependencies import UserWorkspaceDependency
@@ -167,6 +168,7 @@ async def summary(graph_db: CurrentGraphDbDependency, workspace: UserWorkspaceDe
167168
async def model(
168169
graph_db: CurrentGraphDbDependency,
169170
kind: Optional[List[str]] = Query(default=None, description="Kinds to return."),
171+
kind_filter: Optional[List[str]] = Query(default=None, description="Kind filter to apply."),
170172
with_bases: bool = Query(default=False, description="Include base kinds."),
171173
with_property_kinds: bool = Query(default=False, description="Include property kinds."),
172174
aggregate_roots_only: bool = Query(default=True, description="Include only aggregate roots."),
@@ -179,6 +181,7 @@ async def model(
179181
graph_db,
180182
result_format="simple",
181183
kind=kind,
184+
kind_filter=kind_filter,
182185
with_bases=with_bases,
183186
with_property_kinds=with_property_kinds,
184187
aggregate_roots_only=aggregate_roots_only,
@@ -410,16 +413,8 @@ async def timeseries(graph_db: CurrentGraphDbDependency, ts: TimeseriesRequest)
410413
aggregation=ts.aggregation,
411414
)
412415

413-
@router.get("/descendant/summary/{level}", tags=["timeseries"])
414-
async def descendant_summary_account(
415-
graph_db: CurrentGraphDbDependency, level: Literal["account", "region", "zone"]
416-
) -> Dict[str, Dict[str, int]]:
417-
return await inventory().descendant_summary(graph_db, level)
418-
419-
@router.get("/descendant/count/{level}", tags=["timeseries"])
420-
async def descendant_count_account(
421-
graph_db: CurrentGraphDbDependency, level: Literal["account", "region", "zone"]
422-
) -> Dict[str, int]:
423-
return await inventory().descendant_count_by(graph_db, level)
416+
@router.get("/descendant/summary", tags=["search"])
417+
async def descendant_summary_account(graph_db: CurrentGraphDbDependency) -> Dict[str, KindUsage]:
418+
return await inventory().descendant_summary(graph_db)
424419

425420
return router

fixbackend/inventory/inventory_schemas.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,9 @@ class TimeseriesRequest(BaseModel):
270270
group: Optional[Set[str]] = None
271271
filter_group: Optional[List[str]] = None
272272
aggregation: Optional[str] = None
273+
274+
275+
class KindUsage(BaseModel):
276+
accounts: int = Field(default=0, description="The number of accounts using this kind.")
277+
regions: int = Field(default=0, description="The number of regions using this kind.")
278+
resources: int = Field(default=0, description="The number of resources of this kind.")

fixbackend/inventory/inventory_service.py

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
Scatter,
8888
Scatters,
8989
SearchTableRequest,
90+
KindUsage,
9091
)
9192
from fixbackend.logging_context import set_cloud_account_id, set_fix_cloud_account_id, set_workspace_id
9293
from fixbackend.types import Redis
@@ -860,35 +861,29 @@ async def overall_score() -> Tuple[int, int]:
860861

861862
return await self.cache.call(compute_inventory_info, key=str(dba.workspace_id))(duration)
862863

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]:
866865

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:
870870
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"):
872872
descendant_summary = value_in_path(acc, "metadata.descendant_summary") or {}
873873
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:
886876
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"):
888878
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

Comments
 (0)