#!/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:
# - Cedric Serfon, <cedric.serfon@cern.ch>, 2015

'''
Probe to clean expired replicas on STAGING areas
'''

import sys
import traceback

from rucio.db.sqla.session import get_session

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

if __name__ == "__main__":
    query = """
            declare
                   type staging_rses is table of number(6) index by varchar(64);
                   l_staging_rses staging_rses;
            begin
            for loc in (select id from atlas_rucio.rses where rse like '%STAGING%' and staging_area=1)
            loop
                    l_staging_rses(atlas_rucio.id2rse(loc.id)) := 0;
            end loop;
            for idx in 1..40
            loop
                    for rep in (select /*+ INDEX(replicas REPLICAS_TOMBSTONE_IDX) */ scope, name, bytes, rse_id from atlas_rucio.replicas where
                    (case when tombstone is not null then rse_id END) in (select id from atlas_rucio.rses where rse like '%STAGING%' and staging_area=1) and lock_cnt=0 and rownum<10000)
                    loop
                            delete from atlas_rucio.replicas where scope=rep.scope and name=rep.name and rse_id=rep.rse_id;
                            insert into atlas_rucio.updated_rse_counters (id, rse_id, files, bytes, updated_at, created_at) values (sys_guid(), rep.rse_id, -1, -rep.bytes, sysdate, sysdate);
                            l_staging_rses(atlas_rucio.id2rse(rep.rse_id)) := l_staging_rses(atlas_rucio.id2rse(rep.rse_id)) + 1;
                    end loop;
                    commit;
            end loop;
            for loc in (select id from atlas_rucio.rses where rse like '%STAGING%')
            loop
                    dbms_output.put_line(atlas_rucio.id2rse(loc.id) || ' ' || l_staging_rses(atlas_rucio.id2rse(loc.id)));
            end loop;
            end;
            """
    try:
        session = get_session()
        session.execute(query)
    except:
        print traceback.format_exc()
        sys.exit(CRITICAL)
    sys.exit(OK)
