def upgrade(): """Upgrade database schema and/or data, creating a new revision.""" # pylint: disable=too-many-locals ctg_wf = collections.defaultdict(set) # collect last task start date tg_dates_dict = {} # collect start and end days setup for task group tg_wf = collections.defaultdict(set) # collect task groups for workflows wf_next_cycle_start_dates = {} wf_statuses = {} for row in op.get_bind().execute(MONTHLY_SQL): (wf, wf_next_cycle_start_date, wf_status, wf_recurrences, tgt, start_day, end_day, last_task_start_date) = row if not (start_day and end_day): continue wf_next_cycle_start_dates[wf] = wf_next_cycle_start_date wf_statuses[wf] = (wf_status, wf_recurrences) tg_wf[wf].add(tgt) ctg_wf[wf].add(last_task_start_date) tg_dates_dict[tgt] = (start_day, end_day) today = datetime.date.today() group_days = collections.defaultdict(set) notification_to_update = collections.defaultdict(set) for wf, task_ids in tg_wf.iteritems(): start_dates = [] for task_id in task_ids: start_date, end_date = [datetime.date(YEAR, MONTH, d) for d in tg_dates_dict[task_id]] start_dates.append(start_date) if end_date < start_date: end_date += relativedelta.relativedelta(end_date, months=1) group_days[(start_date, end_date)].add(task_id) # min start_date is the setup start date of workflow # next cycle start date should be calculated based on that date repeat_multiplier, next_cycle_start_date = \ wf_date_updater.get_next_cycle_start_date( min(start_dates), max([min(ctg_wf[wf]), today]), months=1) if wf_statuses[wf] == ("Active", 1): wf_date_updater.update_wf(op, repeat_multiplier, next_cycle_start_date, wf) if wf_statuses[wf] == ("Active", 1) and ( next_cycle_start_date != wf_next_cycle_start_dates[wf]): print ("Next cycle start date changed for " "active workflow {} from {} to {}".format( wf, wf_next_cycle_start_dates[wf], next_cycle_start_date)) for notification_type, offset in NOTIFICATION_OFFSET.iteritems(): key = (notification_type, next_cycle_start_date - datetime.timedelta(offset)) notification_to_update[key].add(wf) wf_date_updater.update_notifications(op, notification_to_update) wf_date_updater.update_task_dates(op, group_days)
def upgrade(): """Upgrade database schema and/or data, creating a new revision.""" # pylint: disable=too-many-locals week_tasks = op.get_bind().execute(WEEKLY_SQL) ctg_wf = collections.defaultdict(set) group_days = collections.defaultdict(set) notification_to_update = collections.defaultdict(set) tg_dates_dict = {} tg_wf = collections.defaultdict(set) today = datetime.date.today() wf_next_cycle_start_dates = {} wf_statuses = {} for (wf, wf_next_cycle_start_date, wf_status, w_recur, tgt, start_day, end_day, last_task_start_date) in week_tasks: if not (start_day and end_day): continue ctg_wf[wf].add(last_task_start_date) tg_dates_dict[tgt] = (start_day, end_day) tg_wf[wf].add(tgt) wf_next_cycle_start_dates[wf] = wf_next_cycle_start_date wf_statuses[wf] = (wf_status, w_recur) for wf, task_ids in tg_wf.iteritems(): start_dates = [] for task_id in task_ids: start_day, end_day = tg_dates_dict[task_id] start_date = DAYS[start_day] start_dates.append(start_date) end_date = DAYS[end_day] if end_date < start_date: end_date += WEEK_DELTA group_days[(start_date, end_date)].add(task_id) repeat_multiplier, next_cycle_start_date = \ wf_date_updater.get_next_cycle_start_date( min(start_dates), max([min(ctg_wf[wf]), today]), days=7 ) if wf_statuses[wf] == ("Active", 1): wf_date_updater.update_wf(op, repeat_multiplier, next_cycle_start_date, wf) if wf_statuses[wf] == ("Active", 1) and ( next_cycle_start_date != wf_next_cycle_start_dates[wf]): print("Next cycle start date changed for " "active workflow {} from {} to {}".format( wf, wf_next_cycle_start_dates[wf], next_cycle_start_date)) for notification_type, offset in NOTIFICATION_OFFSET.iteritems(): key = (notification_type, next_cycle_start_date - datetime.timedelta(offset)) notification_to_update[key].add(wf) wf_date_updater.update_notifications(op, notification_to_update) wf_date_updater.update_task_dates(op, group_days)
def upgrade(): """Upgrade database schema and/or data, creating a new revision.""" # pylint: disable=too-many-locals connection = op.get_bind() annually_tasks = connection.execute(ANNUALLY_SQL) tg_dates_dict = {} tg_wf = collections.defaultdict(set) ctg_wf = collections.defaultdict(set) wf_next_cycle_start_dates = {} wf_statuses = {} for (wf, wf_next_cycle_start_date, wf_status, wf_recur, tgt, start_day, start_month, end_day, end_month, last_task_start_date) in annually_tasks: if not (start_day and end_day and start_month and end_month): continue tg_wf[wf].add(tgt) ctg_wf[wf].add(last_task_start_date) tg_dates_dict[tgt] = (start_day, start_month, end_day, end_month) wf_next_cycle_start_dates[wf] = wf_next_cycle_start_date wf_statuses[wf] = (wf_status, wf_recur) today = datetime.date.today() group_days = collections.defaultdict(set) notification_to_update = collections.defaultdict(set) for wf, task_ids in tg_wf.iteritems(): start_dates = [] for task_id in task_ids: start_day, start_month, end_day, end_month = tg_dates_dict[task_id] _, max_start_day = calendar.monthrange(YEAR, start_month) _, max_end_day = calendar.monthrange(YEAR, end_month) start_date = datetime.date(YEAR, start_month, min(start_day, max_start_day)) end_date = datetime.date(YEAR, end_month, min(end_day, max_end_day)) while end_date < start_date: end_date += relativedelta.relativedelta(end_date, months=12) start_dates.append(start_date) group_days[(start_date, end_date)].add(task_id) repeat_multiplier, next_cycle_start_date = \ wf_date_updater.get_next_cycle_start_date( min(start_dates), max([min(ctg_wf[wf]), today]), months=12 ) if wf_statuses[wf] == ("Active", 1): wf_date_updater.update_wf(op, repeat_multiplier, next_cycle_start_date, wf) if wf_statuses[wf] == ("Active", 1) and ( next_cycle_start_date != wf_next_cycle_start_dates[wf]): print("Next cycle start date changed for " "active workflow {} from {} to {}".format( wf, wf_next_cycle_start_dates[wf], next_cycle_start_date)) for notification_type, offset in NOTIFICATION_OFFSET.iteritems(): key = (notification_type, next_cycle_start_date - datetime.timedelta(offset)) notification_to_update[key].add(wf) wf_date_updater.update_notifications(op, notification_to_update) wf_date_updater.update_task_dates(op, group_days)
def upgrade(): """Upgrade database schema and/or data, creating a new revision.""" # pylint: disable=too-many-locals connection = op.get_bind() annually_tasks = connection.execute(ANNUALLY_SQL) tg_dates_dict = {} tg_wf = collections.defaultdict(set) ctg_wf = collections.defaultdict(set) wf_next_cycle_start_dates = {} wf_statuses = {} for ( wf, wf_next_cycle_start_date, wf_status, wf_recur, tgt, start_day, start_month, end_day, end_month, last_task_start_date) in annually_tasks: if not (start_day and end_day and start_month and end_month): continue tg_wf[wf].add(tgt) ctg_wf[wf].add(last_task_start_date) tg_dates_dict[tgt] = (start_day, start_month, end_day, end_month) wf_next_cycle_start_dates[wf] = wf_next_cycle_start_date wf_statuses[wf] = (wf_status, wf_recur) today = datetime.date.today() group_days = collections.defaultdict(set) notification_to_update = collections.defaultdict(set) for wf, task_ids in tg_wf.iteritems(): start_dates = [] for task_id in task_ids: start_day, start_month, end_day, end_month = tg_dates_dict[task_id] _, max_start_day = calendar.monthrange(YEAR, start_month) _, max_end_day = calendar.monthrange(YEAR, end_month) start_date = datetime.date(YEAR, start_month, min(start_day, max_start_day)) end_date = datetime.date(YEAR, end_month, min(end_day, max_end_day)) while end_date < start_date: end_date += relativedelta.relativedelta(end_date, months=12) start_dates.append(start_date) group_days[(start_date, end_date)].add(task_id) repeat_multiplier, next_cycle_start_date = \ wf_date_updater.get_next_cycle_start_date( min(start_dates), max([min(ctg_wf[wf]), today]), months=12 ) if wf_statuses[wf] == ("Active", 1): wf_date_updater.update_wf(op, repeat_multiplier, next_cycle_start_date, wf) if wf_statuses[wf] == ("Active", 1) and ( next_cycle_start_date != wf_next_cycle_start_dates[wf]): print ("Next cycle start date changed for " "active workflow {} from {} to {}".format( wf, wf_next_cycle_start_dates[wf], next_cycle_start_date)) for notification_type, offset in NOTIFICATION_OFFSET.iteritems(): key = (notification_type, next_cycle_start_date - datetime.timedelta(offset)) notification_to_update[key].add(wf) wf_date_updater.update_notifications(op, notification_to_update) wf_date_updater.update_task_dates(op, group_days)