Exemplo n.º 1
0
    def profiler_minutes(self, astimestamp=False, aslocal=False):
        """ Provide best guess of whole minutes for current time range

            `astimestamp` determines whether to return results in Unix
            timestamp format or as datetime.datetime objects (defaults
            to datetime objects).

            `aslocal` set to True will apply local timezone to datetime
            objects (defaults to UTC).

            Profiler reports out in whole minute increments, and for time
            deltas less than one minute (60 seconds) it will use the rounded
            minute from the latest timestamp.  For time deltas over one
            minute, lowest and highest rounded minutes are used, along with
            all in between.
        """
        def round_to_minute(t):
            return t - datetime.timedelta(seconds=t.second,
                                          microseconds=t.microsecond)

        if aslocal:
            start = timeutils.ensure_timezone(self.start).astimezone(timeutils.tzlocal())
            end = timeutils.ensure_timezone(self.end).astimezone(timeutils.tzlocal())
            tstamp = time.mktime
        else:
            start = timeutils.ensure_timezone(self.start).astimezone(timeutils.tzutc())
            end = timeutils.ensure_timezone(self.end).astimezone(timeutils.tzutc())
            tstamp = calendar.timegm
        delta = end - start

        one_minute = datetime.timedelta(0, 60, 0)
        if delta <= one_minute:
            t = round_to_minute(end)
            if astimestamp:
                return [int(tstamp(t.timetuple()))]
            else:
                return [t]
        else:
            result = []
            t = round_to_minute(start)
            while t <= round_to_minute(end):
                if astimestamp:
                    result.append(int(tstamp(t.timetuple())))
                else:
                    result.append(t)
                t = t + one_minute
            return result
Exemplo n.º 2
0
    def get_iterdata(self, *args, **kwargs):
        """ Return a generator for the combined stream of outputs from each source object
        """
        threshold = timedelta(seconds=1)
        if 'time_thresh' in kwargs:
            threshold = kwargs['time_thresh']
            del kwargs['time_thresh']

        template = [None] * len(self._legend)
        iters = [s.output.get_iterdata(*args, **kwargs) for s in self._sources]
        inputs = [next(i, None) for i in iters]

        # XXX
        infinity = datetime(year=9999, month=12, day=31, tzinfo=tzutc())

        def get_sample_time(s):
            if s is None:
                return infinity
            return s.t

        def min_sample():
            return min(inputs, key=get_sample_time)

        ms = min_sample()
        sample_time = ms.t
        vals = list(template)
        while ms is not None:
            i = inputs.index(ms)
            inputs[i] = next(iters[i], None)

            delta = ms.t - sample_time
            if delta >= threshold:
                yield DictObject.create_from_dict(dict(t=sample_time,
                                                       vals=[vals],
                                                       processed_pkts=None,
                                                       unprocessed_pkts=None))
                
                sample_time = ms.t
                vals = list(template)

            assert len(ms.vals) == 1

            V = ms.vals[0]
            off = self._sources[i].offset
            for j in range(len(V)):
                vals[off + j] = V[j]
                
            ms = min_sample()

        yield DictObject.create_from_dict(dict(t=sample_time,
                                               processed_pkts=None,
                                               unprocessed_pkts=None,
                                               vals=[vals]))