Add typer equivalents for rattail commands

This commit is contained in:
Lance Edgar 2024-05-16 20:55:42 -05:00
parent d8b865da71
commit 9a39db4546
2 changed files with 158 additions and 35 deletions

View file

@ -2,7 +2,7 @@
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2017 Lance Edgar
# Copyright © 2010-2024 Lance Edgar
#
# This file is part of Rattail.
#
@ -24,19 +24,139 @@
Tempmon commands
"""
from __future__ import unicode_literals, absolute_import
import datetime
import logging
from enum import Enum
from pathlib import Path
from rattail import commands
from rattail.time import localtime, make_utc
import typer
from typing_extensions import Annotated
from rattail.commands import rattail_typer, Subcommand, ImportSubcommand
from rattail.commands.typer import importer_command, typer_get_runas_user
from rattail.commands.importing import ImportCommandHandler
log = logging.getLogger(__name__)
class ExportHotCooler(commands.ImportSubcommand):
class ServiceAction(str, Enum):
start = 'start'
stop = 'stop'
@rattail_typer.command()
@importer_command
def export_hotcooler(
ctx: typer.Context,
**kwargs
):
"""
Export data from Rattail-Tempmon to HotCooler
"""
config = ctx.parent.rattail_config
progress = ctx.parent.rattail_progress
handler = ImportCommandHandler(
config,
import_handler_spec='rattail_tempmon.hotcooler.importing.tempmon:FromTempmonToHotCooler')
kwargs['user'] = typer_get_runas_user(ctx)
handler.run(kwargs, progress=progress)
@rattail_typer.command()
def purge_tempmon(
ctx: typer.Context,
keep_days: Annotated[
int,
typer.Option('--keep',
help="Number of days for which data should be kept.")] = ...,
dry_run: Annotated[
bool,
typer.Option('--dry-run',
help="Go through the full motions and allow logging etc. to "
"occur, but rollback (abort) the transaction at the end.")] = False,
):
"""
Purge stale data from Tempmon database
"""
config = ctx.parent.rattail_config
progress = ctx.parent.rattail_progress
do_purge(config, keep_days, dry_run=dry_run, progress=progress)
@rattail_typer.command()
def tempmon_client(
ctx: typer.Context,
action: Annotated[
ServiceAction,
typer.Argument(help="Action to perform for the service.")] = ...,
pid_file: Annotated[
Path,
typer.Option('--pidfile', '-p',
help="Path to PID file.")] = None,
# TODO: deprecate / remove this
daemonize: Annotated[
bool,
typer.Option('--daemonize',
help="Daemonize when starting.")] = False,
):
"""
Manage the tempmon-client daemon
"""
from rattail_tempmon.client import make_daemon
config = ctx.parent.rattail_config
daemon = make_daemon(config, pidfile)
if action == 'start':
daemon.start(daemonize)
elif action == 'stop':
daemon.stop()
@rattail_typer.command()
def tempmon_problems(
ctx: typer.Context,
):
"""
Email report(s) of various Tempmon data problems
"""
from rattail_tempmon import problems
config = ctx.parent.rattail_config
progress = ctx.parent.rattail_progress
problems.disabled_probes(config, progress=progress)
@rattail_typer.command()
def tempmon_server(
ctx: typer.Context,
action: Annotated[
ServiceAction,
typer.Argument(help="Action to perform for the service.")] = ...,
pid_file: Annotated[
Path,
typer.Option('--pidfile', '-p',
help="Path to PID file.")] = None,
# TODO: deprecate / remove this
daemonize: Annotated[
bool,
typer.Option('--daemonize',
help="Daemonize when starting.")] = False,
):
"""
Manage the tempmon-server daemon
"""
from rattail_tempmon.server import make_daemon
config = ctx.parent.rattail_config
daemon = make_daemon(config, pidfile)
if action == 'start':
daemon.start(daemonize)
elif action == 'stop':
daemon.stop()
class ExportHotCooler(ImportSubcommand):
"""
Export data from Rattail-Tempmon to HotCooler
"""
@ -45,7 +165,7 @@ class ExportHotCooler(commands.ImportSubcommand):
handler_spec = 'rattail_tempmon.hotcooler.importing.tempmon:FromTempmonToHotCooler'
class PurgeTempmon(commands.Subcommand):
class PurgeTempmon(Subcommand):
"""
Purge stale data from Tempmon database
"""
@ -60,34 +180,34 @@ class PurgeTempmon(commands.Subcommand):
"occur, but rollback (abort) the transaction at the end.")
def run(self, args):
from rattail_tempmon.db import Session as TempmonSession, model as tempmon
do_purge(self.config, args.keep, dry_run=args.dry_run, progress=self.progress)
cutoff = localtime(self.config).date() - datetime.timedelta(days=args.keep)
cutoff = localtime(self.config, datetime.datetime.combine(cutoff, datetime.time(0)))
session = TempmonSession()
readings = session.query(tempmon.Reading)\
.filter(tempmon.Reading.taken < make_utc(cutoff))
count = readings.count()
def do_purge(self, config, keep_days, dry_run=False, progress=None):
from rattail_tempmon.db import Session, model
from rattail.db.util import finalize_session
app = config.get_app()
cutoff = app.today() - datetime.timedelta(days=keep_days)
cutoff = app.localtime(datetime.datetime.combine(cutoff, datetime.time(0)))
session = Session()
readings = session.query(model.Reading)\
.filter(model.Reading.taken < app.make_utc(cutoff))\
.all()
def purge(reading, i):
session.delete(reading)
if i % 200 == 0:
session.flush()
self.progress_loop(purge, readings, count=count, message="Purging stale readings")
log.info("deleted {} stale readings".format(count))
if args.dry_run:
session.rollback()
log.info("dry run, so transaction was rolled back")
else:
session.commit()
log.info("transaction was committed")
session.close()
app.progress_loop(purge, readings, progress,
message="Purging stale readings")
log.info("deleted %s stale readings", len(readings))
finalize_session(session, dry_run=dry_run)
class TempmonClient(commands.Subcommand):
class TempmonClient(Subcommand):
"""
Manage the tempmon-client daemon
"""
@ -117,7 +237,7 @@ class TempmonClient(commands.Subcommand):
daemon.stop()
class TempmonServer(commands.Subcommand):
class TempmonServer(Subcommand):
"""
Manage the tempmon-server daemon
"""
@ -147,7 +267,7 @@ class TempmonServer(commands.Subcommand):
daemon.stop()
class TempmonProblems(commands.Subcommand):
class TempmonProblems(Subcommand):
"""
Email report(s) of various Tempmon data problems
"""

View file

@ -40,6 +40,9 @@ rattail.subcommands =
tempmon-problems = rattail_tempmon.commands:TempmonProblems
tempmon-server = rattail_tempmon.commands:TempmonServer
rattail.typer_imports =
rattail_tempmon = rattail_tempmon.commands
rattail.config.extensions =
tempmon = rattail_tempmon.config:TempmonConfigExtension