def store_run(self, session, struct, uid, project="", iter=0): project = project or config.default_project self._create_project_if_not_exists(session, project) run = self._get_run(session, uid, project, iter) if not run: run = Run( uid=uid, project=project, iteration=iter, state=run_state(struct), start_time=run_start_time(struct) or datetime.now(), ) labels = run_labels(struct) update_labels(run, labels) run.struct = struct self._upsert(session, run, ignore=True)
def update_run(self, session, updates: dict, uid, project="", iter=0): project = project or config.default_project run = self._get_run(session, uid, project, iter) if not run: raise DBError(f"run {uid}:{project} not found") struct = run.struct for key, val in updates.items(): update_in(struct, key, val) run.struct = struct new_state = run_state(struct) if new_state: run.state = new_state start_time = run_start_time(struct) if start_time: run.start_time = start_time run.labels.clear() for name, value in run_labels(struct).items(): lbl = Run.Label(name=name, value=value, parent=run.id) run.labels.append(lbl) session.merge(run) session.commit() self._delete_empty_labels(session, Run.Label)
def test_list_runs_times_filters(db: Session, client: TestClient) -> None: timestamp1 = datetime.now(timezone.utc) time.sleep(1) timestamp2 = datetime.now(timezone.utc) normal_run_1_uid = "normal_run_1_uid" normal_run_1 = { "metadata": { "uid": normal_run_1_uid }, "status": { "last_update": timestamp2.isoformat() }, } run = Run( uid=normal_run_1_uid, project=config.default_project, iteration=0, start_time=timestamp1, ) run.struct = normal_run_1 get_db()._upsert(db, run, ignore=True) timestamp3 = datetime.now(timezone.utc) run_without_last_update_uid = "run_without_last_update_uid" run_without_last_update = { "metadata": { "uid": run_without_last_update_uid }, "status": {}, } run = Run( uid=run_without_last_update_uid, project=config.default_project, iteration=0, start_time=timestamp3, ) run.struct = run_without_last_update get_db()._upsert(db, run, ignore=True) timestamp4 = datetime.now(timezone.utc) time.sleep(1) timestamp5 = datetime.now(timezone.utc) normal_run_2_uid = "normal_run_2_uid" normal_run_2 = { "metadata": { "uid": normal_run_2_uid }, "status": { "last_update": timestamp5.isoformat() }, } run = Run( uid=normal_run_2_uid, project=config.default_project, iteration=0, start_time=timestamp4, ) run.struct = normal_run_2 get_db()._upsert(db, run, ignore=True) # all start time range assert_time_range_request( client, [normal_run_1_uid, normal_run_2_uid, run_without_last_update_uid]) assert_time_range_request( client, [normal_run_1_uid, normal_run_2_uid, run_without_last_update_uid], start_time_from=timestamp1.isoformat(), start_time_to=timestamp5.isoformat(), ) assert_time_range_request( client, [normal_run_1_uid, normal_run_2_uid, run_without_last_update_uid], start_time_from=timestamp1.isoformat(), ) assert_time_range_request( client, [normal_run_1_uid, normal_run_2_uid, run_without_last_update_uid], start_time_to=timestamp5.isoformat(), ) # all last update time range (shouldn't contain run_without_last_update) assert_time_range_request( client, [normal_run_1_uid, normal_run_2_uid], last_update_time_from=timestamp1, last_update_time_to=timestamp5, ) assert_time_range_request( client, [normal_run_1_uid, normal_run_2_uid], last_update_time_from=timestamp1, ) assert_time_range_request( client, [normal_run_1_uid, normal_run_2_uid], last_update_time_to=timestamp5, ) # catch only first assert_time_range_request( client, [normal_run_1_uid], start_time_from=timestamp1, start_time_to=timestamp2, ) assert_time_range_request( client, [normal_run_1_uid], start_time_to=timestamp2, ) assert_time_range_request( client, [normal_run_1_uid], last_update_time_from=timestamp1, last_update_time_to=timestamp2, ) # catch run_without_last_update and last assert_time_range_request( client, [normal_run_2_uid, run_without_last_update_uid], start_time_from=timestamp3, start_time_to=timestamp5, ) assert_time_range_request( client, [normal_run_2_uid, run_without_last_update_uid], start_time_from=timestamp3, )
def test_list_runs_state_filter(db: DBInterface, db_session: Session): run_without_state_uid = "run_without_state_uid" run_without_state = { "metadata": { "uid": run_without_state_uid }, "bla": "blabla" } db.store_run( db_session, run_without_state, run_without_state_uid, ) run_with_json_state_state = "some_json_state" run_with_json_state_uid = "run_with_json_state_uid" run_with_json_state = { "metadata": { "uid": run_with_json_state_uid }, "status": { "state": run_with_json_state_state }, } run = Run( uid=run_with_json_state_uid, project=config.default_project, iteration=0, start_time=datetime.now(timezone.utc), ) run.struct = run_with_json_state db._upsert(db_session, run, ignore=True) run_with_record_state_state = "some_record_state" run_with_record_state_uid = "run_with_record_state_uid" run_with_record_state = { "metadata": { "uid": run_with_record_state_uid }, "bla": "blabla", } run = Run( uid=run_with_record_state_uid, project=config.default_project, iteration=0, state=run_with_record_state_state, start_time=datetime.now(timezone.utc), ) run.struct = run_with_record_state db._upsert(db_session, run, ignore=True) run_with_equal_json_and_record_state_state = "some_equal_json_and_record_state" run_with_equal_json_and_record_state_uid = ( "run_with_equal_json_and_record_state_uid") run_with_equal_json_and_record_state = { "metadata": { "uid": run_with_equal_json_and_record_state_uid }, "status": { "state": run_with_equal_json_and_record_state_state }, } db.store_run( db_session, run_with_equal_json_and_record_state, run_with_equal_json_and_record_state_uid, ) run_with_unequal_json_and_record_state_json_state = "some_unequal_json_state" run_with_unequal_json_and_record_state_record_state = "some_unequal_record_state" run_with_unequal_json_and_record_state_uid = ( "run_with_unequal_json_and_record_state_uid") run_with_unequal_json_and_record_state = { "metadata": { "uid": run_with_unequal_json_and_record_state_uid }, "status": { "state": run_with_unequal_json_and_record_state_json_state }, } run = Run( uid=run_with_unequal_json_and_record_state_uid, project=config.default_project, iteration=0, state=run_with_unequal_json_and_record_state_record_state, start_time=datetime.now(timezone.utc), ) run.struct = run_with_unequal_json_and_record_state db._upsert(db_session, run, ignore=True) runs = db.list_runs(db_session) assert len(runs) == 5 runs = db.list_runs(db_session, state="some_") assert len(runs) == 4 assert run_without_state_uid not in [ run["metadata"]["uid"] for run in runs ] runs = db.list_runs(db_session, state=run_with_json_state_state) assert len(runs) == 1 assert runs[0]["metadata"]["uid"] == run_with_json_state_uid runs = db.list_runs(db_session, state=run_with_record_state_state) assert len(runs) == 1 assert runs[0]["metadata"]["uid"] == run_with_record_state_uid runs = db.list_runs(db_session, state=run_with_equal_json_and_record_state_state) assert len(runs) == 1 assert runs[0]["metadata"][ "uid"] == run_with_equal_json_and_record_state_uid runs = db.list_runs( db_session, state=run_with_unequal_json_and_record_state_json_state) assert len(runs) == 1 assert runs[0]["metadata"][ "uid"] == run_with_unequal_json_and_record_state_uid runs = db.list_runs( db_session, state=run_with_unequal_json_and_record_state_record_state) assert len(runs) == 0