my.bluemaestro: minor cleanup

This commit is contained in:
Dima Gerasimov 2023-06-21 20:10:40 +01:00 committed by karlicoss
parent c25ab51664
commit 88a3aa8d67

View file

@ -4,13 +4,26 @@
"""
# todo most of it belongs to DAL... but considering so few people use it I didn't bother for now
from dataclasses import dataclass
from datetime import datetime, timedelta
from pathlib import Path
import re
import sqlite3
from typing import Iterable, Sequence, Set, Optional
from my.core import get_files, make_logger, dataclass, Res
import pytz
from my.core import (
get_files,
make_logger,
Res,
stat,
Stats,
influxdb,
)
from my.core.common import mcachew
from my.core.error import unwrap
from my.core.pandas import DataFrameT, as_dataframe
from my.core.sqlite import sqlite_connect_immutable
from my.config import bluemaestro as config
@ -27,6 +40,7 @@ Celsius = float
Percent = float
mBar = float
@dataclass
class Measurement:
dt: datetime # todo aware/naive
@ -38,7 +52,6 @@ class Measurement:
# fixme: later, rely on the timezone provider
# NOTE: the timezone should be set with respect to the export date!!!
import pytz
tz = pytz.timezone('Europe/London')
# TODO when I change tz, check the diff
@ -49,9 +62,7 @@ def is_bad_table(name: str) -> bool:
return False if delegate is None else delegate(name)
from my.core.cachew import cache_dir
from my.core.common import mcachew
@mcachew(depends_on=inputs, cache_path=cache_dir('bluemaestro'))
@mcachew(depends_on=inputs)
def measurements() -> Iterable[Res[Measurement]]:
# todo ideally this would be via arguments... but needs to be lazy
dbs = inputs()
@ -68,7 +79,9 @@ def measurements() -> Iterable[Res[Measurement]]:
with sqlite_connect_immutable(f) as db:
db_dt: Optional[datetime] = None
try:
datas = db.execute(f'SELECT "{f.name}" as name, Time, Temperature, Humidity, Pressure, Dewpoint FROM data ORDER BY log_index')
datas = db.execute(
f'SELECT "{f.name}" as name, Time, Temperature, Humidity, Pressure, Dewpoint FROM data ORDER BY log_index'
)
oldfmt = True
db_dts = list(db.execute('SELECT last_download FROM info'))[0][0]
if db_dts == 'N/A':
@ -178,12 +191,11 @@ def measurements() -> Iterable[Res[Measurement]]:
# for k, v in merged.items():
# yield Point(dt=k, temp=v) # meh?
from my.core import stat, Stats
def stats() -> Stats:
return stat(measurements)
from my.core.pandas import DataFrameT, as_dataframe
def dataframe() -> DataFrameT:
"""
%matplotlib gtk
@ -197,7 +209,6 @@ def dataframe() -> DataFrameT:
def fill_influxdb() -> None:
from my.core import influxdb
influxdb.fill(measurements(), measurement=__name__)
@ -205,7 +216,6 @@ def check() -> None:
temps = list(measurements())
latest = temps[:-2]
from my.core.error import unwrap
prev = unwrap(latest[-2]).dt
last = unwrap(latest[-1]).dt