def test_multiple_generators(self): self.maxDiff = None q = Query( x - y for x, y in Query((x.max for x in "metric1"), (x.min for x in "metric2")).by("tag1") ) a = AST(q.generators, q.groupings) actual = a.decompile() expected = Group( BinarySubtract( LoadAttr(Metric("metric1"), Value("max")), LoadAttr(Metric("metric2"), Value("min")) ), Value([["tag1"], "mean"]), ) self.assertEqual(actual, expected)
def test_complex_case(self): q = Query( moving_average(x, size=5) + y.mean + z for x, y, z in ("test", "test2", "test3") if z.label == "something" and z.label == "another_one" ).by("host") a = AST(q.generators, q.groupings) actual = a.decompile() expected = Group( BinaryAdd( BinaryAdd( FuncCall( Value("moving_average"), FuncArgs(Value([Metric("test")]), Value({"size": 5})), ), LoadAttr(Metric("test2"), Value("mean")), ), Filter( Filter(Metric("test3"), CompareEqual(Value("label"), Value("something"))), CompareEqual(Value("label"), Value("another_one")), ), ), Value([["host"], "mean"]), ) self.assertEqual(actual, expected)
def test_query_with_string_variable_for_metric(self): value = "test" q = Query(x for x in value if x.label1 == "prod") a = AST(q.generators, q.groupings) actual = a.decompile() expected = Filter(Metric("test"), CompareEqual(Value("label1"), Value("prod"))) self.assertEqual(actual, expected)
def test_query_with_varname_in_filter(self): value = "prod" q = Query(x for x in "test" if x.label1 == value) a = AST(q.generators, q.groupings) actual = a.decompile() expected = Filter(Metric("test"), CompareEqual(Value("label1"), Value("prod"))) self.assertEqual(actual, expected)
def test_for_loop_filter_variables(self): filters = ["filter1", "filter2", "filter3"] for i in range(len(filters)): q = Query(x for x in "test" if x.label1 == filters[i]) a = AST(q.generators, q.groupings) actual = a.decompile() expected = Filter(Metric("test"), CompareEqual(Value("label1"), Value(filters[i]))) self.assertEqual(actual, expected)
def test_aggregation_functions(self): q = Query(mean(x) for x in "test" if x.label1 == "prod") a = AST(q.generators, q.groupings) actual = a.decompile() expected = FuncCall( Value("mean"), FuncArgs( Value([Filter(Metric("test"), CompareEqual(Value("label1"), Value("prod")))]), Value({}), ), ) self.assertEqual(actual, expected)
def test_iteration(self): i = 0 q = Query(x.mean for x in "test") for i, _ in enumerate(q): pass self.assertEqual(0, i)
def test_gather_period(self): t1 = Query(x.mean for x in "test")[3600] t1_timeindex = np.array( [np.float64(t1.time[0] + x) for x in range(0, 3600, Query().DEFAULT_RESOLUTION)], dtype=np.float64, ) t2 = Query(x.mean for x in "test")[3600:1800] t2_timeindex = np.array( [np.float64(t2.time[0] + x) for x in range(0, 1800, Query().DEFAULT_RESOLUTION)], dtype=np.float64, ) t3 = Query(x.mean for x in "test")[3600:1800:300] t3_timeindex = np.array( [np.float64(t3.time[0] + x) for x in range(0, 1800, 300)], dtype=np.float64 ) t4 = Query(x.mean for x in "test").fetch() t4_timeindex = np.array( [np.float64(t4.time[0] + x) for x in range(0, 3600, Query().DEFAULT_RESOLUTION)], dtype=np.float64, ) t5 = Query(x.mean for x in "test")["1h"] now = int(time.time()) t6 = Query(x.mean for x in "test").range(start=now - 3600, end=now - 1800, resolution=1) self.assertTrue(np.array_equal(t1.time, t1_timeindex)) self.assertTrue(np.array_equal(t2.time, t2_timeindex)) self.assertTrue(np.array_equal(t3.time, t3_timeindex)) self.assertTrue(np.array_equal(t4.time, t4_timeindex)) self.assertTrue(np.array_equal(t5.time, t1_timeindex)) self.assertTrue(np.array_equal(t6.time, t2_timeindex)) with self.assertRaises(IndexError): Query(x.mean for x in "test")["1hkbkgvk"] with self.assertRaises(IndexError): Query(x.mean for x in "test")[1.04884]
def test_query_for_iterator(self): q = Query(x for x in Query(x for x in "test")) a = AST(q.generators, q.groupings) actual = a.decompile() expected = Metric('test') self.assertEqual(actual, expected)