#!/usr/bin/env python
# Copyright 2012-2018 CERN for the benefit of the ATLAS collaboration.
#
# 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
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Authors:
#  - Cedric Serfon, <cedric.serfon@cern.ch>, 2018
#
# PY3K COMPATIBLE

from __future__ import print_function
import sys

from rucio.db.sqla.constants import BadFilesStatus
from rucio.db.sqla.session import get_session
from rucio.core.replica import list_replicas, declare_bad_file_replicas


UNKNOWN = 3
CRITICAL = 2
WARNING = 1
OK = 0

if __name__ == '__main__':

    try:
        CNT_THRESHOLD = sys.argv[1]
    except IndexError:
        print('No threshold value defined for CNT_THRESHOLD, will use the default one : 10')
        CNT_THRESHOLD = 10
    try:
        NB_DAYS = sys.argv[2]
    except IndexError:
        print('No threshold value defined for NB_DAYS, will use the default one : 3')
        NB_DAYS = 3

    EXITVALUE = OK

    SESSION = get_session()

    QUERY = '''
SELECT
    COUNT(*),
    scope,
    name,
    atlas_rucio.id2rse(rse_id)
FROM
    atlas_rucio.bad_replicas a
WHERE
    state='S'
AND created_at>SYSDATE-%s
AND atlas_rucio.id2rse(rse_id) like '%%DATADISK'
AND EXISTS
    (
        SELECT
            1
        FROM
            atlas_rucio.replicas
        WHERE
            state='A'
        AND scope=a.scope
        AND name=a.name
        AND rse_id!=a.rse_id)
AND NOT EXISTS
    (
        SELECT
            1
        FROM
            atlas_rucio.bad_replicas
        WHERE
            scope=a.scope
        AND name=a.name
        AND rse_id=a.rse_id
        AND created_at>SYSDATE-%s
        AND state IN ('B',
                      'D',
                      'R',
                      'L'))
GROUP BY
    scope,
    name,
    rse_id
HAVING
    COUNT(*)>%s
''' % (NB_DAYS, NB_DAYS, CNT_THRESHOLD)

    BAD_SURLS = {}
    try:
        RESULT = SESSION.execute(QUERY)
        for cnt, scope, name, rse in RESULT:
            if rse not in BAD_SURLS:
                BAD_SURLS[rse] = []
            for rep in list_replicas([{'scope': scope, 'name': name}]):
                for site in rep['rses']:
                    if site == rse:
                        BAD_SURLS[rse].append(rep['rses'][site][0])
    except Exception as error:
        print(error)
        EXITVALUE = CRITICAL
        sys.exit(EXITVALUE)

    for rse in BAD_SURLS:
        print('Declaring %s bad replicas on %s : %s' % (len(BAD_SURLS[rse]), rse, BAD_SURLS[rse]))
        if len(BAD_SURLS[rse]) > 100:
            print('Too many (%s) suspicious files on %s. There might be a problem. Please investigate.' % (len(BAD_SURLS[rse]), rse))
            EXITVALUE = max(WARNING, EXITVALUE)
        else:
            try:
                declare_bad_file_replicas(pfns=BAD_SURLS[rse], reason='Lost or bad. Automatic recovery', issuer='root', status=BadFilesStatus.BAD, session=None)
            except Exception as error:
                print(error)
                EXITVALUE = CRITICAL

    sys.exit(EXITVALUE)
