<commit_before>import os
import sys
import datetime
import base64
import hmac
import hashlib
import bcrypt

from uuid import uuid4
from six import string_types
from pymongo import MongoClient, ASCENDING, TEXT, ReturnDocument

try:
    from urllib.parse import urlparse
except ImportError:
    from urlparse import urlparse

from alerta.app import app, severity_code, status_code
from alerta.alert import AlertDocument
from alerta.heartbeat import HeartbeatDocument


LOG = app.logger


class Mongo(object):

    def __init__(self):

        self.connect()

    def connect(self):

        mongo_uri = (os.environ.get('MONGO_URI', None) or
                     os.environ.get('MONGOHQ_URL', None) or
                     os.environ.get('MONGOLAB_URI', None))
        if mongo_uri:
            try:
                self._client = MongoClient(mongo_uri, connect=False)
            except Exception as e:
                LOG.error('MongoDB Client connection error - %s : %s', mongo_uri, e)
                sys.exit(1)

            uri = urlparse(mongo_uri)

            app.config['MONGO_HOST'] = uri.hostname
            app.config['MONGO_PORT'] = uri.port
            app.config['MONGO_DATABASE'] = uri.path.lstrip('/')
            app.config['MONGO_REPLSET'] = None

            if uri.username:
                app.config['MONGO_USERNAME'] = uri.username
                app.config['MONGO_PASSWORD'] = uri.password

        elif 'MONGO_PORT' in os.environ and 'tcp://' in os.environ['MONGO_PORT']:  # Docker
            host, port = os.environ['MONGO_PORT'][6:].split(':')
            try:
                self._client = MongoClient(host, int(port), connect=False)
            except Exception as e:
                LOG.error('MongoDB Client connection error - %s : %s', os.environ['MONGO_PORT'], e)
                sys.exit(1)

        elif not app.config['MONGO_REPLSET']:
            try:
                self._client = MongoClient(app.config['MONGO_HOST'], app.config['MONGO_PORT'], connect=False)
            except Exception as e:
                LOG.error('MongoDB Client connection error - %s:%s : %s', app.config['MONGO_HOST'], app.config['MONGO_PORT'], e)
                sys.exit(1)
            LOG.debug('Connected to mongodb://%s:%s/%s', app.config['MONGO_HOST'], app.config['MONGO_PORT'], app.config['MONGO_DATABASE'])

        else:
            try:
                self._client = MongoClient(app.config['MONGO_HOST'], app.config['MONGO_PORT'], replicaSet=app.config['MONGO_REPLSET'], connect=False)
            except Exception as e:
                LOG.error('MongoDB Client ReplicaSet connection error - %s:%s (replicaSet=%s) : %s',
                          app.config['MONGO_HOST'], app.config['MONGO_PORT'], app.config['MONGO_REPLSET'], e)
                sys.exit(1)
            LOG.debug('Connected to mongodb://%s:%s/%s?replicaSet=%s', app.config['MONGO_HOST'],
                      app.config['MONGO_PORT'], app.config['MONGO_DATABASE'], app.config['MONGO_REPLSET'])

        self._db = self._client[app.config['MONGO_DATABASE']]

        if app.config['MONGO_PASSWORD']:
            try:
                self._db.authenticate(app.config['MONGO_USERNAME'], password=app.config['MONGO_PASSWORD'])
            except Exception as e:
                LOG.error('MongoDB authentication failed: %s', e)
                sys.exit(1)

        # self._create_indexes()

    def _create_indexes(self):

        self._db.alerts.create_index([('environment', ASCENDING), ('resource', ASCENDING), ('event', ASCENDING), ('severity', ASCENDING)])
        self._db.alerts.create_index([('status', ASCENDING), ('lastReceiveTime', ASCENDING)])
        self._db.alerts.create_index([('status', ASCENDING), ('lastReceiveTime', ASCENDING), ('environment', ASCENDING)])
        self._db.alerts.create_index([('status', ASCENDING), ('service', ASCENDING)])
        self._db.alerts.create_index([('status', ASCENDING), ('environment', ASCENDING)])
        self._db.alerts.create_index([('status', ASCENDING), ('expireTime', ASCENDING)])
        self._db.alerts.create_index([('status', ASCENDING)])

        major, minor, patch = [int(v) for v in self.get_version().split('.')]
        if (major == 2 and minor > 4) or major >= 3:
            self._db.alerts.create_index([('$**', TEXT)])

    def get_db(self):

        return self._db

    def get_info(self):

        return self._db.name
