55 lines
1.3 KiB
Python
55 lines
1.3 KiB
Python
|
|
import datetime
|
|
import logging
|
|
import os
|
|
|
|
from port_stats import utils
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
def _parse_stamped_line(line):
|
|
try:
|
|
return datetime.datetime.strptime(
|
|
line.split(' :: ')[0], '%Y-%b-%d %H:%M:%S')
|
|
except Exception:
|
|
return None
|
|
|
|
|
|
def log_timestamps(filename):
|
|
first = None
|
|
last = None
|
|
with open(filename, 'r') as f:
|
|
parsed = (_parse_stamped_line(line) for line in f)
|
|
filtered = (stamp for stamp in parsed if stamp)
|
|
for last in filtered:
|
|
if not first:
|
|
first = last
|
|
return first, last
|
|
|
|
|
|
def log_seconds(filename):
|
|
first, last = log_timestamps(filename)
|
|
if first and last:
|
|
return (last - first).total_seconds()
|
|
else:
|
|
LOG.warn("Failed to find timestamps in %s", filename)
|
|
return 0
|
|
|
|
|
|
def task_event_logs(info):
|
|
if not info:
|
|
return []
|
|
path = os.path.join(info['task_path'], 'logs')
|
|
if not os.path.exists(path):
|
|
return []
|
|
res = sorted((f for f in os.listdir(path) if f.startswith('events')),
|
|
key=lambda f: [utils.maybe_int(x) for x in f.split('.')])
|
|
return [os.path.join(path, x) for x in res]
|
|
|
|
|
|
def build_time(info, idx=0):
|
|
log_file = task_event_logs(info)[idx]
|
|
return log_seconds(log_file)
|