<commit_before>#!/usr/bin/env python
# Copyright (c) 2014 Montavista Software, LLC.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
#
import ceilometerclient.client as cc
import uuid

from congress.datasources.datasource_driver import DataSourceDriver
from congress.openstack.common import log as logging
from congress.utils import value_to_congress

LOG = logging.getLogger(__name__)


def d6service(name, keys, inbox, datapath, args):
    """This method is called by d6cage to create a dataservice
    instance.  There are a couple of parameters we found useful
    to add to that call, so we included them here instead of
    modifying d6cage (and all the d6cage.createservice calls).
    """
    return CeilometerDriver(name, keys, inbox, datapath, args)


# TODO(thinrichs): figure out how to move even more of this boilerplate
#   into DataSourceDriver.  E.g. change all the classes to Driver instead of
#   NeutronDriver, CeilometerDriver, etc. and move the d6instantiate function
#   to DataSourceDriver.
class CeilometerDriver(DataSourceDriver):
    METERS = "meters"
    ALARMS = "alarms"
    EVENTS = "events"
    EVENT_TRAITS = "events.traits"
    ALARM_THRESHOLD_RULE = "alarms.threshold_rule"

    def __init__(self, name='', keys='', inbox=None, datapath=None, args=None):
        if args is None:
            args = self.empty_credentials()
        super(CeilometerDriver, self).__init__(name, keys, inbox,
                                               datapath, args)
        if 'client' in args:
            self.ceilometer_client = args['client']
        else:
            self.creds = self.get_ceilometer_credentials_v2(name, args)
            self.ceilometer_client = cc.get_client(**self.creds)

        self.raw_state = {}

    def update_from_datasource(self):
        self.state = {}
        self.meters = []
        self.alarms = []
        self.alarm_threshold_rule = []
        self.events = []
        self.event_traits = []

        meters = self.ceilometer_client.meters.list()
        if ('meters' not in self.raw_state or meters !=
            self.raw_state['meters']):
            self.raw_state['meters'] = meters
            self._translate_meters(meters)
        else:
            self.raw_state['meters'] = meters
            self._translate_meters(meters)

        alarms = self.ceilometer_client.alarms.list()
        if ('alarms' not in self.raw_state or alarms !=
            self.raw_state['alarms']):
            self.raw_state['alarms'] = alarms
            self._translate_alarms(alarms)
        else:
            self.alarms = self.state[self.ALARMS]
            self.alarm_threshold_rule = self.state[self.ALARM_THRESHOLD_RULE]

        events = self.ceilometer_client.events.list()
        if ('events' not in self.raw_state or events !=
            self.raw_state['events']):
            self.raw_state['events'] = events
            self._translate_events(events)
        else:
            self.events = self.state[self.EVENTS]
            self.event_traits = self.state[self.EVENT_TRAITS]

        LOG.debug("EVENTS obtained from ceilometer %s" % self.events)
        # set state
        # TODO(thinrichs): use self.state everywhere instead of self.meters...
        self.state[self.METERS] = set(self.meters)
        self.state[self.ALARMS] = set(self.alarms)
        self.state[self.ALARM_THRESHOLD_RULE] = set(self.alarm_threshold_rule)
        self.state[self.EVENTS] = set(self.events)
        self.state[self.EVENT_TRAITS] = set(self.event_traits)

    @classmethod
    def get_schema(cls):
        d = {}
        d[cls.METERS] = ("meter_id", "name", "type", "unit", "source",
                         "resource_id", "user_id", "project_id")
        d[cls.ALARMS] = ("alarm_id", "name", "state", "enabled",
                         "threshold_rule", "type", "description",
                         "time_constraints", "user_id", "project_id",
                         "alarm_actions", "ok_actions",<commit_msg>Clear state later in ceilometer driver<commit_after>#!/usr/bin/env python
# Copyright (c) 2014 Montavista Software, LLC.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
#
import ceilometerclient.client as cc
import uuid

from congress.datasources.datasource_driver import DataSourceDriver
from congress.openstack.common import log as logging
from congress.utils import value_to_congress

LOG = logging.getLogger(__name__)


def d6service(name, keys, inbox, datapath, args):
    """This method is called by d6cage to create a dataservice
    instance.  There are a couple of parameters we found useful
    to add to that call, so we included them here instead of
    modifying d6cage (and all the d6cage.createservice calls).
    """
    return CeilometerDriver(name, keys, inbox, datapath, args)


# TODO(thinrichs): figure out how to move even more of this boilerplate
#   into DataSourceDriver.  E.g. change all the classes to Driver instead of
#   NeutronDriver, CeilometerDriver, etc. and move the d6instantiate function
#   to DataSourceDriver.
class CeilometerDriver(DataSourceDriver):
    METERS = "meters"
    ALARMS = "alarms"
    EVENTS = "events"
    EVENT_TRAITS = "events.traits"
    ALARM_THRESHOLD_RULE = "alarms.threshold_rule"

    def __init__(self, name='', keys='', inbox=None, datapath=None, args=None):
        if args is None:
            args = self.empty_credentials()
        super(CeilometerDriver, self).__init__(name, keys, inbox,
                                               datapath, args)
        if 'client' in args:
            self.ceilometer_client = args['client']
        else:
            self.creds = self.get_ceilometer_credentials_v2(name, args)
            self.ceilometer_client = cc.get_client(**self.creds)

        self.raw_state = {}

    def update_from_datasource(self):
        self.meters = []
        self.alarms = []
        self.alarm_threshold_rule = []
        self.events = []
        self.event_traits = []

        meters = self.ceilometer_client.meters.list()
        if ('meters' not in self.raw_state or meters !=
            self.raw_state['meters']):
            self.raw_state['meters'] = meters
            self._translate_meters(meters)
        else:
            self.raw_state['meters'] = meters
            self._translate_meters(meters)

        alarms = self.ceilometer_client.alarms.list()
        if ('alarms' not in self.raw_state or alarms !=
            self.raw_state['alarms']):
            self.raw_state['alarms'] = alarms
            self._translate_alarms(alarms)
        else:
            self.alarms = self.state[self.ALARMS]
            self.alarm_threshold_rule = self.state[self.ALARM_THRESHOLD_RULE]

        events = self.ceilometer_client.events.list()
        if ('events' not in self.raw_state or events !=
            self.raw_state['events']):
            self.raw_state['events'] = events
            self._translate_events(events)
        else:
            self.events = self.state[self.EVENTS]
            self.event_traits = self.state[self.EVENT_TRAITS]

        LOG.debug("EVENTS obtained from ceilometer %s" % self.events)
        # set state
        # TODO(thinrichs): use self.state everywhere instead of self.meters...
        self.state = {}
        self.state[self.METERS] = set(self.meters)
        self.state[self.ALARMS] = set(self.alarms)
        self.state[self.ALARM_THRESHOLD_RULE] = set(self.alarm_threshold_rule)
        self.state[self.EVENTS] = set(self.events)
        self.state[self.EVENT_TRAITS] = set(self.event_traits)

    @classmethod
    def get_schema(cls):
        d = {}
        d[cls.METERS] = ("meter_id", "name", "type", "unit", "source",
                         "resource_id", "user_id", "project_id")
        d[cls.ALARMS] = ("alarm_id", "name", "state", "enabled",
                         "threshold_rule", "type", "description",
                         "time_constraints", "user_id", "project_id",
                         "alarm_actions", "ok_actions",
{'fraction_non_alphanumeric': 0.10225130309415549, 'fraction_numerical': 0.0031052456471110127, 'mean_word_length': 2.5729001584786055, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5551322', 'n_tokens_mistral': 2724, 'n_tokens_neox': 2638, 'n_words': 698}