From c72625ad8ee674d5ee4d03abc08075646f12b416 Mon Sep 17 00:00:00 2001 From: norohind <60548839+norohind@users.noreply.github.com> Date: Tue, 26 Oct 2021 01:27:47 +0300 Subject: [PATCH] Handle edge case with high squad ID When already updated squad with high id but still running retrospectively so utils.get_last_known_id would return high id + 1 but not anymore --- main.py | 10 ++++++---- sql_requests.py | 7 +++++++ utils.py | 16 ++++++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 878ebff..ec8d3a4 100644 --- a/main.py +++ b/main.py @@ -56,7 +56,8 @@ def discover(): :return: """ - id_to_try = utils.get_last_known_id(db) + # id_to_try = utils.get_last_known_id(db) + # id_to_try = utils.get_next_id_for_discover(db) - 1 tries: int = 0 failed: list = list() TRIES_LIMIT_RETROSPECTIVELY: int = 5000 @@ -64,7 +65,7 @@ def discover(): def smart_tries_limit(squad_id: int) -> int: # something smarter but still have to be better - if id_to_try < 65000: + if squad_id < 65000: return TRIES_LIMIT_RETROSPECTIVELY else: @@ -73,11 +74,12 @@ def discover(): """ tries_limit, probably, should be something more smart because on retrospectively scan we can have large spaces of dead squadrons but when we are discovering on real time, large value of tries_limit - will just waste our time and, probable, confuses FDEV + will just waste our time and, probable, confuses FDEV + *Outdated but it still can be more smart* """ while True: - id_to_try = id_to_try + 1 + id_to_try = utils.get_next_hole_id_for_discover(db) # logger.debug(f'Starting discover loop iteration, tries: {tries} of {tries_limit}, id to try {id_to_try}, ' # f'failed list: {failed}') diff --git a/sql_requests.py b/sql_requests.py index 0e8ea64..516b27d 100644 --- a/sql_requests.py +++ b/sql_requests.py @@ -72,3 +72,10 @@ from squads_states where tag is not null order by inserted_timestamp asc limit ?;""" + +select_first_hole_id: str = """select squad_id+1 as a +from squads_states except + select squad_id + from squads_states + order by a asc + limit 1;""" \ No newline at end of file diff --git a/utils.py b/utils.py index e70f534..11ed0dc 100644 --- a/utils.py +++ b/utils.py @@ -309,3 +309,19 @@ def get_last_known_id(db_conn: sqlite3.Connection) -> int: else: logger.debug(f'last know id from DB: {sql_request_result[0]}') return sql_request_result[0] + + +def get_next_hole_id_for_discover(db_conn: sqlite3.Connection) -> int: + """Returns first unexisting id in DB + :param db_conn: + :return: last known id if we iterate from 1 to ... + """ + + sql_req = db_conn.execute(sql_requests.select_first_hole_id).fetchone() + if sql_req is None: + logger.debug(f"Can't get last know id from DB, defaulting to 1") + return 1 + + else: + logger.debug(f'last know id from DB: {sql_req[0]}') + return sql_req[0]