mirror of
https://github.com/Xevion/linkpulse.git
synced 2026-01-31 06:24:47 -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
|
- Improved documentation in multiple areas
|
||||||
- `__main__.py`
|
- `__main__.py`
|
||||||
- `logging.py`
|
- `logging.py`
|
||||||
|
- A `get_db` utility function to retrieve a reference to the database (with type hinting).
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
@@ -25,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
- Mildly reformatted `README.md`
|
- Mildly reformatted `README.md`
|
||||||
- A development mode check for the `app.state.ip_pool`'s initialization (caused application failure in production only)
|
- 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
|
### Fixed
|
||||||
|
|
||||||
|
|||||||
@@ -59,9 +59,12 @@ def main(*args):
|
|||||||
|
|
||||||
# import most useful objects, models, and functions
|
# import most useful objects, models, and functions
|
||||||
lp = linkpulse # alias
|
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
|
from linkpulse.models import BaseModel, IPAddress
|
||||||
|
|
||||||
|
db = get_db()
|
||||||
|
|
||||||
# start REPL
|
# start REPL
|
||||||
from bpython import embed # type: ignore
|
from bpython import embed # type: ignore
|
||||||
|
|
||||||
|
|||||||
@@ -18,19 +18,19 @@ from fastapi_cache.backends.inmemory import InMemoryBackend
|
|||||||
from fastapi_cache.decorator import cache
|
from fastapi_cache.decorator import cache
|
||||||
from linkpulse.logging import setup_logging
|
from linkpulse.logging import setup_logging
|
||||||
from linkpulse.middleware import LoggingMiddleware
|
from linkpulse.middleware import LoggingMiddleware
|
||||||
from linkpulse.utilities import get_ip, hide_ip, is_development
|
from linkpulse.utilities import get_ip, hide_ip, is_development, get_db
|
||||||
from peewee import PostgresqlDatabase
|
|
||||||
from psycopg2.extras import execute_values
|
from psycopg2.extras import execute_values
|
||||||
|
|
||||||
load_dotenv(dotenv_path=".env")
|
load_dotenv(dotenv_path=".env")
|
||||||
|
|
||||||
from linkpulse import models, responses # type: ignore
|
from linkpulse import models, responses # type: ignore
|
||||||
|
|
||||||
db: PostgresqlDatabase = models.BaseModel._meta.database # type: ignore
|
db = get_db()
|
||||||
|
|
||||||
|
|
||||||
def flush_ips():
|
def flush_ips():
|
||||||
if len(app.state.buffered_updates) == 0:
|
if len(app.state.buffered_updates) == 0:
|
||||||
|
logger.debug("No IPs to flush to Database")
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -55,7 +55,7 @@ def flush_ips():
|
|||||||
logger.error("Failed to flush IPs to Database", error=e)
|
logger.error("Failed to flush IPs to Database", error=e)
|
||||||
|
|
||||||
i = len(app.state.buffered_updates)
|
i = len(app.state.buffered_updates)
|
||||||
logger.debug("Flushed IPs to Database", count=i)
|
logger.debug("IPs written to database", count=i)
|
||||||
|
|
||||||
# Finish up
|
# Finish up
|
||||||
app.state.buffered_updates.clear()
|
app.state.buffered_updates.clear()
|
||||||
|
|||||||
@@ -66,8 +66,9 @@ def main(*args: str) -> None:
|
|||||||
Args are fed directly from sys.argv.
|
Args are fed directly from sys.argv.
|
||||||
"""
|
"""
|
||||||
from linkpulse import models
|
from linkpulse import models
|
||||||
|
from linkpulse.utilities import get_db
|
||||||
|
|
||||||
db: PostgresqlDatabase = models.BaseModel._meta.database
|
db = get_db()
|
||||||
router = ExtendedRouter(
|
router = ExtendedRouter(
|
||||||
database=db,
|
database=db,
|
||||||
migrate_dir="linkpulse/migrations",
|
migrate_dir="linkpulse/migrations",
|
||||||
|
|||||||
@@ -1,10 +1,23 @@
|
|||||||
import os
|
import os
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from fastapi import Request
|
from fastapi import Request
|
||||||
|
from peewee import PostgresqlDatabase
|
||||||
|
|
||||||
is_development = os.getenv("ENVIRONMENT") == "development"
|
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:
|
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.
|
Pluralize a word based on count. Returns 's' if count is not 1, '' (empty string) otherwise.
|
||||||
|
|||||||
Reference in New Issue
Block a user