#!/usr/bin/env python # Copyright (c) 2007, Corey Goldberg (corey@goldb.org) # # This file is part of PerfLog. # # PerfLog is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. import time class TimeSeries(object): def __init__(self, data_set, interval, parse=True): # data_set is a list of tuples: (epoch_time, datapoint) # interval is in secs self.data_set = self.__sort_epoch(data_set) self.interval = interval self.epoch_start = data_set[0][0] self.epoch_finish = data_set[len(data_set) - 1][0] self.date_time_start = self.__convert_from_epoch(self.epoch_start) self.date_time_finish = self.__convert_from_epoch(self.epoch_finish) self.timespan = self.epoch_finish - self.epoch_start if parse: self.series = self.__make_series() self.counts = self.__count_series() def __make_series(self): series = [] current_marker = self.epoch_start next_marker = self.epoch_start + self.interval while current_marker <= self.epoch_finish: series.append([timeval[1] for timeval in self.data_set if current_marker <= timeval[0] < next_marker]) current_marker = next_marker next_marker += self.interval return series def __count_series(self): counts = [len(x) for x in self.series] return counts def __sort_epoch(self, data_set): data_set.sort(cmp=lambda x, y: cmp(x[0], y[0])) return data_set def __convert_from_epoch(self, epoch): date_time = time.strftime('%m-%d-%Y %H:%M:%S', time.gmtime(epoch)) return date_time