<commit_msg>Create unique compound index to prevent multiple alerts<commit_after>import os
import sys
import datetime
import base64
import hmac
import hashlib
import bcrypt

from uuid import uuid4
from six import string_types
from pymongo import MongoClient, ASCENDING, TEXT, ReturnDocument

try:
    from urllib.parse import urlparse
except ImportError:
    from urlparse import urlparse

from alerta.app import app, severity_code, status_code
from alerta.alert import AlertDocument
from alerta.heartbeat import HeartbeatDocument


LOG = app.logger


class Mongo(object):

    def __init__(self):

        self.connect()

    def connect(self):

        mongo_uri = (os.environ.get('MONGO_URI', None) or
                     os.environ.get('MONGOHQ_URL', None) or
                     os.environ.get('MONGOLAB_URI', None))
        if mongo_uri:
            try:
                self._client = MongoClient(mongo_uri, connect=False)
            except Exception as e:
                LOG.error('MongoDB Client connection error - %s : %s', mongo_uri, e)
                sys.exit(1)

            uri = urlparse(mongo_uri)

            app.config['MONGO_HOST'] = uri.hostname
            app.config['MONGO_PORT'] = uri.port
            app.config['MONGO_DATABASE'] = uri.path.lstrip('/')
            app.config['MONGO_REPLSET'] = None

            if uri.username:
                app.config['MONGO_USERNAME'] = uri.username
                app.config['MONGO_PASSWORD'] = <PASSWORD>

        elif 'MONGO_PORT' in os.environ and 'tcp://' in os.environ['MONGO_PORT']:  # Docker
            host, port = os.environ['MONGO_PORT'][6:].split(':')
            try:
                self._client = MongoClient(host, int(port), connect=False)
            except Exception as e:
                LOG.error('MongoDB Client connection error - %s : %s', os.environ['MONGO_PORT'], e)
                sys.exit(1)

        elif not app.config['MONGO_REPLSET']:
            try:
                self._client = MongoClient(app.config['MONGO_HOST'], app.config['MONGO_PORT'], connect=False)
            except Exception as e:
                LOG.error('MongoDB Client connection error - %s:%s : %s', app.config['MONGO_HOST'], app.config['MONGO_PORT'], e)
                sys.exit(1)
            LOG.debug('Connected to mongodb://%s:%s/%s', app.config['MONGO_HOST'], app.config['MONGO_PORT'], app.config['MONGO_DATABASE'])

        else:
            try:
                self._client = MongoClient(app.config['MONGO_HOST'], app.config['MONGO_PORT'], replicaSet=app.config['MONGO_REPLSET'], connect=False)
            except Exception as e:
                LOG.error('MongoDB Client ReplicaSet connection error - %s:%s (replicaSet=%s) : %s',
                          app.config['MONGO_HOST'], app.config['MONGO_PORT'], app.config['MONGO_REPLSET'], e)
                sys.exit(1)
            LOG.debug('Connected to mongodb://%s:%s/%s?replicaSet=%s', app.config['MONGO_HOST'],
                      app.config['MONGO_PORT'], app.config['MONGO_DATABASE'], app.config['MONGO_REPLSET'])

        self._db = self._client[app.config['MONGO_DATABASE']]

        if app.config['MONGO_PASSWORD']:
            try:
                self._db.authenticate(app.config['MONGO_USERNAME'], password=app.config['MONGO_PASSWORD'])
            except Exception as e:
                LOG.error('MongoDB authentication failed: %s', e)
                sys.exit(1)

        self._create_indexes()

    def _create_indexes(self):

        self._db.alerts.create_index([('environment', ASCENDING), ('resource', ASCENDING), ('event', ASCENDING)], unique=True)
        self._db.alerts.create_index([('$**', TEXT)])

    def get_db(self):

        return self._db

    def get_info(self):

        return self._db.name

{'fraction_non_alphanumeric': 0.1336275591516489, 'fraction_numerical': 0.0025744759102611253, 'mean_word_length': 2.4774083546462062, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4588048', 'n_tokens_mistral': 2640, 'n_tokens_neox': 2459, 'n_words': 515}