mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 06:07:17 +00:00
Set statistics columns to double precision (#55053)
This commit is contained in:
parent
96056f3fce
commit
0624859bf4
@ -351,7 +351,7 @@ def _drop_foreign_key_constraints(connection, engine, table, columns):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _apply_update(engine, session, new_version, old_version):
|
def _apply_update(engine, session, new_version, old_version): # noqa: C901
|
||||||
"""Perform operations to bring schema up to date."""
|
"""Perform operations to bring schema up to date."""
|
||||||
connection = session.connection()
|
connection = session.connection()
|
||||||
if new_version == 1:
|
if new_version == 1:
|
||||||
@ -486,6 +486,21 @@ def _apply_update(engine, session, new_version, old_version):
|
|||||||
start = now.replace(minute=0, second=0, microsecond=0)
|
start = now.replace(minute=0, second=0, microsecond=0)
|
||||||
start = start - timedelta(hours=1)
|
start = start - timedelta(hours=1)
|
||||||
session.add(StatisticsRuns(start=start))
|
session.add(StatisticsRuns(start=start))
|
||||||
|
elif new_version == 20:
|
||||||
|
# This changed the precision of statistics from float to double
|
||||||
|
if engine.dialect.name in ["mysql", "oracle", "postgresql"]:
|
||||||
|
_modify_columns(
|
||||||
|
connection,
|
||||||
|
engine,
|
||||||
|
"statistics",
|
||||||
|
[
|
||||||
|
"mean DOUBLE PRECISION",
|
||||||
|
"min DOUBLE PRECISION",
|
||||||
|
"max DOUBLE PRECISION",
|
||||||
|
"state DOUBLE PRECISION",
|
||||||
|
"sum DOUBLE PRECISION",
|
||||||
|
],
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"No schema migration defined for version {new_version}")
|
raise ValueError(f"No schema migration defined for version {new_version}")
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ from sqlalchemy import (
|
|||||||
Text,
|
Text,
|
||||||
distinct,
|
distinct,
|
||||||
)
|
)
|
||||||
from sqlalchemy.dialects import mysql
|
from sqlalchemy.dialects import mysql, oracle, postgresql
|
||||||
from sqlalchemy.orm import declarative_base, relationship
|
from sqlalchemy.orm import declarative_base, relationship
|
||||||
from sqlalchemy.orm.session import Session
|
from sqlalchemy.orm.session import Session
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ import homeassistant.util.dt as dt_util
|
|||||||
# pylint: disable=invalid-name
|
# pylint: disable=invalid-name
|
||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
|
|
||||||
SCHEMA_VERSION = 19
|
SCHEMA_VERSION = 20
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -66,6 +66,12 @@ ALL_TABLES = [
|
|||||||
DATETIME_TYPE = DateTime(timezone=True).with_variant(
|
DATETIME_TYPE = DateTime(timezone=True).with_variant(
|
||||||
mysql.DATETIME(timezone=True, fsp=6), "mysql"
|
mysql.DATETIME(timezone=True, fsp=6), "mysql"
|
||||||
)
|
)
|
||||||
|
DOUBLE_TYPE = (
|
||||||
|
Float()
|
||||||
|
.with_variant(mysql.DOUBLE(asdecimal=False), "mysql")
|
||||||
|
.with_variant(oracle.DOUBLE_PRECISION(), "oracle")
|
||||||
|
.with_variant(postgresql.DOUBLE_PRECISION, "postgresql")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class Events(Base): # type: ignore
|
class Events(Base): # type: ignore
|
||||||
@ -240,11 +246,11 @@ class Statistics(Base): # type: ignore
|
|||||||
index=True,
|
index=True,
|
||||||
)
|
)
|
||||||
start = Column(DATETIME_TYPE, index=True)
|
start = Column(DATETIME_TYPE, index=True)
|
||||||
mean = Column(Float())
|
mean = Column(DOUBLE_TYPE)
|
||||||
min = Column(Float())
|
min = Column(DOUBLE_TYPE)
|
||||||
max = Column(Float())
|
max = Column(DOUBLE_TYPE)
|
||||||
state = Column(Float())
|
state = Column(DOUBLE_TYPE)
|
||||||
sum = Column(Float())
|
sum = Column(DOUBLE_TYPE)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_stats(metadata_id: str, start: datetime, stats: StatisticData):
|
def from_stats(metadata_id: str, start: datetime, stats: StatisticData):
|
||||||
|
@ -293,10 +293,21 @@ def compile_statistics(
|
|||||||
reset = False
|
reset = False
|
||||||
if old_state is None:
|
if old_state is None:
|
||||||
reset = True
|
reset = True
|
||||||
|
_LOGGER.info(
|
||||||
|
"Compiling initial sum statistics for %s, zero point set to %s",
|
||||||
|
entity_id,
|
||||||
|
fstate,
|
||||||
|
)
|
||||||
elif state_class == STATE_CLASS_TOTAL_INCREASING and (
|
elif state_class == STATE_CLASS_TOTAL_INCREASING and (
|
||||||
old_state is None or (new_state is not None and fstate < new_state)
|
old_state is None or (new_state is not None and fstate < new_state)
|
||||||
):
|
):
|
||||||
reset = True
|
reset = True
|
||||||
|
_LOGGER.info(
|
||||||
|
"Detected new cycle for %s, zero point set to %s (old zero point %s)",
|
||||||
|
entity_id,
|
||||||
|
fstate,
|
||||||
|
new_state,
|
||||||
|
)
|
||||||
|
|
||||||
if reset:
|
if reset:
|
||||||
# The sensor has been reset, update the sum
|
# The sensor has been reset, update the sum
|
||||||
|
Loading…
x
Reference in New Issue
Block a user