Skip to content

Commit 6a5e3e5

Browse files
committed
fix real cost
1 parent 4d94459 commit 6a5e3e5

File tree

4 files changed

+23
-20
lines changed

4 files changed

+23
-20
lines changed

tests/test_web/test_webapi.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
PROJECT_NAME = "default"
2828
FLEX_UNIT = 1.0
2929

30+
3031
def make_sim():
3132
"""Makes a simulation."""
3233
return td.Simulation(size=(1, 1, 1), grid_spec=td.GridSpec.auto(wavelength=1.0), run_time=1e-12)
@@ -83,7 +84,7 @@ def mock_get_info(monkeypatch, set_api_key):
8384
"data": {
8485
"taskId": TASK_ID,
8586
"createdAt": CREATED_AT,
86-
"real_flex_unit": FLEX_UNIT,
87+
"realFlexUnit": FLEX_UNIT,
8788
}
8889
},
8990
status=200,
@@ -506,9 +507,10 @@ def test_monitor(mock_monitor):
506507
monitor(TASK_ID, verbose=True)
507508
monitor(TASK_ID, verbose=False)
508509

510+
509511
@responses.activate
510512
def test_real_cost(mock_get_info):
511-
real_cost(TASK_ID)
513+
assert real_cost(TASK_ID) == FLEX_UNIT
512514

513515

514516
""" Containers """
@@ -526,6 +528,7 @@ def test_job(mock_webapi, monkeypatch):
526528
j.get_info()
527529
# j.download
528530
j.delete
531+
assert j.real_cost() == FLEX_UNIT
529532

530533

531534
@pytest.fixture
@@ -543,6 +546,7 @@ def test_batch(mock_webapi, mock_job_status):
543546
sims = {TASK_NAME: make_sim()}
544547
b = Batch(simulations=sims, folder_name=PROJECT_NAME)
545548
batch_data = b.run(path_dir="tests/tmp/")
549+
assert b.real_cost() == FLEX_UNIT * len(sims)
546550

547551

548552
""" Async """

tidy3d/web/container.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ def delete(self):
170170
"""Delete server-side data associated with :class:`Job`."""
171171
web.delete(self.task_id)
172172

173+
def real_cost(self):
174+
"""Get the billed cost for the task associated with this job."""
175+
return web.real_cost(self.task_id)
176+
173177

174178
class BatchData(Tidy3dBaseModel):
175179
"""Holds a collection of :class:`.SimulationData` returned by :class:`.Batch`."""
@@ -512,3 +516,12 @@ def delete(self):
512516
"""Delete server-side data associated with each task in the batch."""
513517
for _, job in self.jobs.items():
514518
job.delete()
519+
520+
def real_cost(self):
521+
"""Get the sum of billed costs for each task associated with this batch."""
522+
real_cost_sum = 0.0
523+
for _, job in self.jobs.items():
524+
cost_job = job.real_cost()
525+
if cost_job is not None:
526+
real_cost_sum += cost_job
527+
return real_cost_sum or None

tidy3d/web/simulation_task.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class SimulationTask(ResourceLifecycle, Submittable, extra=Extra.allow):
125125
status: Optional[str] = Field(title="status", description="Simulation task status.")
126126

127127
real_flex_unit: float = Field(
128-
None, title="real flex units", description="Billed flex units.", alias="realFlexUnit"
128+
None, title="real flex units", description="Billed flex units.", alias="realCost"
129129
)
130130

131131
created_at: Optional[datetime] = Field(
@@ -376,18 +376,6 @@ def estimate_cost(self, solver_version=None, protocol_version=None) -> float:
376376
)
377377
return resp
378378

379-
def real_cost(self) -> float:
380-
"""Get the billed cost for given task after it has been run.
381-
382-
Note
383-
----
384-
The billed cost may not be immediately available
385-
when the task status is set to ``success``,
386-
but should be available shortly after.
387-
"""
388-
assert self.task_id
389-
return self.get(self.task_id).real_flex_unit
390-
391379
def get_simulation_hdf5(
392380
self, to_file: str, verbose: bool = True, progress_callback: Callable[[float], None] = None
393381
) -> None:

tidy3d/web/webapi.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -640,13 +640,11 @@ def real_cost(task_id: str) -> float:
640640
The billed cost may not be immediately available when the task status is set to ``success``,
641641
but should be available shortly after.
642642
"""
643-
task = SimulationTask.get(task_id)
644-
if not task:
645-
raise ValueError("Task not found.")
646-
flex_unit = task.real_flex_unit
643+
task_info = get_info(task_id)
644+
flex_unit = task_info.realFlexUnit
647645
if not flex_unit:
648646
log.warning(
649-
f"Billed FlexUnit for task '{task_id}'' is not available. If the task has been "
647+
f"Billed FlexUnit for task '{task_id}' is not available. If the task has been "
650648
"successfully run, it should be available shortly."
651649
)
652650
return flex_unit

0 commit comments

Comments
 (0)