<commit_before># Copyright (C) 2014 Linaro Ltd.
#
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

"""Make sure indexes are created at startup."""

import pymongo

import models


def ensure_indexes(database):
    """Ensure that mongodb indexes exists, if not create them.

    This should be called at server startup.

    :param database: The database connection.
    """
    _ensure_job_indexes(database)
    _ensure_boot_indexes(database)
    _ensure_build_indexes(database)
    _ensure_token_indexes(database)
    _ensure_lab_indexes(database)
    _ensure_bisect_indexes(database)
    _ensure_error_logs_indexes(database)


def _ensure_job_indexes(database):
    """Ensure indexes exists for the 'job' collection.

    :param database: The database connection.
    """
    collection = database[models.JOB_COLLECTION]
    collection.ensure_index(
        [
            (models.CREATED_KEY, pymongo.DESCENDING)
        ],
        background=True
    )
    collection.ensure_index(
        [
            (models.JOB_KEY, pymongo.ASCENDING)
        ],
        background=True
    )
    collection.ensure_index(
        [
            (models.KERNEL_KEY, pymongo.ASCENDING)
        ],
        background=True
    )


def _ensure_boot_indexes(database):
    """Ensure indexes exists for the 'boot' collection.

    :param database: The database connection.
    """
    collection = database[models.BOOT_COLLECTION]
    collection.ensure_index(
        [
            (models.CREATED_KEY, pymongo.DESCENDING)
        ],
        background=True
    )
    collection.ensure_index(
        [
            (models.STATUS_KEY, pymongo.ASCENDING),
            (models.CREATED_KEY, pymongo.DESCENDING)
        ],
        background=True
    )
    collection.ensure_index(
        [
            (models.STATUS_KEY, pymongo.ASCENDING),
            (models.JOB_KEY, pymongo.ASCENDING),
            (models.KERNEL_KEY, pymongo.ASCENDING)
        ],
        background=True
    )
    collection.ensure_index(
        [
            (models.JOB_KEY, pymongo.ASCENDING),
            (models.KERNEL_KEY, pymongo.ASCENDING)
        ],
        background=True
    )


def _ensure_build_indexes(database):
    """Ensure indexes exists for the 'build' collection.

    :param database: The database connection.
    """
    collection = database[models.BUILD_COLLECTION]

    collection.ensure_index(
        [
            (models.CREATED_KEY, pymongo.DESCENDING)
        ],
        background=True
    )
    collection.ensure_index(
        [
            (models.STATUS_KEY, pymongo.ASCENDING),
            (models.CREATED_KEY, pymongo.DESCENDING)
        ],
        background=True
    )
    collection.ensure_index(
        [
            (models.STATUS_KEY, pymongo.ASCENDING),
            (models.JOB_KEY, pymongo.ASCENDING),
            (models.KERNEL_KEY, pymongo.ASCENDING)
        ],
        background=True
    )
    collection.ensure_index(
        [
            (models.JOB_KEY, pymongo.ASCENDING)
        ],
        background=True
    )
    collection.ensure_index(
        [
            (models.KERNEL_KEY, pymongo.ASCENDING)
        ],
        background=True
    )


def _ensure_token_indexes(database):
    """Ensure indexes exists for the 'token' collection.

    :param database: The database connection.
    """
    collection = database[models.TOKEN_COLLECTION]

    collection.ensure_index(
        [(models.TOKEN_KEY, pymongo.DESCENDING)], background=True
    )


def _ensure_lab_indexes(database):
    """Ensure indexes exists for the 'lab' collection.

    :param database: The database connection.
    """
    collection = database[models.LAB_COLLECTION]

    collection.ensure_index(
        [
            (models.NAME_KEY, pymongo.ASCENDING),
            (models.TOKEN_KEY, pymongo.ASCENDING)
        ],
        background=True
    )


def _ensure_bisect_indexes(database):
    """Ensure indexes exists for the 'bisect' collection.

    :param database: The database connection.
    """
    collection = database[models.BISECT_COLLECTION]

    collection.ensure_index(
        [(models.NAME_KEY, pymongo.DESCENDING)],
        background=True
    )


def _ensure_error_logs_indexes(database):
    """Ensure indexes exists for the 'error_logs' collection.

    :param database: The database connection.
    """
    collection = database[models.ERROR_LOGS_COLLECTION]
    collection.ensure_index(
        [(models.BUILD_ID_KEY, pymongo.DESCENDING)],
        background=True
    )
<commit_msg>Add index for the daily stats collection.<commit_after># Copyright (C) 2014 Linaro Ltd.
#
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

"""Make sure indexes are created at startup."""

import pymongo

import models


