#!/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 updated dids.
'''
import sys
import traceback

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

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

if __name__ == "__main__":
    try:
        session = get_session()
        result = session.execute('select count(1) from atlas_rucio.updated_dids').fetchone()[0]
        monitor.record_gauge(stat='judge.updated_dids', value=result)
        # created_at, count, max, min, avg, stdev = 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
        # result = session.execute('select * from atlas_rucio.concurency_stats where created_at > sysdate - 1/1440')
        # for row in result:
        #   created_at, count, max, min, avg, stdev = row
        # monitor.record_gauge(stat='judge.updated_dids_per_min.count', value=count or 0)
        # monitor.record_gauge(stat='judge.updated_dids_per_min.max', value=max or 0)
        # monitor.record_gauge(stat='judge.updated_dids_per_min.min', value=min or 0)
        # monitor.record_gauge(stat='judge.updated_dids_per_min.avg', value=avg or 0)
        # monitor.record_gauge(stat='judge.updated_dids_per_min.stdev', value=stdev or 0)
        # print created_at, count, max, min, avg, stdev
    except:
        print traceback.format_exc()
        sys.exit(UNKNOWN)
    sys.exit(OK)
