Пример #1
0
    def ready(self):
        from varapp.common import manage_dbs, utils, db_utils
        from varapp.common.versioning import add_versions
        from varapp.stats.stats_service import stats_service
        from varapp.variants.genotypes_service import genotypes_service

        # Check that there are tables in the users_db,
        # because this code is also run when manage.py is used,
        # for instance to generate the tables.
        # It needs more that 1 table, which could be only django_migrations.
        user_db_ready = db_utils.connection_has_tables('default', 5)

        # Manage.py must work without the following to execute.
        #print(user_db_ready)
        #print(''.join(sys.argv))
        if user_db_ready and "migrate" not in sys.argv:
            # At startup, fill settings.DATABASES with what is in VariantsDb.
            # Do not add any new db here, as unlike deactivation, inserts
            # are not idempotent and this code could be executed several times.
            # Return the valid databases added to connections,
            # since they need to be in there to be read for vcf_header, stats etc.
            added_connections = manage_dbs.copy_VariantsDb_to_settings()

            # Check that the Redis service is running.
            # It is necessary for stats and genotypes cache.
            redis_ready = utils.check_redis_connection()
            if redis_ready:
                # Fill the stats cache
                if settings.WARMUP_STATS_CACHE:
                    for dbname in added_connections:
                        stats_service(dbname)

                # Fill the genotypes cache
                if settings.WARMUP_GENOTYPES_CACHE:
                    for dbname in added_connections:
                        genotypes_service(dbname)

                # Update the *annotation* table with versions of all programs used,
                # i.e. Gemini, VEP, their dbs, etc.
                for dbname in added_connections:
                    add_versions(dbname)
            else:
                logger.warning(
                    "(!) Could not connect to Redis. Make sure Redis is installed, "
                    "is up and running (try `redis-cli ping`) "
                    "and serves at 127.0.0.1:6379 (or whatever is defined in settings)."
                )
                return 2

            return 0

        else:
            logger.warning("(!) Users db is not ready or it is migration.")
            return 1
Пример #2
0
 def __init__(self, request, db):
     """Extract filters etc. information from http request."""
     self.db = db
     self.sort = sort_from_request(request)
     self.pg = pagination_from_request(request)
     self.ss = samples_selection_from_request(request, db)
     self.fc = variant_filters_from_request(request, db, self.ss)
     self.stats = stats_service(db)
Пример #3
0
 def __init__(self, request, db):
     """Extract filters etc. information from http request."""
     self.db = db
     self.sort = sort_from_request(request)
     self.pg = pagination_from_request(request)
     self.ss = samples_selection_from_request(request, db)
     self.fc = variant_filters_from_request(request, db, self.ss)
     self.stats = stats_service(db)
Пример #4
0
def stats(request, db, user=None):
    """Return a JSON with stats over the whole db (to query only once at startup)."""
    stat = stats_service(db).get_global_stats()
    return JsonResponse(stat.expose(), safe=False)
Пример #5
0
def stats(request, db, user=None):
    """Return a JSON with stats over the whole db (to query only once at startup)."""
    stat = stats_service(db).get_global_stats()
    return JsonResponse(stat.expose(), safe=False)
