#!/usr/bin/env python3
# Copyright 2015 Canonical Ltd.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; version 2.1.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Author: Benjamin Zeller <benjamin.zeller@canonical.com>

import babeltrace
import sys
import getopt


class MyCounter(dict):
    def __missing__(self, key):
        return 0

if __name__ == '__main__':
    verbose_mode = False
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hi:v", ["help",
                                   "inputdata="])
    except getopt.GetoptError:
        print("app-launch-profiler-lttng -i <inputdata>")
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print("app-launch-profiler-lttng -i <inputdata>")
            sys.exit()
        elif opt in ("-i", "--idata"):
            inputdata = arg
        elif opt in ("-v", "--verbose"):
            verbose_mode = True
        else:
            assert False, "unhandled option"
            sys.exit(2)
    col = babeltrace.TraceCollection()
    if col.add_traces_recursive(inputdata, 'ctf') is None:
        raise RuntimeError('Cannot add trace')
    first_event = -1
    iterations = 0
    events_in_iteration = 2
    numbers = MyCounter()
    minNumbers = MyCounter()
    maxNumbers = MyCounter()

    for event in col.events:
        if (event.name == "app:invokeApplauncher"):
            if (events_in_iteration != 2):
                raise RuntimeError("Wrong Nr of events: " +
                                   str(events_in_iteration))

            events_in_iteration = 1
            first_event = event.timestamp
#           print("Event "+event.name+" occurs after: "+str(0))
            iterations += 1
        else:
            events_in_iteration += 1
            duration = event.timestamp-first_event
            numbers[event.name] += duration

            if minNumbers[event.name] == 0:
                minNumbers[event.name] = duration
            elif minNumbers[event.name] > duration:
                minNumbers[event.name] = duration

            if maxNumbers[event.name] < duration:
                maxNumbers[event.name] = duration

#            print("Event "+event.name+" occurs after: " +
#                  str((event.timestamp-first_event) / 1000 / 1000 / 1000))

    for event in numbers:
        if (verbose_mode):
            print("---------- Event "+event+" ----------")
            print("Min: " + str(round(minNumbers[event] /
                                      1000 / 1000 / 1000, 4)))
            print("Max: " + str(round(maxNumbers[event] /
                                      1000 / 1000 / 1000, 4)))
            print("Avg: " + str(round(numbers[event] / iterations /
                                      1000000/1000, 4)))
        else:
            print(str(round(numbers[event]/iterations/1000000/1000, 4)))
