mirror of
https://github.com/Xevion/linkpulse.git
synced 2025-12-07 05:15:36 -06:00
Add get_db utility function
- Minor changes in flush_ips log messages
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user