Skip to content

Commit ee3c189

Browse files
committed
fix[web]: use dedicated endpoint for web.delete_old()
1 parent 73046e6 commit ee3c189

File tree

4 files changed

+13
-60
lines changed

4 files changed

+13
-60
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3030
- Small (around 1e-4) numerical precision improvements in EME solver.
3131
- Adjoint source frequency width is adjusted to decay sufficiently before zero frequency when possible to improve accuracy of simulation normalization when using custom current sources.
3232
- Change `VisualizationSpec` validator for checking validity of user specified colors to only issue a warning if matplotlib is not installed instead of an error.
33+
- Improved performance of `tidy3d.web.delete_old()` for large folders.
3334

3435
### Changed
3536
- `tidy3d.plugins.autograd.interpolate_spline()` and `tidy3d.plugins.autograd.add_at()` can now be called with keyword arguments during tracing.

tests/test_web/test_webapi.py

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -448,60 +448,14 @@ def test_delete_old(set_api_key):
448448
json={"data": {"projectId": TASK_ID, "projectName": PROJECT_NAME}},
449449
status=200,
450450
)
451-
452-
responses.add(
453-
responses.GET,
454-
f"{Env.current.web_api_endpoint}/tidy3d/projects/{TASK_ID}/tasks",
455-
json={"data": [{"taskId": TASK_ID, "createdAt": CREATED_AT}]},
456-
status=200,
457-
)
458-
459-
responses.add(
460-
responses.GET,
461-
f"{Env.current.web_api_endpoint}/tidy3d/tasks/{TASK_ID}",
462-
json={
463-
"data": {
464-
"taskId": TASK_ID,
465-
"groupId": "group123",
466-
"version": "v1",
467-
"createdAt": CREATED_AT,
468-
}
469-
},
470-
status=200,
471-
)
472-
473-
responses.add(
474-
responses.DELETE,
475-
f"{Env.current.web_api_endpoint}/tidy3d/group/group123/versions",
476-
match=[
477-
matchers.json_params_matcher(
478-
{
479-
"versions": ["v1"],
480-
}
481-
)
482-
],
483-
json={
484-
"data": {
485-
"taskId": TASK_ID,
486-
"createdAt": CREATED_AT,
487-
}
488-
},
489-
status=200,
490-
)
491-
492451
responses.add(
493452
responses.DELETE,
494-
f"{Env.current.web_api_endpoint}/tidy3d/tasks/{TASK_ID}",
495-
json={
496-
"data": {
497-
"taskId": TASK_ID,
498-
"createdAt": CREATED_AT,
499-
}
500-
},
453+
f"{Env.current.web_api_endpoint}/tidy3d/tasks/{FOLDER_ID}/tasks",
454+
json={"data": 0, "warning": "string"},
501455
status=200,
502456
)
503457

504-
delete_old(100)
458+
delete_old(days_old=100)
505459

506460

507461
@responses.activate

tidy3d/web/api/webapi.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
import os
55
import tempfile
66
import time
7-
from datetime import datetime, timedelta
87
from typing import Callable, Dict, List, Union
98

10-
import pytz
119
from requests import HTTPError
1210
from rich.progress import Progress
1311

@@ -867,15 +865,7 @@ def delete_old(
867865
folder = Folder.get(folder)
868866
if not folder:
869867
return 0
870-
tasks = folder.list_tasks()
871-
if not tasks:
872-
return 0
873-
tasks = list(
874-
filter(lambda t: t.created_at < datetime.now(pytz.utc) - timedelta(days=days_old), tasks)
875-
)
876-
for task in tasks:
877-
task.delete()
878-
return len(tasks)
868+
return folder.delete_old(days_old)
879869

880870

881871
@wait_for_connection

tidy3d/web/core/task_core.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ def delete(self):
101101

102102
http.delete(f"tidy3d/projects/{self.folder_id}")
103103

104+
def delete_old(self, days_old: int) -> int:
105+
"""Remove folder contents older than ``days_old``."""
106+
107+
return http.delete(
108+
f"tidy3d/tasks/{self.folder_id}/tasks",
109+
params={"daysOld": days_old},
110+
)
111+
104112
def list_tasks(self) -> List[Tidy3DResource]:
105113
"""List all tasks in this folder.
106114

0 commit comments

Comments
 (0)