Skip to content

Commit aae6f4b

Browse files
authored
[CVAT] Refactor oracle configs (#2780)
* Remove extra files * Remove outdated validation settings * Remove unused aws storage region * Remove cvat_ prefix for cvat settings * Refactor env var access * Update exchange oracle cron settings * Add comment * Add missing template vars * Format code * Merge * Add missing type conversions * Fix invalid webhook handling * Fix merge * Remove extra config parameter for polygons iou * Update code formatting
1 parent 408d284 commit aae6f4b

File tree

14 files changed

+211
-226
lines changed

14 files changed

+211
-226
lines changed

packages/examples/cvat/exchange-oracle/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ ignore = [
114114
"ANN002", # Missing type annotation for `*args`
115115
"TRY300", # Consider moving this statement to an `else` block
116116
"C901", # Function is too complex
117+
"PLW1508", # invalid-envvar-default. Alerts only for os.getenv(), but not for os.environ.get()
117118
"PLW2901", # Variable overwritten by assignment target
118119
"PTH118", # Prefer pathlib instead of os.path
119120
"PTH119", # `os.path.basename()` should be replaced by `Path.name`

packages/examples/cvat/exchange-oracle/src/.env.template

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ PROCESS_RECORDING_ORACLE_WEBHOOKS_CHUNK_SIZE=
4444
PROCESS_REPUTATION_ORACLE_WEBHOOKS_CHUNK_SIZE=
4545
PROCESS_REPUTATION_ORACLE_WEBHOOKS_INT=
4646
TRACK_COMPLETED_PROJECTS_INT=
47-
TRACK_COMPLETED_PROJECTS_CHUNK_SIZE=
4847
TRACK_COMPLETED_TASKS_INT=
49-
TRACK_COMPLETED_TASKS_CHUNK_SIZE=
5048
TRACK_COMPLETED_ESCROWS_INT=
5149
TRACK_COMPLETED_ESCROWS_CHUNK_SIZE=
5250
TRACK_ESCROW_VALIDATIONS_INT=
@@ -79,13 +77,11 @@ CVAT_IOU_THRESHOLD=
7977
CVAT_OKS_SIGMA=
8078
CVAT_EXPORT_TIMEOUT=
8179
CVAT_IMPORT_TIMEOUT=
82-
CVAT_POLYGONS_IOU_THRESHOLD=
8380

8481
# Storage Config (S3/GCS)
8582

8683
STORAGE_PROVIDER=
8784
STORAGE_ENDPOINT_URL=
88-
STORAGE_REGION=
8985
STORAGE_ACCESS_KEY=
9086
STORAGE_SECRET_KEY=
9187
STORAGE_KEY_FILE_PATH=
@@ -96,6 +92,7 @@ STORAGE_USE_SSL=
9692

9793
ENABLE_CUSTOM_CLOUD_HOST=
9894
REQUEST_LOGGING_ENABLED=
95+
PROFILING_ENABLED=
9996
MANIFEST_CACHE_TTL=
10097

10198
# Core
@@ -112,16 +109,22 @@ HUMAN_APP_JWT_KEY=
112109
# API config
113110

114111
DEFAULT_API_PAGE_SIZE=
112+
MIN_API_PAGE_SIZE=
113+
MAX_API_PAGE_SIZE=
114+
STATS_RPS_LIMIT=
115115

116116
# Localhost
117117

118+
LOCALHOST_RPC_API_URL=
119+
LOCALHOST_AMOY_ADDR=
118120
LOCALHOST_RECORDING_ORACLE_ADDRESS=
119121
LOCALHOST_RECORDING_ORACLE_URL=
120122
LOCALHOST_JOB_LAUNCHER_URL=
121123
LOCALHOST_REPUTATION_ORACLE_ADDRESS=
122124
LOCALHOST_REPUTATION_ORACLE_URL=
123125

124126
# Encryption
127+
125128
PGP_PRIVATE_KEY=
126129
PGP_PASSPHRASE=
127130
PGP_PUBLIC_KEY_URL=

packages/examples/cvat/exchange-oracle/src/core/config.py

Lines changed: 102 additions & 116 deletions
Large diffs are not rendered by default.

packages/examples/cvat/exchange-oracle/src/crons/webhooks/recording_oracle.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ def handle_recording_oracle_event(webhook: Webhook, *, db_session: Session, logg
6060
)
6161
return
6262

63-
chunk_size = CronConfig.accepted_projects_chunk_size
6463
project_ids = cvat_db_service.get_project_cvat_ids_by_escrow_address(
6564
db_session, webhook.escrow_address
6665
)
@@ -71,6 +70,7 @@ def handle_recording_oracle_event(webhook: Webhook, *, db_session: Session, logg
7170
)
7271
return
7372

73+
chunk_size = CronConfig.process_accepted_projects_chunk_size
7474
for ids_chunk in take_by(project_ids, chunk_size):
7575
projects_chunk = cvat_db_service.get_projects_by_cvat_ids(
7676
db_session, ids_chunk, for_update=True, limit=chunk_size
@@ -138,7 +138,7 @@ def handle_recording_oracle_event(webhook: Webhook, *, db_session: Session, logg
138138
)
139139
rejected_project_cvat_ids = set(j.cvat_project_id for j in rejected_jobs)
140140

141-
chunk_size = CronConfig.rejected_projects_chunk_size
141+
chunk_size = CronConfig.process_rejected_projects_chunk_size
142142
for chunk_ids in take_by(rejected_project_cvat_ids, chunk_size):
143143
projects_chunk = cvat_db_service.get_projects_by_cvat_ids(
144144
db_session, chunk_ids, for_update=True, limit=chunk_size

packages/examples/cvat/exchange-oracle/src/cvat/api_calls.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def _get_annotations(
7777
time_begin = utcnow()
7878

7979
if timeout is _NOTSET:
80-
timeout = Config.cvat_config.cvat_export_timeout
80+
timeout = Config.cvat_config.export_timeout
8181

8282
while True:
8383
(_, response) = endpoint.call_with_http_info(
@@ -118,23 +118,23 @@ def get_api_client() -> ApiClient:
118118
return current_api_client
119119

120120
configuration = Configuration(
121-
host=Config.cvat_config.cvat_url,
122-
username=Config.cvat_config.cvat_admin,
123-
password=Config.cvat_config.cvat_admin_pass,
121+
host=Config.cvat_config.host_url,
122+
username=Config.cvat_config.admin_login,
123+
password=Config.cvat_config.admin_pass,
124124
)
125125

126126
api_client = ApiClient(configuration=configuration)
127-
api_client.set_default_header("X-organization", Config.cvat_config.cvat_org_slug)
127+
api_client.set_default_header("X-organization", Config.cvat_config.org_slug)
128128

129129
return api_client
130130

131131

132132
def get_sdk_client() -> Client:
133133
client = make_client(
134-
host=Config.cvat_config.cvat_url,
135-
credentials=(Config.cvat_config.cvat_admin, Config.cvat_config.cvat_admin_pass),
134+
host=Config.cvat_config.host_url,
135+
credentials=(Config.cvat_config.admin_login, Config.cvat_config.admin_pass),
136136
)
137-
client.organization_slug = Config.cvat_config.cvat_org_slug
137+
client.organization_slug = Config.cvat_config.org_slug
138138

139139
return client
140140

@@ -291,11 +291,11 @@ def create_cvat_webhook(project_id: int) -> models.WebhookRead:
291291
logger = logging.getLogger("app")
292292
with get_api_client() as api_client:
293293
webhook_write_request = models.WebhookWriteRequest(
294-
target_url=Config.cvat_config.cvat_incoming_webhooks_url,
294+
target_url=Config.cvat_config.incoming_webhooks_url,
295295
description="Exchange Oracle notification",
296296
type=models.WebhookType("project"),
297297
content_type=models.WebhookContentType("application/json"),
298-
secret=Config.cvat_config.cvat_webhook_secret,
298+
secret=Config.cvat_config.webhook_secret,
299299
is_active=True,
300300
# enable_ssl=True,
301301
project_id=project_id,
@@ -403,7 +403,7 @@ def put_task_data(
403403
data_request = models.DataRequest(
404404
chunk_size=chunk_size,
405405
cloud_storage_id=cloudstorage_id,
406-
image_quality=Config.cvat_config.cvat_default_image_quality,
406+
image_quality=Config.cvat_config.image_quality,
407407
use_cache=True,
408408
use_zip_chunks=True,
409409
sorting_method=sorting_method,
@@ -641,7 +641,7 @@ def upload_gt_annotations(
641641
*,
642642
format_name: str,
643643
sleep_interval: int = 5,
644-
timeout: int | None = Config.cvat_config.cvat_import_timeout,
644+
timeout: int | None = Config.cvat_config.import_timeout,
645645
) -> None:
646646
# FUTURE-TODO: use job.import_annotations when CVAT supports a waiting timeout
647647
start_time = datetime.now(timezone.utc)
@@ -728,8 +728,8 @@ def update_quality_control_settings(
728728
*,
729729
target_metric_threshold: float,
730730
target_metric: str = "accuracy",
731-
max_validations_per_job: int = Config.cvat_config.cvat_max_validation_checks,
732-
iou_threshold: float = Config.cvat_config.cvat_iou_threshold,
731+
max_validations_per_job: int = Config.cvat_config.max_validation_checks,
732+
iou_threshold: float = Config.cvat_config.iou_threshold,
733733
oks_sigma: float | None = None,
734734
point_size_base: str | None = None,
735735
match_empty_frames: bool | None = None,
@@ -823,7 +823,7 @@ def get_user_id(user_email: str) -> int:
823823
try:
824824
(invitation, _) = api_client.invitations_api.create(
825825
models.InvitationWriteRequest(role="worker", email=user_email),
826-
org=Config.cvat_config.cvat_org_slug,
826+
org=Config.cvat_config.org_slug,
827827
)
828828
except exceptions.ApiException as e:
829829
logger.exception(f"Exception when calling get_user_id(): {e}\n")
@@ -839,7 +839,7 @@ def remove_user_from_org(user_id: int):
839839
try:
840840
(page, _) = api_client.users_api.list(
841841
filter='{"==":[{"var":"id"},"%s"]}' % user_id, # noqa: UP031
842-
org=Config.cvat_config.cvat_org_slug,
842+
org=Config.cvat_config.org_slug,
843843
)
844844
if not page.results:
845845
return
@@ -849,7 +849,7 @@ def remove_user_from_org(user_id: int):
849849

850850
(page, _) = api_client.memberships_api.list(
851851
user=user.username,
852-
org=Config.cvat_config.cvat_org_slug,
852+
org=Config.cvat_config.org_slug,
853853
)
854854
if page.results:
855855
api_client.memberships_api.destroy(page.results[0].id)

packages/examples/cvat/exchange-oracle/src/handlers/job_creation.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def _wait_task_creation(self, task_id: int) -> cvat_api.UploadStatus:
203203
if task_status not in [cvat_api.UploadStatus.STARTED, cvat_api.UploadStatus.QUEUED]:
204204
return task_status
205205

206-
sleep(Config.cvat_config.cvat_task_creation_check_interval)
206+
sleep(Config.cvat_config.task_creation_check_interval)
207207

208208
def _setup_gt_job_for_cvat_task(
209209
self, task_id: int, gt_dataset: dm.Dataset, *, dm_export_format: str = "coco"
@@ -409,7 +409,7 @@ def build(self):
409409

410410
for data_subset in self._split_dataset_per_task(
411411
data_to_be_annotated,
412-
subset_size=Config.cvat_config.cvat_max_jobs_per_task * segment_size,
412+
subset_size=Config.cvat_config.max_jobs_per_task * segment_size,
413413
):
414414
cvat_task = cvat_api.create_task(
415415
cvat_project.id, escrow_address, segment_size=segment_size
@@ -552,7 +552,7 @@ def build(self):
552552

553553
class PolygonTaskBuilder(SimpleTaskBuilder):
554554
def _setup_quality_settings(self, task_id: int, **overrides) -> None:
555-
values = {"iou_threshold": Config.cvat_config.cvat_polygons_iou_threshold, **overrides}
555+
values = {"iou_threshold": Config.cvat_config.iou_threshold, **overrides}
556556
super()._setup_quality_settings(task_id, **values)
557557

558558

@@ -1554,7 +1554,7 @@ def _create_on_cvat(self):
15541554

15551555
for data_subset in self._split_dataset_per_task(
15561556
self._roi_filenames_to_be_annotated,
1557-
subset_size=Config.cvat_config.cvat_max_jobs_per_task * segment_size,
1557+
subset_size=Config.cvat_config.max_jobs_per_task * segment_size,
15581558
):
15591559
cvat_task = cvat_api.create_task(
15601560
cvat_project.id, self.escrow_address, segment_size=segment_size
@@ -2354,7 +2354,7 @@ def _prepare_task_params(self):
23542354
label_id=label_id, roi_ids=task_data_roi_ids, roi_gt_ids=label_gt_roi_ids
23552355
)
23562356
for task_data_roi_ids in take_by(
2357-
label_data_roi_ids, Config.cvat_config.cvat_max_jobs_per_task * segment_size
2357+
label_data_roi_ids, Config.cvat_config.max_jobs_per_task * segment_size
23582358
)
23592359
]
23602360
)
@@ -2603,7 +2603,7 @@ def _save_cvat_gt_dataset_to_oracle_bucket(
26032603

26042604
def _setup_quality_settings(self, task_id: int, **overrides) -> None:
26052605
values = {
2606-
"oks_sigma": Config.cvat_config.cvat_oks_sigma,
2606+
"oks_sigma": Config.cvat_config.oks_sigma,
26072607
"point_size_base": "image_size", # we don't expect any boxes or groups, so ignore them
26082608
}
26092609
values.update(overrides)
@@ -2767,7 +2767,7 @@ def _task_params_label_key(ts):
27672767
cvat_task.id, gt_point_dataset, dm_export_format="cvat"
27682768
)
27692769
self._setup_quality_settings(
2770-
cvat_task.id, oks_sigma=Config.cvat_config.cvat_oks_sigma
2770+
cvat_task.id, oks_sigma=Config.cvat_config.oks_sigma
27712771
)
27722772

27732773
db_service.create_data_upload(session, cvat_task.id)

packages/examples/cvat/exchange-oracle/src/utils/assignments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def compose_assignment_url(task_id: int, job_id: int, *, project: Project) -> st
2424
if project.job_type == TaskTypes.image_skeletons_from_boxes:
2525
query_params += "&defaultPointsCount=1"
2626

27-
return urljoin(Config.cvat_config.cvat_url, f"/tasks/{task_id}/jobs/{job_id}{query_params}")
27+
return urljoin(Config.cvat_config.host_url, f"/tasks/{task_id}/jobs/{job_id}{query_params}")
2828

2929

3030
def get_default_assignment_timeout(task_type: TaskTypes) -> int:

packages/examples/cvat/exchange-oracle/src/validators/signature.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async def validate_cvat_signature(request: Request, x_signature_256: str):
3232
signature = (
3333
"sha256="
3434
+ hmac.new(
35-
Config.cvat_config.cvat_webhook_secret.encode("utf-8"),
35+
Config.cvat_config.webhook_secret.encode("utf-8"),
3636
data,
3737
digestmod=sha256,
3838
).hexdigest()

packages/examples/cvat/exchange-oracle/tests/integration/cron/test_process_job_launcher_webhooks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ def tearDown(self):
4343
self.session.close()
4444

4545
def test_process_incoming_job_launcher_webhooks_escrow_created_type(self):
46-
webhok_id = str(uuid.uuid4())
46+
webhook_id = str(uuid.uuid4())
4747
webhook = Webhook(
48-
id=webhok_id,
48+
id=webhook_id,
4949
signature="signature",
5050
escrow_address=escrow_address,
5151
chain_id=chain_id,
@@ -93,7 +93,7 @@ def test_process_incoming_job_launcher_webhooks_escrow_created_type(self):
9393
process_incoming_job_launcher_webhooks()
9494

9595
updated_webhook = (
96-
self.session.execute(select(Webhook).where(Webhook.id == webhok_id)).scalars().first()
96+
self.session.execute(select(Webhook).where(Webhook.id == webhook_id)).scalars().first()
9797
)
9898

9999
assert updated_webhook.status == OracleWebhookStatuses.completed.value

packages/examples/cvat/exchange-oracle/tests/utils/setup_cvat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def generate_cvat_signature(data: dict):
1818
return (
1919
"sha256="
2020
+ hmac.new(
21-
CvatConfig.cvat_webhook_secret.encode("utf-8"),
21+
CvatConfig.webhook_secret.encode("utf-8"),
2222
b_data,
2323
digestmod=sha256,
2424
).hexdigest()

0 commit comments

Comments
 (0)