<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# Copyright (C) 2015-2015: Alignak team, see AUTHORS.txt file for contributors
#
# This file is part of Alignak.
#
# Alignak is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Alignak is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Alignak.  If not, see <http://www.gnu.org/licenses/>.
"""
This module is used to send logs and livestate to alignak-backend with broker
"""

import time

from alignak_backend_client.client import Backend
# pylint: disable=F0401
from alignak.basemodule import BaseModule
# pylint: disable=F0401
from alignak.log import logger

# pylint: disable=C0103
properties = {
    'daemons': ['broker'],
    'type': 'alignakbackendbrok',
    'external': True,
    }


def get_instance(mod_conf):
    """Return a module instance for the plugin manager

    :param mod_conf: Configuration object
    :type mod_conf: object
    :return: AlignakBackendArbit instance
    :rtype: object
    """
    logger.info("[Backend Broker] Get a Alignak log & livestate module for plugin %s",
                mod_conf.get_name())
    instance = AlignakBackendBrok(mod_conf)
    return instance


class AlignakBackendBrok(BaseModule):
    """ This class is used to send logs and livestate to alignak-backend
    """

    def __init__(self, modconf):
        BaseModule.__init__(self, modconf)
        self.url = getattr(modconf, 'api_url', 'http://localhost:5000')
        self.backend = Backend(self.url)
        self.backend.token = getattr(modconf, 'token', '')
        if self.backend.token == '':
            self.getToken(getattr(modconf, 'username', ''), getattr(modconf, 'password', ''),
                          getattr(modconf, 'allowgeneratetoken', False))
        self.ref_live = {
            'host': {},
            'service': {}
        }
        self.mapping = {
            'host': {},
            'service': {}
        }
        self.hosts = {}
        self.services = {}
        self.loaded_hosts = False
        self.loaded_services = False

    # Common functions
    def do_loop_turn(self):
        """This function is called/used when you need a module with
        a loop function (and use the parameter 'external': True)
        """
        logger.info("[Backend Broker] In loop")
        time.sleep(1)

    def getToken(self, username, password, generatetoken):
        """
        Authenticate and get the token

        :param username: login name
        :type username: str
        :param password: password
        :type password: str
        :param generatetoken: if True allow generate token, otherwise not generate
        :type generatetoken: bool
        :return: None
        """
        generate = 'enabled'
        if not generatetoken:
            generate = 'disabled'
        self.backend.login(username, password, generate)

    def get_refs(self, type_data):
        """
        Get the _id in the backend for hosts and services

        :param type_data: livestate type to get: livehost or liveservice
        :type type_data: str
        :return: None
        """
        if type_data == 'livehost':
            params = {'projection': '{"name":1}'}
            content = self.backend.get_all('host', params)
            for item in content:
                self.mapping['host'][item['name']] = item['_id']
            # get all livehost
            params = {'projection': '{"host_name":1,"state":1,"state_type":1,"_realm":1}',
                      'where': '{"type":"host"}'}
            contentlh = self.backend.get_all('livestate', params)
            for item in contentlh:
                self.ref_live['host'][item['host_name']] = {
                    '_id': item['_id'],
                    '_etag': item['_etag'],
                    '_realm': item['_realm'],
                    'initial_state': item['state'],
                    'initial_state_type': item['state_type']
                }
            self.loaded_hosts = True
        elif type_data == 'liveservice':
            params = {'projection': '{"name":1}'}
            contenth = self.backend.get_all('host', params)
            hosts = {}
            for item in contenth:
                hosts[item['_id']] = item['name']
            params = {'projection': '{"name":1,"host_name":1}'}
            content = self.backend.get_all('service', params)
            for item in content:
                self.mapping['service'][''.join([hosts[item['host_name']],
                                                 item['name']])] = item['_id']
            # get all liveservice
            params = {'projection': '{"service_description":1,"state":1,"state_type":1,"_realm":1}',
                      'where': '{"type":"service"}}'}
            contentls = self.backend.get_all('livestate', params)
            for item in contentls:
                self.ref_live['service'][item['service_description']] = {
                    '_id': item['_id'],
                    '_etag': item['_etag'],
                    '_realm': item['_realm'],
                    'initial_state': item['state'],
                    'initial_state_type': item['state_type']
                }
            self.loaded_services = True

    def update(self, data, obj_type):
        """
        Update livehost and liveservice

        :param data: dictionary of data from scheduler
        :type data: dict<commit_msg>Fix another error<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# Copyright (C) 2015-2015: Alignak team, see AUTHORS.txt file for contributors
#
# This file is part of Alignak.
#
# Alignak is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Alignak is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Alignak.  If not, see <http://www.gnu.org/licenses/>.
"""
This module is used to send logs and livestate to alignak-backend with broker
"""

import time

from alignak_backend_client.client import Backend
# pylint: disable=F0401
from alignak.basemodule import BaseModule
# pylint: disable=F0401
from alignak.log import logger

# pylint: disable=C0103
properties = {
    'daemons': ['broker'],
    'type': 'alignakbackendbrok',
    'external': True,
    }


def get_instance(mod_conf):
    """Return a module instance for the plugin manager

    :param mod_conf: Configuration object
    :type mod_conf: object
    :return: AlignakBackendArbit instance
    :rtype: object
    """
    logger.info("[Backend Broker] Get a Alignak log & livestate module for plugin %s",
                mod_conf.get_name())
    instance = AlignakBackendBrok(mod_conf)
    return instance


class AlignakBackendBrok(BaseModule):
    """ This class is used to send logs and livestate to alignak-backend
    """

    def __init__(self, modconf):
        BaseModule.__init__(self, modconf)
        self.url = getattr(modconf, 'api_url', 'http://localhost:5000')
        self.backend = Backend(self.url)
        self.backend.token = getattr(modconf, 'token', '')
        if self.backend.token == '':
            self.getToken(getattr(modconf, 'username', ''), getattr(modconf, 'password', ''),
                          getattr(modconf, 'allowgeneratetoken', False))
        self.ref_live = {
            'host': {},
            'service': {}
        }
        self.mapping = {
            'host': {},
            'service': {}
        }
        self.hosts = {}
        self.services = {}
        self.loaded_hosts = False
        self.loaded_services = False

    # Common functions
    def do_loop_turn(self):
        """This function is called/used when you need a module with
        a loop function (and use the parameter 'external': True)
        """
        logger.info("[Backend Broker] In loop")
        time.sleep(1)

    def getToken(self, username, password, generatetoken):
        """
        Authenticate and get the token

        :param username: login name
        :type username: str
        :param password: password
        :type password: str
        :param generatetoken: if True allow generate token, otherwise not generate
        :type generatetoken: bool
        :return: None
        """
        generate = 'enabled'
        if not generatetoken:
            generate = 'disabled'
        self.backend.login(username, password, generate)

    def get_refs(self, type_data):
        """
        Get the _id in the backend for hosts and services

        :param type_data: livestate type to get: livehost or liveservice
        :type type_data: str
        :return: None
        """
        if type_data == 'livehost':
            params = {'projection': '{"name":1}'}
            content = self.backend.get_all('host', params)
            for item in content:
                self.mapping['host'][item['name']] = item['_id']
            # get all livehost
            params = {'projection': '{"host_name":1,"state":1,"state_type":1,"_realm":1}',
                      'where': '{"type":"host"}'}
            contentlh = self.backend.get_all('livestate', params)
            for item in contentlh:
                self.ref_live['host'][item['host_name']] = {
                    '_id': item['_id'],
                    '_etag': item['_etag'],
                    '_realm': item['_realm'],
                    'initial_state': item['state'],
                    'initial_state_type': item['state_type']
                }
            self.loaded_hosts = True
        elif type_data == 'liveservice':
            params = {'projection': '{"name":1}'}
            contenth = self.backend.get_all('host', params)
            hosts = {}
            for item in contenth:
                hosts[item['_id']] = item['name']
            params = {'projection': '{"name":1,"host_name":1}'}
            content = self.backend.get_all('service', params)
            for item in content:
                self.mapping['service'][''.join([hosts[item['host_name']],
                                                 item['name']])] = item['_id']
            # get all liveservice
            params = {'projection': '{"service_description":1,"state":1,"state_type":1,"_realm":1}',
                      'where': '{"type":"service"}'}
            contentls = self.backend.get_all('livestate', params)
            for item in contentls:
                self.ref_live['service'][item['service_description']] = {
                    '_id': item['_id'],
                    '_etag': item['_etag'],
                    '_realm': item['_realm'],
                    'initial_state': item['state'],
                    'initial_state_type': item['state_type']
                }
            self.loaded_services = True

    def update(self, data, obj_type):
        """
        Update livehost and liveservice

        :param data: dictionary of data from scheduler
        :type data: dict
{'fraction_non_alphanumeric': 0.12023962344886607, 'fraction_numerical': 0.006675224646983312, 'mean_word_length': 2.185064050149905, 'pattern_counts': {'":': 28, '<': 5, '<?xml version=': 0, '>': 5, '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': '961309', 'n_tokens_mistral': 3301, 'n_tokens_neox': 3205, 'n_words': 933}