mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 06:17:07 +00:00
Optimize logbook SQL query (#12608)
This commit is contained in:
parent
6aa8916654
commit
9ca67c36cd
@ -47,6 +47,11 @@ CONFIG_SCHEMA = vol.Schema({
|
|||||||
}),
|
}),
|
||||||
}, extra=vol.ALLOW_EXTRA)
|
}, extra=vol.ALLOW_EXTRA)
|
||||||
|
|
||||||
|
ALL_EVENT_TYPES = [
|
||||||
|
EVENT_STATE_CHANGED, EVENT_LOGBOOK_ENTRY,
|
||||||
|
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP
|
||||||
|
]
|
||||||
|
|
||||||
GROUP_BY_MINUTES = 15
|
GROUP_BY_MINUTES = 15
|
||||||
|
|
||||||
CONTINUOUS_DOMAINS = ['proximity', 'sensor']
|
CONTINUOUS_DOMAINS = ['proximity', 'sensor']
|
||||||
@ -266,15 +271,18 @@ def humanify(events):
|
|||||||
|
|
||||||
def _get_events(hass, config, start_day, end_day):
|
def _get_events(hass, config, start_day, end_day):
|
||||||
"""Get events for a period of time."""
|
"""Get events for a period of time."""
|
||||||
from homeassistant.components.recorder.models import Events
|
from homeassistant.components.recorder.models import Events, States
|
||||||
from homeassistant.components.recorder.util import (
|
from homeassistant.components.recorder.util import (
|
||||||
execute, session_scope)
|
execute, session_scope)
|
||||||
|
|
||||||
with session_scope(hass=hass) as session:
|
with session_scope(hass=hass) as session:
|
||||||
query = session.query(Events).order_by(
|
query = session.query(Events).order_by(Events.time_fired) \
|
||||||
Events.time_fired).filter(
|
.outerjoin(States, (Events.event_id == States.event_id)) \
|
||||||
(Events.time_fired > start_day) &
|
.filter(Events.event_type.in_(ALL_EVENT_TYPES)) \
|
||||||
(Events.time_fired < end_day))
|
.filter((Events.time_fired > start_day)
|
||||||
|
& (Events.time_fired < end_day)) \
|
||||||
|
.filter((States.last_updated == States.last_changed)
|
||||||
|
| (States.last_updated.is_(None)))
|
||||||
events = execute(query)
|
events = execute(query)
|
||||||
return humanify(_exclude_events(events, config))
|
return humanify(_exclude_events(events, config))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user