# Path: django_prometheus/exports.py
# def SetupPrometheusExportsFromConfig():
#     """Exports metrics so Prometheus can collect them."""
#     port = getattr(settings, "PROMETHEUS_METRICS_EXPORT_PORT", None)
#     port_range = getattr(settings, "PROMETHEUS_METRICS_EXPORT_PORT_RANGE", None)
#     addr = getattr(settings, "PROMETHEUS_METRICS_EXPORT_ADDRESS", "")
#     if port_range:
#         SetupPrometheusEndpointOnPortRange(port_range, addr)
#     elif port:
#         SetupPrometheusEndpointOnPort(port, addr)
# 
# Path: django_prometheus/migrations.py
# def ExportMigrations():
#     """Exports counts of unapplied migrations.
# 
#     This is meant to be called during app startup, ideally by
#     django_prometheus.apps.AppConfig.
#     """
# 
#     # Import MigrationExecutor lazily. MigrationExecutor checks at
#     # import time that the apps are ready, and they are not when
#     # django_prometheus is imported. ExportMigrations() should be
#     # called in AppConfig.ready(), which signals that all apps are
#     # ready.
#     from django.db.migrations.executor import MigrationExecutor
# 
#     if "default" in connections and (
#         isinstance(connections["default"], DatabaseWrapper)
#     ):
#         # This is the case where DATABASES = {} in the configuration,
#         # i.e. the user is not using any databases. Django "helpfully"
#         # adds a dummy database and then throws when you try to
#         # actually use it. So we don't do anything, because trying to
#         # export stats would crash the app on startup.
#         return
#     for alias in connections.databases:
#         executor = MigrationExecutor(connections[alias])
#         ExportMigrationsForDatabase(alias, executor)


from django.apps import AppConfig
from django.conf import settings
from django_prometheus.exports import SetupPrometheusExportsFromConfig
from django_prometheus.migrations import ExportMigrations
import django_prometheus



class DjangoPrometheusConfig(AppConfig):
    name = django_prometheus.__name__
    verbose_name = "Django-Prometheus"

    def ready(self):
        """Initializes the Prometheus exports if they are enabled in the config.

        Note that this is called even for other management commands
        than `runserver`. As such, it is possible to scrape the
        metrics of a running `manage.py test` or of another command,
        which shouldn't be done for real monitoring (since these jobs
        are usually short-lived), but can be useful for debugging.
        """

# Path: django_prometheus/exports.py
# def SetupPrometheusExportsFromConfig():
#     """Exports metrics so Prometheus can collect them."""
#     port = getattr(settings, "PROMETHEUS_METRICS_EXPORT_PORT", None)
#     port_range = getattr(settings, "PROMETHEUS_METRICS_EXPORT_PORT_RANGE", None)
#     addr = getattr(settings, "PROMETHEUS_METRICS_EXPORT_ADDRESS", "")
#     if port_range:
#         SetupPrometheusEndpointOnPortRange(port_range, addr)
#     elif port:
#         SetupPrometheusEndpointOnPort(port, addr)
# 
# Path: django_prometheus/migrations.py
# def ExportMigrations():
#     """Exports counts of unapplied migrations.
# 
#     This is meant to be called during app startup, ideally by
#     django_prometheus.apps.AppConfig.
#     """
# 
#     # Import MigrationExecutor lazily. MigrationExecutor checks at
#     # import time that the apps are ready, and they are not when
#     # django_prometheus is imported. ExportMigrations() should be
#     # called in AppConfig.ready(), which signals that all apps are
#     # ready.
#     from django.db.migrations.executor import MigrationExecutor
# 
#     if "default" in connections and (
#         isinstance(connections["default"], DatabaseWrapper)
#     ):
#         # This is the case where DATABASES = {} in the configuration,
#         # i.e. the user is not using any databases. Django "helpfully"
#         # adds a dummy database and then throws when you try to
#         # actually use it. So we don't do anything, because trying to
#         # export stats would crash the app on startup.
#         return
#     for alias in connections.databases:
#         executor = MigrationExecutor(connections[alias])
#         ExportMigrationsForDatabase(alias, executor)


# Path: django_prometheus/apps.py
from django.apps import AppConfig
from django.conf import settings
from django_prometheus.exports import SetupPrometheusExportsFromConfig
from django_prometheus.migrations import ExportMigrations
import django_prometheus




class DjangoPrometheusConfig(AppConfig):
    name = django_prometheus.__name__
    verbose_name = "Django-Prometheus"

    def ready(self):
        """Initializes the Prometheus exports if they are enabled in the config.

        Note that this is called even for other management commands
        than `runserver`. As such, it is possible to scrape the
        metrics of a running `manage.py test` or of another command,
        which shouldn't be done for real monitoring (since these jobs
        are usually short-lived), but can be useful for debugging.
        """

        SetupPrometheusExportsFromConfig()