1# coding=utf-8 2""" 3© 2014 LinkedIn Corp. All rights reserved. 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 7 8Unless required by applicable law or agreed to in writing, software 9distributed under the License is distributed on an "AS IS" BASIS, 10WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11""" 12""" 13Utilities for luminol 14""" 15import calendar 16import csv 17import datetime 18import re 19import time 20 21from luminol import constants, exceptions 22 23 24def compute_ema(smoothing_factor, points): 25 """ 26 Compute exponential moving average of a list of points. 27 :param float smoothing_factor: the smoothing factor. 28 :param list points: the data points. 29 :return list: all ema in a list. 30 """ 31 ema = [] 32 # The initial point has a ema equal to itself. 33 if(len(points) > 0): 34 ema.append(points[0]) 35 for i in range(1, len(points)): 36 ema.append(smoothing_factor * points[i] + (1 - smoothing_factor) * ema[i - 1]) 37 return ema 38 39 40def read_csv(csv_name): 41 """ 42 Read data from a csv file into a dictionary. 43 :param str csv_name: path to a csv file. 44 :return dict: a dictionary represents the data in file. 45 """ 46 data = {} 47 if not isinstance(csv_name, (str, unicode)): 48 raise exceptions.InvalidDataFormat('luminol.utils: csv_name has to be a string!') 49 with open(csv_name, 'r') as csv_data: 50 reader = csv.reader(csv_data, delimiter=',', quotechar='|') 51 for row in reader: 52 try: 53 key = to_epoch(row[0]) 54 value = float(row[1]) 55 data[key] = value 56 except ValueError: 57 pass 58 return data 59 60 61def to_epoch(t_str): 62 """ 63 Covert a timestamp string to an epoch number. 64 :param str t_str: a timestamp string. 65 :return int: epoch number of the timestamp. 66 """ 67 try: 68 t = float(t_str) 69 return t 70 except: 71 for format in constants.TIMESTAMP_STR_FORMATS: 72 try: 73 t = datetime.datetime.strptime(t_str, format) 74 return float(time.mktime(t.utctimetuple()) * 1000.0 + t.microsecond / 1000.0) 75 except: 76 pass 77 raise exceptions.InvalidDataFormat 78