From 8ce3f8f0bce0116c5b5875f9d6026cbfff5b31f0 Mon Sep 17 00:00:00 2001 From: peterdudfield Date: Mon, 9 Dec 2024 20:26:51 +0000 Subject: [PATCH 1/5] log usage of package --- README.md | 8 ++++ pyproject.toml | 1 + quartz_solar_forecast/forecast.py | 6 +++ quartz_solar_forecast/utils/sentry_logging.py | 42 +++++++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 quartz_solar_forecast/utils/sentry_logging.py diff --git a/README.md b/README.md index 5de89ed3..8a44dde5 100644 --- a/README.md +++ b/README.md @@ -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 latitude and longitude is rounded to 2 decimals places for 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. diff --git a/pyproject.toml b/pyproject.toml index c6a7a4f0..54f10d5c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,6 +26,7 @@ dependencies = [ "uvicorn", "pydantic_settings", "httpx", + "sentry_sdk" ] [project.urls] diff --git a/quartz_solar_forecast/forecast.py b/quartz_solar_forecast/forecast.py index a75647b0..a5d8fe83 100644 --- a/quartz_solar_forecast/forecast.py +++ b/quartz_solar_forecast/forecast.py @@ -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__) @@ -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, "model": model, "ts": ts, "nwp_source": nwp_source}) + if model == "gb": return predict_ocf(site, None, ts, nwp_source) diff --git a/quartz_solar_forecast/utils/sentry_logging.py b/quartz_solar_forecast/utils/sentry_logging.py new file mode 100644 index 00000000..0e569171 --- /dev/null +++ b/quartz_solar_forecast/utils/sentry_logging.py @@ -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 = bool(os.getenv("QUARTZ_SOLAR_FORECAST_LOGGING", True)) + +SENTRY_DSN = 'https://b2b6f3c97299f81464bc16ad0d516d0b@o400768.ingest.us.sentry.io/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 From ed676f451cc77c96f5e2849befb2c44a2afd5a33 Mon Sep 17 00:00:00 2001 From: peterdudfield Date: Mon, 9 Dec 2024 20:47:53 +0000 Subject: [PATCH 2/5] add "round_latitude_and_longitude" method --- quartz_solar_forecast/forecast.py | 2 +- quartz_solar_forecast/pydantic_models.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/quartz_solar_forecast/forecast.py b/quartz_solar_forecast/forecast.py index a5d8fe83..ba5eab50 100644 --- a/quartz_solar_forecast/forecast.py +++ b/quartz_solar_forecast/forecast.py @@ -128,7 +128,7 @@ def run_forecast( # 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, "model": model, "ts": ts, "nwp_source": nwp_source}) + write_sentry({"site": site.copy(), "model": model, "ts": ts, "nwp_source": nwp_source}) if model == "gb": return predict_ocf(site, None, ts, nwp_source) diff --git a/quartz_solar_forecast/pydantic_models.py b/quartz_solar_forecast/pydantic_models.py index 7dd2b307..9dee52b3 100644 --- a/quartz_solar_forecast/pydantic_models.py +++ b/quartz_solar_forecast/pydantic_models.py @@ -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()) From 21ec9a4bc666701dccfca2cf1c39b4463faea00e Mon Sep 17 00:00:00 2001 From: peterdudfield Date: Mon, 9 Dec 2024 21:12:07 +0000 Subject: [PATCH 3/5] typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8a44dde5..efdffbc6 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ This can solve the [bug: \_\_\_kmpc_for_static_fini](https://github.com/openclim 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 latitude and longitude is rounded to 2 decimals places for in order to anonymize the data. +Note that any latitude and longitude is 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`. From c1b1e8d0d73e74d88705f49b38c40fcd969b4fe2 Mon Sep 17 00:00:00 2001 From: peterdudfield Date: Mon, 9 Dec 2024 21:13:03 +0000 Subject: [PATCH 4/5] text --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index efdffbc6..0df42ead 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ This can solve the [bug: \_\_\_kmpc_for_static_fini](https://github.com/openclim 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 latitude and longitude is rounded to 2 decimals places in order to anonymize the data. +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`. From beafdc77445bf17b2576c7601bcdad637481663c Mon Sep 17 00:00:00 2001 From: peterdudfield Date: Tue, 10 Dec 2024 08:26:28 +0000 Subject: [PATCH 5/5] PR comment --- quartz_solar_forecast/utils/sentry_logging.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quartz_solar_forecast/utils/sentry_logging.py b/quartz_solar_forecast/utils/sentry_logging.py index 0e569171..c34bd268 100644 --- a/quartz_solar_forecast/utils/sentry_logging.py +++ b/quartz_solar_forecast/utils/sentry_logging.py @@ -5,7 +5,7 @@ import os -quartz_solar_forecast_logging = bool(os.getenv("QUARTZ_SOLAR_FORECAST_LOGGING", True)) +quartz_solar_forecast_logging = os.getenv("QUARTZ_SOLAR_FORECAST_LOGGING", "True").lower() != "false" SENTRY_DSN = 'https://b2b6f3c97299f81464bc16ad0d516d0b@o400768.ingest.us.sentry.io/4508439933157376' sentry_sdk.init(