Add get_db utility function

- Minor changes in flush_ips log messages
This commit is contained in:
2024-11-01 18:01:26 -05:00
parent 85a2d82832
commit cf7536a39b
5 changed files with 25 additions and 6 deletions

View File

@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Improved documentation in multiple areas
- `__main__.py`
- `logging.py`
- A `get_db` utility function to retrieve a reference to the database (with type hinting).
### Fixed
@@ -25,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Mildly reformatted `README.md`
- A development mode check for the `app.state.ip_pool`'s initialization (caused application failure in production only)
- Applied `get_db` utility function in all applicable areas.
### Fixed

View File

@@ -59,9 +59,12 @@ def main(*args):
# import most useful objects, models, and functions
lp = linkpulse # alias
from linkpulse.app import app, db
from linkpulse.utilities import get_db
from linkpulse.app import app
from linkpulse.models import BaseModel, IPAddress
db = get_db()
# start REPL
from bpython import embed # type: ignore

View File

@@ -18,19 +18,19 @@ from fastapi_cache.backends.inmemory import InMemoryBackend
from fastapi_cache.decorator import cache
from linkpulse.logging import setup_logging
from linkpulse.middleware import LoggingMiddleware
from linkpulse.utilities import get_ip, hide_ip, is_development
from peewee import PostgresqlDatabase
from linkpulse.utilities import get_ip, hide_ip, is_development, get_db
from psycopg2.extras import execute_values
load_dotenv(dotenv_path=".env")
from linkpulse import models, responses # type: ignore
db: PostgresqlDatabase = models.BaseModel._meta.database # type: ignore
db = get_db()
def flush_ips():
if len(app.state.buffered_updates) == 0:
logger.debug("No IPs to flush to Database")
return
try:
@@ -55,7 +55,7 @@ def flush_ips():
logger.error("Failed to flush IPs to Database", error=e)
i = len(app.state.buffered_updates)
logger.debug("Flushed IPs to Database", count=i)
logger.debug("IPs written to database", count=i)
# Finish up
app.state.buffered_updates.clear()

View File

@@ -66,8 +66,9 @@ def main(*args: str) -> None:
Args are fed directly from sys.argv.
"""
from linkpulse import models
from linkpulse.utilities import get_db
db: PostgresqlDatabase = models.BaseModel._meta.database
db = get_db()
router = ExtendedRouter(
database=db,
migrate_dir="linkpulse/migrations",

View File

@@ -1,10 +1,23 @@
import os
from typing import Optional
from fastapi import Request
from peewee import PostgresqlDatabase
is_development = os.getenv("ENVIRONMENT") == "development"
def get_db() -> PostgresqlDatabase:
"""
Acquires the database connector from the BaseModel class.
This is not a cursor, but a connection to the database.
"""
# Might not be necessary, but I'd prefer to not import heavy modules with side effects in a utility module.
from linkpulse import models
return models.BaseModel._meta.database # type: ignore
def pluralize(count: int, word: Optional[str] = None) -> str:
"""
Pluralize a word based on count. Returns 's' if count is not 1, '' (empty string) otherwise.