Skip to content

Commit

Permalink
Allign database with reactMap
Browse files Browse the repository at this point in the history
  • Loading branch information
JabLuszko committed Sep 21, 2024
1 parent cf1610e commit 6816772
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 100 deletions.
66 changes: 0 additions & 66 deletions alembic/versions/676b8ac0e60a_stations.py

This file was deleted.

71 changes: 71 additions & 0 deletions alembic/versions/985738e37bdb_stations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""Stations
Revision ID: 985738e37bdb
Revises: b533c33be802
Create Date: 2024-09-21 16:31:01.118960
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql
import mapadroid

# revision identifiers, used by Alembic.
revision = '985738e37bdb'
down_revision = 'b533c33be802'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('station',
sa.Column('id', sa.String(length=50, collation='utf8mb4_unicode_ci'), nullable=False),
sa.Column('lat', sa.Double(asdecimal=True), nullable=False),
sa.Column('lon', sa.Double(asdecimal=True), nullable=False),
sa.Column('name', sa.String(length=128, collation='utf8mb4_unicode_ci'), nullable=False),
sa.Column('start_time', mapadroid.db.TZDateTime.TZDateTime(), nullable=False),
sa.Column('end_time', mapadroid.db.TZDateTime.TZDateTime(), nullable=False),
sa.Column('is_battle_available', sa.BOOLEAN(), nullable=False),
sa.Column('is_inactive', sa.BOOLEAN(), nullable=False),
sa.Column('battle_level', mysql.TINYINT(display_width=1, unsigned=True), nullable=True),
sa.Column('battle_spawn', mapadroid.db.TZDateTime.TZDateTime(), nullable=True),
sa.Column('battle_start', mapadroid.db.TZDateTime.TZDateTime(), nullable=True),
sa.Column('battle_end', mapadroid.db.TZDateTime.TZDateTime(), nullable=True),
sa.Column('battle_pokemon_id', mysql.SMALLINT(display_width=6, unsigned=True), nullable=True),
sa.Column('battle_pokemon_form', mysql.SMALLINT(display_width=6, unsigned=True), nullable=True),
sa.Column('battle_pokemon_costume', mysql.SMALLINT(display_width=6, unsigned=True), nullable=True),
sa.Column('battle_pokemon_gender', mysql.TINYINT(display_width=1, unsigned=True), nullable=True),
sa.Column('battle_pokemon_alignment', mysql.SMALLINT(display_width=6, unsigned=True),
nullable=True),
sa.Column('battle_pokemon_bread_mode', mysql.SMALLINT(display_width=6, unsigned=True),
nullable=True),
sa.Column('battle_pokemon_move_1', mysql.SMALLINT(display_width=6, unsigned=True), nullable=True),
sa.Column('battle_pokemon_move_2', mysql.SMALLINT(display_width=6, unsigned=True), nullable=True),
sa.Column('reward_pokemon_id', mysql.SMALLINT(display_width=6, unsigned=True), nullable=True),
sa.Column('reward_pokemon_form', mysql.SMALLINT(display_width=6, unsigned=True), nullable=True),
sa.Column('reward_pokemon_costume', mysql.SMALLINT(display_width=6, unsigned=True), nullable=True),
sa.Column('reward_pokemon_gender', mysql.TINYINT(display_width=1, unsigned=True), nullable=True),
sa.Column('reward_pokemon_alignment', mysql.SMALLINT(display_width=6, unsigned=True),
nullable=True),
sa.Column('reward_pokemon_bread_mode', mysql.SMALLINT(display_width=6, unsigned=True),
nullable=True),
sa.Column('total_stationed_pokemon', mysql.SMALLINT(display_width=6, unsigned=True), nullable=True),
sa.Column('updated', mapadroid.db.TZDateTime.TZDateTime(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_station_battle_end'), 'station', ['battle_end'], unique=False)
op.create_index(op.f('ix_station_battle_pokemon_id'), 'station', ['battle_pokemon_id'], unique=False)
op.create_index(op.f('ix_station_end_time'), 'station', ['end_time'], unique=False)
op.create_index(op.f('ix_station_updated'), 'station', ['updated'], unique=False)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_station_updated'), table_name='station')
op.drop_index(op.f('ix_station_end_time'), table_name='station')
op.drop_index(op.f('ix_station_battle_pokemon_id'), table_name='station')
op.drop_index(op.f('ix_station_battle_end'), table_name='station')
op.drop_table('station')
# ### end Alembic commands ###
25 changes: 13 additions & 12 deletions mapadroid/db/DbPogoProtoSubmitRaw.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import math
import time
from datetime import datetime, timedelta
Expand Down Expand Up @@ -531,7 +530,7 @@ async def update_seen_type_stats(self, session: AsyncSession, **kwargs):
await nested_transaction.commit()
except sqlalchemy.exc.IntegrityError as e:
await nested_transaction.rollback()
logger.debug("Failed submitting stat...")
logger.debug("Failed submitting mon stat {}", e)

async def spawnpoints(self, session: AsyncSession, map_proto: pogoprotos.GetMapObjectsOutProto,
received_timestamp: int):
Expand Down Expand Up @@ -1345,17 +1344,17 @@ async def stations(self, session: AsyncSession, received_timestamp: int,
station_obj: Optional[Station] = await StationHelper.get(session, station_id)
if not station_obj:
station_obj: Station = Station()
station_obj.station_id = station_id
station_obj.latitude = latitude
station_obj.longitude = longitude
station_obj.id = station_id
station_obj.lat = latitude
station_obj.lon = longitude
station_obj.name = name

if station.battle_details and station.battle_details.battle_window_end_ms > 0:
bdetails: pogoprotos.BreadBattleDetailProto = station.battle_details
station_obj.battle_spawn = DatetimeWrapper.fromtimestamp(float(bdetails.battle_spawn_ms / 1000))
station_obj.battle_window_start = DatetimeWrapper.fromtimestamp(
station_obj.battle_start = DatetimeWrapper.fromtimestamp(
float(bdetails.battle_window_start_ms / 1000))
station_obj.battle_window_end = DatetimeWrapper.fromtimestamp(
station_obj.battle_end = DatetimeWrapper.fromtimestamp(
float(bdetails.battle_window_end_ms / 1000))
station_obj.battle_level = bdetails.battle_level

Expand All @@ -1382,8 +1381,8 @@ async def stations(self, session: AsyncSession, received_timestamp: int,
station_obj.battle_pokemon_move_2 = pokemon_data.move2
else:
station_obj.battle_spawn = None
station_obj.battle_window_start = None
station_obj.battle_window_end = None
station_obj.battle_start = None
station_obj.battle_end = None
station_obj.battle_level = None
station_obj.reward_pokemon_id = None
station_obj.reward_pokemon_form = None
Expand All @@ -1399,12 +1398,14 @@ async def stations(self, session: AsyncSession, received_timestamp: int,
station_obj.battle_pokemon_bread_mode = None
station_obj.battle_pokemon_move_1 = None
station_obj.battle_pokemon_move_2 = None
station_obj.total_stationed_pokemon = None

station_obj.start_time = start_time
station_obj.end_time = end_time
station_obj.bread_battle_available = bread_battle_available
station_obj.inactive = inactive
station_obj.last_updated = received_at
station_obj.end_wtf_time = end_time
station_obj.is_battle_available = bread_battle_available
station_obj.is_inactive = inactive
station_obj.updated = received_at
async with session.begin_nested() as nested_transaction:
try:
session.add(station_obj)
Expand Down
4 changes: 2 additions & 2 deletions mapadroid/db/helper/StationHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
class StationHelper:
@staticmethod
async def get(session: AsyncSession, station_id: str) -> Optional[Station]:
stmt = select(Station).where(Station.station_id == station_id)
stmt = select(Station).where(Station.id == station_id)
result = await session.execute(stmt)
return result.scalars().first()

@staticmethod
async def get_changed_since(session: AsyncSession, _timestamp: int) -> List[Station]:
stmt = select(Station).where(Station.last_updated > DatetimeWrapper.fromtimestamp(_timestamp))
stmt = select(Station).where(Station.updated > DatetimeWrapper.fromtimestamp(_timestamp))
result = await session.execute(stmt)
return result.scalars().all()
25 changes: 13 additions & 12 deletions mapadroid/db/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,16 +873,22 @@ class Route(Base):
end_poi_image_url = Column(String(255, 'utf8mb4_unicode_ci'), nullable=True)
last_updated = Column(TZDateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))


class Station(Base):
__tablename__ = 'station'

station_id = Column(String(50, 'utf8mb4_unicode_ci'), primary_key=True)
latitude = Column(Double(asdecimal=True), nullable=False)
longitude = Column(Double(asdecimal=True), nullable=False)
id = Column(String(50, 'utf8mb4_unicode_ci'), primary_key=True)
lat = Column(Double(asdecimal=True), nullable=False)
lon = Column(Double(asdecimal=True), nullable=False)
name = Column(String(128, 'utf8mb4_unicode_ci'), nullable=False)
start_time = Column(TZDateTime, nullable=False)
end_time = Column(TZDateTime, index=True, nullable=False)
is_battle_available = Column(BOOLEAN, nullable=False)
is_inactive = Column(BOOLEAN, nullable=False)
battle_level = Column(TINYINT(1, unsigned=True))
battle_spawn = Column(TZDateTime)
battle_window_start = Column(TZDateTime)
battle_window_end = Column(TZDateTime, index=True)
battle_start = Column(TZDateTime)
battle_end = Column(TZDateTime, index=True)
battle_pokemon_id = Column(SMALLINT(6, unsigned=True), index=True)
battle_pokemon_form = Column(SMALLINT(6, unsigned=True))
battle_pokemon_costume = Column(SMALLINT(6, unsigned=True))
Expand All @@ -891,16 +897,11 @@ class Station(Base):
battle_pokemon_bread_mode = Column(SMALLINT(6, unsigned=True))
battle_pokemon_move_1 = Column(SMALLINT(6, unsigned=True))
battle_pokemon_move_2 = Column(SMALLINT(6, unsigned=True))
battle_level = Column(TINYINT(1, unsigned=True))
reward_pokemon_id = Column(SMALLINT(6, unsigned=True))
reward_pokemon_form = Column(SMALLINT(6, unsigned=True))
reward_pokemon_costume = Column(SMALLINT(6, unsigned=True))
reward_pokemon_gender = Column(TINYINT(1, unsigned=True))
reward_pokemon_alignment = Column(SMALLINT(6, unsigned=True))
reward_pokemon_bread_mode = Column(SMALLINT(6, unsigned=True))
start_time = Column(TZDateTime, nullable=False)
end_time = Column(TZDateTime, index=True, nullable=False)
bread_battle_available = Column(BOOLEAN, nullable=False)
inactive = Column(BOOLEAN, nullable=False)
last_updated = Column(TZDateTime, index=True, nullable=False)

total_stationed_pokemon = Column(SMALLINT(6, unsigned=True))
updated = Column(TZDateTime, index=True, nullable=False)
16 changes: 8 additions & 8 deletions mapadroid/webhook/webhookworker.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,21 +782,21 @@ async def __prepare_station_data(self, session: AsyncSession, _timestamp: int):
continue

station_payload = {
"station_id": station.station_id,
"latitude": station.latitude,
"longitude": station.longitude,
"station_id": station.id,
"latitude": station.lat,
"longitude": station.lon,
"start": station.start_time,
"end": station.end_time,
"name": station.name,
"inactive": station.inactive,
"bread_battle_available": station.bread_battle_available,
"last_updated": station.last_updated,
"inactive": station.is_inactive,
"bread_battle_available": station.is_battle_available,
"last_updated": station.updated,
}

if station.battle_spawn is not None:
station_payload["battle_spawn"] = int(station.battle_spawn.timestamp())
station_payload["battle_start"] = int(station.battle_window_start.timestamp())
station_payload["battle_end"] = int(station.battle_window_end.timestamp())
station_payload["battle_start"] = int(station.battle_start.timestamp())
station_payload["battle_end"] = int(station.battle_end.timestamp())
station_payload["battle_level"] = station.battle_level

if station.battle_pokemon_id is not None:
Expand Down

0 comments on commit 6816772

Please sign in to comment.