Ejemplo n.º 1
0
 def __init__(self, **kwargs):
     super(GnocchiStorage, self).__init__(**kwargs)
     conf = kwargs.get('conf') or ck_utils.load_conf(
         CONF.collect.metrics_conf)
     self.conf = validate_conf(conf)
     self.auth = ks_loading.load_auth_from_conf_options(
         CONF,
         GNOCCHI_STORAGE_OPTS)
     self.session = ks_loading.load_session_from_conf_options(
         CONF,
         GNOCCHI_STORAGE_OPTS,
         auth=self.auth)
     self._conn = gclient.Client(
         '1',
         session=self.session,
         adapter_options={'connect_retries': 3,
                          'interface': CONF.storage_gnocchi.interface})
     self._archive_policy_name = (
         CONF.storage_gnocchi.archive_policy_name)
     self._archive_policy_definition = json.loads(
         CONF.storage_gnocchi.archive_policy_definition)
     self._period = kwargs.get('period') or CONF.collect.period
     self._measurements = dict()
     self._resource_type_data = dict()
     self._init_resource_types()
Ejemplo n.º 2
0
    def to_cloudkitty(self, collector=None):
        # Rating informations
        rating_dict = {}
        rating_dict['price'] = self.rate

        # Volume informations
        vol_dict = {}
        vol_dict['qty'] = self.qty.normalize()
        vol_dict['unit'] = self.unit
        res_dict = {}

        # Encapsulate informations in a resource dict
        res_dict['rating'] = rating_dict
        res_dict['desc'] = json.loads(self.desc)
        res_dict['vol'] = vol_dict
        res_dict['desc']['tenant_id'] = self.tenant_id

        # Add resource to the usage dict
        usage_dict = {}
        usage_dict[self.res_type] = [res_dict]

        # Time informations
        period_dict = {}
        period_dict['begin'] = self.begin
        period_dict['end'] = self.end

        # Add period to the resource informations
        ck_dict = {}
        ck_dict['period'] = period_dict
        ck_dict['usage'] = usage_dict
        return ck_dict
Ejemplo n.º 3
0
    def to_cloudkitty(self, collector=None):
        # Rating informations
        rating_dict = {}
        rating_dict['price'] = self.rate

        # Volume informations
        vol_dict = {}
        vol_dict['qty'] = self.qty.normalize()
        vol_dict['unit'] = self.unit
        res_dict = {}

        # Encapsulate informations in a resource dict
        res_dict['rating'] = rating_dict
        res_dict['desc'] = json.loads(self.desc)
        res_dict['vol'] = vol_dict
        res_dict['tenant_id'] = self.tenant_id

        # Add resource to the usage dict
        usage_dict = {}
        usage_dict[self.res_type] = [res_dict]

        # Time informations
        period_dict = {}
        period_dict['begin'] = ck_utils.dt2iso(self.begin)
        period_dict['end'] = ck_utils.dt2iso(self.end)

        # Add period to the resource informations
        ck_dict = {}
        ck_dict['period'] = period_dict
        ck_dict['usage'] = usage_dict
        return ck_dict
Ejemplo n.º 4
0
 def _load(self):
     try:
         state_file = self._open()
         raw_data = state_file.read()
         if raw_data:
             state_data = json.loads(raw_data)
             self._ts = state_data['timestamp']
             self._metadata = state_data['metadata']
         state_file.close()
     except IOError:
         pass
Ejemplo n.º 5
0
 def test_json_standard(self):
     self.assertEqual(
         json.loads(dataframe.DataPoint(**self.default_params).json()), {
             "vol": {
                 "unit": "undefined",
                 "qty": decimal.Decimal(0)
             },
             "rating": {
                 "price": decimal.Decimal(0)
             },
             "groupby": {},
             "metadata": {},
         })
Ejemplo n.º 6
0
 def test_json_legacy(self):
     self.assertEqual(
         json.loads(
             dataframe.DataPoint(**self.default_params).json(legacy=True)),
         {
             "vol": {
                 "unit": "undefined",
                 "qty": decimal.Decimal(0)
             },
             "rating": {
                 "price": decimal.Decimal(0)
             },
             "desc": {},
         })
Ejemplo n.º 7
0
    def test_json(self):
        start = datetime.datetime(2019, 3, 4, 1, tzinfo=tz.UTC)
        end = datetime.datetime(2019, 3, 4, 2, tzinfo=tz.UTC)
        df = dataframe.DataFrame(start=start, end=end)
        a_points = [
            dataframe.DataPoint(**TestDataPoint.default_params)
            for _ in range(2)
        ]
        b_points = [
            dataframe.DataPoint(**TestDataPoint.default_params)
            for _ in range(4)
        ]
        df.add_points(a_points, 'service_a')
        df.add_points(b_points, 'service_b')

        self.maxDiff = None
        self.assertEqual(
            json.loads(df.json()),
            json.loads(
                json.dumps({
                    'period': {
                        'begin': start.isoformat(),
                        'end': end.isoformat()
                    },
                    'usage': {
                        'service_a': [
                            dataframe.DataPoint(
                                **TestDataPoint.default_params).as_dict()
                            for _ in range(2)
                        ],
                        'service_b': [
                            dataframe.DataPoint(
                                **TestDataPoint.default_params).as_dict()
                            for _ in range(4)
                        ],
                    }
                })))
Ejemplo n.º 8
0
    def get_metadata(self):
        """Get metadata attached to the state."""

        data = self._db.get_metadata(self._state_name)
        if data:
            return json.loads(data)
 def json(self):
     return json.loads(self.text)