feat(product-suite): persist current generation rounds

This commit is contained in:
chengma
2026-07-16 23:27:25 +08:00
parent c146c0b41d
commit f3defdeb95
11 changed files with 806 additions and 16 deletions
+171 -1
View File
@@ -62,6 +62,7 @@ class ImageStudioTests(TempDirMixin, unittest.TestCase):
"target_main_count",
"target_detail_count",
"suite_settings_json",
"current_generation_round_key",
"deleted_at",
}.issubset(projects_columns)
)
@@ -69,7 +70,20 @@ class ImageStudioTests(TempDirMixin, unittest.TestCase):
row["name"]
for row in conn.execute("PRAGMA table_info(image_studio_jobs)").fetchall()
}
self.assertIn("recovery_action", jobs_columns)
self.assertTrue(
{
"recovery_action",
"generation_round_key",
"generation_slot_index",
}.issubset(jobs_columns)
)
indexes = {
row["name"]
for row in conn.execute(
"PRAGMA index_list(image_studio_jobs)"
).fetchall()
}
self.assertIn("idx_image_studio_jobs_generation_round", indexes)
finally:
conn.close()
@@ -820,6 +834,162 @@ class ImageStudioTests(TempDirMixin, unittest.TestCase):
self.assertEqual(image_studio.JOB_RECOVERY_REGENERATE, recovery_actions["legacy-failed"])
self.assertEqual(image_studio.JOB_RECOVERY_NONE, recovery_actions["legacy-success"])
legacy_jobs = image_studio.list_generation_round_current_jobs(
1,
None,
path=db_path,
)
self.assertEqual([1, 2, 3, 4], [job.id for job in legacy_jobs])
self.assertTrue(
all(job.generation_round_key is None for job in legacy_jobs)
)
self.assert_removed(temp_dir)
def test_generation_round_queries_keep_slots_attempts_and_project_boundaries(self):
with self.make_temp_dir() as temp_dir:
db_path = os.path.join(temp_dir, "cmshopee.db")
db.init_db(db_path)
project = image_studio.create_or_get_project(
account_alias="alias-a",
account_slug="alias-a",
item_id="51100639510",
path=db_path,
)
other_project = image_studio.create_or_get_project(
account_alias="alias-b",
account_slug="alias-b",
item_id="51100639510",
path=db_path,
)
source = image_studio.add_asset(
project.id,
image_studio.ASSET_KIND_ORIGINAL,
path=db_path,
)
legacy = image_studio.create_job(
project.id,
source_asset_id=source.id,
task_key="legacy-generation-round",
path=db_path,
)
round_one = "round-one"
first = image_studio.create_job(
project.id,
source_asset_id=source.id,
task_key="round-one-slot-0",
generation_round_key=round_one,
generation_slot_index=0,
path=db_path,
)
first = image_studio.update_job_status(
first.id,
"succeeded",
path=db_path,
)
failed = image_studio.create_job(
project.id,
source_asset_id=source.id,
task_key="round-one-slot-1-failed",
generation_round_key=round_one,
generation_slot_index=1,
path=db_path,
)
failed = image_studio.update_job_status(
failed.id,
"failed",
path=db_path,
)
retry = image_studio.create_job(
project.id,
source_asset_id=source.id,
task_key="round-one-slot-1-retry",
generation_round_key=round_one,
generation_slot_index=1,
path=db_path,
)
retry = image_studio.update_job_status(
retry.id,
"succeeded",
path=db_path,
)
failed_round = "round-two-all-failed"
failed_round_job = image_studio.create_job(
project.id,
source_asset_id=source.id,
task_key="round-two-slot-0",
generation_round_key=failed_round,
generation_slot_index=0,
path=db_path,
)
image_studio.update_job_status(
failed_round_job.id,
"failed",
path=db_path,
)
current = image_studio.set_current_generation_round(
project.id,
round_one,
path=db_path,
)
self.assertEqual(round_one, current.current_generation_round_key)
self.assertEqual(
round_one,
image_studio.get_current_generation_round(project.id, path=db_path),
)
self.assertFalse(
image_studio.promote_generation_round_if_success(
project.id,
failed_round,
path=db_path,
)
)
self.assertEqual(
round_one,
image_studio.get_current_generation_round(project.id, path=db_path),
)
with self.assertRaisesRegex(db.DbError, "不属于"):
image_studio.set_current_generation_round(
other_project.id,
round_one,
path=db_path,
)
current_jobs = image_studio.list_generation_round_current_jobs(
project.id,
round_one,
path=db_path,
)
self.assertEqual([first.id, retry.id], [job.id for job in current_jobs])
self.assertEqual([0, 1], [job.generation_slot_index for job in current_jobs])
attempts = image_studio.list_generation_round_attempts(
project.id,
round_one,
path=db_path,
)
self.assertEqual([first.id, failed.id, retry.id], [job.id for job in attempts])
legacy_jobs = image_studio.list_generation_round_current_jobs(
project.id,
None,
path=db_path,
)
self.assertEqual([legacy.id], [job.id for job in legacy_jobs])
rounds = image_studio.list_generation_rounds(project.id, path=db_path)
self.assertEqual(
[failed_round, round_one, None],
[round_.generation_round_key for round_ in rounds],
)
summary = rounds[1]
self.assertTrue(summary.is_current)
self.assertEqual(3, summary.job_count)
self.assertEqual(2, summary.slot_count)
self.assertEqual(1, summary.retry_count)
self.assertEqual(2, summary.succeeded_count)
self.assertEqual(1, summary.failed_count)
self.assertTrue(rounds[-1].is_legacy)
self.assert_removed(temp_dir)
def test_selections_are_consecutive_unique_and_replaceable(self):