Skip to content

Commit 12196ca

Browse files
committed
python(feat): Reduce API calls when creating rules
1 parent a1d2504 commit 12196ca

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

python/lib/sift_py/_internal/channel.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import functools
12
import warnings
23
from typing import List, Optional, cast
34

@@ -15,6 +16,7 @@ def channel_fqn(name: str, component: Optional[str] = None) -> str:
1516
return name if component is None or len(component) == 0 else f"{component}.{name}"
1617

1718

19+
@functools.cache
1820
def get_channels(
1921
channel_service: ChannelServiceStub,
2022
filter: str,

python/lib/sift_py/_internal/user.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import functools
12
from typing import List, cast
23

34
from sift.common.type.v1.user_pb2 import User
45
from sift.users.v2.users_pb2 import ListActiveUsersRequest, ListActiveUsersResponse
56
from sift.users.v2.users_pb2_grpc import UserServiceStub
67

78

9+
@functools.cache
810
def get_active_users(
911
user_service: UserServiceStub,
1012
filter: str,

python/lib/sift_py/rule/service.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from __future__ import annotations
22

3+
import functools
34
from dataclasses import dataclass
45
from pathlib import Path
5-
from typing import Any, Dict, List, Optional, Union, cast
6+
from typing import Any, Dict, List, Optional, Tuple, Union, cast
67

78
from sift.annotations.v1.annotations_pb2 import AnnotationType
89
from sift.assets.v1.assets_pb2 import Asset
@@ -262,7 +263,7 @@ def detach_asset(self, rule: Union[str, RuleConfig], asset_names: List[str]) ->
262263
def _attach_or_detach_asset(
263264
self, rule: Union[str, RuleConfig], asset_names: List[str], attach: bool
264265
) -> RuleConfig:
265-
assets = self._get_assets(names=asset_names)
266+
assets = self._get_assets(names=tuple(sorted(asset_names)))
266267
if not assets:
267268
raise ValueError(
268269
f"Cannot find all assets in list '{asset_names}'. One of these assets does not exist."
@@ -388,7 +389,11 @@ def _update_req_from_rule_config(
388389
)
389390

390391
# TODO: once we have TagService_ListTags we can do asset-agnostic rules via tags
391-
assets = self._get_assets(names=config.asset_names) if config.asset_names else None
392+
assets = (
393+
self._get_assets(names=tuple(sorted(config.asset_names)))
394+
if config.asset_names
395+
else None
396+
)
392397

393398
actions = []
394399
if config.action.kind() == RuleActionKind.NOTIFICATION:
@@ -557,7 +562,7 @@ def get_rule(self, rule: str) -> Optional[RuleConfig]:
557562
)
558563

559564
assets = self._get_assets(
560-
ids=[asset_id for asset_id in rule_pb.asset_configuration.asset_ids]
565+
ids=tuple(sorted([asset_id for asset_id in rule_pb.asset_configuration.asset_ids]))
561566
)
562567
asset_names = [asset.name for asset in assets]
563568

@@ -597,7 +602,8 @@ def _get_rule_from_rule_id(self, rule_id: str) -> Optional[Rule]:
597602
except:
598603
return None
599604

600-
def _get_assets(self, names: List[str] = [], ids: List[str] = []) -> List[Asset]:
605+
@functools.cache
606+
def _get_assets(self, names: Tuple[str] = [], ids: Tuple[str] = []) -> List[Asset]:
601607
return list_assets_impl(self._asset_service_stub, names, ids)
602608

603609

0 commit comments

Comments
 (0)