Skip to content

reformatting_attempt #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

target_metadata = Base.metadata


def run_migrations_offline() -> None:
url = config.get_main_option("sqlalchemy.url")
context.configure(
Expand All @@ -32,6 +33,7 @@ def run_migrations_offline() -> None:
with context.begin_transaction():
context.run_migrations()


def run_migrations_online() -> None:
connectable = engine_from_config(
config.get_section(config.config_ini_section, {}),
Expand All @@ -40,14 +42,13 @@ def run_migrations_online() -> None:
)

with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)
context.configure(connection=connection, target_metadata=target_metadata)

with context.begin_transaction():
context.run_migrations()


if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
run_migrations_online()
26 changes: 20 additions & 6 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ def format_x_axis(ax, period: int):
ax.xaxis.set_major_locator(mdates.DayLocator(interval=7))


def create_plot(server: Server, player_counts: List[PlayerCount], period: int) -> BytesIO:
def create_plot(
server: Server, player_counts: List[PlayerCount], period: int
) -> BytesIO:
"""Create matplotlib plot and return as BytesIO buffer"""
timestamps = [pc.timestamp for pc in player_counts]
counts = [pc.player_count for pc in player_counts]
Expand All @@ -95,7 +97,11 @@ def create_plot(server: Server, player_counts: List[PlayerCount], period: int) -


def generate_html_content(
server: Server, server_id: str, period: int, player_counts: List[PlayerCount], image_base64: str
server: Server,
server_id: str,
period: int,
player_counts: List[PlayerCount],
image_base64: str,
) -> str:
"""Generate HTML content for the graph page using Jinja2 template"""
end_date = datetime.utcnow()
Expand Down Expand Up @@ -127,7 +133,9 @@ def get_servers(db: Session = Depends(get_db)):
@app.get("/graph/{server_id}")
async def generate_graph(
server_id: str,
period: int = Query(default=7, ge=1, le=365, description="Number of days to display"),
period: int = Query(
default=7, ge=1, le=365, description="Number of days to display"
),
db: Session = Depends(get_db),
):
"""Generate a line graph of player counts for a specific server"""
Expand All @@ -137,14 +145,18 @@ async def generate_graph(
buffer = create_plot(server, player_counts, period)
image_base64 = base64.b64encode(buffer.getvalue()).decode()

html_content = generate_html_content(server, server_id, period, player_counts, image_base64)
html_content = generate_html_content(
server, server_id, period, player_counts, image_base64
)
return HTMLResponse(content=html_content)


@app.get("/graph/{server_id}/image")
async def get_graph_image(
server_id: str,
period: int = Query(default=7, ge=1, le=365, description="Number of days to display"),
period: int = Query(
default=7, ge=1, le=365, description="Number of days to display"
),
db: Session = Depends(get_db),
):
"""Get just the graph image as PNG (for embedding in other pages)"""
Expand All @@ -158,7 +170,9 @@ async def get_graph_image(
@app.get("/stats/{server_id}")
def get_server_stats(
server_id: str,
period: int = Query(default=7, ge=1, le=365, description="Number of days for statistics"),
period: int = Query(
default=7, ge=1, le=365, description="Number of days for statistics"
),
db: Session = Depends(get_db),
):
"""Get statistics for a specific server"""
Expand Down
3 changes: 1 addition & 2 deletions src/models/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
pool_pre_ping=True, # Test connections before use (prevents stale connections)
pool_recycle=3600, # Recycle connections after 1 hour
pool_timeout=30, # Timeout when getting connection from pool
echo=False # Set to True for SQL debugging

echo=False, # Set to True for SQL debugging
)

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Expand Down
4 changes: 3 additions & 1 deletion src/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ def save_results(db: Session, results):

# Create player count entry
player_count = PlayerCount(
server_id=result["server_id"], timestamp=timestamp, player_count=result["player_count"]
server_id=result["server_id"],
timestamp=timestamp,
player_count=result["player_count"],
)
db.add(player_count)
db.flush() # Get the ID
Expand Down
Loading