diff --git a/dtable_events/automations/automations_pipeline.py b/dtable_events/automations/automations_pipeline.py index d6f40dd7..53b5c30f 100644 --- a/dtable_events/automations/automations_pipeline.py +++ b/dtable_events/automations/automations_pipeline.py @@ -1,8 +1,5 @@ import json -import os import time -from copy import deepcopy -from dataclasses import dataclass, field from datetime import datetime, timedelta, date from threading import Thread, Lock @@ -21,7 +18,8 @@ from dtable_events.utils.utils_metric import AUTOMATION_QUEUE_10_METRIC_HELP, AUTOMATION_QUEUE_20_METRIC_HELP, \ AUTOMATION_QUEUE_30_METRIC_HELP, REALTIME_AUTOMATION_RULES_HEARTBEAT_HELP, \ REALTIME_AUTOMATION_RULES_TRIGGERED_COUNT_HELP, SCHEDULED_AUTOMATION_RULES_TRIGGERED_COUNT_HELP, publish_metric -from dtable_events.automations.entities import AutomationResult, AutomationTask, QUEUE_AUTOMATION_TASKS_10, QUEUE_AUTOMATION_TASKS_20, QUEUE_AUTOMATION_TASKS_30 +from dtable_events.automations.entities import AutomationResult, AutomationTask, \ + QUEUE_AUTOMATION_TASKS_10, QUEUE_AUTOMATION_TASKS_20, QUEUE_AUTOMATION_TASKS_30 class RateLimiter: diff --git a/dtable_events/automations/entities.py b/dtable_events/automations/entities.py index 7b1ac6f5..4ce6e616 100644 --- a/dtable_events/automations/entities.py +++ b/dtable_events/automations/entities.py @@ -8,9 +8,7 @@ PER_UPDATE = 'per_update' PER_MONTH = 'per_month' CRON_CONDITIONS = (PER_DAY, PER_WEEK, PER_MONTH) -ALL_CONDITIONS = (PER_DAY, PER_WEEK, PER_MONTH, PER_UPDATE) -CONDITION_ROWS_MODIFIED = 'rows_modified' CONDITION_ROWS_ADDED = 'rows_added' CONDITION_FILTERS_SATISFY = 'filters_satisfy' CONDITION_PERIODICALLY = 'run_periodically' @@ -44,15 +42,34 @@ def to_dict(self): def append_warning(self, warning): self.warnings.append(warning) + def is_cron_time_matched(self): + if self.run_condition not in CRON_CONDITIONS: + return False + cur_datetime = datetime.now() + cur_hour = cur_datetime.hour + cur_week_day = cur_datetime.isoweekday() + cur_month_day = cur_datetime.day + if self.run_condition == PER_DAY: + return cur_hour == self.trigger.get('notify_hour', 12) + if self.run_condition == PER_WEEK: + return cur_hour == self.trigger.get('notify_week_hour', 12) \ + and cur_week_day == self.trigger.get('notify_week_day', 7) + return cur_hour == self.trigger.get('notify_month_hour', 12) \ + and cur_month_day == self.trigger.get('notify_month_day', 1) + def can_do_actions(self): - if self.trigger.get('condition') not in (CONDITION_FILTERS_SATISFY, CONDITION_PERIODICALLY, CONDITION_ROWS_ADDED, CONDITION_PERIODICALLY_BY_CONDITION): + if self.trigger.get('condition') not in ( + CONDITION_FILTERS_SATISFY, + CONDITION_PERIODICALLY, + CONDITION_ROWS_ADDED, + CONDITION_PERIODICALLY_BY_CONDITION): return False if self.trigger.get('condition') == CONDITION_ROWS_ADDED: if self.data.get('op_type') not in ['insert_row', 'append_rows', 'insert_rows']: return False - if self.trigger.get('condition') in [CONDITION_FILTERS_SATISFY, CONDITION_ROWS_MODIFIED]: + if self.trigger.get('condition') == CONDITION_FILTERS_SATISFY: if self.data.get('op_type') not in ['modify_row', 'modify_rows', 'add_link', 'update_links', 'update_rows_links', 'remove_link', 'move_group_rows']: return False @@ -60,25 +77,7 @@ def can_do_actions(self): return True if self.run_condition in CRON_CONDITIONS: - cur_datetime = datetime.now() - cur_hour = cur_datetime.hour - cur_week_day = cur_datetime.isoweekday() - cur_month_day = cur_datetime.day - if self.run_condition == PER_DAY: - trigger_hour = self.trigger.get('notify_hour', 12) - if cur_hour != trigger_hour: - return False - elif self.run_condition == PER_WEEK: - trigger_hour = self.trigger.get('notify_week_hour', 12) - trigger_day = self.trigger.get('notify_week_day', 7) - if cur_hour != trigger_hour or cur_week_day != trigger_day: - return False - else: - trigger_hour = self.trigger.get('notify_month_hour', 12) - trigger_day = self.trigger.get('notify_month_day', 1) - if cur_hour != trigger_hour or cur_month_day != trigger_day: - return False - return True + return self.is_cron_time_matched() return False diff --git a/dtable_events/automations/general_actions.py b/dtable_events/automations/general_actions.py index 9eafd031..c5112a2c 100644 --- a/dtable_events/automations/general_actions.py +++ b/dtable_events/automations/general_actions.py @@ -34,7 +34,6 @@ CONDITION_ROWS_MODIFIED = 'rows_modified' CONDITION_ROWS_ADDED = 'rows_added' CONDITION_FILTERS_SATISFY = 'filters_satisfy' -CONDITION_NEAR_DEADLINE = 'near_deadline' CONDITION_PERIODICALLY = 'run_periodically' CONDITION_PERIODICALLY_BY_CONDITION = 'run_periodically_by_condition'