mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Speed up logbook timestamp processing (#123126)
This commit is contained in:
parent
b09dd95dbd
commit
6b7307df81
@ -97,7 +97,7 @@ class LogbookRun:
|
|||||||
event_cache: EventCache
|
event_cache: EventCache
|
||||||
entity_name_cache: EntityNameCache
|
entity_name_cache: EntityNameCache
|
||||||
include_entity_name: bool
|
include_entity_name: bool
|
||||||
format_time: Callable[[Row | EventAsRow], Any]
|
timestamp: bool
|
||||||
memoize_new_contexts: bool = True
|
memoize_new_contexts: bool = True
|
||||||
|
|
||||||
|
|
||||||
@ -126,16 +126,13 @@ class EventProcessor:
|
|||||||
self.context_id = context_id
|
self.context_id = context_id
|
||||||
logbook_config: LogbookConfig = hass.data[DOMAIN]
|
logbook_config: LogbookConfig = hass.data[DOMAIN]
|
||||||
self.filters: Filters | None = logbook_config.sqlalchemy_filter
|
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(
|
self.logbook_run = LogbookRun(
|
||||||
context_lookup={None: None},
|
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),
|
||||||
include_entity_name=include_entity_name,
|
include_entity_name=include_entity_name,
|
||||||
format_time=format_time,
|
timestamp=timestamp,
|
||||||
)
|
)
|
||||||
self.context_augmenter = ContextAugmenter(self.logbook_run)
|
self.context_augmenter = ContextAugmenter(self.logbook_run)
|
||||||
|
|
||||||
@ -217,7 +214,7 @@ def _humanify(
|
|||||||
event_cache = logbook_run.event_cache
|
event_cache = logbook_run.event_cache
|
||||||
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
|
timestamp = logbook_run.timestamp
|
||||||
memoize_new_contexts = logbook_run.memoize_new_contexts
|
memoize_new_contexts = logbook_run.memoize_new_contexts
|
||||||
|
|
||||||
# Process rows
|
# Process rows
|
||||||
@ -231,6 +228,15 @@ def _humanify(
|
|||||||
|
|
||||||
if event_type == EVENT_CALL_SERVICE:
|
if event_type == EVENT_CALL_SERVICE:
|
||||||
continue
|
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:
|
if event_type is PSEUDO_EVENT_STATE_CHANGED:
|
||||||
entity_id = row[ENTITY_ID_POS]
|
entity_id = row[ENTITY_ID_POS]
|
||||||
assert entity_id is not None
|
assert entity_id is not None
|
||||||
@ -244,7 +250,7 @@ def _humanify(
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
LOGBOOK_ENTRY_WHEN: format_time(row),
|
LOGBOOK_ENTRY_WHEN: when,
|
||||||
LOGBOOK_ENTRY_STATE: row[STATE_POS],
|
LOGBOOK_ENTRY_STATE: row[STATE_POS],
|
||||||
LOGBOOK_ENTRY_ENTITY_ID: entity_id,
|
LOGBOOK_ENTRY_ENTITY_ID: entity_id,
|
||||||
}
|
}
|
||||||
@ -265,7 +271,7 @@ def _humanify(
|
|||||||
"Error with %s describe event for %s", domain, event_type
|
"Error with %s describe event for %s", domain, event_type
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
data[LOGBOOK_ENTRY_WHEN] = format_time(row)
|
data[LOGBOOK_ENTRY_WHEN] = when
|
||||||
data[LOGBOOK_ENTRY_DOMAIN] = domain
|
data[LOGBOOK_ENTRY_DOMAIN] = domain
|
||||||
context_augmenter.augment(data, row, context_id_bin)
|
context_augmenter.augment(data, row, context_id_bin)
|
||||||
yield data
|
yield data
|
||||||
@ -279,7 +285,7 @@ def _humanify(
|
|||||||
if entry_domain is None and entry_entity_id is not None:
|
if entry_domain is None and entry_entity_id is not None:
|
||||||
entry_domain = split_entity_id(str(entry_entity_id))[0]
|
entry_domain = split_entity_id(str(entry_entity_id))[0]
|
||||||
data = {
|
data = {
|
||||||
LOGBOOK_ENTRY_WHEN: format_time(row),
|
LOGBOOK_ENTRY_WHEN: when,
|
||||||
LOGBOOK_ENTRY_NAME: event_data.get(ATTR_NAME),
|
LOGBOOK_ENTRY_NAME: event_data.get(ATTR_NAME),
|
||||||
LOGBOOK_ENTRY_MESSAGE: event_data.get(ATTR_MESSAGE),
|
LOGBOOK_ENTRY_MESSAGE: event_data.get(ATTR_MESSAGE),
|
||||||
LOGBOOK_ENTRY_DOMAIN: entry_domain,
|
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:
|
class EntityNameCache:
|
||||||
"""A cache to lookup the name for an entity.
|
"""A cache to lookup the name for an entity.
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ def mock_humanify(hass_, rows):
|
|||||||
event_cache,
|
event_cache,
|
||||||
entity_name_cache,
|
entity_name_cache,
|
||||||
include_entity_name=True,
|
include_entity_name=True,
|
||||||
format_time=processor._row_time_fired_isoformat,
|
timestamp=False,
|
||||||
)
|
)
|
||||||
context_augmenter = processor.ContextAugmenter(logbook_run)
|
context_augmenter = processor.ContextAugmenter(logbook_run)
|
||||||
return list(
|
return list(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user