#!/usr/bin/env python
# Copyright European Organization for Nuclear Research (CERN) 2013
#
# Licensed under the Apache License, Version 2.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Authors:
# - Vincent Garonne, <vincent.garonne@cern.ch>, 2013

'''
Probe to check the backlog of dids waiting for rule evaluation.
'''

import sys

from rucio.core import monitor
from rucio.db.session import get_session

# Exit statuses
UNKNOWN, OK, WARNING, CRITICAL = -1, 0, 1, 2

if __name__ == "__main__":
    try:
        session = get_session()
        result = session.execute('SELECT COUNT(*) FROM ATLAS_RUCIO.updated_dids').fetchone()[0]
        monitor.record_gauge(stat='judge.waiting_dids', value=result)
        print result
    except:
        sys.exit(UNKNOWN)
    sys.exit(OK)