def ensure_indexes(database):
    """Ensure that mongodb indexes exists, if not create them.

    This should be called at server startup.

    :param database: The database connection.
    """
    _ensure_job_indexes(database)
    _ensure_boot_indexes(database)
    _ensure_build_indexes(database)
    _ensure_token_indexes(database)
    _ensure_lab_indexes(database)
    _ensure_bisect_indexes(database)
    _ensure_error_logs_indexes(database)
    _ensure_stats_indexes(database)


def _ensure_job_indexes(database):
    """Ensure indexes exists for the 'job' collection.

    :param database: The database connection.
    """
    collection = database[models.JOB_COLLECTION]
    collection.ensure_index(
        [
            (models.CREATED_KEY, pymongo.DESCENDING)
        ],
        background=True
    )
    collection.ensure_index(
        [
            (models.JOB_KEY, pymongo.ASCENDING)
        ],
        background=True
    )
    collection.ensure_index(
        [
            (models.KERNEL_KEY, pymongo.ASCENDING)
        ],
        background=True
    )


def _ensure_boot_indexes(database):
    """Ensure indexes exists for the 'boot' collection.

    :param database: The database connection.
    """
    collection = database[models.BOOT_COLLECTION]
    collection.ensure_index(
        [
            (models.CREATED_KEY, pymongo.DESCENDING)
        ],
        background=True
    )
    collection.ensure_index(
        [
            (models.STATUS_KEY, pymongo.ASCENDING),
            (models.CREATED_KEY, pymongo.DESCENDING)
        ],
        background=True
    )
    collection.ensure_index(
        [
            (models.STATUS_KEY, pymongo.ASCENDING),
            (models.JOB_KEY, pymongo.ASCENDING),
            (models.KERNEL_KEY, pymongo.ASCENDING)
        ],
        background=True
    )
    collection.ensure_index(
        [
            (models.JOB_KEY, pymongo.ASCENDING),
            (models.KERNEL_KEY, pymongo.ASCENDING)
        ],
        background=True
    )


def _ensure_build_indexes(database):
    """Ensure indexes exists for the 'build' collection.

    :param database: The database connection.
    """
    collection = database[models.BUILD_COLLECTION]

    collection.ensure_index(
        [
            (models.CREATED_KEY, pymongo.DESCENDING)
        ],
        background=True
    )
    collection.ensure_index(
        [
            (models.STATUS_KEY, pymongo.ASCENDING),
            (models.CREATED_KEY, pymongo.DESCENDING)
        ],
        background=True
    )
    collection.ensure_index(
        [
            (models.STATUS_KEY, pymongo.ASCENDING),
            (models.JOB_KEY, pymongo.ASCENDING),
            (models.KERNEL_KEY, pymongo.ASCENDING)
        ],
        background=True
    )
    collection.ensure_index(
        [
            (models.JOB_KEY, pymongo.ASCENDING)
        ],
        background=True
    )
    collection.ensure_index(
        [
            (models.KERNEL_KEY, pymongo.ASCENDING)
        ],
        background=True
    )


def _ensure_token_indexes(database):
    """Ensure indexes exists for the 'token' collection.

    :param database: The database connection.
    """
    collection = database[models.TOKEN_COLLECTION]

    collection.ensure_index(
        [(models.TOKEN_KEY, pymongo.DESCENDING)], background=True
    )


def _ensure_lab_indexes(database):
    """Ensure indexes exists for the 'lab' collection.

    :param database: The database connection.
    """
    collection = database[models.LAB_COLLECTION]

    collection.ensure_index(
        [
            (models.NAME_KEY, pymongo.ASCENDING),
            (models.TOKEN_KEY, pymongo.ASCENDING)
        ],
        background=True
    )


def _ensure_bisect_indexes(database):
    """Ensure indexes exists for the 'bisect' collection.

    :param database: The database connection.
    """
    collection = database[models.BISECT_COLLECTION]

    collection.ensure_index(
        [(models.NAME_KEY, pymongo.DESCENDING)],
        background=True
    )


def _ensure_error_logs_indexes(database):
    """Ensure indexes exists for the 'error_logs' collection.

    :param database: The database connection.
    """
    collection = database[models.ERROR_LOGS_COLLECTION]
    collection.ensure_index(
        [(models.BUILD_ID_KEY, pymongo.DESCENDING)],
        background=True
    )


def _ensure_stats_indexes(database):
    """Ensure indexes exists for the 'daily_stats' collection.

    :param database: The database connection.
    """
    collection = database[models.DAILY_STATS_COLLECTION]
    collection.ensure_index(
        [(models.CREATED_KEY, pymongo.DESCENDING)], background=True)

{'fraction_non_alphanumeric': 0.08870815207111783, 'fraction_numerical': 0.0009457159069415548, 'mean_word_length': 2.5944935418082937, 'pattern_counts': {'":': 0, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1041812', 'n_tokens_mistral': 3154, 'n_tokens_neox': 3083, 'n_words': 709}