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

# Copyright (C) 2009-2014:
#   <NAME>, <EMAIL>
#   <NAME>, <EMAIL>
#   <NAME>, <EMAIL>
#   <NAME>, <EMAIL>
#   <NAME>, <EMAIL>
#   <NAME>, <EMAIL>
#
# This file is part of Shinken.
#
# Shinken 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.
#
# Shinken 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 Shinken.  If not, see <http://www.gnu.org/licenses/>.


import re
import itertools
import time


from shinken.misc.datamanager import DataManager


# Sort hosts and services by impact, states and co
def hst_srv_sort(s1, s2):
    if s1.business_impact > s2.business_impact:
        return -1
    if s2.business_impact > s1.business_impact:
        return 1

    # Ok, we compute a importance value so
    # For host, the order is UP, UNREACH, DOWN
    # For service: OK, UNKNOWN, WARNING, CRIT
    # And DOWN is before CRITICAL (potential more impact)
    tab = {'host': {0: 0, 1: 4, 2: 1},
           'service': {0: 0, 1: 2, 2: 3, 3: 1}
           }
    state1 = tab[s1.__class__.my_type].get(s1.state_id, 0)
    state2 = tab[s2.__class__.my_type].get(s2.state_id, 0)
    # ok, here, same business_impact
    # Compare warn and crit state
    if state1 > state2:
        return -1
    if state2 > state1:
        return 1

    # Ok, so by name...
    if s1.get_full_name() > s2.get_full_name():
        return 1
    else:
        return -1


# Sort hosts and services by impact, states and co
def worse_first(s1, s2):
    # Ok, we compute a importance value so
    # For host, the order is UP, UNREACH, DOWN
    # For service: OK, UNKNOWN, WARNING, CRIT
    # And DOWN is before CRITICAL (potential more impact)
    tab = {'host': {0: 0, 1: 4, 2: 1},
           'service': {0: 0, 1: 2, 2: 3, 3: 1}
           }
    state1 = tab[s1.__class__.my_type].get(s1.state_id, 0)
    state2 = tab[s2.__class__.my_type].get(s2.state_id, 0)

    # ok, here, same business_impact
    # Compare warn and crit state
    if state1 > state2:
        return -1
    if state2 > state1:
        return 1

    # Same? ok by business impact
    if s1.business_impact > s2.business_impact:
        return -1
    if s2.business_impact > s1.business_impact:
        return 1

    # Ok, so by name...
    # Ok, so by name...
    if s1.get_full_name() > s2.get_full_name():
        return -1
    else:
        return 1


# Sort hosts and services by last_state_change time
def last_state_change_earlier(s1, s2):
    # ok, here, same business_impact
    # Compare warn and crit state
    if s1.last_state_change > s2.last_state_change:
        return -1
    if s1.last_state_change < s2.last_state_change:
        return 1

    return 0


