From c892ac1aab409ef6bbe972ca0b5bb6b97be3f4b0 Mon Sep 17 00:00:00 2001 From: redxef Date: Tue, 14 Nov 2023 18:42:55 +0100 Subject: [PATCH] Allow different time ranges for the different sources. --- main.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index dc689f4..48c1a0e 100755 --- a/main.py +++ b/main.py @@ -45,6 +45,16 @@ def range_str_to_timedelta(value): weeks=values['w'] ) +def multiple_timedeltas(value): + mappings = {} + for v in value.split(','): + kv = v.split(':') + if len(kv) == 1: + mappings[-1] = range_str_to_timedelta(kv[0]) + continue + mappings[int(kv[0])] = range_str_to_timedelta(kv[1]) + return mappings + config_schema = Schema({ 'sources': [dict], 'sinks': [dict], @@ -52,12 +62,12 @@ config_schema = Schema({ Optional('level', default=':WARNING,wp_cal:INFO'): Use(str), }, Optional('range', default='365d'): Or( + dict, And( Use(str), - Regex(r'[0-9]+[smhdw]'), - Use(range_str_to_timedelta), + Regex(r'(([0-9]+:)?[0-9]+[smhdw],?)*'), + Use(multiple_timedeltas), ), - lambda x: isinstance(x, datetime.timedelta) ), }) @@ -149,9 +159,9 @@ def main(config, dryrun, level, range): # gather events events = [] source_results = [] - for source in sources: + for i, source in enumerate(sources): try: - events += source.get_events_resolved(until=config['range']) + events += source.get_events_resolved(until=config['range'].get(i, config['range'][-1])) source_results += [True] except Exception: logger.exception('failed to get events from source %s', source)