Skip to content

Commit

Permalink
Dirty use text() for DbCleanup with limits
Browse files Browse the repository at this point in the history
  • Loading branch information
Grennith committed Sep 23, 2023
1 parent 3019e16 commit b537739
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
25 changes: 17 additions & 8 deletions mapadroid/db/helper/PokemonHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ async def get_pokemon_spawn_counts(session: AsyncSession, hours: int = None) ->

@staticmethod
async def get_to_be_encountered(session: AsyncSession, geofence_helper: Optional[GeofenceHelper],
min_time_left_seconds: int, eligible_mon_ids: Optional[List[int]]) -> List[Tuple[int, Location, int]]:
min_time_left_seconds: int, eligible_mon_ids: Optional[List[int]]) -> List[
Tuple[int, Location, int]]:
if min_time_left_seconds is None or not eligible_mon_ids:
logger.warning(
"DbWrapper::get_to_be_encountered: Not returning any encounters since no time left or "
Expand Down Expand Up @@ -144,8 +145,8 @@ async def get_mons_in_rectangle(session: AsyncSession,
@staticmethod
async def get_all_shiny(session: AsyncSession, timestamp_after: Optional[int] = None,
timestamp_before: Optional[int] = None) -> Dict[int,
Tuple[Pokemon,
List[TrsStatsDetectWildMonRaw]]]:
Tuple[Pokemon,
List[TrsStatsDetectWildMonRaw]]]:
"""
Used to be DbStatsReader::get_shiny_stats_v2
Args:
Expand Down Expand Up @@ -294,8 +295,8 @@ async def get_noniv_encounters_count(session: AsyncSession,
@staticmethod
async def get_changed_since(session: AsyncSession, _timestamp: int,
mon_types: Optional[Set[MonSeenTypes]] = None) -> List[Tuple[Pokemon, TrsSpawn,
Optional[Pokestop],
Optional[PokemonDisplay]]]:
Optional[Pokestop],
Optional[PokemonDisplay]]]:
if not mon_types:
mon_types = {MonSeenTypes.encounter, MonSeenTypes.lure_encounter}

Expand All @@ -320,10 +321,18 @@ async def delete_older_than_n_hours(session: AsyncSession, hours: int, limit: Op
where_condition = Pokemon.disappear_time < DatetimeWrapper.now() - datetime.timedelta(hours=hours)
stmt = delete(Pokemon).where(where_condition)
if limit:
stmt = stmt.with_dialect_options(mysql_limit=limit, mariadb_limit=limit)
await session.execute(stmt)
# Rather ugly construct as stmt.with_dialect_options currently does not work
# See https://groups.google.com/g/sqlalchemy/c/WDKhyAt6eAk/m/feteFNZnAAAJ
stmt = text(f"{str(stmt)} LIMIT :limit")
await session.execute(stmt,
{
"disappear_time_1": DatetimeWrapper.now() - datetime.timedelta(hours=hours),
"limit": limit
})
else:
await session.execute(stmt)

@staticmethod
async def run_optimize(session: AsyncSession) -> None:
stmt = text(f"OPTIMIZE {Pokemon.__tablename__}")
stmt = text(f"OPTIMIZE TABLE {Pokemon.__tablename__}")
await session.execute(stmt)
17 changes: 13 additions & 4 deletions mapadroid/db/helper/PokestopIncidentHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,20 @@ async def get(session: AsyncSession, pokestop_id: str, incident_id: str) -> Opti
async def delete_older_than_n_hours(session: AsyncSession, hours: int, limit: Optional[int]) -> None:
where_condition = PokestopIncident.incident_expiration < DatetimeWrapper.now() - datetime.timedelta(hours=hours)
stmt = delete(PokestopIncident).where(where_condition)
if limit is not None:
stmt = stmt.with_dialect_options(mysql_limit=limit, mariadb_limit=limit)
await session.execute(stmt)
if limit:
# Rather ugly construct as stmt.with_dialect_options currently does not work
# See https://groups.google.com/g/sqlalchemy/c/WDKhyAt6eAk/m/feteFNZnAAAJ
stmt = text(f"{str(stmt)} LIMIT :limit")
await session.execute(stmt,
{
"incident_expiration_1": DatetimeWrapper.now() - datetime.timedelta(hours=hours),
"limit": limit
}
)
else:
await session.execute(stmt)

@staticmethod
async def run_optimize(session: AsyncSession) -> None:
stmt = text(f"OPTIMIZE {PokestopIncident.__tablename__}")
stmt = text(f"OPTIMIZE TABLE {PokestopIncident.__tablename__}")
await session.execute(stmt)

0 comments on commit b537739

Please sign in to comment.