Update return types, start get_missing_records function

This commit is contained in:
chris committed 2026-03-05 18:42:00 -05:00
1 parent 3e6cb29425
commit fe2fa4f9cb
1 file changed
+38 -10
+38 -10
View File
@@ -3,14 +3,14 @@
Functions for collecting data for insertion into the databse or pulling data from the database for analysis
"""
from datetime import datetime
from datetime import datetime, timedelta
from enum import Enum
from time import sleep
from sqlalchemy import func
from sqlalchemy import func, text
from sqlalchemy.exc import IntegrityError
from data.database import Base, dal, MegaMillionsDraw, PowerballDraw, project_variables
from data.database import Base, dal, MegaMillionsDraw, PowerballDraw
import util.drawing as drawutil
import util.environment as envutil
import util.scrape as scrapeutil
@@ -18,23 +18,51 @@ import util.scrape as scrapeutil
project_variables = envutil.load_environment_variables()
def get_record_count(table_name: str) -> str | None:
def get_record_count(table_name: str) -> int | None:
"""get the most recent draw record from the specified table"""
if drawutil.check_table_name(table_name):
game_dict = drawutil.check_table_name(table_name=table_name)
if game_dict:
dal.connect()
session = dal.Session()
target_table = Base.registry._class_registry.get(table_name)
return session.query(func.count(target_table.draw_date)).first()[0]
target_table = Base.metadata.tables.get(game_dict['db_table_name'])
return session.execute(text(f"select count(draw_date) from {target_table.name}")).first()[0]
else:
raise ValueError('An invalid table name was specified.')
def get_recent_draw_date(table_name: str) -> datetime | None:
"""get the most recent draw date from the specified table"""
if drawutil.check_table_name(table_name):
game_dict = drawutil.check_table_name(table_name=table_name)
if game_dict:
dal.connect()
session = dal.Session()
target_table = Base.registry._class_registry.get(table_name)
return session.query(func.max(target_table.draw_date)).first()[0]
target_table = Base.metadata.tables.get(game_dict['db_table_name'])
return session.execute(text(f"select max(draw_date) from {target_table.name}")).first()[0]
else:
raise ValueError('An invalid table name was specified.')
def get_missing_records(table_name: str) -> int:
"""collect and insert missing records, returning a count of insertions"""
# if the table_name is valid
game_dict = drawutil.check_table_name(table_name=table_name)
if game_dict:
# connect to the database and establish a session
dal.connect()
session = dal.Session()
# get the ORM table value
target_table = Base.metadata.tables.get(game_dict['db_table_name'])
record_count = get_record_count(table_name=table_name)
target_game = game_dict['game_name']
end_year = datetime.today().year + 1
start_year = game_dict['inception_year'] if record_count > 0 else get_recent_draw_date(table_name=table_name).year
# check to see if there are records in the table
if get_record_count(table_name) > 0:
pass
else:
pass
else:
raise ValueError('An invalid table name was specified.')