def test_after_range(self): t = time.time() start = t - 10 end = t - 5 assert iswithintimerange(t=t, start_time=start, end_time=end) is False # Same test with datetimes t = datetime.fromtimestamp(t) start = datetime.fromtimestamp(start) end = datetime.fromtimestamp(end) assert iswithintimerange(t=t, start_time=start, end_time=end) is False
def test_within_range(self): t = time.time() start = t - 5 end = t + 5 assert iswithintimerange(t=t, start_time=start, end_time=end) is True # Same test with datetimes t = datetime.fromtimestamp(t) start = datetime.fromtimestamp(start) end = datetime.fromtimestamp(end) assert iswithintimerange(t=t, start_time=start, end_time=end) is True
def test_before_range(self): # Test before range t = time.time() start = t + 5 end = t + 10 assert iswithintimerange(t=t, start_time=start, end_time=end) is False # Same test with datetimes t = datetime.fromtimestamp(t) start = datetime.fromtimestamp(start) end = datetime.fromtimestamp(end) assert iswithintimerange(t=t, start_time=start, end_time=end) is False
def test_is_none(self): t = time.time() start = t - 5 end = t + 5 assert comparetimerange(t=None, start_time=start, end_time=end) is None assert iswithintimerange(t=None, start_time=start, end_time=end) is None # Same test with datetimes t = datetime.fromtimestamp(t) start = datetime.fromtimestamp(start) end = datetime.fromtimestamp(end) assert iswithintimerange(t=None, start_time=start, end_time=end) is None
def get_logs_in_time_range_with_timestamps(self, file_paths, start_time=None, end_time=None): '''Return a generator that provides the log lines within the specified times. Orders files based on timestamp present in first log line if available. Lines not matching FileTools.date_format will not be returned.''' file_handles = [] for file_path in file_paths: f = self._open_log_file(file_path) first, last = self.get_first_last_lines(f) first_timestamp = self._gettimestamp(first) last_timestamp = self._gettimestamp(last) # Skip the file if the timestamps aren't in the right range if not iswithintimerange( first_timestamp, start_time=None, end_time=end_time): self.log.debug( f"Skipping file {file_path} since first timestamp in file {first_timestamp} is after the end_time: {end_time}" ) continue if not iswithintimerange( last_timestamp, start_time=start_time, end_time=None): self.log.debug( f"Skipping file {file_path} since last timestamp in file {first_timestamp} is before the start_time: {start_time}" ) continue file_handles.append((f, first_timestamp)) file_handles.sort(key=lambda x: x[1]) file_handles = [ f for f, timestamp in file_handles ] # Remove the timestamps so file_handles is storing sorted file_handles without timestamps now. for f in file_handles: pos = self._get_first_log_line_position_binary_search( file_handle=f, start_time=start_time, end_time=end_time) f.seek(pos) for line in f: timestamp = self._gettimestamp(line) if iswithintimerange(timestamp, start_time=start_time, end_time=end_time): yield timestamp, line else: continue
def _getdmesgoutput(self, start_time=None, end_time=None): result = [] out, err, returncode = self.shell_call('/usr/bin/dmesg', stream=True) boot_time = self._get_boot_time() dmesg_pattern = re.compile('\[(.*?)\] (.*)') for line in out: m = dmesg_pattern.match(line) dmesg_time = m.group(1).strip() timestamp = boot_time + float(dmesg_time) if iswithintimerange(timestamp, start_time, end_time): message = m.group(2) dt = datetime.fromtimestamp(timestamp) date_str = dt.strftime('%Y/%m/%d %H:%M:%S') result.append(' '.join([date_str, message])) if result: result.reverse() return '\n'.join(result)