Implement weectl database check

This commit is contained in:
Tom Keffer
2023-07-03 13:17:38 -07:00
parent dea4b30317
commit b05689a0d8
3 changed files with 66 additions and 14 deletions

View File

@@ -15,8 +15,8 @@ command line option, others require `--config=`.
Finish migrating `wee_database` to `weectl database`
- I think we can do without `check-strings` and `fix-strings`.
- Implement `check`. It will only check if the daily summaries need to be reweighted.
- Implement `update`.
- Allow rebuild daily summaries for only selected types.
- Edit documentation to show `weectl database` instead.
- Remove old `wee_database` references.

View File

@@ -41,6 +41,8 @@ calc_missing_usage = f"""{bcolors.BOLD}weectl database calc-missing
[--date=YYYY-mm-dd | [--from=YYYY-mm-dd[THH:MM]] [--to=YYYY-mm-dd[THH:MM]]]
[--config=CONFIG-PATH] [--binding=BINDING-NAME] \\
[--dry-run]{bcolors.ENDC}"""
check_usage = f"""{bcolors.BOLD}weectl database check \\
[--config=CONFIG-PATH] [--binding=BINDING-NAME]{bcolors.ENDC}"""
reweight_usage = f"""{bcolors.BOLD}weectl database reweight \\
[[--date=YYYY-mm-dd] | [--from=YYYY-mm-dd] [--to=YYYY-mm-dd]] \\
[--config=CONFIG-PATH] [--binding=BINDING-NAME] \\
@@ -320,6 +322,22 @@ def add_subparser(subparsers):
'do it.')
calc_missing_parser.set_defaults(func=calc_missing)
# ---------- Action 'check' ----------
check_parser = action_parser.add_parser('check',
description="Check the database for any issues.",
usage=check_usage,
help="Check the database for any issues.")
check_parser.add_argument('--config',
metavar='CONFIG-PATH',
help=f'Path to configuration file. '
f'Default is "{weecfg.default_config_path}".')
check_parser.add_argument("--binding", metavar="BINDING-NAME",
default='wx_binding',
help="The data binding to use. Default is 'wx_binding'.")
check_parser.set_defaults(func=check)
# ---------- Action 'reweight' ----------
reweight_parser = action_parser.add_parser('reweight',
description="Recalculate the weighted sums in "
"the daily summaries.",
@@ -378,6 +396,7 @@ def rebuild_daily(namespace):
def add_column(namespace):
"""Add a column to a WeeWX database"""
column_type = namespace.column_type.upper()
if column_type == 'INT':
column_type = "INTEGER"
@@ -389,6 +408,7 @@ def add_column(namespace):
def rename_column(namespace):
"""Rename a column in a WeeWX database."""
weectllib.db_actions.rename_column(namespace.config,
column_name=namespace.column_name,
new_name=namespace.new_name,
@@ -397,6 +417,7 @@ def rename_column(namespace):
def drop_columns(namespace):
"""Drop (remove) one or more columns in a WeeWX database."""
weectllib.db_actions.drop_columns(namespace.config,
column_names=namespace.column_names,
db_binding=namespace.binding,
@@ -404,12 +425,14 @@ def drop_columns(namespace):
def reconfigure_database(namespace):
"""Replicate a database, using current configuration settings."""
weectllib.db_actions.reconfigure_database(namespace.config,
db_binding=namespace.binding,
dry_run=namespace.dry_run)
def transfer_database(namespace):
"""Copy a database to a new database."""
weectllib.db_actions.transfer_database(namespace.config,
dest_binding=namespace.dest_binding,
db_binding=namespace.binding,
@@ -417,6 +440,7 @@ def transfer_database(namespace):
def calc_missing(namespace):
"""Calculate derived variables in a database."""
weectllib.db_actions.calc_missing(namespace.config,
date=namespace.date,
from_date=namespace.from_date,
@@ -425,11 +449,17 @@ def calc_missing(namespace):
dry_run=namespace.dry_run)
def check(namespace):
"""Check the integrity of a WeeWX database."""
weectllib.db_actions.check(namespace.config,
namespace.binding)
def reweight_daily(namespace):
"""Rebuild the daily summary in a WeeWX database"""
weectllib.db_actions.reweight(namespace.config,
date=namespace.date,
from_date=namespace.from_date,
to_date=namespace.to_date,
db_binding=namespace.binding,
dry_run=namespace.dry_run)
"""Recalculate the weights in a WeeWX database."""
weectllib.db_actions.reweight_daily(namespace.config,
date=namespace.date,
from_date=namespace.from_date,
to_date=namespace.to_date,
db_binding=namespace.binding,
dry_run=namespace.dry_run)

View File

@@ -519,12 +519,34 @@ def calc_missing(config_path,
print(msg)
def reweight(config_path,
date=None,
from_date=None,
to_date=None,
db_binding='wx_binding',
dry_run=False):
def check(config_path, db_binding='wx_binding'):
"""Check the database for any issues."""
config_path, config_dict, database_name = _prepare(config_path, db_binding, dry_run=False)
print("Checking daily summary tables version...")
with weewx.manager.open_manager_with_config(config_dict, db_binding) as dbm:
daily_summary_version = dbm._read_metadata('Version')
msg = f"Daily summary tables are at version {daily_summary_version}."
log.info(msg)
print(msg)
if daily_summary_version is not None and daily_summary_version >= '2.0':
# interval weighting fix has been applied
msg = "Interval Weighting Fix is not required."
log.info(msg)
print(msg)
else:
print("Recommend running --update to recalculate interval weightings.")
def reweight_daily(config_path,
date=None,
from_date=None,
to_date=None,
db_binding='wx_binding',
dry_run=False):
"""Recalculate the weighted sums in the daily summaries."""
config_path, config_dict, database_name = _prepare(config_path, db_binding, dry_run)