Skip to content

Commit 3443888

Browse files
committed
fodlder_id fix
1 parent 2d81323 commit 3443888

File tree

2 files changed

+104
-119
lines changed

2 files changed

+104
-119
lines changed

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

Lines changed: 67 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,7 @@ def execute(self):
963963
self.reporter.warning_messages("Empty scores.")
964964
return self._response
965965

966+
966967
class DownloadAnnotations(BaseReportableUseCase):
967968
def __init__(
968969
self,
@@ -1053,96 +1054,100 @@ def coroutine_wrapper(coroutine):
10531054
loop.close()
10541055
return count
10551056

1056-
async def _download_big_annotation(self, item, export_path):
1057+
async def _download_big_annotation(self, item, export_path, folder_id):
10571058
postfix = self.get_postfix()
1058-
response = await self._backend_client.download_big_annotation(
1059-
item = item,
1059+
await self._backend_client.download_big_annotation(
1060+
item=item,
10601061
team_id=self._project.team_id,
10611062
project_id=self._project.id,
1062-
folder_id=self._folder.uuid,
1063+
folder_id=folder_id,
10631064
reporter=self.reporter,
10641065
download_path=f"{export_path}{'/' + self._folder.name if not self._folder.is_root else ''}",
10651066
postfix=postfix,
10661067
callback=self._callback,
10671068
)
10681069

1069-
return
1070-
1071-
async def download_big_annotations(self, queue_idx, export_path):
1070+
async def download_big_annotations(self, queue_idx, export_path, folder_id):
10721071
while True:
10731072
cur_queue = self._big_file_queues[queue_idx]
10741073
item = await cur_queue.get()
10751074
cur_queue.task_done()
10761075
if item:
1077-
await self._download_big_annotation(item, export_path)
1076+
await self._download_big_annotation(item, export_path, folder_id)
10781077
else:
10791078
cur_queue.put_nowait(None)
10801079
break
10811080

1082-
async def download_small_annotations(self, queue_idx, export_path):
1083-
max_chunk_size = 50000
1084-
1081+
async def download_small_annotations(self, queue_idx, export_path, folder_id):
10851082
cur_queue = self._small_file_queues[queue_idx]
10861083

10871084
items = []
1088-
i = 0
10891085
item = ""
1090-
10911086
postfix = self.get_postfix()
10921087
while item is not None:
10931088
item = await cur_queue.get()
10941089
if item:
10951090
items.append(item)
1096-
1097-
10981091
await self._backend_client.download_small_annotations(
1099-
team_id=self._project.team_id,
1100-
project_id=self._project.id,
1101-
folder_id=self._folder.uuid,
1102-
items=items,
1103-
reporter=self.reporter,
1104-
download_path=f"{export_path}{'/' + self._folder.name if not self._folder.is_root else ''}",
1105-
postfix=postfix,
1106-
callback=self._callback,
1107-
)
1108-
1109-
async def distribute_to_queues(self, item_names, sm_queue_id, l_queue_id, folder_id):
1110-
1111-
team_id=self._project.team_id
1112-
project_id=self._project.id
1113-
1114-
resp = self._backend_client.sort_items_by_size(item_names, team_id, project_id, folder_id)
1092+
team_id=self._project.team_id,
1093+
project_id=self._project.id,
1094+
folder_id=folder_id,
1095+
items=items,
1096+
reporter=self.reporter,
1097+
download_path=f"{export_path}{'/' + self._folder.name if not self._folder.is_root else ''}",
1098+
postfix=postfix,
1099+
callback=self._callback,
1100+
)
11151101

1116-
for item in resp['large']:
1117-
await self._big_file_queues[l_queue_id].put(item)
1102+
async def distribute_to_queues(
1103+
self, item_names, sm_queue_id, l_queue_id, folder_id
1104+
):
1105+
try:
1106+
team_id = self._project.team_id
1107+
project_id = self._project.id
11181108

1119-
for item in resp['small']:
1120-
await self._small_file_queues[sm_queue_id].put(item['name'])
1109+
resp = self._backend_client.sort_items_by_size(
1110+
item_names, team_id, project_id, folder_id
1111+
)
11211112

1113+
for item in resp["large"]:
1114+
await self._big_file_queues[l_queue_id].put(item)
11221115

1123-
await self._big_file_queues[l_queue_id].put(None)
1124-
await self._small_file_queues[sm_queue_id].put(None)
1116+
for item in resp["small"]:
1117+
await self._small_file_queues[sm_queue_id].put(item["name"])
1118+
finally:
1119+
await self._big_file_queues[l_queue_id].put(None)
1120+
await self._small_file_queues[sm_queue_id].put(None)
11251121

11261122
async def run_workers(self, item_names, folder_id, export_path):
11271123
try:
11281124
self._big_file_queues.append(asyncio.Queue())
11291125
self._small_file_queues.append(asyncio.Queue())
11301126
small_file_queue_idx = len(self._small_file_queues) - 1
11311127
big_file_queue_idx = len(self._big_file_queues) - 1
1132-
11331128
res = await asyncio.gather(
1134-
self.distribute_to_queues(item_names, small_file_queue_idx, big_file_queue_idx, folder_id),
1135-
self.download_big_annotations(big_file_queue_idx, export_path),
1136-
self.download_big_annotations(big_file_queue_idx, export_path),
1137-
self.download_big_annotations(big_file_queue_idx, export_path),
1138-
self.download_small_annotations(small_file_queue_idx, export_path),
1139-
return_exceptions=True
1129+
self.distribute_to_queues(
1130+
item_names, small_file_queue_idx, big_file_queue_idx, folder_id
1131+
),
1132+
self.download_big_annotations(
1133+
big_file_queue_idx, export_path, folder_id
1134+
),
1135+
self.download_big_annotations(
1136+
big_file_queue_idx, export_path, folder_id
1137+
),
1138+
self.download_big_annotations(
1139+
big_file_queue_idx, export_path, folder_id
1140+
),
1141+
self.download_small_annotations(
1142+
small_file_queue_idx, export_path, folder_id
1143+
),
1144+
return_exceptions=True,
11401145
)
1141-
1146+
if any(res):
1147+
self.reporter.log_error(f"Error {str([i for i in res if i])}")
11421148
except Exception as e:
11431149
self.reporter.log_error(f"Error {str(e)}")
11441150

1145-
11461151
def per_folder_execute(self, item_names, folder_id, export_path):
11471152
asyncio.run(self.run_workers(item_names, folder_id, export_path))
11481153

@@ -1167,43 +1172,41 @@ def execute(self):
11671172
& Condition("project_id", self._project.id, EQ),
11681173
)
11691174
folders.append(self._folder)
1170-
postfix = self.get_postfix()
1171-
1172-
import nest_asyncio
1173-
import platform
1174-
1175-
if platform.system().lower() == "windows":
1176-
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
1177-
1178-
nest_asyncio.apply()
11791175

