From 873ceaba3aeb8211860908506fa829cd0f06e44d Mon Sep 17 00:00:00 2001 From: Feilong Wang Date: Mon, 8 Jan 2018 16:14:49 +1300 Subject: [PATCH] Add new transformer for Magnum pollster Change-Id: If45b8b822cc9469135501cea9ab0c1cfab9a5121 --- .../tests/unit/transformer/test_conversion.py | 35 +++++++++++++++++++ distil/transformer/conversion.py | 22 ++++++++++++ etc/meter_mappings.yml.sample | 12 +++++++ 3 files changed, 69 insertions(+) diff --git a/distil/tests/unit/transformer/test_conversion.py b/distil/tests/unit/transformer/test_conversion.py index a67ee74..798665d 100644 --- a/distil/tests/unit/transformer/test_conversion.py +++ b/distil/tests/unit/transformer/test_conversion.py @@ -399,3 +399,38 @@ class TestNetworkServiceTransformer(base.DistilTestCase): FAKE_DATA.t1) self.assertEqual({'fake_meter': 0}, usage) + +@mock.patch.object(general, 'get_transformer_config', + mock.Mock(return_value=FAKE_CONFIG)) +class TestMagnumTransformer(base.DistilTestCase): + def test_basic_sum(self): + """Tests that the transformer correctly calculate the sum value. + """ + + data = [ + {'timestamp': '2014-01-01T00:00:00', 'volume': 1}, + {'timestamp': '2014-01-01T00:10:00', 'volume': 0}, + {'timestamp': '2014-01-01T01:00:00', 'volume': 2}, + ] + + xform = conversion.MagnumTransformer() + usage = xform.transform_usage('fake_meter', data, FAKE_DATA.t0, + FAKE_DATA.t1) + + self.assertEqual({'fake_meter': 1}, usage) + + def test_only_non_charged(self): + """Tests that the transformer correctly calculate the sum value. + """ + + data = [ + {'timestamp': '2014-01-01T00:00:00', 'volume': 1}, + {'timestamp': '2014-01-01T00:10:00', 'volume': 1}, + {'timestamp': '2014-01-01T01:00:00', 'volume': 18}, + ] + + xform = conversion.MagnumTransformer() + usage = xform.transform_usage('fake_meter', data, FAKE_DATA.t0, + FAKE_DATA.t1) + + self.assertEqual({'fake_meter': 0}, usage) diff --git a/distil/transformer/conversion.py b/distil/transformer/conversion.py index 3074bcc..866a034 100644 --- a/distil/transformer/conversion.py +++ b/distil/transformer/conversion.py @@ -161,3 +161,25 @@ class NetworkServiceTransformer(BaseTransformer): max_vol = max(volumes) if len(volumes) else 0 hours = (end - start).total_seconds() / 3600.0 return {name: max_vol * hours} + + +class MagnumTransformer(BaseTransformer): + """Transformer for Magnum clusters. + """ + + def _transform_usage(self, name, data, start, end): + # NOTE(flwang): The magnum pollster of Ceilometer is using + # status as the volume(see ceilometer/cim/magnum.py), so we have + # to check the volume to make sure only the active clusters are + # charged. + charged_cluster_status = [2, 3, 4, 5, 9, 10, 11, + 12, 13, 14, 15, 16, 17] + volumes = [v["volume"] for v in data if + v["volume"] in charged_cluster_status] + + # If there is any valid status in this hour, then we think it's a valid + # sample. + max_vol = 1 if len(volumes) else 0 + hours = (end - start).total_seconds() / 3600.0 + return {name: max_vol * hours} + diff --git a/etc/meter_mappings.yml.sample b/etc/meter_mappings.yml.sample index 8e20c62..32ab119 100644 --- a/etc/meter_mappings.yml.sample +++ b/etc/meter_mappings.yml.sample @@ -241,3 +241,15 @@ name: sources: - name + +- + meter: cim.coe.cluster + service: p1.coe + type: COE Cluster + transformer: MagnumTransformer + unit: hour + metadata: + name: + sources: + - name +