Reduce overhead to store context ids in the database (#88942)

This commit is contained in:
J. Nick Koston 2023-03-08 14:51:45 -10:00 committed by GitHub
parent 386533a16f
commit 170a13302c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 676 additions and 160 deletions

View file

@ -323,9 +323,9 @@ def create_state_changed_event_from_old_new(
"event_data",
"time_fired",
"time_fired_ts",
"context_id",
"context_user_id",
"context_parent_id",
"context_id_bin",
"context_user_id_bin",
"context_parent_id_bin",
"state",
"entity_id",
"domain",
@ -349,12 +349,12 @@ def create_state_changed_event_from_old_new(
row.entity_id = entity_id
row.domain = entity_id and ha.split_entity_id(entity_id)[0]
row.context_only = False
row.context_id = None
row.context_id_bin = None
row.friendly_name = None
row.icon = None
row.old_format_icon = None
row.context_user_id = None
row.context_parent_id = None
row.context_user_id_bin = None
row.context_parent_id_bin = None
row.old_state_id = old_state and 1
row.state_id = new_state and 1
return LazyEventPartialState(row, {})
@ -966,7 +966,7 @@ async def test_logbook_entity_context_id(
await async_recorder_block_till_done(hass)
context = ha.Context(
id="ac5bd62de45711eaaeb351041eec8dd9",
id="01GTDGKBCH00GW0X476W5TVAAA",
user_id="b400facee45711eaa9308bfd3d19e474",
)
@ -1027,7 +1027,7 @@ async def test_logbook_entity_context_id(
# A service call
light_turn_off_service_context = ha.Context(
id="9c5bd62de45711eaaeb351041eec8dd9",
id="01GTDGKBCH00GW0X476W5TVBFC",
user_id="9400facee45711eaa9308bfd3d19e474",
)
hass.states.async_set("light.switch", STATE_ON)
@ -1120,7 +1120,7 @@ async def test_logbook_context_id_automation_script_started_manually(
# An Automation
automation_entity_id_test = "automation.alarm"
automation_context = ha.Context(
id="fc5bd62de45711eaaeb351041eec8dd9",
id="01GTDGKBCH00GW0X476W5TVCCC",
user_id="f400facee45711eaa9308bfd3d19e474",
)
hass.bus.async_fire(
@ -1129,7 +1129,7 @@ async def test_logbook_context_id_automation_script_started_manually(
context=automation_context,
)
script_context = ha.Context(
id="ac5bd62de45711eaaeb351041eec8dd9",
id="01GTDGKBCH00GW0X476W5TVAAA",
user_id="b400facee45711eaa9308bfd3d19e474",
)
hass.bus.async_fire(
@ -1141,7 +1141,7 @@ async def test_logbook_context_id_automation_script_started_manually(
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
script_2_context = ha.Context(
id="1234",
id="01GTDGKBCH00GW0X476W5TVEEE",
user_id="b400facee45711eaa9308bfd3d19e474",
)
hass.bus.async_fire(
@ -1172,12 +1172,12 @@ async def test_logbook_context_id_automation_script_started_manually(
assert json_dict[0]["entity_id"] == "automation.alarm"
assert "context_entity_id" not in json_dict[0]
assert json_dict[0]["context_user_id"] == "f400facee45711eaa9308bfd3d19e474"
assert json_dict[0]["context_id"] == "fc5bd62de45711eaaeb351041eec8dd9"
assert json_dict[0]["context_id"] == "01GTDGKBCH00GW0X476W5TVCCC"
assert json_dict[1]["entity_id"] == "script.mock_script"
assert "context_entity_id" not in json_dict[1]
assert json_dict[1]["context_user_id"] == "b400facee45711eaa9308bfd3d19e474"
assert json_dict[1]["context_id"] == "ac5bd62de45711eaaeb351041eec8dd9"
assert json_dict[1]["context_id"] == "01GTDGKBCH00GW0X476W5TVAAA"
assert json_dict[2]["domain"] == "homeassistant"
@ -1185,7 +1185,7 @@ async def test_logbook_context_id_automation_script_started_manually(
assert json_dict[3]["name"] == "Mock script"
assert "context_entity_id" not in json_dict[1]
assert json_dict[3]["context_user_id"] == "b400facee45711eaa9308bfd3d19e474"
assert json_dict[3]["context_id"] == "1234"
assert json_dict[3]["context_id"] == "01GTDGKBCH00GW0X476W5TVEEE"
assert json_dict[4]["entity_id"] == "switch.new"
assert json_dict[4]["state"] == "off"
@ -1209,7 +1209,7 @@ async def test_logbook_entity_context_parent_id(
await async_recorder_block_till_done(hass)
context = ha.Context(
id="ac5bd62de45711eaaeb351041eec8dd9",
id="01GTDGKBCH00GW0X476W5TVAAA",
user_id="b400facee45711eaa9308bfd3d19e474",
)
@ -1222,8 +1222,8 @@ async def test_logbook_entity_context_parent_id(
)
child_context = ha.Context(
id="2798bfedf8234b5e9f4009c91f48f30c",
parent_id="ac5bd62de45711eaaeb351041eec8dd9",
id="01GTDGKBCH00GW0X476W5TVDDD",
parent_id="01GTDGKBCH00GW0X476W5TVAAA",
user_id="b400facee45711eaa9308bfd3d19e474",
)
hass.bus.async_fire(
@ -1274,8 +1274,8 @@ async def test_logbook_entity_context_parent_id(
# A state change via service call with the script as the parent
light_turn_off_service_context = ha.Context(
id="9c5bd62de45711eaaeb351041eec8dd9",
parent_id="2798bfedf8234b5e9f4009c91f48f30c",
id="01GTDGKBCH00GW0X476W5TVBFC",
parent_id="01GTDGKBCH00GW0X476W5TVDDD",
user_id="9400facee45711eaa9308bfd3d19e474",
)
hass.states.async_set("light.switch", STATE_ON)
@ -1299,8 +1299,8 @@ async def test_logbook_entity_context_parent_id(
# An event with a parent event, but the parent event isn't available
missing_parent_context = ha.Context(
id="fc40b9a0d1f246f98c34b33c76228ee6",
parent_id="c8ce515fe58e442f8664246c65ed964f",
id="01GTDGKBCH00GW0X476W5TEDDD",
parent_id="01GTDGKBCH00GW0X276W5TEDDD",
user_id="485cacf93ef84d25a99ced3126b921d2",
)
logbook.async_log_entry(
@ -1423,7 +1423,7 @@ async def test_logbook_context_from_template(
await hass.async_block_till_done()
switch_turn_off_context = ha.Context(
id="9c5bd62de45711eaaeb351041eec8dd9",
id="01GTDGKBCH00GW0X476W5TVBFC",
user_id="9400facee45711eaa9308bfd3d19e474",
)
hass.states.async_set(
@ -1506,7 +1506,7 @@ async def test_logbook_(
await hass.async_block_till_done()
switch_turn_off_context = ha.Context(
id="9c5bd62de45711eaaeb351041eec8dd9",
id="01GTDGKBCH00GW0X476W5TVBFC",
user_id="9400facee45711eaa9308bfd3d19e474",
)
hass.states.async_set(
@ -1692,7 +1692,7 @@ async def test_logbook_multiple_entities(
await hass.async_block_till_done()
switch_turn_off_context = ha.Context(
id="9c5bd62de45711eaaeb351041eec8dd9",
id="01GTDGKBCH00GW0X476W5TVBFC",
user_id="9400facee45711eaa9308bfd3d19e474",
)
hass.states.async_set(
@ -2394,7 +2394,7 @@ async def test_get_events(
hass.states.async_set("light.kitchen", STATE_ON, {"brightness": 400})
await hass.async_block_till_done()
context = ha.Context(
id="ac5bd62de45711eaaeb351041eec8dd9",
id="01GTDGKBCH00GW0X476W5TVAAA",
user_id="b400facee45711eaa9308bfd3d19e474",
)
@ -2474,7 +2474,7 @@ async def test_get_events(
"id": 5,
"type": "logbook/get_events",
"start_time": now.isoformat(),
"context_id": "ac5bd62de45711eaaeb351041eec8dd9",
"context_id": "01GTDGKBCH00GW0X476W5TVAAA",
}
)
response = await client.receive_json()
@ -2651,7 +2651,7 @@ async def test_get_events_with_device_ids(
hass.states.async_set("light.kitchen", STATE_ON, {"brightness": 400})
await hass.async_block_till_done()
context = ha.Context(
id="ac5bd62de45711eaaeb351041eec8dd9",
id="01GTDGKBCH00GW0X476W5TVAAA",
user_id="b400facee45711eaa9308bfd3d19e474",
)
@ -2740,7 +2740,7 @@ async def test_logbook_select_entities_context_id(
await async_recorder_block_till_done(hass)
context = ha.Context(
id="ac5bd62de45711eaaeb351041eec8dd9",
id="01GTDGKBCH00GW0X476W5TVAAA",
user_id="b400facee45711eaa9308bfd3d19e474",
)
@ -2799,7 +2799,7 @@ async def test_logbook_select_entities_context_id(
# A service call
light_turn_off_service_context = ha.Context(
id="9c5bd62de45711eaaeb351041eec8dd9",
id="01GTDGKBCH00GW0X476W5TVBFC",
user_id="9400facee45711eaa9308bfd3d19e474",
)
hass.states.async_set("light.switch", STATE_ON)
@ -2880,7 +2880,7 @@ async def test_get_events_with_context_state(
hass.states.async_set("light.kitchen2", STATE_OFF)
context = ha.Context(
id="ac5bd62de45711eaaeb351041eec8dd9",
id="01GTDGKBCH00GW0X476W5TVAAA",
user_id="b400facee45711eaa9308bfd3d19e474",
)
hass.states.async_set("binary_sensor.is_light", STATE_OFF, context=context)