11801176
if not folders:
11811177
folders.append(self._folder)
1182-
1183-
11841178
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
11851179
futures = []
1186-
11871180
for folder in folders:
11881181
if not self._item_names:
11891182
condition = (
11901183
Condition("team_id", self._project.team_id, EQ)
11911184
& Condition("project_id", self._project.id, EQ)
11921185
& Condition("folder_id", folder.uuid, EQ)
11931186
)
1194-
item_names = [item.name for item in self._images.get_all(condition)]
1187+
item_names = [
1188+
item.name for item in self._images.get_all(condition)
1189+
]
11951190
else:
11961191
item_names = self._item_names
11971192

11981193
new_export_path = export_path
1199-
if folder.name != 'root':
1200-
new_export_path += f'/{folder.name}'
1201-
future = executor.submit(self.per_folder_execute, item_names, folder.uuid, new_export_path)
1194+
if folder.name != "root":
1195+
new_export_path += f"/{folder.name}"
1196+
1197+
# TODO check
1198+
if not item_names:
1199+
continue
1200+
future = executor.submit(
1201+
self.per_folder_execute,
1202+
item_names,
1203+
folder.uuid,
1204+
new_export_path,
1205+
)
12021206
futures.append(future)
12031207

12041208
for future in concurrent.futures.as_completed(futures):
1205-
pass
1206-
1209+
print(future.result())
12071210

12081211
self.reporter.stop_spinner()
12091212
count = self.get_items_count(export_path)

src/superannotate/lib/infrastructure/services.py

Lines changed: 37 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
import time
99
from contextlib import contextmanager
1010
from functools import lru_cache
11+
from pathlib import Path
1112
from typing import Any
1213
from typing import Callable
1314
from typing import Dict
1415
from typing import Iterable
1516
from typing import List
1617
from typing import Tuple
1718
from typing import Union
18-
from pathlib import Path
1919
from urllib.parse import urljoin
2020

2121
import aiohttp
@@ -56,7 +56,7 @@ class BaseBackendService(SuperannotateServiceProvider):
5656
PAGINATE_BY = 100
5757
LIMIT = 100
5858
MAX_ITEMS_COUNT = 50 * 1000
59-
ASSETS_PROVIDER_VERSION = 'v2'
59+
ASSETS_PROVIDER_VERSION = "v2"
6060

