Skip to content

Commit

Permalink
Speed up logbook timestamp processing (home-assistant#123126)
Browse files Browse the repository at this point in the history
bdraco authored Aug 4, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent b09dd95 commit 6b7307d
Showing 2 changed files with 16 additions and 22 deletions.
36 changes: 15 additions & 21 deletions homeassistant/components/logbook/processor.py
Original file line number Diff line number Diff line change
@@ -97,7 +97,7 @@ class LogbookRun:
event_cache: EventCache
entity_name_cache: EntityNameCache
include_entity_name: bool
format_time: Callable[[Row | EventAsRow], Any]
timestamp: bool
memoize_new_contexts: bool = True


@@ -126,16 +126,13 @@ def __init__(
self.context_id = context_id
logbook_config: LogbookConfig = hass.data[DOMAIN]
self.filters: Filters | None = logbook_config.sqlalchemy_filter
format_time = (
_row_time_fired_timestamp if timestamp else _row_time_fired_isoformat
)
self.logbook_run = LogbookRun(
context_lookup={None: None},
external_events=logbook_config.external_events,
event_cache=EventCache({}),
entity_name_cache=EntityNameCache(self.hass),
include_entity_name=include_entity_name,
format_time=format_time,
timestamp=timestamp,
)
self.context_augmenter = ContextAugmenter(self.logbook_run)

@@ -217,7 +214,7 @@ def _humanify(
event_cache = logbook_run.event_cache
entity_name_cache = logbook_run.entity_name_cache
include_entity_name = logbook_run.include_entity_name
format_time = logbook_run.format_time
timestamp = logbook_run.timestamp
memoize_new_contexts = logbook_run.memoize_new_contexts

# Process rows
@@ -231,6 +228,15 @@ def _humanify(

if event_type == EVENT_CALL_SERVICE:
continue

time_fired_ts = row[TIME_FIRED_TS_POS]
if timestamp:
when = time_fired_ts or time.time()
else:
when = process_timestamp_to_utc_isoformat(
dt_util.utc_from_timestamp(time_fired_ts) or dt_util.utcnow()
)

if event_type is PSEUDO_EVENT_STATE_CHANGED:
entity_id = row[ENTITY_ID_POS]
assert entity_id is not None
@@ -244,7 +250,7 @@ def _humanify(
continue

data = {
LOGBOOK_ENTRY_WHEN: format_time(row),
LOGBOOK_ENTRY_WHEN: when,
LOGBOOK_ENTRY_STATE: row[STATE_POS],
LOGBOOK_ENTRY_ENTITY_ID: entity_id,
}
@@ -265,7 +271,7 @@ def _humanify(
"Error with %s describe event for %s", domain, event_type
)
continue
data[LOGBOOK_ENTRY_WHEN] = format_time(row)
data[LOGBOOK_ENTRY_WHEN] = when
data[LOGBOOK_ENTRY_DOMAIN] = domain
context_augmenter.augment(data, row, context_id_bin)
yield data
@@ -279,7 +285,7 @@ def _humanify(
if entry_domain is None and entry_entity_id is not None:
entry_domain = split_entity_id(str(entry_entity_id))[0]
data = {
LOGBOOK_ENTRY_WHEN: format_time(row),
LOGBOOK_ENTRY_WHEN: when,
LOGBOOK_ENTRY_NAME: event_data.get(ATTR_NAME),
LOGBOOK_ENTRY_MESSAGE: event_data.get(ATTR_MESSAGE),
LOGBOOK_ENTRY_DOMAIN: entry_domain,
@@ -399,18 +405,6 @@ def _rows_match(row: Row | EventAsRow, other_row: Row | EventAsRow) -> bool:
)


def _row_time_fired_isoformat(row: Row | EventAsRow) -> str:
"""Convert the row timed_fired to isoformat."""
return process_timestamp_to_utc_isoformat(
dt_util.utc_from_timestamp(row[TIME_FIRED_TS_POS]) or dt_util.utcnow()
)


def _row_time_fired_timestamp(row: Row | EventAsRow) -> float:
"""Convert the row timed_fired to timestamp."""
return row[TIME_FIRED_TS_POS] or time.time()


class EntityNameCache:
"""A cache to lookup the name for an entity.
2 changes: 1 addition & 1 deletion tests/components/logbook/common.py
Original file line number Diff line number Diff line change
@@ -79,7 +79,7 @@ def mock_humanify(hass_, rows):
event_cache,
entity_name_cache,
include_entity_name=True,
format_time=processor._row_time_fired_isoformat,
timestamp=False,
)
context_augmenter = processor.ContextAugmenter(logbook_run)
return list(

0 comments on commit 6b7307d

Please sign in to comment.