#!/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 packetloss from perfsonar per link.
"""

import datetime
import json
import time

from elasticsearch import Elasticsearch

if __name__ == '__main__':

    ES = Elasticsearch([{'host': 'atlas-kibana.mwt2.org', 'port': 9200}])

    BEGIN_TIME = int(time.mktime((datetime.datetime.now() - datetime.timedelta(hours=1)).timetuple()) * 1000)
    END_TIME = int(time.mktime((datetime.datetime.now()).timetuple()) * 1000)

    BODY = """
    {
      "size": 0,
      "query": {
        "bool": {
          "must": [
            {
              "query_string": {
                "query": "srcVO:ATLAS AND destVO:ATLAS AND _type:packet_loss_rate",
                "analyze_wildcard": true,
                "lowercase_expanded_terms": false
              }
            },
            {
              "range": {
                "timestamp": {
                  "gte": %s,
                  "lte": %s,
                  "format": "epoch_millis"
                }
              }
            }
          ],
          "must_not": []
        }
      },
      "_source": {
        "excludes": []
      },
      "aggs": {
        "2": {
          "terms": {
            "field": "srcSite",
            "size": 999,
            "order": {
              "_term": "desc"
            }
          },
          "aggs": {
            "3": {
              "terms": {
                "field": "destSite",
                "size": 999,
                "order": {
                  "_term": "desc"
                }
              },
              "aggs": {
                "1": {
                  "percentiles": {
                    "field": "packet_loss",
                    "percents": [
                      95
                    ],
                    "keyed": false
                  }
                }
              }
            }
          }
        }
      }
    }
    """ % (BEGIN_TIME, END_TIME)

    RES = ES.search(index='network_weather-2017*', body=BODY)

    DATA = {}
    for src_site in RES['aggregations']['2']['buckets']:
        for dst_site in src_site['3']['buckets']:
            link = '%s:%s' % (src_site['key'], dst_site['key'])
            if link.startswith(':') or link.endswith(':'):
                continue
            packetloss = dst_site['1']['values'][0]['value']
            DATA[link] = {'packetloss': {'latest': round(packetloss, 3),  # 3 decimal places in source data
                                         'timestamp': datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S')}}

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

    with open('/data/metrix/data/packetloss-perfsonar/latest.json', 'w') as f:
        json.dump(DATA, f, indent=1, sort_keys=True)
