-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #228 from BrewBlox/develop
Edge release
- Loading branch information
Showing
36 changed files
with
2,508 additions
and
2,388 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.venv/ | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,3 +75,4 @@ victoria/* | |
|
||
redis/* | ||
!redis/.gitkeep | ||
.appenv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
FROM python:3.11-bookworm as base | ||
|
||
ENV PIP_EXTRA_INDEX_URL=https://www.piwheels.org/simple | ||
ENV PIP_FIND_LINKS=/wheeley | ||
ENV VENV=/app/.venv | ||
ENV PATH="$VENV/bin:$PATH" | ||
|
||
COPY ./dist /app/dist | ||
|
||
RUN <<EOF | ||
set -ex | ||
|
||
mkdir /wheeley | ||
python3 -m venv $VENV | ||
pip3 install --upgrade pip wheel setuptools | ||
pip3 wheel --wheel-dir=/wheeley -r /app/dist/requirements.txt | ||
pip3 wheel --wheel-dir=/wheeley /app/dist/*.tar.gz | ||
EOF | ||
|
||
FROM python:3.11-slim-bookworm | ||
EXPOSE 5000 | ||
WORKDIR /app | ||
|
||
ENV PIP_FIND_LINKS=/wheeley | ||
ENV VENV=/app/.venv | ||
ENV PATH="$VENV/bin:$PATH" | ||
|
||
COPY --from=base /wheeley /wheeley | ||
COPY ./parse_appenv.py ./parse_appenv.py | ||
COPY ./entrypoint.sh ./entrypoint.sh | ||
|
||
RUN <<EOF | ||
set -ex | ||
|
||
python3 -m venv $VENV | ||
pip3 install --no-index brewblox_history | ||
pip3 freeze | ||
rm -rf /wheeley | ||
EOF | ||
|
||
|
||
ENTRYPOINT ["bash", "./entrypoint.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,3 @@ | ||
# History Service | ||
|
||
The history service is the gatekeeper for Brewblox databases. It writes data from history events, and offers REST interfaces for querying the InfluxDB and Redis databases. | ||
|
||
## Features | ||
|
||
### QueryClient ([influx.py](./brewblox_history/influx.py)) | ||
|
||
Handles directly querying InfluxDB. API functions eventually call this. | ||
|
||
### InfluxWriter ([influx.py](./brewblox_history/influx.py)) | ||
|
||
Periodically writes scheduled data points to InfluxDB. | ||
|
||
Publicly offers the `write_soon()` function, where data can be scheduled for writing. | ||
|
||
### DataRelay ([relays.py](./brewblox_history/relays.py)) | ||
|
||
Subscribes to the `--history-topic` topic on the event bus, and schedules all received data for writing to the database. | ||
|
||
### Datastore ([redis.py](./brewblox_history/redis.py)) | ||
|
||
Offers a simple wrapper around the Redis API. | ||
Changes are broadcast to the `--datastore-topic` topic. | ||
|
||
## REST API | ||
|
||
### queries ([query_api.py](./brewblox_history/query_api.py)) | ||
|
||
REST API for the InfluxDB database. Also offers WebSocket endpoints for streamed values. | ||
|
||
### datastore ([datastore_api](./brewblox_history/datastore_api.py)) | ||
|
||
REST API for the Redis datastore. | ||
The history service is the gatekeeper for Brewblox databases. It writes data from history events, and offers REST interfaces for querying the Victoria and Redis databases. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import logging | ||
from contextlib import AsyncExitStack, asynccontextmanager | ||
from pprint import pformat | ||
|
||
from fastapi import FastAPI, Request | ||
from fastapi.responses import JSONResponse | ||
|
||
from . import (datastore_api, mqtt, redis, relays, timeseries_api, utils, | ||
victoria) | ||
from .models import ErrorResponse | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def setup_logging(debug: bool): | ||
level = logging.DEBUG if debug else logging.INFO | ||
unimportant_level = logging.INFO if debug else logging.WARN | ||
format = '%(asctime)s.%(msecs)03d [%(levelname).1s:%(name)s:%(lineno)d] %(message)s' | ||
datefmt = '%Y/%m/%d %H:%M:%S' | ||
|
||
logging.basicConfig(level=level, format=format, datefmt=datefmt) | ||
logging.captureWarnings(True) | ||
|
||
logging.getLogger('gmqtt').setLevel(unimportant_level) | ||
logging.getLogger('httpx').setLevel(unimportant_level) | ||
logging.getLogger('httpcore').setLevel(logging.WARN) | ||
logging.getLogger('uvicorn.access').setLevel(unimportant_level) | ||
logging.getLogger('uvicorn.error').disabled = True | ||
|
||
|
||
def add_exception_handlers(app: FastAPI): | ||
config = utils.get_config() | ||
logger = logging.getLogger('history.error') | ||
|
||
@app.exception_handler(Exception) | ||
async def catchall_handler(request: Request, exc: Exception) -> JSONResponse: | ||
short = utils.strex(exc) | ||
details = utils.strex(exc, tb=config.debug) | ||
content = ErrorResponse(error=str(exc), | ||
details=details) | ||
|
||
logger.error(f'[{request.url}] => {short}') | ||
logger.debug(details) | ||
return JSONResponse(content.model_dump(), status_code=500) | ||
|
||
|
||
@asynccontextmanager | ||
async def lifespan(app: FastAPI): | ||
LOGGER.info(utils.get_config()) | ||
LOGGER.debug('ROUTES:\n' + pformat(app.routes)) | ||
LOGGER.debug('LOGGERS:\n' + pformat(logging.root.manager.loggerDict)) | ||
|
||
async with AsyncExitStack() as stack: | ||
await stack.enter_async_context(mqtt.lifespan()) | ||
await stack.enter_async_context(redis.lifespan()) | ||
yield | ||
|
||
|
||
def create_app() -> FastAPI: | ||
config = utils.get_config() | ||
setup_logging(config.debug) | ||
|
||
# Call setup functions for modules | ||
mqtt.setup() | ||
redis.setup() | ||
victoria.setup() | ||
relays.setup() | ||
|
||
# Create app | ||
# OpenApi endpoints are set to /api/doc for backwards compatibility | ||
prefix = f'/{config.name}' | ||
app = FastAPI(lifespan=lifespan, | ||
docs_url=f'{prefix}/api/doc', | ||
redoc_url=f'{prefix}/api/redoc', | ||
openapi_url=f'{prefix}/openapi.json') | ||
|
||
# Set standardized error response | ||
add_exception_handlers(app) | ||
|
||
# Include all endpoints declared by modules | ||
app.include_router(datastore_api.router, prefix=prefix) | ||
app.include_router(timeseries_api.router, prefix=prefix) | ||
|
||
return app |
Oops, something went wrong.