Skip to content

Commit

Permalink
log usage of package (#230) #minor
Browse files Browse the repository at this point in the history
* log usage of package

* add "round_latitude_and_longitude" method

* typo

* text

* PR comment
  • Loading branch information
peterdudfield authored Dec 10, 2024
1 parent f39fef9 commit 778752e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ conda install -c conda-forge pyresample

This can solve the [bug: \_\_\_kmpc_for_static_fini](https://github.com/openclimatefix/Open-Source-Quartz-Solar-Forecast/issues/32).

### Logging

The package logs when `run_forecast` is used. This is useful for OCF to determine how the package is being used
and how we can make improvements in the future.
Note that any latitudes and longitudes are rounded to 2 decimals places in order to anonymize the data.
If you would like to disable this logging, you can do so by setting the environment variable `QUARTZ_SOLAR_FORECAST_LOGGING` to `False`.


## Model

Two models are currently available to make predictions.
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies = [
"uvicorn",
"pydantic_settings",
"httpx",
"sentry_sdk"
]

[project.urls]
Expand Down
6 changes: 6 additions & 0 deletions quartz_solar_forecast/forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from quartz_solar_forecast.data import get_nwp, make_pv_data
from quartz_solar_forecast.forecasts import forecast_v1_tilt_orientation, TryolabsSolarPowerPredictor
from quartz_solar_forecast.pydantic_models import PVSite
from quartz_solar_forecast.utils.sentry_logging import write_sentry

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -124,6 +125,11 @@ def run_forecast(
:return: The PV forecast of the site for time (ts) for 48 hours
"""

# log usage to sentry, if you dont want to log usage to sentry, you can
# 1. set environmental variable QUARTZ_SOLAR_FORECAST_LOGGING='false', or
# 2. comment out this line
write_sentry({"site": site.copy(), "model": model, "ts": ts, "nwp_source": nwp_source})

if model == "gb":
return predict_ocf(site, None, ts, nwp_source)

Expand Down
8 changes: 8 additions & 0 deletions quartz_solar_forecast/pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ class PVSite(BaseModel):
json_schema_extra=["enphase", "solis", "givenergy", "solarman", None],
)

def round_latitude_and_longitude(self):
""" Round the latitude and longitude to 2 decimal places
This is to ensure that the location of the site is not stored exactly.
"""
self.latitude = round(self.latitude, 2)
self.longitude = round(self.longitude, 2)

def get_inverter(self):
if self.inverter_type == 'enphase':
return EnphaseInverter(EnphaseSettings())
Expand Down
42 changes: 42 additions & 0 deletions quartz_solar_forecast/utils/sentry_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
""" Log usage of this package to Sentry """

import sentry_sdk
from quartz_solar_forecast.pydantic_models import PVSite

import os

quartz_solar_forecast_logging = os.getenv("QUARTZ_SOLAR_FORECAST_LOGGING", "True").lower() != "false"

SENTRY_DSN = 'https://[email protected]/4508439933157376'
sentry_sdk.init(
dsn=SENTRY_DSN,
)


def write_sentry(params):
"""
Log usage of this package to Sentry
"""

if not quartz_solar_forecast_logging:
return

try:
for key, value in params.items():

# we want to make sure we don't store the exact location of the site
if isinstance(value, PVSite):
value.round_latitude_and_longitude()

# set sentry tag
sentry_sdk.set_tag(key, value)

message = "quartz_solar_forecast is being used"

if os.getenv("PYTEST_CURRENT_TEST") is not None:
message += ": in CI tests"

sentry_sdk.capture_message(message)

except Exception as _: # noqa
pass

0 comments on commit 778752e

Please sign in to comment.