aboutsummaryrefslogtreecommitdiff
path: root/trappy/stats/Aggregator.py
blob: 4c33abf37088f17d703cc3ca8e6c1cf6a9861c83 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#    Copyright 2015-2016 ARM Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""Aggregators are responsible for aggregating information
for further analysis. These aggregations can produce
both scalars and vectors and each aggregator implementation
is expected to handle its "aggregation" mechanism.
"""


from trappy.utils import listify
from trappy.stats.Indexer import MultiTriggerIndexer
from abc import ABCMeta, abstractmethod


class AbstractAggregator(object):
    """Abstract class for all aggregators

    :param indexer: Indexer is passed on by the Child class
        for handling indices during correlation
    :type indexer: :mod:`trappy.stats.Indexer.Indexer`

    :param aggfunc: Function that accepts a pandas.Series and
        process it for aggregation.
    :type aggfunc: function
    """

    __metaclass__ = ABCMeta

    # The current implementation needs the index to
    # be unified across data frames to account for
    # variable sampling across data frames
    def __init__(self, indexer, aggfunc=None):

        self._result = {}
        self._aggregated = False
        self._aggfunc = aggfunc
        self.indexer = indexer

    def _add_result(self, pivot, series):
        """Add the result for the given pivot and trace

        :param pivot: The pivot for which the result is being generated
        :type pivot(hashable)

        :param series: series to be added to result
        :type series: :mod:`pandas.Series`
        """

        if pivot not in self._result:
            self._result[pivot] = self.indexer.series()

        for idx in series.index:
                self._result[pivot][idx] = series[idx]

    @abstractmethod
    def aggregate(self, trace_idx, **kwargs):
        """Abstract Method for aggregating data for various
        pivots.

        :param trace_idx: Index of the trace to be aggregated
        :type trace_idx: int

        :return: The aggregated result

        """

        raise NotImplementedError("Method Not Implemented")


class MultiTriggerAggregator(AbstractAggregator):

    """This aggregator accepts a list of triggers and each trigger has
     a value associated with it.
    """

    def __init__(self, triggers, topology, aggfunc=None):
        """
        :param triggers: trappy.stat.Trigger): A list or a singular trigger object
        :type triggers: :mod:`trappy.stat.Trigger.Trigger`

        :param topology (trappy.stat.Topology): A topology object for aggregation
                levels
        :type topology: :mod:`trappy.stat.Topology`

        :param aggfunc: A function to be applied on each series being aggregated.
                For each topology node, a series will be generated and this
                will be processed by the aggfunc
        :type aggfunc: function
        """
        self._triggers = triggers
        self.topology = topology
        super(
            MultiTriggerAggregator,
            self).__init__(MultiTriggerIndexer(triggers), aggfunc)

    def aggregate(self, **kwargs):
        """
        Aggregate implementation that aggregates
        triggers for a given topological level. All the arguments passed to
        it are forwarded to the aggregator function except level (if present)

        :return: A scalar or a vector aggregated result. Each group in the
            level produces an element in the result list with a one to one
            index correspondence
            ::

                groups["level"] = [[1,2], [3,4]]
                result = [result_1, result_2]
        """

        level = kwargs.pop("level", "all")

        # This function is a hot spot in the code. It is
        # worth considering a memoize decorator to cache
        # the function. The memoization can also be
        # maintained by the aggregator object. This will
        # help the code scale efficeintly
        level_groups = self.topology.get_level(level)
        result = []


        if not self._aggregated:
            self._aggregate_base()

        for group in level_groups:
            group = listify(group)
            if self._aggfunc is not None:
                level_res = self._aggfunc(self._result[group[0]], **kwargs)
            else:
                level_res = self._result[group[0]]

            for node in group[1:]:
                if self._aggfunc is not None:
                    node_res = self._aggfunc(self._result[node], **kwargs)
                else:
                    node_res = self._result[node]

                level_res += node_res

            result.append(level_res)

        return result

    def _aggregate_base(self):
        """A memoized function to generate the base series
        for each node in the flattened topology
        ::

            topo["level_1"] = [[1, 2], [3, 4]]

       This function will generate the fundamental
       aggregations for all nodes 1, 2, 3, 4 and
       store the result in _agg_result
       """

        for trigger in self._triggers:
            for node in self.topology.flatten():
                result_series = trigger.generate(node)
                self._add_result(node, result_series)

        self._aggregated = True