#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.aws import *  # pylint: disable=wildcard-import,unused-wildcard-import
AWSCostAndUageMetrics = [
    ("Unblended", "UnblendedCost", "unblended"),
]


def parse_aws_costs_and_usage(info):
    parsed = {}
    for row in parse_aws(info):
        timeperiod = row['TimePeriod']['Start']
        for group in row.get('Groups', []):
            service_name = " ".join(group['Keys'])
            for metric_name, metrics in group['Metrics'].items():
                try:
                    costs = float(metrics['Amount'])
                    unit = metrics['Unit']
                except (KeyError, ValueError):
                    continue
                else:
                    parsed.setdefault((timeperiod, service_name), {})\
                          .setdefault(metric_name, (costs, unit))
    return parsed


#   .--summary-------------------------------------------------------------.
#   |                                                                      |
#   |           ___ _   _ _ __ ___  _ __ ___   __ _ _ __ _   _             |
#   |          / __| | | | '_ ` _ \| '_ ` _ \ / _` | '__| | | |            |
#   |          \__ \ |_| | | | | | | | | | | | (_| | |  | |_| |            |
#   |          |___/\__,_|_| |_| |_|_| |_| |_|\__,_|_|   \__, |            |
#   |                                                    |___/             |
#   '----------------------------------------------------------------------'


def inventory_aws_costs_and_usage_summary(parsed):
    if parsed:
        return [("Summary", {})]


def check_aws_costs_and_usage_summary(item, params, parsed):
    amounts_by_metrics = collections.defaultdict(float)
    for (timeperiod, _service_name), metrics in parsed.items():
        for title, metric_name, key, in AWSCostAndUageMetrics:
            costs, unit = metrics[metric_name]
            amounts_by_metrics[(timeperiod, title, unit, key)] += costs

    for (timeperiod, title, unit, key), costs in amounts_by_metrics.items():
        yield check_levels(costs,
                           "aws_costs_%s" % key,
                           params.get('levels_%s' % key, (None, None)),
                           infoname="(%s) Total %s %s" % (timeperiod, title, unit))


check_info['aws_costs_and_usage'] = {
    'parse_function': parse_aws_costs_and_usage,
    'inventory_function': inventory_aws_costs_and_usage_summary,
    'check_function': check_aws_costs_and_usage_summary,
    'service_description': 'AWS/CE %s',
    'group': 'aws_costs_and_usage',
    'has_perfdata': True,
}

#.
#   .--per service---------------------------------------------------------.
#   |                                                _                     |
#   |          _ __   ___ _ __   ___  ___ _ ____   _(_) ___ ___            |
#   |         | '_ \ / _ \ '__| / __|/ _ \ '__\ \ / / |/ __/ _ \           |
#   |         | |_) |  __/ |    \__ \  __/ |   \ V /| | (_|  __/           |
#   |         | .__/ \___|_|    |___/\___|_|    \_/ |_|\___\___|           |
#   |         |_|                                                          |
#   '----------------------------------------------------------------------'


def inventory_aws_costs_and_usage_per_service(parsed):
    for (_timeperiod, service_name) in parsed:
        yield service_name, {}


def check_aws_costs_and_usage_per_service(item, params, parsed):
    data = None
    timeperiod = None
    for (timeperiod, service_name), metrics in parsed.items():
        if item == service_name:
            data = metrics
            break
    if not data:
        return

    for title, metric_name, key in AWSCostAndUageMetrics:
        costs, unit = data[metric_name]
        yield check_levels(costs,
                           "aws_costs_%s" % key,
                           params.get('levels_%s' % key, (None, None)),
                           infoname="(%s) %s %s" % (timeperiod, title, unit))


check_info['aws_costs_and_usage.per_service'] = {
    'inventory_function': inventory_aws_costs_and_usage_per_service,
    'check_function': check_aws_costs_and_usage_per_service,
    'service_description': 'AWS/CE %s',
    'group': 'aws_costs_and_usage',
    'has_perfdata': True,
}
