This commit is contained in:
J. Nick Koston
2023-04-10 13:52:33 -10:00
parent 8b5c650ce3
commit a39865ba1a

View File

@@ -67,8 +67,8 @@ def _stmt_and_join_attributes(
return _select return _select
def _stmt_and_join_attributes_for_epoch_time( def _stmt_and_join_attributes_for_start_state(
no_attributes: bool, include_last_changed: bool, epoch_time: float no_attributes: bool, include_last_changed: bool
) -> Select: ) -> Select:
"""Return the statement and if StateAttributes should be joined.""" """Return the statement and if StateAttributes should be joined."""
_select = select(States.metadata_id, States.state) _select = select(States.metadata_id, States.state)
@@ -160,14 +160,12 @@ def _significant_states_stmt(
stmt = stmt.order_by(States.metadata_id, States.last_updated_ts) stmt = stmt.order_by(States.metadata_id, States.last_updated_ts)
if not include_start_time_state or not run_start_ts: if not include_start_time_state or not run_start_ts:
return stmt return stmt
start_time_ts_str = str(start_time_ts)
return _select_from_subquery( return _select_from_subquery(
union_all( union_all(
_select_from_subquery( _select_from_subquery(
_get_start_time_state_stmt( _get_start_time_state_stmt(
run_start_ts, run_start_ts,
start_time_ts, start_time_ts,
start_time_ts_str,
single_metadata_id, single_metadata_id,
metadata_ids, metadata_ids,
no_attributes, no_attributes,
@@ -301,7 +299,6 @@ def get_full_significant_states_with_session(
def _state_changed_during_period_stmt( def _state_changed_during_period_stmt(
start_time_ts: float, start_time_ts: float,
start_time_ts_str: str,
end_time_ts: float | None, end_time_ts: float | None,
single_metadata_id: int, single_metadata_id: int,
no_attributes: bool, no_attributes: bool,
@@ -340,7 +337,6 @@ def _state_changed_during_period_stmt(
_select_from_subquery( _select_from_subquery(
_get_single_entity_start_time_stmt( _get_single_entity_start_time_stmt(
start_time_ts, start_time_ts,
start_time_ts_str,
single_metadata_id, single_metadata_id,
no_attributes, no_attributes,
False, False,
@@ -388,12 +384,10 @@ def state_changes_during_period(
): ):
include_start_time_state = False include_start_time_state = False
start_time_ts = dt_util.utc_to_timestamp(start_time) start_time_ts = dt_util.utc_to_timestamp(start_time)
start_time_ts_str = str(start_time_ts)
end_time_ts = datetime_to_timestamp_or_none(end_time) end_time_ts = datetime_to_timestamp_or_none(end_time)
stmt = lambda_stmt( stmt = lambda_stmt(
lambda: _state_changed_during_period_stmt( lambda: _state_changed_during_period_stmt(
start_time_ts, start_time_ts,
start_time_ts_str,
end_time_ts, end_time_ts,
single_metadata_id, single_metadata_id,
no_attributes, no_attributes,
@@ -501,7 +495,6 @@ def get_last_state_changes(
def _get_start_time_state_for_entities_stmt( def _get_start_time_state_for_entities_stmt(
run_start_ts: float, run_start_ts: float,
epoch_time: float, epoch_time: float,
epoch_time_string: str,
metadata_ids: list[int], metadata_ids: list[int],
no_attributes: bool, no_attributes: bool,
include_last_changed: bool, include_last_changed: bool,
@@ -509,8 +502,8 @@ def _get_start_time_state_for_entities_stmt(
"""Baked query to get states for specific entities.""" """Baked query to get states for specific entities."""
# We got an include-list of entities, accelerate the query by filtering already # We got an include-list of entities, accelerate the query by filtering already
# in the inner query. # in the inner query.
stmt = _stmt_and_join_attributes_for_epoch_time( stmt = _stmt_and_join_attributes_for_start_state(
no_attributes, include_last_changed, epoch_time_string no_attributes, include_last_changed
).join( ).join(
( (
most_recent_states_for_entities_by_date := ( most_recent_states_for_entities_by_date := (
@@ -560,7 +553,6 @@ def _get_run_start_ts_for_utc_point_in_time(
def _get_start_time_state_stmt( def _get_start_time_state_stmt(
run_start_ts: float, run_start_ts: float,
epoch_time: float, epoch_time: float,
epoch_time_str: str,
single_metadata_id: int | None, single_metadata_id: int | None,
metadata_ids: list[int], metadata_ids: list[int],
no_attributes: bool, no_attributes: bool,
@@ -572,7 +564,6 @@ def _get_start_time_state_stmt(
# have a single entity id # have a single entity id
return _get_single_entity_start_time_stmt( return _get_single_entity_start_time_stmt(
epoch_time, epoch_time,
epoch_time_str,
single_metadata_id, single_metadata_id,
no_attributes, no_attributes,
include_last_changed, include_last_changed,
@@ -582,7 +573,6 @@ def _get_start_time_state_stmt(
return _get_start_time_state_for_entities_stmt( return _get_start_time_state_for_entities_stmt(
run_start_ts, run_start_ts,
epoch_time, epoch_time,
epoch_time_str,
metadata_ids, metadata_ids,
no_attributes, no_attributes,
include_last_changed, include_last_changed,
@@ -591,7 +581,6 @@ def _get_start_time_state_stmt(
def _get_single_entity_start_time_stmt( def _get_single_entity_start_time_stmt(
epoch_time: float, epoch_time: float,
epoch_time_str: str,
metadata_id: int, metadata_id: int,
no_attributes: bool, no_attributes: bool,
include_last_changed: bool, include_last_changed: bool,
@@ -599,9 +588,7 @@ def _get_single_entity_start_time_stmt(
# Use an entirely different (and extremely fast) query if we only # Use an entirely different (and extremely fast) query if we only
# have a single entity id # have a single entity id
stmt = ( stmt = (
_stmt_and_join_attributes_for_epoch_time( _stmt_and_join_attributes_for_start_state(no_attributes, include_last_changed)
no_attributes, include_last_changed, epoch_time_str
)
.filter( .filter(
States.last_updated_ts < epoch_time, States.last_updated_ts < epoch_time,
States.metadata_id == metadata_id, States.metadata_id == metadata_id,
@@ -670,6 +657,9 @@ def _sorted_states_to_dict(
key_func = itemgetter(metadata_id_idx) key_func = itemgetter(metadata_id_idx)
states_iter = groupby(states, key_func) states_iter = groupby(states, key_func)
state_idx = field_map["state"]
last_updated_ts_idx = field_map["last_updated_ts"]
# Append all changes to it # Append all changes to it
for metadata_id, group in states_iter: for metadata_id, group in states_iter:
attr_cache: dict[str, dict[str, Any]] = {} attr_cache: dict[str, dict[str, Any]] = {}
@@ -694,14 +684,11 @@ def _sorted_states_to_dict(
if not ent_results: if not ent_results:
if (first_state := next(group, None)) is None: if (first_state := next(group, None)) is None:
continue continue
prev_state = first_state.state prev_state = first_state[state_idx]
ent_results.append( ent_results.append(
state_class(first_state, attr_cache, start_time_ts, entity_id=entity_id) # type: ignore[call-arg] state_class(first_state, attr_cache, start_time_ts, entity_id=entity_id) # type: ignore[call-arg]
) )
state_idx = field_map["state"]
last_updated_ts_idx = field_map["last_updated_ts"]
# #
# minimal_response only makes sense with last_updated == last_updated # minimal_response only makes sense with last_updated == last_updated
# #