mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 06:37:52 +00:00
Remove legacy context lookup implementation from logbook (#91710)
* Remove legacy context lookup implemention from logbook This object can now be replaced with a simple dict * Remove legacy context lookup implemention from logbook This object can now be replaced with a simple dict * scope * fix order issue
This commit is contained in:
parent
24fe6dfc63
commit
56a6244d90
@ -66,7 +66,7 @@ from .queries.common import PSEUDO_EVENT_STATE_CHANGED
|
|||||||
class LogbookRun:
|
class LogbookRun:
|
||||||
"""A logbook run which may be a long running event stream or single request."""
|
"""A logbook run which may be a long running event stream or single request."""
|
||||||
|
|
||||||
context_lookup: ContextLookup
|
context_lookup: dict[bytes | None, Row | EventAsRow | None]
|
||||||
external_events: dict[
|
external_events: dict[
|
||||||
str, tuple[str, Callable[[LazyEventPartialState], dict[str, Any]]]
|
str, tuple[str, Callable[[LazyEventPartialState], dict[str, Any]]]
|
||||||
]
|
]
|
||||||
@ -74,6 +74,7 @@ class LogbookRun:
|
|||||||
entity_name_cache: EntityNameCache
|
entity_name_cache: EntityNameCache
|
||||||
include_entity_name: bool
|
include_entity_name: bool
|
||||||
format_time: Callable[[Row | EventAsRow], Any]
|
format_time: Callable[[Row | EventAsRow], Any]
|
||||||
|
memoize_new_contexts: bool = True
|
||||||
|
|
||||||
|
|
||||||
class EventProcessor:
|
class EventProcessor:
|
||||||
@ -105,7 +106,7 @@ class EventProcessor:
|
|||||||
_row_time_fired_timestamp if timestamp else _row_time_fired_isoformat
|
_row_time_fired_timestamp if timestamp else _row_time_fired_isoformat
|
||||||
)
|
)
|
||||||
self.logbook_run = LogbookRun(
|
self.logbook_run = LogbookRun(
|
||||||
context_lookup=ContextLookup(hass),
|
context_lookup={None: None},
|
||||||
external_events=logbook_config.external_events,
|
external_events=logbook_config.external_events,
|
||||||
event_cache=EventCache({}),
|
event_cache=EventCache({}),
|
||||||
entity_name_cache=EntityNameCache(self.hass),
|
entity_name_cache=EntityNameCache(self.hass),
|
||||||
@ -126,6 +127,7 @@ class EventProcessor:
|
|||||||
"""
|
"""
|
||||||
self.logbook_run.event_cache.clear()
|
self.logbook_run.event_cache.clear()
|
||||||
self.logbook_run.context_lookup.clear()
|
self.logbook_run.context_lookup.clear()
|
||||||
|
self.logbook_run.memoize_new_contexts = False
|
||||||
|
|
||||||
def get_events(
|
def get_events(
|
||||||
self,
|
self,
|
||||||
@ -207,10 +209,14 @@ def _humanify(
|
|||||||
entity_name_cache = logbook_run.entity_name_cache
|
entity_name_cache = logbook_run.entity_name_cache
|
||||||
include_entity_name = logbook_run.include_entity_name
|
include_entity_name = logbook_run.include_entity_name
|
||||||
format_time = logbook_run.format_time
|
format_time = logbook_run.format_time
|
||||||
|
memoize_new_contexts = logbook_run.memoize_new_contexts
|
||||||
|
memoize_context = context_lookup.setdefault
|
||||||
|
|
||||||
# Process rows
|
# Process rows
|
||||||
for row in rows:
|
for row in rows:
|
||||||
context_id_bin = context_lookup.memorize(row)
|
context_id_bin: bytes = row.context_id_bin
|
||||||
|
if memoize_new_contexts:
|
||||||
|
memoize_context(context_id_bin, row)
|
||||||
if row.context_only:
|
if row.context_only:
|
||||||
continue
|
continue
|
||||||
event_type = row.event_type
|
event_type = row.event_type
|
||||||
@ -269,33 +275,6 @@ def _humanify(
|
|||||||
yield data
|
yield data
|
||||||
|
|
||||||
|
|
||||||
class ContextLookup:
|
|
||||||
"""A lookup class for context origins."""
|
|
||||||
|
|
||||||
def __init__(self, hass: HomeAssistant) -> None:
|
|
||||||
"""Memorize context origin."""
|
|
||||||
self.hass = hass
|
|
||||||
self._memorize_new = True
|
|
||||||
self._lookup: dict[bytes | None, Row | EventAsRow | None] = {None: None}
|
|
||||||
|
|
||||||
def memorize(self, row: Row | EventAsRow) -> bytes | None:
|
|
||||||
"""Memorize a context from the database."""
|
|
||||||
if self._memorize_new:
|
|
||||||
context_id_bin: bytes = row.context_id_bin
|
|
||||||
self._lookup.setdefault(context_id_bin, row)
|
|
||||||
return context_id_bin
|
|
||||||
return None
|
|
||||||
|
|
||||||
def clear(self) -> None:
|
|
||||||
"""Clear the context origins and stop recording new ones."""
|
|
||||||
self._lookup.clear()
|
|
||||||
self._memorize_new = False
|
|
||||||
|
|
||||||
def get(self, context_id_bin: bytes) -> Row | EventAsRow | None:
|
|
||||||
"""Get the context origin."""
|
|
||||||
return self._lookup.get(context_id_bin)
|
|
||||||
|
|
||||||
|
|
||||||
class ContextAugmenter:
|
class ContextAugmenter:
|
||||||
"""Augment data with context trace."""
|
"""Augment data with context trace."""
|
||||||
|
|
||||||
@ -311,8 +290,10 @@ class ContextAugmenter:
|
|||||||
self, context_id_bin: bytes | None, row: Row | EventAsRow
|
self, context_id_bin: bytes | None, row: Row | EventAsRow
|
||||||
) -> Row | EventAsRow | None:
|
) -> Row | EventAsRow | None:
|
||||||
"""Get the context row from the id or row context."""
|
"""Get the context row from the id or row context."""
|
||||||
if context_id_bin:
|
if context_id_bin is not None and (
|
||||||
return self.context_lookup.get(context_id_bin)
|
context_row := self.context_lookup.get(context_id_bin)
|
||||||
|
):
|
||||||
|
return context_row
|
||||||
if (context := getattr(row, "context", None)) is not None and (
|
if (context := getattr(row, "context", None)) is not None and (
|
||||||
origin_event := context.origin_event
|
origin_event := context.origin_event
|
||||||
) is not None:
|
) is not None:
|
||||||
|
@ -221,8 +221,6 @@ async def _async_events_consumer(
|
|||||||
event_processor: EventProcessor,
|
event_processor: EventProcessor,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Stream events from the queue."""
|
"""Stream events from the queue."""
|
||||||
event_processor.switch_to_live()
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
events: list[Event] = [await stream_queue.get()]
|
events: list[Event] = [await stream_queue.get()]
|
||||||
# If the event is older than the last db
|
# If the event is older than the last db
|
||||||
@ -430,6 +428,7 @@ async def ws_event_stream(
|
|||||||
event_processor,
|
event_processor,
|
||||||
partial=False,
|
partial=False,
|
||||||
)
|
)
|
||||||
|
event_processor.switch_to_live()
|
||||||
|
|
||||||
|
|
||||||
def _ws_formatted_get_events(
|
def _ws_formatted_get_events(
|
||||||
|
@ -64,7 +64,7 @@ def mock_humanify(hass_, rows):
|
|||||||
entity_name_cache = processor.EntityNameCache(hass_)
|
entity_name_cache = processor.EntityNameCache(hass_)
|
||||||
ent_reg = er.async_get(hass_)
|
ent_reg = er.async_get(hass_)
|
||||||
event_cache = processor.EventCache({})
|
event_cache = processor.EventCache({})
|
||||||
context_lookup = processor.ContextLookup(hass_)
|
context_lookup = {}
|
||||||
logbook_config = hass_.data.get(logbook.DOMAIN, LogbookConfig({}, None, None))
|
logbook_config = hass_.data.get(logbook.DOMAIN, LogbookConfig({}, None, None))
|
||||||
external_events = logbook_config.external_events
|
external_events = logbook_config.external_events
|
||||||
logbook_run = processor.LogbookRun(
|
logbook_run = processor.LogbookRun(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user