# Path: pyca/db.py
# class ServiceStatus(Constants):
#     '''Service status type definitions
#     '''
#     STOPPED = 1
#     IDLE = 2
#     BUSY = 3
# 
# class Service(Constants):
#     '''Service type definitions
#     '''
#     AGENTSTATE = 1
#     CAPTURE = 2
#     INGEST = 3
#     SCHEDULE = 4
# 
# Path: pyca/utils.py
# @db.with_session
# def get_service_status(dbs, service):
#     '''Update the status of a particular service in the database.
#     '''
#     srvs = dbs.query(db.ServiceStates).filter(db.ServiceStates.type == service)
# 
#     if srvs.count():
#         return srvs[0].status
# 
#     return db.ServiceStatus.STOPPED


from prometheus_client.metrics_core import StateSetMetricFamily
from prometheus_client.registry import REGISTRY
from pyca.db import ServiceStatus, Service
from pyca.utils import get_service_status



class ProcessStatusCollector(object):

    def __init__(self, registry=REGISTRY):
        registry.register(self)

    def collect(self):
        service_states = StateSetMetricFamily(
            'pyca_service_state_info',
            'Service State of pyCA processes',
            labels=['service', 'state']
        )

        for service in Service.values():
            service_states.add_metric(
                [Service.str(service)],
                self.get_state_dict(get_service_status(service))
            )

        yield service_states

    @staticmethod
    def get_state_dict(state: str):
        return {ServiceStatus.str(s): s == state

# Path: pyca/db.py
# class ServiceStatus(Constants):
#     '''Service status type definitions
#     '''
#     STOPPED = 1
#     IDLE = 2
#     BUSY = 3
# 
# class Service(Constants):
#     '''Service type definitions
#     '''
#     AGENTSTATE = 1
#     CAPTURE = 2
#     INGEST = 3
#     SCHEDULE = 4
# 
# Path: pyca/utils.py
# @db.with_session
# def get_service_status(dbs, service):
#     '''Update the status of a particular service in the database.
#     '''
#     srvs = dbs.query(db.ServiceStates).filter(db.ServiceStates.type == service)
# 
#     if srvs.count():
#         return srvs[0].status
# 
#     return db.ServiceStatus.STOPPED


# Path: pyca/ui/process_status_collector.py
from prometheus_client.metrics_core import StateSetMetricFamily
from prometheus_client.registry import REGISTRY
from pyca.db import ServiceStatus, Service
from pyca.utils import get_service_status




class ProcessStatusCollector(object):

    def __init__(self, registry=REGISTRY):
        registry.register(self)

    def collect(self):
        service_states = StateSetMetricFamily(
            'pyca_service_state_info',
            'Service State of pyCA processes',
            labels=['service', 'state']
        )

        for service in Service.values():
            service_states.add_metric(
                [Service.str(service)],
                self.get_state_dict(get_service_status(service))
            )

        yield service_states

    @staticmethod
    def get_state_dict(state: str):
        return {ServiceStatus.str(s): s == state

                for s in ServiceStatus.values()}