def setUp(self): # Variables values _dict = { 'x': 4, 'y': 5, 'metric_687a68bb895125cb02f768473985569f': 120004608.0, 'metric_f64a796c2e49d9ac3ba8cd1cdf350795': 3212440371.0, 'metric_02bc472f705c074b4156850e1b5adcf4': 90193784012.0 } self.formula = Formulas(_dict)
def metric_raw(self, results, formula): #nmetric = results[1] metrics, _ = results # Build points dictionnary points = {} length = False for m in metrics: cid = m['meta']['data_id'] mid = 'metric_' + hashlib.md5(cid).hexdigest() mname = self.retreive_metric_name(cid) # Replace metric name in formula by the unique id formula = formula.replace(mname, mid) self.logger.debug("Metric {0} - {1}".format(mname, mid)) points[mid] = m['points'] # Make sure we treat the same amount of points by selecting # The metric with less points. if not length or len(m['points']) < length: length = len(m['points']) self.logger.debug('formula: {}'.format(formula)) #self.logger.debug('points: {}'.format(points)) mids = points.keys() finalSerie = [] # Now loop over all points to calculate the final serie for i in range(length): data = {} ts = 0 for j in range(len(mids)): mid = mids[j] # Get point value at timestamp "i" for metric "mid" data[mid] = points[mid][i][1] # Set timestamp ts = points[mid][i][0] # import data in math context math = Formulas(data) # Evaluate the mathematic formula pointval = math.evaluate(formula) # Add computed point in the serie finalSerie.append([ts * 1000, pointval]) # Remove variables values from math context math.reset() self.logger.debug('finalserie: {}'.format(finalSerie)) return finalSerie, points[mid]
def setUp(self): # Variables values _dict = { "x": 4, "y": 5, "metric_687a68bb895125cb02f768473985569f": 120004608.0, "metric_f64a796c2e49d9ac3ba8cd1cdf350795": 3212440371.0, "metric_02bc472f705c074b4156850e1b5adcf4": 90193784012.0, } self.formula = Formulas(_dict)
class FormulasTest(TestCase): """docstring for FormulasTest""" def setUp(self): # Variables values _dict = { 'x': 4, 'y': 5, 'metric_687a68bb895125cb02f768473985569f': 120004608.0, 'metric_f64a796c2e49d9ac3ba8cd1cdf350795': 3212440371.0, 'metric_02bc472f705c074b4156850e1b5adcf4': 90193784012.0 } self.formula = Formulas(_dict) def test(self): '''abs''' expressions = { "x^2 + 9*x + 5": 4**2 + 9*4 + 5, "x^y": 4**5, "x^y + x + 2*y": 4**5 + 4 + 2*5, "-9": -9, "-E": -math.e, "9 + 3 + 6": 18, "2*3.14159": 2*3.14159, "PI * PI / 10": math.pi * math.pi / 10, "PI^2": math.pi**2, "E^PI": math.e**math.pi, "2^3^2": 2**3**2, "sgn(-2)": -1, "sin(PI/2)": math.sin(math.pi/2), "trunc(E)": int(math.e), "round(E)": round(math.e), "(x + y + 1)/3": float(10)/float(3), "( metric_02bc472f705c074b4156850e1b5adcf4+ metric_687a68bb895125cb02f768473985569f+ metric_f64a796c2e49d9ac3ba8cd1cdf350795)/3": 31175409663.666668, # NOQA "MIN(2, 5, y, x)": 2, "MAX(2, 8, 45)": 45, "sum(7, 13.56, 0.04)": 20.60 } for k, v in expressions.iteritems(): self.assertEqual(self.formula.evaluate(k), v)
class FormulasTest(TestCase): """docstring for FormulasTest""" def setUp(self): # Variables values _dict = { 'x': 4, 'y': 5, 'metric_687a68bb895125cb02f768473985569f': 120004608.0, 'metric_f64a796c2e49d9ac3ba8cd1cdf350795': 3212440371.0, 'metric_02bc472f705c074b4156850e1b5adcf4': 90193784012.0 } self.formula = Formulas(_dict) def test(self): '''abs''' expressions = { "x^2 + 9*x + 5": 4**2 + 9 * 4 + 5, "x^y": 4**5, "x^y + x + 2*y": 4**5 + 4 + 2 * 5, "-9": -9, "-E": -math.e, "9 + 3 + 6": 18, "2*3.14159": 2 * 3.14159, "PI * PI / 10": math.pi * math.pi / 10, "PI^2": math.pi**2, "E^PI": math.e**math.pi, "2^3^2": 2**3**2, "sgn(-2)": -1, "sin(PI/2)": math.sin(math.pi / 2), "trunc(E)": int(math.e), "round(E)": round(math.e), "(x + y + 1)/3": float(10) / float(3), "( metric_02bc472f705c074b4156850e1b5adcf4+ metric_687a68bb895125cb02f768473985569f+ metric_f64a796c2e49d9ac3ba8cd1cdf350795)/3": 31175409663.666668, # NOQA "MIN(2, 5, y, x)": 2, "MAX(2, 8, 45)": 45, "sum(7, 13.56, 0.04)": 20.60 } for k, v in expressions.iteritems(): self.assertEqual(self.formula.evaluate(k), v)