1- import re
2- from datetime import datetime , timedelta
3-
4- from c42eventextractor .common import convert_datetime_to_timestamp
51from py42 .sdk .queries .fileevents .filters .event_filter import EventTimestamp
62
7- _MAX_LOOK_BACK_DAYS = 90
8- _FORMAT_VALUE_ERROR_MESSAGE = u"input must be a date/time string (e.g. 'YYYY-MM-DD', 'YY-MM-DD HH:MM', 'YY-MM-DD HH:MM:SS'), or a short value in days, hours, or minutes (e.g. 30d, 24h, 15m)"
9-
10-
11- class DateArgumentException (Exception ):
12- def __init__ (self , message = _FORMAT_VALUE_ERROR_MESSAGE ):
13- super (DateArgumentException , self ).__init__ (message )
14-
15-
16- TIMESTAMP_REGEX = re .compile (u"(\d{4}-\d{2}-\d{2})\s*(.*)?" )
17- MAGIC_TIME_REGEX = re .compile (u"(\d+)([dhm])$" )
3+ from code42cli .date_helper import DateArgumentException , parse_min_timestamp , parse_max_timestamp
184
195
206def create_event_timestamp_filter (begin_date = None , end_date = None ):
@@ -25,16 +11,16 @@ def create_event_timestamp_filter(begin_date=None, end_date=None):
2511 end_date: The end date for the range.
2612 """
2713 if begin_date and end_date :
28- min_timestamp = _parse_min_timestamp (begin_date )
29- max_timestamp = _parse_max_timestamp (end_date )
14+ min_timestamp = parse_min_timestamp (begin_date )
15+ max_timestamp = parse_max_timestamp (end_date )
3016 return _create_in_range_filter (min_timestamp , max_timestamp )
3117
3218 elif begin_date and not end_date :
33- min_timestamp = _parse_min_timestamp (begin_date )
19+ min_timestamp = parse_min_timestamp (begin_date )
3420 return _create_on_or_after_filter (min_timestamp )
3521
3622 elif end_date and not begin_date :
37- max_timestamp = _parse_max_timestamp (end_date )
23+ max_timestamp = parse_max_timestamp (end_date )
3824 return _create_on_or_before_filter (max_timestamp )
3925
4026
@@ -43,90 +29,16 @@ def _create_in_range_filter(min_timestamp, max_timestamp):
4329 return EventTimestamp .in_range (min_timestamp , max_timestamp )
4430
4531
46- def _create_on_or_after_filter (min_timestamp ):
47- return EventTimestamp .on_or_after (min_timestamp )
48-
49-
50- def _create_on_or_before_filter (max_timestamp ):
51- return EventTimestamp .on_or_before (max_timestamp )
52-
53-
54- def _parse_timestamp (date_str , rounding_func ):
55- timestamp_match = TIMESTAMP_REGEX .match (date_str )
56- magic_match = MAGIC_TIME_REGEX .match (date_str )
57-
58- if timestamp_match :
59- date , time = timestamp_match .groups ()
60- dt = _get_dt_from_date_time_pair (date , time )
61- if not time :
62- dt = rounding_func (dt )
63-
64- elif magic_match :
65- num , period = magic_match .groups ()
66- dt = _get_dt_from_magic_time_pair (num , period )
67- if period == u"d" :
68- dt = rounding_func (dt )
69-
70- else :
71- raise DateArgumentException ()
72- return dt
73-
74-
75- def _parse_min_timestamp (begin_date_str ):
76- dt = _parse_timestamp (begin_date_str , _round_datetime_to_day_start )
77-
78- boundary_date = _round_datetime_to_day_start (
79- datetime .utcnow () - timedelta (days = _MAX_LOOK_BACK_DAYS )
80- )
81- if dt < boundary_date :
82- raise DateArgumentException (u"'Begin date' must be within 90 days." )
83-
84- return convert_datetime_to_timestamp (dt )
85-
86-
87- def _parse_max_timestamp (end_date_str ):
88- dt = _parse_timestamp (end_date_str , _round_datetime_to_day_end )
89- return convert_datetime_to_timestamp (dt )
90-
91-
92- def _get_dt_from_date_time_pair (date , time ):
93- date_format = u"%Y-%m-%d %H:%M:%S"
94- if time :
95- time = u"{}:{}:{}" .format (* time .split (":" ) + [u"00" , u"00" ])
96- else :
97- time = u"00:00:00"
98- date_string = u"{} {}" .format (date , time )
99- try :
100- dt = datetime .strptime (date_string , date_format )
101- except ValueError :
102- raise DateArgumentException ()
103- else :
104- return dt
105-
106-
107- def _get_dt_from_magic_time_pair (num , period ):
108- num = int (num )
109- if period == u"d" :
110- dt = datetime .utcnow () - timedelta (days = num )
111- elif period == u"h" :
112- dt = datetime .utcnow () - timedelta (hours = num )
113- elif period == u"m" :
114- dt = datetime .utcnow () - timedelta (minutes = num )
115- else :
116- raise DateArgumentException (u"Couldn't parse magic time string: {}{}" .format (num , period ))
117- return dt
118-
119-
12032def _verify_timestamp_order (min_timestamp , max_timestamp ):
12133 if min_timestamp is None or max_timestamp is None :
12234 return
12335 if min_timestamp >= max_timestamp :
12436 raise DateArgumentException (u"Begin date cannot be after end date" )
12537
12638
127- def _round_datetime_to_day_start ( dt ):
128- return dt . replace ( hour = 0 , minute = 0 , second = 0 , microsecond = 0 )
39+ def _create_on_or_after_filter ( min_timestamp ):
40+ return EventTimestamp . on_or_after ( min_timestamp )
12941
13042
131- def _round_datetime_to_day_end ( dt ):
132- return dt . replace ( hour = 23 , minute = 59 , second = 59 , microsecond = 999000 )
43+ def _create_on_or_before_filter ( max_timestamp ):
44+ return EventTimestamp . on_or_before ( max_timestamp )
0 commit comments