#!/usr/bin/env python3
#emacs: -*- mode: python; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
#ex: set sts=4 ts=4 sw=4 noet:
"""Little helper to annotate logfile with difference between timestamps in consecutive lines

It prints time estimated from previous line on the previous line, with 0 always printed as well
so it becomes possible to sort -n the output to see from what line it took longest to the next
"""

import re
import sys
from datetime import datetime

reg = re.compile('^\d{4}-\d{2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2},\d{1,3}')
prevt = None
maxl = 0
prevl = None
warned_negative = False

if len(sys.argv) <= 1:
    in_ = sys.stdin
else:
    in_ = open(sys.argv[1])

trailer = []
for l in in_:
    res = reg.search(l)
    dtstr = ''
    if res:
        end = res.end()
        t = datetime.strptime(l[:end], '%Y-%m-%d %H:%M:%S,%f')
        if prevt is not None:
            dt = t - prevt
            secs = dt.total_seconds()
            # negative dt has very weird form of -1 day + huge number of seconds
            if secs < 0:
                if not warned_negative:
                    sys.stderr.write(f"Negative delta of {secs} seconds detected.  Might be due to threading etc\n")
                    warned_negative = True

            ms = secs * 1000
            dtstr = ("%5d" % ms if ms else '    0')

            maxl = max(maxl, len(dtstr))
            dtstr = '%%%ds' % maxl % dtstr
            prevl = "%s %s" % (dtstr, prevl)
        prevt = t
    else:
        # no timestamp -- add to the trailer
        trailer.append(l)
        continue

    if prevl is not None:
        for l_ in trailer:
            sys.stdout.write("    - " + l_)
        trailer = []
        sys.stdout.write(prevl)

    if res:
        prevl = l

if prevl:
    sys.stdout.write("----- " + prevl)
for l_ in trailer:
    sys.stdout.write("    - " + l_)
