-
-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reading from real DB #10
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,26 @@ | ||
# Build a virtualenv using venv | ||
# * Install required compilation tools for wheels via apt | ||
FROM debian:12-slim AS build | ||
RUN apt -qq update && apt -qq install -y python3-venv gcc libpython3-dev && \ | ||
python3 -m venv /venv && \ | ||
/venv/bin/pip install --upgrade -q pip setuptools wheel | ||
FROM python:3.11-slim | ||
|
||
# Install packages into the virtualenv as a separate step | ||
# * Only re-execute this step when the requirements files change | ||
FROM build AS install-deps | ||
# install requirements | ||
RUN apt-get clean | ||
RUN apt-get update && apt-get install -y --no-install-recommends \ | ||
build-essential \ | ||
libatlas-base-dev \ | ||
libgdal-dev \ | ||
gfortran | ||
|
||
# Copy required files. | ||
WORKDIR /app | ||
COPY pyproject.toml pyproject.toml | ||
RUN /venv/bin/pip install -q . --no-cache-dir --no-binary=india_api | ||
|
||
# Build binary for the package | ||
# * The package is versioned via setuptools_git_versioning | ||
# hence the .git directory is required | ||
# * The README.md is required for the long description | ||
FROM install-deps AS build-app | ||
COPY src src | ||
COPY .git .git | ||
COPY README.md README.md | ||
RUN /venv/bin/pip install . | ||
RUN ls /venv/bin | ||
|
||
# Copy the virtualenv into a distroless image | ||
# * These are small images that only contain the runtime dependencies | ||
FROM gcr.io/distroless/python3-debian12 | ||
COPY --from=build-app /venv /venv | ||
# set working directory | ||
WORKDIR /app | ||
|
||
# Install python requirements. | ||
RUN pip install . | ||
|
||
# health check and entrypoint | ||
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \ | ||
CMD curl -f http://localhost:${PORT}/health || exit 1 | ||
ENTRYPOINT ["/venv/bin/india-api"] | ||
|
||
ENTRYPOINT ["india-api"] |
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
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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from . import dummydb | ||
from . import dummydb, indiadb | ||
|
||
__all__ = [ | ||
"indiadb", | ||
"dummydb", | ||
] |
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,5 @@ | ||
"""Defines sources of data for the API.""" | ||
|
||
from .client import Client | ||
|
||
__all__ = ["Client"] |
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,169 @@ | ||
"""India DB client that conforms to the DatabaseInterface.""" | ||
import datetime as dt | ||
import logging | ||
|
||
from pvsite_datamodel import DatabaseConnection | ||
from pvsite_datamodel.read import ( | ||
get_sites_by_country, | ||
get_latest_forecast_values_by_site, | ||
get_pv_generation_by_sites, | ||
) | ||
from pvsite_datamodel.sqlmodels import SiteAssetType, ForecastValueSQL | ||
from sqlalchemy.orm import Session | ||
|
||
from india_api import internal | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class Client(internal.DatabaseInterface): | ||
"""Defines India DB client that conforms to the DatabaseInterface.""" | ||
|
||
session: Session = None | ||
|
||
def __init__(self, database_url: str) -> None: | ||
"""Initialize the client with a SQLAlchemy database connection and session.""" | ||
|
||
self.connection = DatabaseConnection(url=database_url, echo=False) | ||
|
||
def _get_session(self): | ||
"""Allows for overriding the default session (useful for testing)""" | ||
if self.session is None: | ||
return self.connection.get_session() | ||
else: | ||
return self.session | ||
|
||
def get_predicted_yields_for_location( | ||
self, | ||
location: str, | ||
asset_type: SiteAssetType, | ||
) -> list[internal.PredictedPower]: | ||
"""Gets the predicted yields for a location.""" | ||
|
||
# Get the window | ||
start, end = _getWindow() | ||
|
||
# get site uuid | ||
with self._get_session() as session: | ||
sites = get_sites_by_country(session, country="india") | ||
|
||
# just select wind site | ||
sites = [s for s in sites if s.asset_type == asset_type] | ||
site = sites[0] | ||
|
||
# read actual generations | ||
values = get_latest_forecast_values_by_site( | ||
session, site_uuids=[site.site_uuid], start_utc=start | ||
) | ||
forecast_values: [ForecastValueSQL] = values[site.site_uuid] | ||
|
||
# convert ForecastValueSQL to PredictedPower | ||
values = [ | ||
internal.PredictedPower( | ||
PowerKW=value.forecast_power_kw, Time=value.start_utc.astimezone(dt.UTC) | ||
) | ||
for value in forecast_values | ||
] | ||
|
||
return values | ||
|
||
def get_generation_for_location( | ||
self, | ||
location: str, | ||
asset_type: SiteAssetType, | ||
) -> [internal.PredictedPower]: | ||
"""Gets the predicted yields for a location.""" | ||
|
||
# Get the window | ||
start, end = _getWindow() | ||
|
||
# get site uuid | ||
with self._get_session() as session: | ||
sites = get_sites_by_country(session, country="india") | ||
|
||
# just select wind site | ||
sites = [site for site in sites if site.asset_type == asset_type] | ||
site = sites[0] | ||
|
||
# read actual generations | ||
values = get_pv_generation_by_sites( | ||
session=session, site_uuids=[site.site_uuid], start_utc=start, end_utc=end | ||
) | ||
|
||
# convert from GenerationSQL to PredictedPower | ||
values = [ | ||
internal.ActualPower( | ||
PowerKW=value.generation_power_kw, Time=value.start_utc.astimezone(dt.UTC) | ||
) | ||
for value in values | ||
] | ||
|
||
return values | ||
|
||
def get_predicted_solar_yields_for_location( | ||
self, | ||
location: str, | ||
) -> [internal.PredictedPower]: | ||
""" | ||
Gets the predicted solar yields for a location. | ||
|
||
Args: | ||
location: The location to get the predicted solar yields for. | ||
""" | ||
|
||
return self.get_predicted_yields_for_location( | ||
location=location, asset_type=SiteAssetType.pv | ||
) | ||
|
||
def get_predicted_wind_yields_for_location( | ||
self, | ||
location: str, | ||
) -> list[internal.PredictedPower]: | ||
""" | ||
Gets the predicted wind yields for a location. | ||
|
||
Args: | ||
location: The location to get the predicted wind yields for. | ||
""" | ||
|
||
return self.get_predicted_yields_for_location( | ||
location=location, asset_type=SiteAssetType.wind | ||
) | ||
|
||
def get_actual_solar_yields_for_location(self, location: str) -> list[internal.PredictedPower]: | ||
"""Gets the actual solar yields for a location.""" | ||
|
||
return self.get_generation_for_location(location=location, asset_type=SiteAssetType.pv) | ||
|
||
def get_actual_wind_yields_for_location(self, location: str) -> list[internal.PredictedPower]: | ||
"""Gets the actual wind yields for a location.""" | ||
|
||
return self.get_generation_for_location(location=location, asset_type=SiteAssetType.wind) | ||
|
||
def get_wind_regions(self) -> list[str]: | ||
"""Gets the valid wind regions.""" | ||
return ["ruvnl"] | ||
|
||
def get_solar_regions(self) -> list[str]: | ||
"""Gets the valid solar regions.""" | ||
return ["ruvnl"] | ||
|
||
|
||
def _getWindow() -> tuple[dt.datetime, dt.datetime]: | ||
"""Returns the start and end of the window for timeseries data.""" | ||
|
||
# Window start is the beginning of the day two days ago | ||
start = (dt.datetime.now(tz=dt.UTC) - dt.timedelta(days=2)).replace( | ||
hour=0, | ||
minute=0, | ||
second=0, | ||
microsecond=0, | ||
) | ||
# Window end is the beginning of the day two days ahead | ||
end = (dt.datetime.now(tz=dt.UTC) + dt.timedelta(days=2)).replace( | ||
hour=0, | ||
minute=0, | ||
second=0, | ||
microsecond=0, | ||
) | ||
return start, end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is duplicated from a different model, should we refactor tor remove duplciate code. Happy if its put as an issue and dealt with later