Skip to content

Commit f0c38ee

Browse files
authored
Merge pull request #779 from superannotateai/fix_cache_repo
Fix cache context checks
2 parents aaefe9b + 65848bd commit f0c38ee

File tree

11 files changed

+102
-58
lines changed

11 files changed

+102
-58
lines changed

pytest.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ minversion = 3.7
33
log_cli=true
44
python_files = test_*.py
55
;pytest_plugins = ['pytest_profiling']
6-
;addopts = -n 6 --dist loadscope
6+
addopts = -n 6 --dist loadscope

src/superannotate/lib/core/serviceproviders.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,13 +851,17 @@ def invite_contributors(
851851

852852
@abstractmethod
853853
def list_custom_field_names(
854-
self, pk, entity: CustomFieldEntityEnum, parent: CustomFieldEntityEnum
854+
self,
855+
context: dict,
856+
entity: CustomFieldEntityEnum,
857+
parent: CustomFieldEntityEnum,
855858
) -> List[str]:
856859
raise NotImplementedError
857860

858861
@abstractmethod
859862
def get_custom_field_id(
860863
self,
864+
context: dict,
861865
field_name: str,
862866
entity: CustomFieldEntityEnum,
863867
parent: CustomFieldEntityEnum,
@@ -867,6 +871,7 @@ def get_custom_field_id(
867871
@abstractmethod
868872
def get_custom_field_name(
869873
self,
874+
context: dict,
870875
field_id: int,
871876
entity: CustomFieldEntityEnum,
872877
parent: CustomFieldEntityEnum,
@@ -884,6 +889,9 @@ def get_custom_field_component_id(
884889

885890
@abstractmethod
886891
def get_custom_fields_templates(
887-
self, entity: CustomFieldEntityEnum, parent: CustomFieldEntityEnum
892+
self,
893+
context: dict,
894+
entity: CustomFieldEntityEnum,
895+
parent: CustomFieldEntityEnum,
888896
):
889897
raise NotImplementedError

src/superannotate/lib/core/usecases/projects.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,9 @@ def execute(self):
154154
else:
155155
project.users = []
156156
if self._include_custom_fields:
157+
context = {"team_id": self._project.team_id}
157158
custom_fields_names = self._service_provider.list_custom_field_names(
158-
self._project.team_id,
159+
context,
159160
entity=CustomFieldEntityEnum.PROJECT,
160161
parent=CustomFieldEntityEnum.TEAM,
161162
)
@@ -173,6 +174,7 @@ def execute(self):
173174
custom_fields_name_value_map = {}
174175
for name in custom_fields_names:
175176
field_id = self._service_provider.get_custom_field_id(
177+
context,
176178
name,
177179
entity=CustomFieldEntityEnum.PROJECT,
178180
parent=CustomFieldEntityEnum.TEAM,

src/superannotate/lib/infrastructure/controller.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,10 @@ def serialize_custom_fields(
8383
entity: CustomFieldEntityEnum,
8484
parent_entity: CustomFieldEntityEnum,
8585
) -> List[dict]:
86-
pk = (
87-
project_id
88-
if entity == CustomFieldEntityEnum.PROJECT
89-
else (team_id if parent_entity == CustomFieldEntityEnum.TEAM else project_id)
90-
)
86+
context = {"team_id": team_id, "project_id": project_id}
9187

9288
existing_custom_fields = service_provider.list_custom_field_names(
93-
pk, entity, parent=parent_entity
89+
context, entity, parent=parent_entity
9490
)
9591
for i in range(len(data)):
9692
if not data[i]:
@@ -111,7 +107,7 @@ def serialize_custom_fields(
111107
field_value /= 1000 # Convert timestamp
112108

113109
new_field_name = service_provider.get_custom_field_name(
114-
field_id, entity=entity, parent=parent_entity
110+
context, field_id, entity=entity, parent=parent_entity
115111
)
116112
updated_fields[new_field_name] = field_value
117113

@@ -151,11 +147,12 @@ def set_custom_field_value(
151147
field_name: str,
152148
value: Any,
153149
):
154-
_context = {}
150+
_context = {"team_id": self.service_provider.client.team_id}
155151
if entity == CustomFieldEntityEnum.PROJECT:
156152
_context["project_id"] = entity_id
153+
157154
template_id = self.service_provider.get_custom_field_id(
158-
field_name, entity=entity, parent=parent_entity
155+
_context, field_name, entity=entity, parent=parent_entity
159156
)
160157
component_id = self.service_provider.get_custom_field_component_id(
161158
template_id, entity=entity, parent=parent_entity
@@ -178,16 +175,17 @@ def set_custom_field_value(
178175
def list_users(
179176
self, include: List[Literal["custom_fields"]] = None, project=None, **filters
180177
):
178+
context = {"team_id": self.service_provider.client.team_id}
181179
if project:
182180
parent_entity = CustomFieldEntityEnum.PROJECT
183-
project_id = project.id
181+
project_id = context["project_id"] = project.id
184182
else:
185183
parent_entity = CustomFieldEntityEnum.TEAM
186184
project_id = None
187185
valid_fields = generate_schema(
188186
UserFilters.__annotations__,
189187
self.service_provider.get_custom_fields_templates(
190-
CustomFieldEntityEnum.CONTRIBUTOR, parent=parent_entity
188+
context, CustomFieldEntityEnum.CONTRIBUTOR, parent=parent_entity
191189
),
192190
)
193191
chain = QueryBuilderChain(
@@ -574,7 +572,9 @@ def list_projects(
574572
valid_fields = generate_schema(
575573
ProjectFilters.__annotations__,
576574
self.service_provider.get_custom_fields_templates(
577-
CustomFieldEntityEnum.PROJECT, parent=CustomFieldEntityEnum.TEAM
575+
{"team_id": self.service_provider.client.team_id},
576+
CustomFieldEntityEnum.PROJECT,
577+
parent=CustomFieldEntityEnum.TEAM,
578578
),
579579
)
580580
chain = QueryBuilderChain(

src/superannotate/lib/infrastructure/query_builder.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,25 +175,18 @@ def __init__(
175175
self._team_id = team_id
176176
self._project_id = project_id
177177

178-
@property
179-
def pk(self):
180-
if self._entity == CustomFieldEntityEnum.PROJECT:
181-
return self._project_id
182-
if self._parent == CustomFieldEntityEnum.TEAM:
183-
return self._team_id
184-
return self._project_id
185-
186178
def _handle_custom_field_key(self, key) -> Tuple[str, str, Optional[str]]:
179+
context = {"team_id": self._team_id, "project_id": self._project_id}
187180
for custom_field in sorted(
188181
self._service_provider.list_custom_field_names(
189-
self.pk, self._entity, parent=self._parent
182+
context, self._entity, parent=self._parent
190183
),
191184
key=len,
192185
reverse=True,
193186
):
194187
if custom_field in key:
195188
custom_field_id = self._service_provider.get_custom_field_id(
196-
custom_field, entity=self._entity, parent=self._parent
189+
context, custom_field, entity=self._entity, parent=self._parent
197190
)
198191
component_id = self._service_provider.get_custom_field_component_id(
199192
custom_field_id, entity=self._entity, parent=self._parent

src/superannotate/lib/infrastructure/serviceprovider.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from lib.infrastructure.services.telemetry_scoring import TelemetryScoringService
2525
from lib.infrastructure.services.work_management import WorkManagementService
2626
from lib.infrastructure.utils import CachedWorkManagementRepository
27+
from lib.infrastructure.utils import EntityContext
2728

2829

2930
class ServiceProvider(BaseServiceProvider):
@@ -72,17 +73,23 @@ def __init__(self, client: HttpClient):
7273
)
7374

7475
def get_custom_fields_templates(
75-
self, entity: CustomFieldEntityEnum, parent: CustomFieldEntityEnum
76+
self,
77+
context: EntityContext,
78+
entity: CustomFieldEntityEnum,
79+
parent: CustomFieldEntityEnum,
7680
):
7781
return self._cached_work_management_repository.list_templates(
78-
self.client.team_id, entity=entity, parent=parent
82+
context, entity=entity, parent=parent
7983
)
8084

8185
def list_custom_field_names(
82-
self, pk, entity: CustomFieldEntityEnum, parent: CustomFieldEntityEnum
86+
self,
87+
context: EntityContext,
88+
entity: CustomFieldEntityEnum,
89+
parent: CustomFieldEntityEnum,
8390
) -> List[str]:
8491
return self._cached_work_management_repository.list_custom_field_names(
85-
pk,
92+
context,
8693
entity=entity,
8794
parent=parent,
8895
)
@@ -96,22 +103,24 @@ def get_category_id(
96103

97104
def get_custom_field_id(
98105
self,
106+
context: EntityContext,
99107
field_name: str,
100108
entity: CustomFieldEntityEnum,
101109
parent: CustomFieldEntityEnum,
102110
) -> int:
103111
return self._cached_work_management_repository.get_custom_field_id(
104-
self.client.team_id, field_name, entity=entity, parent=parent
112+
context, field_name, entity=entity, parent=parent
105113
)
106114

107115
def get_custom_field_name(
108116
self,
117+
context: EntityContext,
109118
field_id: int,
110119
entity: CustomFieldEntityEnum,
111120
parent: CustomFieldEntityEnum,
112121
) -> str:
113122
return self._cached_work_management_repository.get_custom_field_name(
114-
self.client.team_id, field_id, entity=entity, parent=parent
123+
context, field_id, entity=entity, parent=parent
115124
)
116125

117126
def get_custom_field_component_id(
@@ -121,7 +130,7 @@ def get_custom_field_component_id(
121130
parent: CustomFieldEntityEnum,
122131
) -> str:
123132
return self._cached_work_management_repository.get_custom_field_component_id(
124-
self.client.team_id, field_id, entity=entity, parent=parent
133+
{"team_id": self.client.team_id}, field_id, entity=entity, parent=parent
125134
)
126135

127136
def get_role_id(self, project: entities.ProjectEntity, role_name: str) -> int:

src/superannotate/lib/infrastructure/services/work_management.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,7 @@ def set_custom_field_value(
375375
url=self.URL_SET_CUSTOM_ENTITIES.format(pk=entity_id),
376376
method="patch",
377377
headers={
378-
"x-sa-entity-context": self._generate_context(
379-
team_id=self.client.team_id, **context
380-
),
378+
"x-sa-entity-context": self._generate_context(**context),
381379
},
382380
data={"customField": {"custom_field_values": {template_id: data}}},
383381
params={

src/superannotate/lib/infrastructure/stream_data_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,4 @@ def _store_annotation(path, annotation: dict, callback: Callable = None):
174174
def _process_data(self, data):
175175
if data and self._map_function:
176176
return self._map_function(data)
177-
return data
177+
return data

src/superannotate/lib/infrastructure/utils.py

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
import logging
33
import time
4+
import typing
45
from abc import ABC
56
from abc import abstractmethod
67
from functools import wraps
@@ -24,6 +25,11 @@
2425
logger = logging.getLogger("sa")
2526

2627

28+
class EntityContext(typing.TypedDict, total=False):
29+
team_id: int
30+
project_id: Optional[int]
31+
32+
2733
def divide_to_chunks(it, size):
2834
it = iter(it)
2935
return iter(lambda: tuple(islice(it, size)), ())
@@ -324,78 +330,104 @@ def get_annotation_status_name(self, project, status_value: int) -> str:
324330

325331
def get_custom_field_id(
326332
self,
327-
team_id: int,
333+
context: EntityContext,
328334
field_name: str,
329335
entity: CustomFieldEntityEnum,
330336
parent: CustomFieldEntityEnum,
331337
) -> int:
332338
if entity == CustomFieldEntityEnum.PROJECT:
333-
custom_field_data = self._project_custom_field_cache.get(team_id)
339+
custom_field_data = self._project_custom_field_cache.get(context["team_id"])
334340
else:
335341
if parent == CustomFieldEntityEnum.TEAM:
336-
custom_field_data = self._team_user_custom_field_cache.get(team_id)
342+
custom_field_data = self._team_user_custom_field_cache.get(
343+
context["team_id"]
344+
)
337345
else:
338-
custom_field_data = self._project_user_custom_field_cache.get(team_id)
346+
custom_field_data = self._project_user_custom_field_cache.get(
347+
context["project_id"]
348+
)
339349
if field_name in custom_field_data["custom_fields_name_id_map"]:
340350
return custom_field_data["custom_fields_name_id_map"][field_name]
341351
raise AppException("Invalid custom field name provided.")
342352

343353
def get_custom_field_name(
344354
self,
345-
team_id: int,
355+
context: EntityContext,
346356
field_id: int,
347357
entity: CustomFieldEntityEnum,
348358
parent: CustomFieldEntityEnum,
349359
) -> str:
350360
if entity == CustomFieldEntityEnum.PROJECT:
351-
custom_field_data = self._project_custom_field_cache.get(team_id)
361+
custom_field_data = self._project_custom_field_cache.get(context["team_id"])
352362
else:
353363
if parent == CustomFieldEntityEnum.TEAM:
354-
custom_field_data = self._team_user_custom_field_cache.get(team_id)
364+
custom_field_data = self._team_user_custom_field_cache.get(
365+
context["team_id"]
366+
)
355367
else:
356-
custom_field_data = self._project_user_custom_field_cache.get(team_id)
368+
custom_field_data = self._project_user_custom_field_cache.get(
369+
context["project_id"]
370+
)
357371
if field_id in custom_field_data["custom_fields_id_name_map"]:
358372
return custom_field_data["custom_fields_id_name_map"][field_id]
359373
raise AppException("Invalid custom field ID provided.")
360374

361375
def get_custom_field_component_id(
362376
self,
363-
team_id: int,
377+
context: EntityContext,
364378
field_id: int,
365379
entity: CustomFieldEntityEnum,
366380
parent: CustomFieldEntityEnum,
367381
) -> str:
368382
if entity == CustomFieldEntityEnum.PROJECT:
369-
custom_field_data = self._project_custom_field_cache.get(team_id)
383+
custom_field_data = self._project_custom_field_cache.get(context["team_id"])
370384
else:
371385
if parent == CustomFieldEntityEnum.TEAM:
372-
custom_field_data = self._team_user_custom_field_cache.get(team_id)
386+
custom_field_data = self._team_user_custom_field_cache.get(
387+
context["team_id"]
388+
)
373389
else:
374-
custom_field_data = self._project_user_custom_field_cache.get(team_id)
390+
custom_field_data = self._project_user_custom_field_cache.get(
391+
context["project_id"]
392+
)
375393
if field_id in custom_field_data["custom_fields_id_component_id_map"]:
376394
return custom_field_data["custom_fields_id_component_id_map"][field_id]
377395
raise AppException("Invalid custom field ID provided.")
378396

379397
def list_custom_field_names(
380-
self, pk: int, entity: CustomFieldEntityEnum, parent: CustomFieldEntityEnum
398+
self,
399+
context: EntityContext,
400+
entity: CustomFieldEntityEnum,
401+
parent: CustomFieldEntityEnum,
381402
) -> list:
382403
if entity == CustomFieldEntityEnum.PROJECT:
383-
custom_field_data = self._project_custom_field_cache.get(pk)
404+
custom_field_data = self._project_custom_field_cache.get(context["team_id"])
384405
else:
385406
if parent == CustomFieldEntityEnum.TEAM:
386-
custom_field_data = self._team_user_custom_field_cache.get(pk)
407+
custom_field_data = self._team_user_custom_field_cache.get(
408+
context["team_id"]
409+
)
387410
else:
388-
custom_field_data = self._project_user_custom_field_cache.get(pk)
411+
custom_field_data = self._project_user_custom_field_cache.get(
412+
context["project_id"]
413+
)
389414
return list(custom_field_data["custom_fields_name_id_map"].keys())
390415

391416
def list_templates(
392-
self, pk: int, entity: CustomFieldEntityEnum, parent: CustomFieldEntityEnum
417+
self,
418+
context: EntityContext,
419+
entity: CustomFieldEntityEnum,
420+
parent: CustomFieldEntityEnum,
393421
):
394422
if entity == CustomFieldEntityEnum.PROJECT:
395-
return self._project_custom_field_cache.get(pk)["templates"]
423+
return self._project_custom_field_cache.get(context["team_id"])["templates"]
396424
elif entity == CustomFieldEntityEnum.CONTRIBUTOR:
397425
if parent == CustomFieldEntityEnum.TEAM:
398-
return self._team_user_custom_field_cache.get(pk)["templates"]
426+
return self._team_user_custom_field_cache.get(context["team_id"])[
427+
"templates"
428+
]
399429
else:
400-
return self._project_user_custom_field_cache.get(pk)["templates"]
430+
return self._project_user_custom_field_cache.get(context["project_id"])[
431+
"templates"
432+
]
401433
raise AppException("Invalid entity provided.")

0 commit comments

Comments
 (0)