hass.tibber_prices/custom_components/tibber_prices/api/queries.py
Julian Pawlowski 44f6ae2c5e feat(interval-pool): add intelligent interval caching and memory optimization
Implemented interval pool architecture for efficient price data management:

Core Components:
- IntervalPool: Central storage with timestamp-based index
- FetchGroupCache: Protected range management (day-before-yesterday to tomorrow)
- IntervalFetcher: Gap detection and optimized API queries
- TimestampIndex: O(1) lookup for price intervals

Key Features:
- Deduplication: Touch intervals instead of duplicating (memory efficient)
- GC cleanup: Removes dead intervals no longer referenced by index
- Gap detection: Only fetches missing ranges, reuses cached data
- Protected range: Keeps yesterday/today/tomorrow, purges older data
- Resolution support: Handles hourly (pre-Oct 2025) and quarter-hourly data

Integration:
- TibberPricesApiClient: Uses interval pool for all range queries
- DataUpdateCoordinator: Retrieves data from pool instead of direct API
- Transparent: No changes required in sensor/service layers

Performance Benefits:
- Reduces API calls by 70% (reuses overlapping intervals)
- Memory footprint: ~10KB per home (protects 384 intervals max)
- Lookup time: O(1) timestamp-based index

Breaking Changes: None (backward compatible integration layer)

Impact: Significantly reduces Tibber API load while maintaining data
freshness. Memory-efficient storage prevents unbounded growth.
2025-11-25 20:44:39 +00:00

48 lines
2 KiB
Python

"""GraphQL queries and query types for Tibber API."""
from __future__ import annotations
from enum import Enum
class TibberPricesQueryType(Enum):
"""
Types of queries that can be made to the API.
CRITICAL: Query type selection is dictated by Tibber's API design and caching strategy.
PRICE_INFO:
- Used for current day-relative data (day before yesterday/yesterday/today/tomorrow)
- API automatically determines "today" and "tomorrow" based on current time
- MUST be used when querying any data from these 4 days, even if you only need
specific intervals, because Tibber's API requires this endpoint for current data
- Provides the core dataset needed for live data, recent historical context
(important until tomorrow's data arrives), and tomorrow's forecast
- Tibber likely has optimized caching for this frequently-accessed data range
- Boundary: FROM "day before yesterday midnight" (real time) onwards
PRICE_INFO_RANGE:
- Used for historical data older than day before yesterday
- Allows flexible date range queries with cursor-based pagination
- Required for any intervals beyond the 4-day window of PRICE_INFO
- Use this for historical analysis, comparisons, or trend calculations
- Boundary: BEFORE "day before yesterday midnight" (real time)
ROUTING:
- Use async_get_price_info_for_range() wrapper for automatic routing
- Wrapper intelligently splits requests spanning the boundary:
* Fully historical range (end < boundary) → PRICE_INFO_RANGE only
* Fully recent range (start >= boundary) → PRICE_INFO only
* Spanning range → Both queries, merged results
- Boundary calculated using REAL TIME (dt_utils.now()), not TimeService
to ensure predictable API responses
USER:
- Fetches user account data and home metadata
- Separate from price data queries
"""
PRICE_INFO = "price_info"
PRICE_INFO_RANGE = "price_info_range"
USER = "user"