#!/usr/bin/python
# Copyright European Organization for Nuclear Research (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
#
# Authors:
# - Mario Lassnig, <mario.lassnig@cern.ch>, 2016-2017

"""
Build the files done per link over the last 1 and 6 hours.
"""

import datetime
import json
import requests

ACTIVITIES = [
    'Data Brokering',
    'Data Consolidation',
    'Data Rebalancing',
    'Deletion',
    'Express',
    'Functional Test',
    'Production Input',
    'Production Output',
    'Recovery',
    'SFO to EOS export',
    'Staging',
    'T0 Export',
    'T0 Tape',
    'User Subscriptions',
    'default']


def get_link_done(hour):

    """ Retrieve the #transfers done from the dashboard. """

    res = []

    for tmp_activity in ACTIVITIES:
        url1 = 'http://dashb-atlas-ddm.cern.ch/dashboard/request.py/matrix.json?activity='
        url2 = '&src_grouping=site&src_grouping=token&dst_grouping=site&dst_grouping=token&interval='
        url = url1 + tmp_activity.replace(' ', '%20') + url2 + str(int(hour * 60))
        tmp = requests.get(url).json()
        for tmp_row in tmp['transfers']['rows']:
            res.append((tmp_row[0], tmp_row[1], tmp_row[2], tmp_row[3], tmp_row[5], tmp_activity))

    return res


if __name__ == '__main__':

    DATA = {}

    DONE_1 = get_link_done(1)

    for row in DONE_1:
        src = '%s_%s' % (row[0], row[1])
        dst = '%s_%s' % (row[2], row[3])
        link = '%s:%s' % (src, dst)
        if link.startswith(':') or link.endswith(':') or link.endswith('_None') or '_None:' in link:
            continue
        if link in DATA.keys():
            DATA[link]['files']['done'][row[5]] = {'1h': row[4],
                                                   '6h': row[4],
                                                   'timestamp': datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S')}
        else:
            DATA[link] = {'files': {'done': {row[5]: {'1h': row[4],
                                                      '6h': row[4],
                                                      'timestamp': datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S')}}}}

    DONE_6 = get_link_done(6)

    for row in DONE_6:
        src = '%s_%s' % (row[0], row[1])
        dst = '%s_%s' % (row[2], row[3])
        link = '%s:%s' % (src, dst)
        if link.startswith(':') or link.endswith(':') or link.endswith('_None') or '_None:' in link:
            continue
        if link in DATA.keys():
            if row[5] in DATA[link]['files']['done'].keys():
                DATA[link]['files']['done'][row[5]]['6h'] = row[4]
            else:
                DATA[link]['files']['done'][row[5]] = {'1h': row[4],
                                                       '6h': row[4],
                                                       'timestamp': datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S')}
        else:
            DATA[link] = {'files': {'done': {row[5]: {'1h': 0,
                                                      '6h': row[4],
                                                      'timestamp': datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S')}}}}

    MAPPING = None
    with open('/data/metrix/data/mapping-rse-site/latest.json', 'r') as f:
        MAPPING = json.load(f)

    for link in DATA:
        src, dst = link.split(':')
        DATA[link]['src_site'] = MAPPING[src]
        DATA[link]['dst_site'] = MAPPING[dst]

    SITE_DATA = {}
    for link in DATA:
        site_link = '%s:%s' % (DATA[link]['src_site'], DATA[link]['dst_site'])
        if site_link in SITE_DATA.keys():
            for activity in DATA[link]['files']['done']:
                if activity in SITE_DATA[site_link]['files']['done'].keys():
                    SITE_DATA[site_link]['files']['done'][activity]['1h'] += DATA[link]['files']['done'][activity]['1h']
                    SITE_DATA[site_link]['files']['done'][activity]['6h'] += DATA[link]['files']['done'][activity]['6h']
                else:
                    SITE_DATA[site_link]['files']['done'][activity] = DATA[link]['files']['done'][activity]
        else:
            SITE_DATA[site_link] = {'files': DATA[link]['files']}

    with open('/data/metrix/data/files-done-dashb/files-done-dashb-{0}.json'.format(datetime.datetime.utcnow().isoformat()[:-7]), 'w') as f:
        json.dump(SITE_DATA, f, indent=1, sort_keys=True)

    with open('/data/metrix/data/files-done-dashb/latest.json', 'w') as f:
        json.dump(SITE_DATA, f, indent=1, sort_keys=True)
