Skip to content

Commit

Permalink
[2024-05-15 10:40] Updates to packages and reinstated Prometheus usag… (
Browse files Browse the repository at this point in the history
#124)

* [2024-05-15 10:40] Updates to packages and reinstated Prometheus usage as default "yes".

Signed-off-by: Raoul Linnenbank <58594297+rflinnenbank@users.noreply.github.com>

* Update pull-request-workflow.yml

Signed-off-by: Raoul Linnenbank <58594297+rflinnenbank@users.noreply.github.com>

---------

Signed-off-by: Raoul Linnenbank <58594297+rflinnenbank@users.noreply.github.com>
  • Loading branch information
rflinnenbank committed May 15, 2024
1 parent 7d1f0be commit d7e812d
Show file tree
Hide file tree
Showing 46 changed files with 259 additions and 225 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/pull-request-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ jobs:
poetry run coverage run -m pytest
poetry run coverage report
# Generate XML Coverage result file
- if: matrix.version == '3.8'
- if: matrix.version == '3.10'
name: Generate Coverage XML file
run: poetry run coverage xml
# Upload coverage data to SonarCloud.io
- if: matrix.version == '3.8'
- if: matrix.version == '3.10'
name: Update SonarCloud.io
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "weather_provider_api"
version = "2.51.0"
version = "2.52.0"
description = "Weather Provider Libraries and API"
authors = ["Verbindingsteam", "Raoul Linnenbank <58594297+rflinnenbank@users.noreply.github.com>"]
license = "MPL-2.0"
Expand Down
2 changes: 1 addition & 1 deletion weather_provider_api/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# SPDX-FileCopyrightText: 2019-2023 Alliander N.V.
# SPDX-License-Identifier: MPL-2.0

""" Main executable module """
"""Main executable module."""

import uvicorn
from loguru import logger
Expand Down
2 changes: 1 addition & 1 deletion weather_provider_api/app_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# SPDX-FileCopyrightText: 2019-2023 Alliander N.V.
# SPDX-License-Identifier: MPL-2.0

""" Version detection module """
"""Version detection module."""

from importlib import metadata

Expand Down
2 changes: 1 addition & 1 deletion weather_provider_api/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# SPDX-FileCopyrightText: 2019-2023 Alliander N.V.
# SPDX-License-Identifier: MPL-2.0

""" Configuration folder """
"""Configuration folder"""

import os
import tempfile
Expand Down
2 changes: 1 addition & 1 deletion weather_provider_api/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use_file_loggers = false
file_logger_folder = "/tmp/wpla_logs"

[components]
prometheus = false
prometheus = true
cors = false
cors_allowed_origins = ['*']
cors_allowed_origins_regex = []
Expand Down
12 changes: 8 additions & 4 deletions weather_provider_api/core/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@
# SPDX-FileCopyrightText: 2019-2023 Alliander N.V.
# SPDX-License-Identifier: MPL-2.0

""" Main Application """
"""Main Application."""

from fastapi import FastAPI
from starlette.responses import RedirectResponse

from weather_provider_api.app_version import APP_VERSION
from weather_provider_api.config import APP_CONFIG
from weather_provider_api.core.initializers.cors import initialize_cors_middleware
from weather_provider_api.core.initializers.exception_handling import initialize_exception_handler
from weather_provider_api.core.initializers.exception_handling import (
initialize_exception_handler,
)
from weather_provider_api.core.initializers.headers import initialize_header_metadata
from weather_provider_api.core.initializers.logging_handler import initialize_logging
from weather_provider_api.core.initializers.mounting import mount_api_version
from weather_provider_api.core.initializers.prometheus import initialize_prometheus_interface
from weather_provider_api.core.initializers.prometheus import (
initialize_prometheus_interface,
)
from weather_provider_api.core.initializers.validation import initialize_api_validation
from weather_provider_api.versions.v1 import app as v1
from weather_provider_api.versions.v2 import app as v2
Expand Down Expand Up @@ -65,7 +69,7 @@ def _build_api_application() -> FastAPI:
# Adding a redirect from the root of the application to our default view
@application.get("/")
def redirect_to_docs():
"""This function redirects the visitors to the default view from the application's base URL"""
"""This function redirects the visitors to the default view from the application's base URL."""
return RedirectResponse(url="/api/v2/docs")

application.openapi()
Expand Down
4 changes: 2 additions & 2 deletions weather_provider_api/core/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# SPDX-FileCopyrightText: 2019-2022 Alliander N.V.
# SPDX-License-Identifier: MPL-2.0

""" Base Model class
"""Base Model class.
An expansion of the PydanticBaseModel used to build each Weather Provider Base class.
"""
Expand All @@ -13,7 +13,7 @@


class BaseModel(PydanticBaseModel):
"""Minimal PydanticBaseMode expansion used in all base classes"""
"""Minimal PydanticBaseMode expansion used in all base classes."""

class Config:
from_attributes = True
2 changes: 1 addition & 1 deletion weather_provider_api/core/initializers/cors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# SPDX-FileCopyrightText: 2019-2023 Alliander N.V.
# SPDX-License-Identifier: MPL-2.0

""" CORS support """
"""CORS support"""

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
Expand Down
6 changes: 3 additions & 3 deletions weather_provider_api/core/initializers/exception_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
# SPDX-FileCopyrightText: 2019-2022 Alliander N.V.
# SPDX-License-Identifier: MPL-2.0

""" Exception Handling
"""Exception Handling
This module holds the API's exception handler.
"""

from fastapi import FastAPI
from loguru import logger

from starlette.exceptions import HTTPException as StarletteHTTPException
from starlette.requests import Request
from starlette.responses import JSONResponse
Expand All @@ -35,13 +34,14 @@ def initialize_exception_handler(application: FastAPI):
Args:
application: The FastAPI application to attach the custom exception handler to.
Returns:
Nothing. The FastAPI application itself is updated.
Notes:
This method assumes that the FastAPI application given has a [title] set.
TODO:
Todo:
Evaluate the dependency on [title] and [root_path] parameters being set for FastAPI applications in ALL
initializers. By either extending the base class to enforce these or improving the code to not be dependent on
these parameters, we can eradiate code smell and chances at Exceptions caused by not having these parameters.
Expand Down
2 changes: 1 addition & 1 deletion weather_provider_api/core/initializers/headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


from fastapi import FastAPI

from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request

Expand All @@ -19,6 +18,7 @@ def initialize_header_metadata(application: FastAPI):
Args:
application: The FastAPI application to attach the custom method to.
Returns:
Nothing. The FastAPI application itself is updated.
"""
Expand Down
4 changes: 2 additions & 2 deletions weather_provider_api/core/initializers/logging_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# SPDX-FileCopyrightText: 2019-2023 Alliander N.V.
# SPDX-License-Identifier: MPL-2.0

""" Logging Handler
"""Logging Handler
This module initializes the logging intercept handler, which intercepts other logging methods and translates them into
the intended loguru logging format.
Expand All @@ -17,7 +17,7 @@

from loguru import logger

from weather_provider_api.config import APP_DEBUGGING, APP_CONFIG, APP_LOG_LEVEL
from weather_provider_api.config import APP_CONFIG, APP_DEBUGGING, APP_LOG_LEVEL


class LoggingInterceptHandler(logging.Handler):
Expand Down
2 changes: 1 addition & 1 deletion weather_provider_api/core/initializers/mounting.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# SPDX-FileCopyrightText: 2019-2023 Alliander N.V.
# SPDX-License-Identifier: MPL-2.0

""" (sub)-API Mounting
"""(sub)-API Mounting
This module handles the mounting of sub-API's onto a main API.
"""
Expand Down
4 changes: 2 additions & 2 deletions weather_provider_api/core/initializers/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
# SPDX-FileCopyrightText: 2019-2022 Alliander N.V.
# SPDX-License-Identifier: MPL-2.0

""" Prometheus Middleware handler """
"""Prometheus Middleware handler"""

from fastapi import FastAPI
from loguru import logger

from starlette_prometheus import PrometheusMiddleware, metrics


Expand All @@ -18,6 +17,7 @@ def initialize_prometheus_interface(application: FastAPI, metrics_endpoint: str
Args:
application: The FastAPI application to attach the Prometheus Middleware to.
metrics_endpoint: The endpoint at which the Prometheus data should become available.
Returns:
Nothing. The FastAPI application itself is updated.
Expand Down
2 changes: 1 addition & 1 deletion weather_provider_api/core/initializers/rate_limiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# SPDX-FileCopyrightText: 2019-2023 Alliander N.V.
# SPDX-License-Identifier: MPL-2.0

""" Request Rate Limiter initializer"""
"""Request Rate Limiter initializer"""

from slowapi import Limiter
from slowapi.util import get_remote_address
Expand Down
6 changes: 4 additions & 2 deletions weather_provider_api/core/initializers/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
# SPDX-FileCopyrightText: 2019-2022 Alliander N.V.
# SPDX-License-Identifier: MPL-2.0

""" API Validation checks """
"""API Validation checks"""

from datetime import datetime
import re
from datetime import datetime

from fastapi import FastAPI
from loguru import logger
Expand All @@ -26,6 +26,7 @@ def initialize_api_validation(application: FastAPI):
Args:
application: The FastAPI application to attach the checker to.
Returns:
Nothing. The application itself is updated.
Expand All @@ -37,6 +38,7 @@ async def check_api_for_validity(request: Request, call_next):
Args:
request: The request to evaluate
call_next: The call_next object for the request (what to do next if this step doesn't raise any exceptions)
Returns:
The next step to execute for this request. This is either the original call_next, or an
HTTP Exception trigger for an APIExpiredException.
Expand Down
12 changes: 6 additions & 6 deletions weather_provider_api/routers/weather/api_view_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
# SPDX-FileCopyrightText: 2019-2022 Alliander N.V.
# SPDX-License-Identifier: MPL-2.0

"""
TODO:
"""TODO:
- Decouple weather factors from the get_weather function
- Add the async process for e.g. CDS and Harmonie
- Improve datetime slicing
Expand All @@ -26,7 +25,9 @@
result_mime_types,
)
from weather_provider_api.routers.weather.controller import WeatherController
from weather_provider_api.routers.weather.sources.weather_alert.weather_alert import WeatherAlert
from weather_provider_api.routers.weather.sources.weather_alert.weather_alert import (
WeatherAlert,
)
from weather_provider_api.routers.weather.utils import serializers
from weather_provider_api.routers.weather.utils.date_helpers import parse_datetime
from weather_provider_api.routers.weather.utils.file_helpers import remove_file
Expand Down Expand Up @@ -73,10 +74,10 @@ async def get_sync_weather(
fmt_args: WeatherFormattingRequestQuery = Depends(),
accept: str = Depends(header_accept_type),
): # pragma: no cover
"""
Function to gather data for a specific model using specific settings (location, period, factors, e.g.).
"""Function to gather data for a specific model using specific settings (location, period, factors, e.g.).
The function then formats this data into the requested output format (file-format and selected output unit) and
returns it.
Args:
source_id: The identifier for the chosen source
model_id: The identifier for the chosen model
Expand All @@ -91,7 +92,6 @@ async def get_sync_weather(
the WeatherContentRequestQuery.
"""

# TODO: Append a proper definition of the accept arg
source_id = source_id.lower()
model_id = model_id.lower()
Expand Down
7 changes: 4 additions & 3 deletions weather_provider_api/routers/weather/api_view_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
result_mime_types,
)
from weather_provider_api.routers.weather.controller import WeatherController
from weather_provider_api.routers.weather.sources.weather_alert.weather_alert import WeatherAlert
from weather_provider_api.routers.weather.sources.weather_alert.weather_alert import (
WeatherAlert,
)
from weather_provider_api.routers.weather.utils import serializers
from weather_provider_api.routers.weather.utils.date_helpers import parse_datetime
from weather_provider_api.routers.weather.utils.file_helpers import remove_file
Expand Down Expand Up @@ -94,8 +96,7 @@ async def get_sync_weather(
fmt_args: WeatherFormattingRequestQuery = Depends(),
accept: str = Depends(header_accept_type),
): # pragma: no cover
"""
<B>Request weather data for a specific Model using the given settings (location, period, weather factors, e.g.). <BR>
"""<B>Request weather data for a specific Model using the given settings (location, period, weather factors, e.g.). <BR>
This data is then formatted as the requested output format (output unit system and file format) before returning the requested data.</B>
<I>(Please note that as some models are predictive or otherwise restricted in the periods available for requests,
Expand Down
13 changes: 6 additions & 7 deletions weather_provider_api/routers/weather/base_models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
import numpy as np
import xarray as xr

from weather_provider_api.core.initializers.exception_handling import NOT_IMPLEMENTED_ERROR
from weather_provider_api.core.initializers.exception_handling import (
NOT_IMPLEMENTED_ERROR,
)
from weather_provider_api.routers.weather.api_models import OutputUnit
from weather_provider_api.routers.weather.utils.geo_position import GeoPosition


class WeatherModelBase(metaclass=ABCMeta):
"""
Base class for all Weather Models. All new models should use this base class!
"""Base class for all Weather Models. All new models should use this base class!
"""

def __init__(self):
Expand All @@ -40,8 +41,7 @@ def is_async(self): # pragma: no cover
raise NotImplementedError(NOT_IMPLEMENTED_ERROR)

def convert_names_and_units(self, weather_data: xr.Dataset, unit: OutputUnit):
"""
Function to convert all names and units in a dataset according to the required translations set in the
"""Function to convert all names and units in a dataset according to the required translations set in the
to_xxxx values for the model itself
Args:
weather_data: A Xarray Dataset containing
Expand Down Expand Up @@ -153,8 +153,7 @@ def dutch_wind_direction_to_degrees_single(x):

@staticmethod
def knmi_visibility_class_to_meter_estimate(xs):
"""
Function to transform KNMI visibility class values to an estimate of meters visibility
"""Function to transform KNMI visibility class values to an estimate of meters visibility
Args:
xs: The visibility class value to be interpreted
Expand Down
12 changes: 7 additions & 5 deletions weather_provider_api/routers/weather/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
from weather_provider_api.routers.weather.api_models import OutputUnit
from weather_provider_api.routers.weather.base_models.model import WeatherModelBase
from weather_provider_api.routers.weather.base_models.source import WeatherSourceBase
from weather_provider_api.routers.weather.exceptions import UnknownModelException, UnknownSourceException
from weather_provider_api.routers.weather.exceptions import (
UnknownModelException,
UnknownSourceException,
)
from weather_provider_api.routers.weather.sources.cds.cds import CDS
from weather_provider_api.routers.weather.sources.knmi.knmi import KNMI
from weather_provider_api.routers.weather.utils.geo_position import GeoPosition


class WeatherController(object): # pragma: no cover
"""
A Controller Object that can handle any request for the hooked up weather models and format it into any of the
"""A Controller Object that can handle any request for the hooked up weather models and format it into any of the
hooked up output formats.
"""

Expand All @@ -42,9 +44,9 @@ def get_weather(
end: Optional[datetime.datetime] = None,
factors: List[str] = None,
):
"""
Function to use the requested weather model from the requested source to get specific weather factors for a
"""Function to use the requested weather model from the requested source to get specific weather factors for a
specific time and specific location(s)
Args:
source_id: The weather source that need to be queried (e.g.: knmi, cds)
model_id: The model identifier of the model that needs to be queried (has to be a model that exists within
Expand Down
Loading

0 comments on commit d7e812d

Please sign in to comment.