Skip to content

Commit

Permalink
Merge pull request #91 from gwww/new-radar
Browse files Browse the repository at this point in the history
Improve radar CPU usage
  • Loading branch information
michaeldavie authored Jun 27, 2024
2 parents 417ea74 + 8e72451 commit 15782ea
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 208 deletions.
32 changes: 32 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.PHONY: install install-core clean isort lint test

clean:
find . -name '*.pyc' -exec rm -f {} +
find . -name '*.pyo' -exec rm -f {} +
find . -name '__pycache__' -exec rm -rf {} +
find . -name '.cache' -exec rm -rf {} +
find . -name '.pytest_cache' -exec rm -rf {} +
rm -rf build dist *.egg-info .venv

install:
ifndef VIRTUAL_ENV
$(error Create venv (python -m venv .venv) and activate virtual env first.)
else
pip install -e .
pip install pytest
pip install pylint
pip install ruff
endif

install-core:
pip install -e .

isort:
sh -c "isort --skip-glob=.tox ."

lint:
pylint --msg-template='{msg_id}({symbol}):{line:3d},{column}: {obj}: {msg}' elkm1_lib
ruff check

test:
pytest tests
11 changes: 5 additions & 6 deletions env_canada/ec_aqhi.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ def site_distance(site):


class ECAirQuality(object):

"""Get air quality data from Environment Canada."""

def __init__(self, **kwargs):
Expand Down Expand Up @@ -172,7 +171,7 @@ async def update(self):
# Fetch current measurement
aqhi_current = await self.get_aqhi_data(url=AQHI_OBSERVATION_URL)

if aqhi_current:
if aqhi_current is not None:
# Update region name
element = aqhi_current.find("region")
self.region_name = element.attrib[
Expand Down Expand Up @@ -202,7 +201,7 @@ async def update(self):
# Update AQHI forecasts
aqhi_forecast = await self.get_aqhi_data(url=AQHI_FORECAST_URL)

if aqhi_forecast:
if aqhi_forecast is not None:
# Update AQHI daily forecasts
for f in aqhi_forecast.findall("./forecastGroup/forecast"):
for p in f.findall("./period"):
Expand All @@ -214,6 +213,6 @@ async def update(self):

# Update AQHI hourly forecasts
for f in aqhi_forecast.findall("./hourlyForecastGroup/hourlyForecast"):
self.forecasts["hourly"][
timestamp_to_datetime(f.attrib["UTCTime"])
] = int(f.text or 0)
self.forecasts["hourly"][timestamp_to_datetime(f.attrib["UTCTime"])] = (
int(f.text or 0)
)
45 changes: 16 additions & 29 deletions env_canada/ec_cache.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,25 @@
from aiohttp import ClientSession
from datetime import datetime, timedelta
from datetime import datetime

from .constants import USER_AGENT

CACHE_EXPIRE_TIME = timedelta(minutes=200) # Time is tuned for 3h radar image

class Cache:
_cache = {}

class CacheClientSession(ClientSession):
"""Shim to cache ClientSession requests."""
@classmethod
def add(cls, cache_key, item, cache_time):
"""Add an entry to the cache."""

_cache = {}
cls._cache[cache_key] = (datetime.now() + cache_time, item)
return item # Returning item useful for chaining calls

def _flush_cache(self):
"""Flush expired cache entries."""
@classmethod
def get(cls, cache_key):
"""Get an entry from the cache."""

# Delete expired entries at start so we don't use expired entries
now = datetime.now()
expired = [key for key, value in self._cache.items() if value[0] < now]
expired = [key for key, value in cls._cache.items() if value[0] < now]
for key in expired:
del self._cache[key]

async def get(self, url, params, cache_time=CACHE_EXPIRE_TIME):
"""Thin wrapper around ClientSession.get to cache responses."""

self._flush_cache() # Flush at start so we don't use expired entries

cache_key = (url, tuple(sorted(params.items())))
result = self._cache.get(cache_key)
if not result:
result = (
datetime.now() + cache_time,
await super().get(
url=url, params=params, headers={"User-Agent": USER_AGENT}
),
)
self._cache[cache_key] = result
del cls._cache[key]

return result[1]
result = cls._cache.get(cache_key)
return result[1] if result else None
Loading

0 comments on commit 15782ea

Please sign in to comment.