summaryrefslogtreecommitdiffstats
path: root/qtip/collector/logfile.py
blob: 2c2e532fea6874da9ed9cecb8dab730d3c198d24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
##############################################################################
# Copyright (c) 2016 ZTE Corp and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

from itertools import chain
from six.moves import reduce
import os

from qtip.base import BaseActor
from qtip.collector import load_parser
from qtip.collector import CollectorProp as CProp
from qtip.loader.file import FileLoader


class LogItem(BaseActor):
    def find(self, filename, paths=None):
        return self._parent.find(filename, paths)


class LogfileCollector(BaseActor):
    """run performance metrics from log files"""
    TYPE = 'logfile'
    LOGS = 'logs'
    PATHS = 'paths'

    def __init__(self, config, parent=None):
        super(LogfileCollector, self).__init__(config)
        self._parent = parent  # plan
        # TODO(yujunz) handle exception of invalid parent
        dirname = os.path.dirname(self._parent.abspath)
        paths = [os.path.join(dirname, p) for p in config.get(self.PATHS, [])]
        self._loader = FileLoader('.', paths)

    def run(self):
        collected = []
        for log_item_config in self._config[self.LOGS]:
            log_item = LogItem(log_item_config, self)
            matches = [load_parser(c[CProp.TYPE])(c, log_item).run()
                       for c in log_item.get_config(CProp.PARSERS)]
            collected = chain(collected, reduce(chain, matches))
        return reduce(merge_matchobj_to_dict, collected, {'groups': (), 'groupdict': {}})

    def find(self, filename, paths=None):
        return self._loader.find(filename, paths)


def merge_matchobj_to_dict(d, m):
    d['groups'] = chain(d['groups'], m.groups())
    d['groupdict'].update(m.groupdict())
    return d