Changeset c859743 in rattail


Ignore:
Timestamp:
11/19/2022 05:38:02 PM (3 months ago)
Author:
Lance Edgar <lance@…>
Branches:
master
Children:
bea5278
Parents:
ec059ba
Message:

Add enabled and per-day scheduling flags for problem reports

report should only run (via rattail problems command) if it's
enabled generally, but also for the current day of week. all such
flags should be effectively ON by default.

also adds the --force command arg, to run a report no matter what
its flags happen to be

nb. manually running a report from tailbone also uses "force" logic

Location:
rattail
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • rattail/commands/problems.py

    rec059ba rc859743  
    33#
    44#  Rattail -- Retail Software Framework
    5 #  Copyright © 2010-2019 Lance Edgar
     5#  Copyright © 2010-2022 Lance Edgar
    66#
    77#  This file is part of Rattail.
     
    5656                            help="Whether to (attempt to) fix the problems, vs. just reporting them.")
    5757
     58        parser.add_argument('--force', action='store_true',
     59                            help="Force running of the report(s), i.e. ignore "
     60                            "the \"enabled\" and scheduling flags.")
     61
    5862        parser.add_argument('--dry-run', action='store_true',
    5963                            help="Go through the full motions and allow logging etc. to "
     
    7983            self.list_reports(handler, reports)
    8084        else:
    81             handler.run_problem_reports(reports, fix=args.fix)
     85            handler.run_problem_reports(reports, fix=args.fix, force=args.force)
    8286
    8387    def list_reports(self, handler, reports):
  • rattail/problems/handlers.py

    rec059ba rc859743  
    2727from __future__ import unicode_literals, absolute_import
    2828
     29import calendar
    2930import logging
    3031
     
    120121            return reports[0]
    121122
     123    def normalize_problem_report(self, report,
     124                                 include_schedule=False,
     125                                 include_recipients=False,
     126                                 **kwargs):
     127        """
     128        Return a normalized data dictionary for the given problem
     129        report.
     130
     131        :param include_schedule: If true, data dict will include the
     132           ``enabled`` field, as well as the ``day0`` thru ``day6``
     133           flag fields.
     134
     135        :param include_recipients: If true, data dict will include the
     136           ``email_recipients`` field, which is a list of email
     137           addresses.
     138        """
     139        data = {
     140            'system_key': report.system_key,
     141            'problem_key': report.problem_key,
     142            'problem_title': report.problem_title,
     143            'description': (report.__doc__ or '').strip() or None,
     144            'email_key': self.get_email_key(report),
     145        }
     146
     147        if include_schedule:
     148            data['enabled'] = self.is_enabled(report)
     149            data['days'] = {}
     150            for day in range(7):
     151                daykey = 'day{}'.format(day)
     152                data[daykey] = self.should_run_for_day(report, day)
     153                data['days'][daykey] = data[daykey]
     154
     155        if include_recipients:
     156            email_handler = self.app.get_email_handler()
     157            email = email_handler.get_email(data['email_key'])
     158            data['email_recipients'] = email.get_recips('all')
     159
     160        return data
     161
     162    def is_enabled(self, report):
     163        """
     164        Returns boolean indicating if the given problem report is
     165        enabled.
     166        """
     167        key = '{}.{}'.format(report.system_key, report.problem_key)
     168        enabled = self.config.getbool('rattail.problems',
     169                                      '{}.enabled'.format(key))
     170        if enabled is not None:
     171            return enabled
     172        return True
     173
     174    def should_run_for_day(self, report, day):
     175        """
     176        Returns boolean indicating if the given problem report should
     177        be ran for the given weekday.
     178
     179        :param day: Integer corresponding to a particular weekday.
     180           Uses the same conventions as Python itself, i.e. Monday is
     181           represented as zero (0).
     182        """
     183        key = '{}.{}'.format(report.system_key, report.problem_key)
     184        enabled = self.config.getbool('rattail.problems',
     185                                      '{}.day{}'.format(key, day))
     186        if enabled is not None:
     187            return enabled
     188        return True
     189
    122190    def organize_problem_reports(self, reports):
    123191        """
     
    140208        return sorted(set([pr.system_key for pr in problem_reports]))
    141209
    142     def run_problem_reports(self, reports, fix=False):
     210    def run_problem_reports(self, reports, fix=False, force=False, **kwargs):
    143211        """
    144212        Run the given set of problem reports.
     
    152220            for problem_key in sorted(system):
    153221                report = system[problem_key]
    154                 self.run_problem_report(report, fix=fix)
     222                self.run_problem_report(report, fix=fix, force=force)
    155223
    156224    def run_problem_report(self, problem_report, fix=False,
    157                            send=True, **kwargs):
    158         """
    159         Run a specific problem report.
    160         """
    161         log.info("running problem report: %s.%s",
    162                  problem_report.system_key,
    163                  problem_report.problem_key)
     225                           send=True, force=False, **kwargs):
     226        """
     227        Run the given problem report, if it is enabled and scheduled
     228        to run for the current day.
     229
     230        :param force: If true, the report will run regardless of
     231           whether it is enabled at all / for the current day.  If
     232           false (the default) then the report's enabled flags will
     233           determine whether we should actually run it now.
     234        """
     235        key = '{}.{}'.format(problem_report.system_key,
     236                             problem_report.problem_key)
     237        log.info("running problem report: %s", key)
     238
     239        if not self.is_enabled(problem_report):
     240            log.debug("problem report is not enabled: %s", key)
     241            if not force:
     242                return
     243
     244        weekday = self.app.today().weekday()
     245        if not self.should_run_for_day(problem_report, weekday):
     246            log.debug("problem report is not scheduled for %s: %s",
     247                      calendar.day_name[weekday], key)
     248            if not force:
     249                return
    164250
    165251        progress = kwargs.pop('progress', self.progress)
Note: See TracChangeset for help on using the changeset viewer.