6161
"""
6262
Base service class
@@ -80,16 +80,12 @@ def __init__(
8080
self._testing = testing
8181
self.get_session()
8282

83-
8483
@property
8584
def assets_provider_url(self):
8685
if self.api_url != constants.BACKEND_URL:
8786
return f"https://assets-provider.devsuperannotate.com/api/{self.ASSETS_PROVIDER_VERSION}/"
8887
return f"https://assets-provider.superannotate.com/api/{self.ASSETS_PROVIDER_VERSION}/"
8988

90-
91-
92-
9389
@lru_cache(maxsize=32)
9490
def _get_session(self, thread_id, ttl=None): # noqa
9591
del ttl
@@ -1165,69 +1161,66 @@ async def download_big_annotation(
11651161
download_path: str,
11661162
postfix: str,
11671163
item: int,
1168-
callback: Callable = None
1164+
callback: Callable = None,
11691165
):
1170-
item_id = item['id']
1171-
item_name = item['name']
1166+
item_id = item["id"]
1167+
item_name = item["name"]
11721168
query_params = {
1173-
"team_id" : team_id,
1169+
"team_id": team_id,
11741170
"project_id": project_id,
11751171
"annotation_type": "MAIN",
1176-
"version":"V1.00"
1172+
"version": "V1.00",
11771173
}
11781174

1179-
url = urljoin(self.assets_provider_url, self.URL_DOWNLOAD_LARGE_ANNOTATION.format(item_id=item_id))
1175+
url = urljoin(
1176+
self.assets_provider_url,
1177+
self.URL_DOWNLOAD_LARGE_ANNOTATION.format(item_id=item_id),
1178+
)
11801179

11811180
sync_params = {
11821181
"team_id": team_id,
11831182
"project_id": project_id,
11841183
"desired_transform_version": "export",
1185-
"desired_version":"V1.00",
1184+
"desired_version": "V1.00",
11861185
"current_transform_version": "V1.00",
1187-
"current_source":"main",
1188-
"desired_source":"secondary"
1186+
"current_source": "main",
1187+
"desired_source": "secondary",
11891188
}
11901189

1191-
sync_url = urljoin(self.assets_provider_url, self.URL_SYNC_LARGE_ANNOTATION.format(item_id=item_id))
1192-
1193-
res = self._request(
1194-
url = sync_url,
1195-
params = sync_params,
1196-
method="POST"
1190+
sync_url = urljoin(
1191+
self.assets_provider_url,
1192+
self.URL_SYNC_LARGE_ANNOTATION.format(item_id=item_id),
11971193
)
11981194

1199-
sync_params.pop('current_source')
1200-
sync_params.pop('desired_source')
1195+
res = self._request(url=sync_url, params=sync_params, method="POST")
12011196

1197+
sync_params.pop("current_source")
1198+
sync_params.pop("desired_source")
12021199

12031200
synced = False
12041201

1205-
sync_status_url = urljoin(self.assets_provider_url, self.URL_SYNC_LARGE_ANNOTATION_STATUS.format(item_id=item_id))
1206-
while synced != 'SUCCESS':
1202+
sync_status_url = urljoin(
1203+
self.assets_provider_url,
1204+
self.URL_SYNC_LARGE_ANNOTATION_STATUS.format(item_id=item_id),
1205+
)
1206+
while synced != "SUCCESS":
12071207
synced = self._request(
1208-
url = sync_status_url,
1209-
params = sync_params,
1210-
method = "GET"
1211-
).json()['status']
1208+
url=sync_status_url, params=sync_params, method="GET"
1209+
).json()["status"]
12121210
await asyncio.sleep(1)
12131211

1214-
12151212
async with aiohttp.ClientSession(
1216-
connector=aiohttp.TCPConnector(ssl=False), headers = self.default_headers,
1213+
connector=aiohttp.TCPConnector(ssl=False),
1214+
headers=self.default_headers,
12171215
) as session:
1218-
1219-
start_response = await session.post(
1220-
url,
1221-
params = query_params
1222-
)
1216+
start_response = await session.post(url, params=query_params)
12231217
res = await start_response.json()
12241218
Path(download_path).mkdir(exist_ok=True, parents=True)
12251219

12261220
dest_path = Path(download_path) / (item_name + postfix)
1227-
with open(dest_path, 'w') as fp:
1221+
with open(dest_path, "w") as fp:
12281222
json.dump(res, fp)
12291223

1230-
12311224
async def download_small_annotations(
12321225
self,
12331226
project_id: int,
@@ -1245,7 +1238,6 @@ async def download_small_annotations(
12451238
"project_id": project_id,
12461239
"folder_id": folder_id,
12471240
}
1248-
12491241
handler = StreamedAnnotations(
12501242
headers=self.default_headers,
12511243
reporter=reporter,
@@ -1596,20 +1588,10 @@ def sort_items_by_size(self, item_names, team_id, project_id, folder_id):
15961588
"folder_id": folder_id,
15971589
}
15981590

1599-
url = urljoin(
1600-
self.assets_provider_url, self.URL_CLASSIFY_ITEM_SIZE
1601-
)
1602-
1603-
body = {
1604-
"item_names": item_names,
1605-
"folder_id": folder_id
1606-
}
1607-
1608-
1609-
return self._request(
1610-
url = url,
1611-
method = "POST",
1612-
params = query_params,
1613-
data = body
1614-
).json()
1591+
url = urljoin(self.assets_provider_url, self.URL_CLASSIFY_ITEM_SIZE)
16151592

1593+
body = {"item_names": item_names, "folder_id": folder_id}
1594+
response = self._request(url=url, method="POST", params=query_params, data=body)
1595+
if not response.ok:
1596+
raise AppException(response.json().get("errors", "Undefined"))
1597+
return response.json()

0 commit comments

Comments
 (0)