#!/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.couchbase import *  # pylint: disable=wildcard-import,unused-wildcard-import


def parse_couchbase_buckets_operations(info):
    parsed = parse_couchbase_lines(info)
    counters = (collections.Counter(data) for data in parsed.values())
    try:
        parsed[None] = sum(counters, collections.Counter())
    except TypeError:
        pass
    return parsed


@get_parsed_item_data
def check_couchbase_buckets_operations(_item, params, data):

    ops = data.get("ops")
    if ops is not None:
        yield check_levels(
            ops,
            "op_s",
            params.get("ops"),
            unit="/s",
            infoname="Total (per server)",
        )

    cmd_get = data.get("cmd_get")
    if cmd_get is not None:
        yield check_levels(
            cmd_get,
            None,
            None,
            unit="/s",
            infoname="Gets",
        )

    cmd_set = data.get("cmd_set")
    if cmd_set is not None:
        yield check_levels(
            cmd_set,
            None,
            None,
            unit="/s",
            infoname="Sets",
        )

    creates = data.get("ep_ops_create")
    if creates is not None:
        yield check_levels(
            creates,
            None,
            None,
            unit="/s",
            infoname="Creates",
        )

    updates = data.get("ep_ops_update")
    if updates is not None:
        yield check_levels(
            updates,
            None,
            None,
            unit="/s",
            infoname="Updates",
        )

    deletes = data.get("ep_num_ops_del_meta")
    if deletes is not None:
        yield check_levels(
            deletes,
            None,
            None,
            unit="/s",
            infoname="Deletes",
        )


check_info["couchbase_buckets_operations"] = {
    "parse_function": parse_couchbase_buckets_operations,
    "inventory_function": discover(lambda k, v: k is not None and "ops" in v),
    "check_function": check_couchbase_buckets_operations,
    "service_description": "Couchbase Bucket %s Operations",
    "group": "couchbase_ops",
    "has_perfdata": True,
}

check_info["couchbase_buckets_operations.total"] = {
    "parse_function": parse_couchbase_buckets_operations,
    "inventory_function": discover(lambda k, v: k is None and "ops" in v),
    "check_function": check_couchbase_buckets_operations,
    "service_description": "Couchbase Bucket Operations",
    "group": "couchbase_ops_buckets",
    "has_perfdata": True,
}
