-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: accept cron schedule for analyzers (#56)
details: - include every N {field} on base regex on CronSchedule - fix order of SplitCron implementation - checks for "," and "-" cases only on the minute field - added tests
- Loading branch information
1 parent
8013b50
commit 16a79c8
Showing
5 changed files
with
139 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class SplitCron: | ||
day_of_week: str | ||
month: str | ||
day_of_month: str | ||
hour: str | ||
minute: str | ||
|
||
|
||
def split_cron_expression(cron: str) -> SplitCron: | ||
"""Split the cron expression into its components.""" | ||
cron_slots = cron.split(" ") | ||
if len(cron_slots) != 5: | ||
raise ValueError("CronSchedule must have 5 fields.") | ||
return SplitCron( | ||
minute=cron_slots[0], | ||
hour=cron_slots[1], | ||
day_of_month=cron_slots[2], | ||
month=cron_slots[3], | ||
day_of_week=cron_slots[4], | ||
) | ||
|
||
|
||
def _is_not_less_granular_than_1_hour(split_cron: SplitCron) -> bool: | ||
"""Check if the cron expression is less granular than 1 hour.""" | ||
if split_cron.minute == "*": | ||
return False | ||
|
||
for item in ["-", ","]: | ||
if item in split_cron.minute: | ||
return False | ||
|
||
if split_cron.minute.startswith("*/"): | ||
try: | ||
divisor = int(split_cron.minute.split("/")[1]) | ||
if divisor < 60: | ||
return False | ||
except ValueError: | ||
pass | ||
|
||
return True | ||
|
||
|
||
def validate_cron_expression(cron: str) -> bool: | ||
split_cron = split_cron_expression(cron) | ||
return _is_not_less_granular_than_1_hour(split_cron=split_cron) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters