def _init_prev_datetime(self): prior = prior_month(self.focus_datetime.year, self.focus_datetime.month) monthrange = calendar.monthrange(prior[0], prior[1]) if self.focus_datetime.day <= monthrange[1]: day = self.focus_datetime.day else: day = monthrange[1] self.prev_datetime = datetime.datetime(prior[0], prior[1], day)
def _init_prev_datetime(self): prior = prior_month(self.focus_datetime.year, self.focus_datetime.month) monthrange = calendar.monthrange(prior[0], prior[1]) if self.focus_datetime.day <= monthrange[1]: day = self.focus_datetime.day else: day = monthrange[1] self.prev_datetime = datetime.datetime(prior[0], prior[1], day)
def _init_prev_datetime(self): minus_one = self.focus_datetime.day - 1 if (minus_one < 1): year, month = prior_month(self.focus_datetime.year, self.focus_datetime.month) day = calendar.monthrange(year, month)[1] else: year = self.focus_datetime.year month = self.focus_datetime.month day = minus_one self.prev_datetime = datetime.datetime(year, month, day)
def _init_prev_datetime(self): minus_one = self.focus_datetime.day - 1 if (minus_one < 1): year, month = prior_month(self.focus_datetime.year, self.focus_datetime.month) day = calendar.monthrange(year, month)[1] else: year = self.focus_datetime.year month = self.focus_datetime.month day = minus_one self.prev_datetime = datetime.datetime(year, month, day)
def main(argv=sys.argv): parser = create_karl_argparser(description="Save metrics to the ZODB") parser.add_argument('--year', dest='year', action='store', help="4 digit year") parser.add_argument('--month', dest='month', action='store', help="Integer month (January is 1)") parser.add_argument('--monthly', dest='monthly', action='store', help='Generate metrics for the previous month.' ' Useful for cron.') parser.add_argument('--range-year-begin', dest='range_year_begin', action='store', help='Beginning year for metrics generation.') parser.add_argument('--range-year-end', dest='range_year_end', action='store', help='End year for metrics generation (inclusive).') parser.add_argument('--range-month-begin', dest='range_month_begin', action='store', help='Beginning month for metrics generation.') parser.add_argument('--range-month-end', dest='range_month_end', action='store', help='End month for metrics generation (inclusive).') args = parser.parse_args(sys.argv[1:]) env = args.bootstrap(args.config_uri) site, registry, closer = env['root'], env['registry'], env['closer'] if args.monthly is not None: # in cron jobs, generate metrics for the previous month # this is much easier to specify than on the last possible # moment of the current month now = datetime.now() cur_year, cur_month = now.year, now.month prev_year, prev_month = prior_month(cur_year, cur_month) generate_metrics(site, prev_year, prev_month) elif args.range_year_begin is not None: # when mass generating metrics, provide year/month start/end ranges year_begin, year_end = (int(args.range_year_begin), int(args.range_year_end)) assert year_begin > 2000, 'Invalid begin year' assert year_end > 2000, 'Invalid end year' assert year_end >= year_begin, 'Begin year > End year' month_begin, month_end = (int(args.range_month_begin), int(args.range_month_end)) assert month_begin >= 1 and month_begin <= 12, 'Bad begin month' assert month_end >= 1 and month_end <= 12, 'Bad end month' for year in range(year_begin, year_end + 1): if year == year_begin: monthstart = month_begin else: monthstart = 1 if year == year_end: monthend = month_end else: monthend = 12 for month in range(monthstart, monthend + 1): print 'generating metrics for %s/%s' % (year, month) generate_metrics(site, year, month) transaction.commit() elif args.year: # generate metrics for a particular year/month year, month = int(args.year), int(args.month) assert year > 2000, "Invalid year" assert month >= 1 and month <= 12, "Invalid month" generate_metrics(site, year, month) transaction.commit()