Skip to content

Commit cf111aa

Browse files
authored
Merge pull request #411 from opsmill/bkr-fix-count
Add partial_match to the client.count()
2 parents f95d394 + d3f8ae9 commit cf111aa

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

infrahub_sdk/client.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -562,18 +562,27 @@ async def count(
562562
at: Timestamp | None = None,
563563
branch: str | None = None,
564564
timeout: int | None = None,
565+
partial_match: bool = False,
565566
**kwargs: Any,
566567
) -> int:
567568
"""Return the number of nodes of a given kind."""
568-
filters = kwargs
569-
schema = await self.schema.get(kind=kind, branch=branch)
569+
filters: dict[str, Any] = dict(kwargs)
570570

571+
if partial_match:
572+
filters["partial_match"] = True
573+
574+
schema = await self.schema.get(kind=kind, branch=branch)
571575
branch = branch or self.default_branch
572576
if at:
573577
at = Timestamp(at)
574578

579+
data: dict[str, Any] = {
580+
"count": None,
581+
"@filters": filters,
582+
}
583+
575584
response = await self.execute_graphql(
576-
query=Query(query={schema.kind: {"count": None, "@filters": filters}}).render(),
585+
query=Query(query={schema.kind: data}).render(),
577586
branch_name=branch,
578587
at=at,
579588
timeout=timeout,
@@ -801,7 +810,7 @@ async def process_batch() -> tuple[list[InfrahubNode], list[InfrahubNode]]:
801810
nodes = []
802811
related_nodes = []
803812
batch_process = await self.create_batch()
804-
count = await self.count(kind=schema.kind, **filters)
813+
count = await self.count(kind=schema.kind, partial_match=partial_match, **filters)
805814
total_pages = (count + pagination_size - 1) // pagination_size
806815

807816
for page_number in range(1, total_pages + 1):
@@ -1683,18 +1692,27 @@ def count(
16831692
at: Timestamp | None = None,
16841693
branch: str | None = None,
16851694
timeout: int | None = None,
1695+
partial_match: bool = False,
16861696
**kwargs: Any,
16871697
) -> int:
16881698
"""Return the number of nodes of a given kind."""
1689-
filters = kwargs
1690-
schema = self.schema.get(kind=kind, branch=branch)
1699+
filters: dict[str, Any] = dict(kwargs)
16911700

1701+
if partial_match:
1702+
filters["partial_match"] = True
1703+
1704+
schema = self.schema.get(kind=kind, branch=branch)
16921705
branch = branch or self.default_branch
16931706
if at:
16941707
at = Timestamp(at)
16951708

1709+
data: dict[str, Any] = {
1710+
"count": None,
1711+
"@filters": filters,
1712+
}
1713+
16961714
response = self.execute_graphql(
1697-
query=Query(query={schema.kind: {"count": None, "@filters": filters}}).render(),
1715+
query=Query(query={schema.kind: data}).render(),
16981716
branch_name=branch,
16991717
at=at,
17001718
timeout=timeout,
@@ -1957,7 +1975,7 @@ def process_batch() -> tuple[list[InfrahubNodeSync], list[InfrahubNodeSync]]:
19571975
related_nodes = []
19581976
batch_process = self.create_batch()
19591977

1960-
count = self.count(kind=schema.kind, **filters)
1978+
count = self.count(kind=schema.kind, partial_match=partial_match, **filters)
19611979
total_pages = (count + pagination_size - 1) // pagination_size
19621980

19631981
for page_number in range(1, total_pages + 1):

0 commit comments

Comments
 (0)