def update_daily_record(target_date, group): from kardboard.models import DailyRecord logger = update_daily_record.get_logger() should_recalc = False # We need all this logic because this job can be pulled # by two workers in parallel leading to a race condition try: dr = DailyRecord.objects.get(date=target_date, group=group) one_minute_ago = datetime.datetime.now() - relativedelta.relativedelta(minutes=1) if dr.updated_at <= one_minute_ago: should_recalc = True else: logger.debug("DailyRecord: %s / %s was recalulcated in the last minute" % (target_date, group)) except DailyRecord.DoesNotExist: should_recalc = True if should_recalc: try: DailyRecord.calculate(date=target_date, group=group) logger.info("Successfully calculated DailyRecord: Date: %s / Group: %s" % (target_date, group)) except Exception: logger.warning("Tried to save duplicate record: Date: %s / Group: %s" % (target_date, group)) raise
def update_daily_record(target_date, group): from kardboard.models import DailyRecord logger = update_daily_record.get_logger() should_recalc = False # We need all this logic because this job can be pulled # by two workers in parallel leading to a race condition try: dr = DailyRecord.objects.get(date=target_date, group=group) one_minute_ago = datetime.datetime.now() - relativedelta.relativedelta( minutes=1) if dr.updated_at <= one_minute_ago: should_recalc = True else: logger.debug( "DailyRecord: %s / %s was recalulcated in the last minute" % (target_date, group)) except DailyRecord.DoesNotExist: should_recalc = True if should_recalc: DailyRecord.calculate(date=target_date, group=group) logger.info( "Successfully calculated DailyRecord: Date: %s / Group: %s" % (target_date, group))
def update_daily_records(days=365): from kardboard.models import DailyRecord from kardboard.app import app report_groups = app.config.get('REPORT_GROUPS', {}) group_slugs = report_groups.keys() group_slugs.append('all') now = datetime.datetime.now() for i in xrange(0, days): target_date = now - relativedelta.relativedelta(days=i) for slug in group_slugs: DailyRecord.calculate(target_date, group=slug)
def main(): DailyRecord._get_collection().drop_indexes() oldest_card = Kard.objects.all().order_by('+backlog_date')[0] start_date = make_start_date(date=oldest_card.backlog_date) end_date = make_end_date(date=datetime.datetime.now()) print "Daily records: %s" % DailyRecord.objects.count() print "Creating daily records" print "%s --> %s" % (start_date, end_date) days = end_date - start_date print "Going back %s days" % days.days r = update_daily_records.apply(args=(days.days,)) r.get() print "DONE!" print "Daily records: %s" % DailyRecord.objects.count()