# Path: pyca/db.py
# def get_session():
#     '''Get a session for database communication. If necessary a new connection
#     to the database will be established.
# 
#     :return:  Database session
#     '''
#     if 'engine' not in globals():
#         init()
#     Session = sessionmaker(bind=engine)
#     return Session()
# 
# class RecordedEvent(Base, BaseEvent):
#     '''List of events pyca tried to record.'''
# 
#     __tablename__ = 'recorded_event'
# 
#     def __init__(self, event=None):
#         if event:
#             self.uid = event.uid
#             self.start = event.start
#             self.end = event.end
#             self.title = event.title
#             self.data = event.data
#             self.status = event.status
# 
# class UpcomingEvent(Base, BaseEvent):
#     '''List of upcoming events'''
# 
#     __tablename__ = 'upcoming_event'
# 
# class Status(Constants):
#     '''Event status definitions
#     '''
#     UPCOMING = 1
#     RECORDING = 2
#     FAILED_RECORDING = 3
#     FINISHED_RECORDING = 4
#     UPLOADING = 5
#     FAILED_UPLOADING = 6
#     FINISHED_UPLOADING = 7
#     PARTIAL_RECORDING = 8
#     PAUSED_AFTER_RECORDING = 9
# 
# Path: pyca/utils.py
# def timestamp():
#     '''Get current unix timestamp
#     '''
#     return int(datetime.now(tzutc()).timestamp())


from prometheus_client.metrics_core import GaugeMetricFamily
from prometheus_client.registry import REGISTRY
from pyca.db import get_session, RecordedEvent, UpcomingEvent, Status
from pyca.utils import timestamp

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

    def collect(self):
        '''
        Return metrics about upcoming and recorded Events
        '''
        recordings = GaugeMetricFamily(
            'pyca_events_count',
            'Count of Recorded Events with different status',
            labels=['type']
        )

        recorded_events = self.db.query(RecordedEvent)

        for status in Status.values():
            recordings.add_metric(
                [Status.str(status)],
                value=recorded_events.filter(RecordedEvent.status == status)
                                     .count()
            )

        recorded_events = self.db.query(RecordedEvent)\
                              .filter(RecordedEvent.end > timestamp())

        recorded_event_ids = [event.uid for event in recorded_events]

        upcoming_events = (self.db.query(UpcomingEvent)

# Path: pyca/db.py
# def get_session():
#     '''Get a session for database communication. If necessary a new connection
#     to the database will be established.
# 
#     :return:  Database session
#     '''
#     if 'engine' not in globals():
#         init()
#     Session = sessionmaker(bind=engine)
#     return Session()
# 
# class RecordedEvent(Base, BaseEvent):
#     '''List of events pyca tried to record.'''
# 
#     __tablename__ = 'recorded_event'
# 
#     def __init__(self, event=None):
#         if event:
#             self.uid = event.uid
#             self.start = event.start
#             self.end = event.end
#             self.title = event.title
#             self.data = event.data
#             self.status = event.status
# 
# class UpcomingEvent(Base, BaseEvent):
#     '''List of upcoming events'''
# 
#     __tablename__ = 'upcoming_event'
# 
# class Status(Constants):
#     '''Event status definitions
#     '''
#     UPCOMING = 1
#     RECORDING = 2
#     FAILED_RECORDING = 3
#     FINISHED_RECORDING = 4
#     UPLOADING = 5
#     FAILED_UPLOADING = 6
#     FINISHED_UPLOADING = 7
#     PARTIAL_RECORDING = 8
#     PAUSED_AFTER_RECORDING = 9
# 
# Path: pyca/utils.py
# def timestamp():
#     '''Get current unix timestamp
#     '''
#     return int(datetime.now(tzutc()).timestamp())


# Path: pyca/ui/recordings_collector.py
from prometheus_client.metrics_core import GaugeMetricFamily
from prometheus_client.registry import REGISTRY
from pyca.db import get_session, RecordedEvent, UpcomingEvent, Status
from pyca.utils import timestamp


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

    def collect(self):
        '''
        Return metrics about upcoming and recorded Events
        '''
        recordings = GaugeMetricFamily(
            'pyca_events_count',
            'Count of Recorded Events with different status',
            labels=['type']
        )

        recorded_events = self.db.query(RecordedEvent)

        for status in Status.values():
            recordings.add_metric(
                [Status.str(status)],
                value=recorded_events.filter(RecordedEvent.status == status)
                                     .count()
            )

        recorded_events = self.db.query(RecordedEvent)\
                              .filter(RecordedEvent.end > timestamp())

        recorded_event_ids = [event.uid for event in recorded_events]

        upcoming_events = (self.db.query(UpcomingEvent)

                               .filter(UpcomingEvent.uid