feat(schedule): update view

This commit is contained in:
2026-04-16 23:05:41 +03:00
parent 29fa6435ce
commit a0e6f30e3b
11 changed files with 84 additions and 39 deletions

View File

@@ -1,13 +1,19 @@
from typing import Generic
from typing import Generic, NamedTuple
from gallery.util import TimeUnit
from .api import API, Api
class CachePreset(NamedTuple):
ttl: int = TimeUnit.HOUR
alias: str = "redis"
DEFAULT_CACHE_PRESET = CachePreset()
class CachedApi(Api, Generic[API]):
CACHE_TTL: int = TimeUnit.HOUR
CACHE_ALIAS: str = "redis"
CACHE_KEY: str
def __init__(self, api: API):

View File

@@ -1,3 +1,4 @@
import asyncio
import datetime
from ..api import Api
@@ -5,6 +6,8 @@ from .model import ChannelId, Schedule
class ScheduleApi(Api):
INTERVAL: float = 0.5
async def get_channels(self) -> list[ChannelId]:
raise NotImplementedError
@@ -12,3 +15,14 @@ class ScheduleApi(Api):
self, channel_id: ChannelId, date: datetime.date
) -> Schedule:
raise NotImplementedError
async def get_all_schedules(self, date: datetime.date) -> list[Schedule]:
channels = await self.get_channels()
results = []
for channel in channels:
results.append(
await self.get_channel_schedule(channel_id=channel, date=date)
)
if self.INTERVAL > 0:
await asyncio.sleep(self.INTERVAL)
return results

View File

@@ -2,21 +2,21 @@ import datetime
from aiocache import cached
from gallery.sketch.cached import CachedApi
from gallery.sketch.cached import CachedApi, CachePreset
from gallery.util import TimeUnit
from .api import ScheduleApi
from .model import ChannelId, Schedule
CACHE_PRESET = CachePreset(ttl=TimeUnit.HOUR * 6)
class CachedScheduleApi(ScheduleApi, CachedApi[ScheduleApi]):
CACHE_KEY = "schedule"
CACHE_TTL = TimeUnit.HOUR * 6
@cached(
key_builder=lambda fun, self: f"api.{self.CACHE_KEY}.{self.provider}.channels",
alias=CachedApi.CACHE_ALIAS,
ttl=CachedApi.CACHE_TTL,
**CACHE_PRESET._asdict(),
)
async def get_channels(self) -> list[ChannelId]:
return await self._api.get_channels()
@@ -25,10 +25,18 @@ class CachedScheduleApi(ScheduleApi, CachedApi[ScheduleApi]):
key_builder=lambda fun, self, channel_id, date: (
f"api.{self.CACHE_KEY}.{self.provider}.channel.{channel_id}.{date}"
),
alias=CachedApi.CACHE_ALIAS,
ttl=CachedApi.CACHE_TTL,
**CACHE_PRESET._asdict(),
)
async def get_channel_schedule(
self, channel_id: ChannelId, date: datetime.date
) -> Schedule:
return await self._api.get_channel_schedule(channel_id, date)
@cached(
key_builder=lambda fun, self, date: (
f"api.{self.CACHE_KEY}.{self.provider}.all.{date}"
),
**CACHE_PRESET._asdict(),
)
async def get_all_schedules(self, date: datetime.date) -> list[Schedule]:
return await self._api.get_all_schedules(date)

View File

@@ -2,19 +2,20 @@ import datetime
from aiocache import cached
from gallery.sketch.cached import CachedApi
from gallery.sketch.cached import DEFAULT_CACHE_PRESET, CachedApi
from .api import WeatherApi
from .model import WeatherResponse
CACHE_PRESET = DEFAULT_CACHE_PRESET
class CachedWeatherApi(WeatherApi, CachedApi[WeatherApi]):
CACHE_KEY = "weather"
@cached(
key_builder=lambda fun, self: f"api.{self.CACHE_KEY}.{self.provider}.locations",
alias=CachedApi.CACHE_ALIAS,
ttl=CachedApi.CACHE_TTL,
**CACHE_PRESET._asdict(),
)
async def get_locations(self) -> list[str]:
return await self._api.get_locations()
@@ -23,8 +24,7 @@ class CachedWeatherApi(WeatherApi, CachedApi[WeatherApi]):
key_builder=lambda fun, self, location_id, date: (
f"api.{self.CACHE_KEY}.{self.provider}.day.{location_id}.{date}"
),
alias=CachedApi.CACHE_ALIAS,
ttl=CachedApi.CACHE_TTL,
**CACHE_PRESET._asdict(),
)
async def get_day(self, location_id: str, date: datetime.date) -> WeatherResponse:
return await self._api.get_day(location_id, date)
@@ -33,8 +33,7 @@ class CachedWeatherApi(WeatherApi, CachedApi[WeatherApi]):
key_builder=lambda fun, self, location_id, date: (
f"api.{self.CACHE_KEY}.{self.provider}.day.{location_id}.{date}"
),
alias=CachedApi.CACHE_ALIAS,
ttl=CachedApi.CACHE_TTL,
**CACHE_PRESET._asdict(),
)
async def get_days(self, location_id: str, days: int) -> WeatherResponse:
return await self._api.get_days(location_id, days)