#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2015-2021 CERN
#
# 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:
# - Thomas Beermann <thomas.beermann@cern.ch>, 2015-2017
# - Vincent Garonne <vincent.garonne@cern.ch>, 2018
# - Hannes Hansen <hannes.jakob.hansen@cern.ch>, 2018
# - Benedikt Ziemons <benedikt.ziemons@cern.ch>, 2020
# - David Población Criado <david.poblacion.criado@cern.ch>, 2021
# - Joel Dierkes <joel.dierkes@cern.ch>, 2021

"""
C-3PO is a dynamic data placement daemon.
"""

import argparse
import signal

from rucio.common.utils import StoreAndDeprecateWarningAction
from rucio.daemons.c3po.c3po import run, stop


def get_parser():
    """
    Returns the argparse parser.
    """
    parser = argparse.ArgumentParser(description="The C3PO daemon is responsible for dynamic data placement.")
    parser.add_argument("--run-once", action="store_true", default=False, help='One iteration only')
    parser.add_argument("--threads", action="store", default=1, type=int, help='Concurrency control: number of threads')
    parser.add_argument("--only-workload", action="store_true", default=False, help='Only run the workload collector')
    parser.add_argument("--dry_run", "--dry-run", new_option_string="--dry-run", action=StoreAndDeprecateWarningAction, default=False, help='Do not create any rules')
    parser.add_argument("--sampling", action="store_true", default=False, help='In the end flip a to decide to create a rule or not')
    parser.add_argument("--algorithms", action="store", default="t2_free_space_only_pop_with_network", type=str, help='The placement algorithm or, if in dry_run, a comma separated list of algorithms')
    parser.add_argument("--datatypes", action="store", default="NTUP,DAOD", type=str, help='Comma separated list of datatype that should trigger the placement')
    parser.add_argument("--dest_rse_expr", "--dest-rse-expr", new_option_string="--dest-rse-expr", action=StoreAndDeprecateWarningAction, default="type=DATADISK", type=str, help='RSE expression defining the allowed destination RSEs')
    parser.add_argument("--max_bytes_hour", "--max-bytes-hour", new_option_string="--max-bytes-hour", action=StoreAndDeprecateWarningAction, default=100000000000000, type=int, help='Max number of bytes that c3po is allow to replicate per hour')
    parser.add_argument("--max_files_hour", "--max-files-hour", new_option_string="--max-files-hour", action=StoreAndDeprecateWarningAction, default=100000, type=int, help='Max number of files that c3po is allow to replicate per hour')
    parser.add_argument("--max_bytes_hour_rse", "--max-bytes-hour-rse", new_option_string="--max-bytes-hour-rse", action=StoreAndDeprecateWarningAction, default=50000000000000, type=int, help='Max number of bytes that c3po is allow to replicate per hour per rse')  # NOQA: E501
    parser.add_argument("--max_files_hour_rse", "--max-files-hour-rse", new_option_string="--max-files-hour-rse", action=StoreAndDeprecateWarningAction, default=10000, type=int, help='Max number of files that c3po is allow to replicate per hour prse_rse')
    parser.add_argument("--min_popularity", "--min-popularity", new_option_string="--min-popularity", action=StoreAndDeprecateWarningAction, default=8, type=int, help='Min number of popularity accesses for a DID in the last 7 days to trigger')
    parser.add_argument("--min_recent_requests", "--min-recent-requests", new_option_string="--min-recent-requests", action=StoreAndDeprecateWarningAction, default=5, type=int, help='Min number of times a DID has to be requested in the last hour to trigger')  # NOQA: E501
    parser.add_argument("--max_replicas", "--max-replicas", new_option_string="--max-replicas", action=StoreAndDeprecateWarningAction, default=5, type=int, help='Max number of replicas above which not to trigger anymore')
    parser.add_argument('--waiting-time-read-free-space', action="store", default=1800, type=int, help='Waiting time for reading free space')
    parser.add_argument('--waiting-time-read-workload', action="store", default=1800, type=int, help='Waiting time for reading workload')
    parser.add_argument('--waiting-time-print-workload', action="store", default=600, type=int, help='Waiting time for printing workload')
    parser.add_argument('--waiting-time-read-dids', action="store", default=60, type=int, help='Waiting time for reading dids')
    parser.add_argument('--waiting-time-place-replica', action="store", default=100, type=int, help='Waiting time for placing replicas')
    parser.add_argument('--sleep-time', action="store", default=10, type=int, help='Concurrency control: thread sleep time after each chunk of work')
    return parser


if __name__ == "__main__":

    signal.signal(signal.SIGTERM, stop)
    parser = get_parser()
    args = parser.parse_args()

    try:
        run(once=args.run_once,
            threads=args.threads,
            only_workload=args.only_workload,
            dry_run=args.dry_run,
            sampling=args.sampling,
            algorithms=args.algorithms,
            datatypes=args.datatypes,
            dest_rse_expr=args.dest_rse_expr,
            max_bytes_hour=args.max_bytes_hour,
            max_files_hour=args.max_files_hour,
            max_bytes_hour_rse=args.max_bytes_hour_rse,
            max_files_hour_rse=args.max_files_hour_rse,
            min_popularity=args.min_popularity,
            min_recent_requests=args.min_recent_requests,
            max_replicas=args.max_replicas,
            waiting_time_read_free_space=args.waiting_time_read_free_space,
            waiting_time_read_workload=args.waiting_time_read_workload,
            waiting_time_print_workload=args.waiting_time_print_workload,
            waiting_time_read_dids=args.waiting_time_read_dids,
            waiting_time_place_replica=args.waiting_time_place_replica,
            sleep_time=args.sleep_time)
    except KeyboardInterrupt:
        stop()