class WebUIDataManager(DataManager):

    def __init__(self, rg=None):
        super(WebUIDataManager, self).__init__()
        self.rg = rg

    @staticmethod
    def _is_related_to(item, contact):
        """ Is the item (host, service, group…) related to the contact?

            In other words, can the user see this item in the WebUI?
            .. todo:: should be a method of Item or Contact…
                      but the shinken model is incomplete,
                      so we have to do it here.

            :returns: True or False
        """
        # if the user is an admin, show all
        if contact is None or contact.is_admin:
            return True

        # May be the user is a direct contact
        if hasattr(item, 'contacts') and contact in item.contacts:
            return True
        # TODO: add a notified_contact pass

        # is it me ?
        if item.__class__.my_type == 'contact':
            return item.contact_name == contact.contact_name

        if item.__class__.my_type == 'contactgroup':
            return contact in item.members

        # May be it's a contact of a linked items
        # source problems or impacts)
        if hasattr(item, 'source_problems'):
            for s in item.source_problems:
                if contact in s.contacts:
                    return True

        if item.__class__.my_type == 'hostgroup':
            for h in item.get_hosts():
                if contact in h.contacts:
                    return True

        # May be it's a contact of a sub item ...
        if item.__class__.my_type == 'servicegroup':
            for s in item.get_services():
                if contact in s.contacts:
                    return True

        # Now impacts related maybe?
        if hasattr(item, 'impacts'):
            for imp in item.impacts:
                if contact in imp.contacts:
                    return True

        return False

    @staticmethod
    def _only_related_to(lst, contact):
        """ This function is just a wrapper to _is_related_to.

            Kept for backward compatibility reasons.

            :returns: List of elements related to the contact
        """
        return [l for l in lst if WebUIDataManager._is_related_to(l, contact)]

    ##
    # Hosts
    ##
    def get_hosts(self, user, get_impacts=True):
        """ Get a list of all hosts.<commit_msg>Fix service displaying for non admin user<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (C) 2009-2014:
#   <NAME>, <EMAIL>
#   <NAME>, <EMAIL>
#   <NAME>, <EMAIL>
#   <NAME>, <EMAIL>
#   <NAME>, <EMAIL>
#   <NAME>, <EMAIL>
#
# This file is part of Shinken.
#
# Shinken 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.
#
# Shinken 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 Shinken.  If not, see <http://www.gnu.org/licenses/>.


import re
import itertools
import time


from shinken.misc.datamanager import DataManager


# Sort hosts and services by impact, states and co
def hst_srv_sort(s1, s2):
    if s1.business_impact > s2.business_impact:
        return -1
    if s2.business_impact > s1.business_impact:
        return 1

    # Ok, we compute a importance value so
    # For host, the order is UP, UNREACH, DOWN
    # For service: OK, UNKNOWN, WARNING, CRIT
    # And DOWN is before CRITICAL (potential more impact)
    tab = {'host': {0: 0, 1: 4, 2: 1},
           'service': {0: 0, 1: 2, 2: 3, 3: 1}
           }
    state1 = tab[s1.__class__.my_type].get(s1.state_id, 0)
    state2 = tab[s2.__class__.my_type].get(s2.state_id, 0)
    # ok, here, same business_impact
    # Compare warn and crit state
    if state1 > state2:
        return -1
    if state2 > state1:
        return 1

    # Ok, so by name...
    if s1.get_full_name() > s2.get_full_name():
        return 1
    else:
        return -1


# Sort hosts and services by impact, states and co
def worse_first(s1, s2):
    # Ok, we compute a importance value so
    # For host, the order is UP, UNREACH, DOWN
    # For service: OK, UNKNOWN, WARNING, CRIT
    # And DOWN is before CRITICAL (potential more impact)
    tab = {'host': {0: 0, 1: 4, 2: 1},
           'service': {0: 0, 1: 2, 2: 3, 3: 1}
           }
    state1 = tab[s1.__class__.my_type].get(s1.state_id, 0)
    state2 = tab[s2.__class__.my_type].get(s2.state_id, 0)

    # ok, here, same business_impact
    # Compare warn and crit state
    if state1 > state2:
        return -1
    if state2 > state1:
        return 1

    # Same? ok by business impact
    if s1.business_impact > s2.business_impact:
        return -1
    if s2.business_impact > s1.business_impact:
        return 1

    # Ok, so by name...
    # Ok, so by name...
    if s1.get_full_name() > s2.get_full_name():
        return -1
    else:
        return 1


# Sort hosts and services by last_state_change time
def last_state_change_earlier(s1, s2):
    # ok, here, same business_impact
    # Compare warn and crit state
    if s1.last_state_change > s2.last_state_change:
        return -1
    if s1.last_state_change < s2.last_state_change:
        return 1

    return 0


class WebUIDataManager(DataManager):

    def __init__(self, rg=None):
        super(WebUIDataManager, self).__init__()
        self.rg = rg

    @staticmethod
    def _is_related_to(item, contact):
        """ Is the item (host, service, group…) related to the contact?

            In other words, can the user see this item in the WebUI?
            .. todo:: should be a method of Item or Contact…
                      but the shinken model is incomplete,
                      so we have to do it here.

            :returns: True or False
        """
        # if the user is an admin, show all
        if contact is None or contact.is_admin:
            return True

        # May be the user is a direct contact
        if hasattr(item, 'contacts') and contact in item.contacts:
            return True
        # TODO: add a notified_contact pass

        # is it me ?
        if item.__class__.my_type == 'contact':
            return item.contact_name == contact.contact_name

        if item.__class__.my_type == 'contactgroup':
            return contact in item.members

        # May be it's a contact of a linked items
        # source problems or impacts)
        if hasattr(item, 'source_problems'):
            for s in item.source_problems:
                if contact in s.contacts:
                    return True

        if item.__class__.my_type == 'hostgroup':
            for h in item.get_hosts():
                if contact in h.contacts:
                    return True

        # May be it's a contact of a sub item ...
        if item.__class__.my_type == 'servicegroup':
            for s in item.get_services():
                if contact in s.contacts:
                    return True

        # May be it's a contact on service host
        if item.__class__.my_type == 'service':
            if contact in item.host.contacts:
                return True

        # Now impacts related maybe?
        if hasattr(item, 'impacts'):
            for imp in item.impacts:
                if contact in imp.contacts:
                    return True

        return False

    @staticmethod
    def _only_related_to(lst, contact):
        """ This function is just a wrapper to _is_related_to.

            Kept for backward compatibility reasons.

            :returns: List of elements related to the contact
        """
        return [l for l in lst if WebUIDataManager._is_related_to(l, contact)]

    ##
    # Hosts
    ##
    def get_hosts(self, user, get_impacts=True):
        """ Get a list of all hosts.
{'fraction_non_alphanumeric': 0.08896005693443644, 'fraction_numerical': 0.017614091273018415, 'mean_word_length': 2.1288616754801004, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5861840', 'n_tokens_mistral': 3604, 'n_tokens_neox': 3365, 'n_words': 1268}