Пример #6
0
    def ready(self):
        from varapp.common import manage_dbs, utils, db_utils
        from varapp.common.versioning import add_versions
        from varapp.stats.stats_service import stats_service
        from varapp.variants.genotypes_service import genotypes_service

        # Check that there are tables in the users_db,
        # because this code is also run when manage.py is used,
        # for instance to generate the tables.
        # It needs more that 1 table, which could be only django_migrations.
        mysql_connection_attempts = 0
        connected_to_mysql = False
        user_db_ready = False
        error = None
        while mysql_connection_attempts < 50 and not connected_to_mysql:
            try:
                mysql_connection_attempts += 1
                user_db_ready = db_utils.connection_has_tables('default', 5)
                connected_to_mysql = True
                logger.info("Connected to MySQL")
            except django.db.utils.OperationalError as err:
                error = str(err)
                if mysql_connection_attempts == 1:
                    logger.error(error)
                logger.warning(
                    "(!) Could not connect to users db. Retrying in 1 second... ({})"
                    .format(mysql_connection_attempts))
                time.sleep(0.5)

        if not connected_to_mysql:
            raise ConnectionError(
                "Could not connect to users db. Check your MySQL connection and settings. Error message: {}"
                .format(error))

        # Manage.py must work without the following to execute.
        if user_db_ready and "migrat" not in ''.join(sys.argv):
            # At startup, fill settings.DATABASES with what is in VariantsDb.
            # Do not add any new db here, as unlike deactivation, inserts
            # are not idempotent and this code could be executed several times.
            # Return the valid databases added to connections,
            # since they need to be in there to be read for vcf_header, stats etc.
            added_connections = manage_dbs.copy_VariantsDb_to_settings()

            # Check that the Redis service is running.
            # It is necessary for stats and genotypes cache.
            redis_ready = utils.check_redis_connection()
            if redis_ready:
                # Fill the stats cache
                if settings.WARMUP_STATS_CACHE:
                    for dbname in added_connections:
                        stats_service(dbname)

                # Fill the genotypes cache
                if settings.WARMUP_GENOTYPES_CACHE:
                    for dbname in added_connections:
                        genotypes_service(dbname)

                # Update the *annotation* table with versions of all programs used,
                # i.e. Gemini, VEP, their dbs, etc.
                for dbname in added_connections:
                    add_versions(dbname)
            else:
                logger.warning(
                    "(!) Could not connect to Redis. Make sure Redis is installed, "
                    "is up and running (try `redis-cli ping`) "
                    "and serves at '{}').".format(
                        settings.CACHES['redis']['LOCATION']))
                return 2

            return 0

        else:
            logger.warning("(!) Users db has no tables.")
            return 1
Пример #7
0
 def test_stats_service(self):
     """stats_service(db) creates a GlobalStatsService"""
     sts = stats_service(db='test')
     self.assertIsInstance(sts, GlobalStatsService)
Пример #8
0
 def test_stats_service(self):
     """stats_service(db) creates a GlobalStatsService"""
     sts = stats_service(db='test')
     self.assertIsInstance(sts, GlobalStatsService)
Пример #9
0
    def ready(self):
        from varapp.common import manage_dbs, utils, db_utils
        from varapp.common.versioning import add_versions
        from varapp.stats.stats_service import stats_service
        from varapp.variants.genotypes_service import genotypes_service

        # Check that there are tables in the users_db,
        # because this code is also run when manage.py is used,
        # for instance to generate the tables.
        # It needs more that 1 table, which could be only django_migrations.
        mysql_connection_attempts = 0
        connected_to_mysql = False
        user_db_ready = False
        error = None
        while mysql_connection_attempts < 50 and not connected_to_mysql:
            try:
                mysql_connection_attempts += 1
                user_db_ready = db_utils.connection_has_tables('default', 5)
                connected_to_mysql = True
                logger.info("Connected to MySQL")
            except django.db.utils.OperationalError as err:
                error = str(err)
                if mysql_connection_attempts == 1:
                    logger.error(error)
                logger.warning("(!) Could not connect to users db. Retrying in 1 second... ({})".format(mysql_connection_attempts))
                time.sleep(0.5)

        if not connected_to_mysql:
            raise ConnectionError("Could not connect to users db. Check your MySQL connection and settings. Error message: {}".format(error))

        # Manage.py must work without the following to execute.
        if user_db_ready and "migrat" not in ''.join(sys.argv):
            # At startup, fill settings.DATABASES with what is in VariantsDb.
            # Do not add any new db here, as unlike deactivation, inserts
            # are not idempotent and this code could be executed several times.
            # Return the valid databases added to connections,
            # since they need to be in there to be read for vcf_header, stats etc.
            added_connections = manage_dbs.copy_VariantsDb_to_settings()

            # Check that the Redis service is running.
            # It is necessary for stats and genotypes cache.
            redis_ready = utils.check_redis_connection()
            if redis_ready:
                # Fill the stats cache
                if settings.WARMUP_STATS_CACHE:
                    for dbname in added_connections:
                        stats_service(dbname)

                # Fill the genotypes cache
                if settings.WARMUP_GENOTYPES_CACHE:
                    for dbname in added_connections:
                        genotypes_service(dbname)

                # Update the *annotation* table with versions of all programs used,
                # i.e. Gemini, VEP, their dbs, etc.
                for dbname in added_connections:
                    add_versions(dbname)
            else:
                logger.warning("(!) Could not connect to Redis. Make sure Redis is installed, "
                                "is up and running (try `redis-cli ping`) "
                                "and serves at '{}').".format(settings.CACHES['redis']['LOCATION']))
                return 2


            return 0

        else:
            logger.warning("(!) Users db has no tables.")
            return 1