# (C) Datadog, Inc. 2018-present
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)

import copy
import os

from datadog_checks.dev import get_here, run_command

CHECK_NAME = "riakcs"
HERE = get_here()
SERVICE_CHECK_NAME = 'riakcs.can_connect'

CONFIG = {"access_id": "foo", "access_secret": "bar", "tags": ["optional:tag1"]}

CONFIG_21 = {
    "access_id": "foo",
    "access_secret": "bar",
    "metrics": ["request_pool_overflow", "request_pool_size", "request_pool_workers"],
    "tags": ["optional:tag1"],
}

EXPECTED_TAGS = ['aggregation_key:localhost:8080', 'optional:tag1']

EXPECTED_METRICS_21 = [
    "riakcs.bucket_delete_out_error_one",
    "riakcs.bucket_delete_out_error_total",
    "riakcs.bucket_delete_out_one",
    "riakcs.bucket_delete_out_total",
    "riakcs.bucket_delete_time_95",
    "riakcs.bucket_delete_time_99",
    "riakcs.bucket_delete_time_mean",
    "riakcs.bucket_delete_time_median",
    "riakcs.bucket_head_out_error_one",
    "riakcs.bucket_head_out_error_total",
    "riakcs.bucket_head_out_one",
    "riakcs.bucket_head_out_total",
    "riakcs.bucket_head_time_95",
    "riakcs.bucket_head_time_99",
    "riakcs.bucket_head_time_mean",
    "riakcs.bucket_head_time_median",
    "riakcs.bucket_put_out_error_one",
    "riakcs.bucket_put_out_error_total",
    "riakcs.bucket_put_out_one",
    "riakcs.bucket_put_out_total",
    "riakcs.bucket_put_time_95",
    "riakcs.bucket_put_time_99",
    "riakcs.bucket_put_time_mean",
    "riakcs.bucket_put_time_median",
    "riakcs.bucket_location_get_out_error_one",
    "riakcs.bucket_location_get_out_error_total",
    "riakcs.bucket_location_get_out_one",
    "riakcs.bucket_location_get_out_total",
    "riakcs.bucket_location_get_time_95",
    "riakcs.bucket_location_get_time_99",
    "riakcs.bucket_location_get_time_mean",
    "riakcs.bucket_location_get_time_median",
    "riakcs.list_uploads_get_out_error_one",
    "riakcs.list_uploads_get_out_error_total",
    "riakcs.list_uploads_get_out_one",
    "riakcs.list_uploads_get_out_total",
    "riakcs.list_uploads_get_time_95",
    "riakcs.list_uploads_get_time_99",
    "riakcs.list_uploads_get_time_mean",
    "riakcs.list_uploads_get_time_median",
    "riakcs.list_objects_get_out_error_one",
    "riakcs.list_objects_get_out_error_total",
    "riakcs.list_objects_get_out_one",
    "riakcs.list_objects_get_out_total",
    "riakcs.list_objects_get_time_95",
    "riakcs.list_objects_get_time_99",
    "riakcs.list_objects_get_time_mean",
    "riakcs.list_objects_get_time_median",
    "riakcs.memory_atom",
    "riakcs.memory_atom_used",
    "riakcs.memory_binary",
    "riakcs.memory_code",
    "riakcs.memory_ets",
    "riakcs.memory_processes",
    "riakcs.memory_processes_used",
    "riakcs.memory_system",
    "riakcs.memory_total",
    "riakcs.multipart_post_out_error_one",
    "riakcs.multipart_post_out_error_total",
    "riakcs.multipart_post_out_one",
    "riakcs.multipart_post_out_total",
    "riakcs.multipart_post_time_95",
    "riakcs.multipart_post_time_99",
    "riakcs.multipart_post_time_mean",
    "riakcs.multipart_post_time_median",
    "riakcs.multipart_upload_delete_out_error_one",
    "riakcs.multipart_upload_delete_out_error_total",
    "riakcs.multipart_upload_delete_out_one",
    "riakcs.multipart_upload_delete_out_total",
    "riakcs.multipart_upload_delete_time_95",
    "riakcs.multipart_upload_delete_time_99",
    "riakcs.multipart_upload_delete_time_mean",
    "riakcs.multipart_upload_delete_time_median",
    "riakcs.multipart_upload_get_out_error_one",
    "riakcs.multipart_upload_get_out_error_total",
    "riakcs.multipart_upload_get_out_one",
    "riakcs.multipart_upload_get_out_total",
    "riakcs.multipart_upload_get_time_95",
    "riakcs.multipart_upload_get_time_99",
    "riakcs.multipart_upload_get_time_mean",
    "riakcs.multipart_upload_get_time_median",
    "riakcs.multipart_upload_post_out_error_one",
    "riakcs.multipart_upload_post_out_error_total",
    "riakcs.multipart_upload_post_out_one",
    "riakcs.multipart_upload_post_out_total",
    "riakcs.multipart_upload_post_time_95",
    "riakcs.multipart_upload_post_time_99",
    "riakcs.multipart_upload_post_time_mean",
    "riakcs.multipart_upload_post_time_median",
    "riakcs.multipart_upload_put_out_error_one",
    "riakcs.multipart_upload_put_out_error_total",
    "riakcs.multipart_upload_put_out_one",
    "riakcs.multipart_upload_put_out_total",
    "riakcs.multipart_upload_put_time_95",
    "riakcs.multipart_upload_put_time_99",
    "riakcs.multipart_upload_put_time_mean",
    "riakcs.multipart_upload_put_time_median",
    "riakcs.multiple_delete_post_out_error_one",
    "riakcs.multiple_delete_post_out_error_total",
    "riakcs.multiple_delete_post_out_one",
    "riakcs.multiple_delete_post_out_total",
    "riakcs.multiple_delete_post_time_95",
    "riakcs.multiple_delete_post_time_99",
    "riakcs.multiple_delete_post_time_mean",
    "riakcs.multiple_delete_post_time_median",
    "riakcs.object_put_out_error_one",
    "riakcs.object_put_out_error_total",
    "riakcs.object_put_out_one",
    "riakcs.object_put_out_total",
    "riakcs.object_put_time_95",
    "riakcs.object_put_time_99",
    "riakcs.object_put_time_mean",
    "riakcs.object_put_time_median",
    "riakcs.object_delete_out_error_one",
    "riakcs.object_delete_out_error_total",
    "riakcs.object_delete_out_one",
    "riakcs.object_delete_out_total",
    "riakcs.object_delete_time_95",
    "riakcs.object_delete_time_99",
    "riakcs.object_delete_time_mean",
    "riakcs.object_delete_time_median",
    "riakcs.object_get_out_error_one",
    "riakcs.object_get_out_error_total",
    "riakcs.object_get_out_one",
    "riakcs.object_get_out_total",
    "riakcs.object_get_time_95",
    "riakcs.object_get_time_99",
    "riakcs.object_get_time_mean",
    "riakcs.object_get_time_median",
    "riakcs.object_head_out_error_one",
    "riakcs.object_head_out_error_total",
    "riakcs.object_head_out_one",
    "riakcs.object_head_out_total",
    "riakcs.object_head_time_95",
    "riakcs.object_head_time_99",
    "riakcs.object_head_time_mean",
    "riakcs.object_head_time_median",
    "riakcs.object_put_copy_out_error_one",
    "riakcs.object_put_copy_out_error_total",
    "riakcs.object_put_copy_out_one",
    "riakcs.object_put_copy_out_total",
    "riakcs.object_put_copy_time_95",
    "riakcs.object_put_copy_time_99",
    "riakcs.object_put_copy_time_mean",
    "riakcs.object_put_copy_time_median",
    "riakcs.request_pool_overflow",
    "riakcs.request_pool_size",
    "riakcs.request_pool_workers",
]

EXPECTED_METRICS = [
    "riakcs.block_delete.latency_95",
    "riakcs.block_delete.latency_99",
    "riakcs.block_delete.latency_mean",
    "riakcs.block_delete.latency_median",
    "riakcs.block_delete.meter_count",
    "riakcs.block_delete.meter_rate",
    "riakcs.block_get_retry.latency_95",
    "riakcs.block_get_retry.latency_99",
    "riakcs.block_get_retry.latency_mean",
    "riakcs.block_get_retry.latency_median",
    "riakcs.block_get_retry.meter_count",
    "riakcs.block_get_retry.meter_rate",
    "riakcs.block_get.latency_95",
    "riakcs.block_get.latency_99",
    "riakcs.block_get.latency_mean",
    "riakcs.block_get.latency_median",
    "riakcs.block_get.meter_count",
    "riakcs.block_get.meter_rate",
    "riakcs.block_put.latency_95",
    "riakcs.block_put.latency_99",
    "riakcs.block_put.latency_mean",
    "riakcs.block_put.latency_median",
    "riakcs.block_put.meter_count",
    "riakcs.block_put.meter_rate",
    "riakcs.bucket_create.latency_95",
    "riakcs.bucket_create.latency_99",
    "riakcs.bucket_create.latency_mean",
    "riakcs.bucket_create.latency_median",
    "riakcs.bucket_create.meter_count",
    "riakcs.bucket_create.meter_rate",
    "riakcs.bucket_delete.latency_95",
    "riakcs.bucket_delete.latency_99",
    "riakcs.bucket_delete.latency_mean",
    "riakcs.bucket_delete.latency_median",
    "riakcs.bucket_delete.meter_count",
    "riakcs.bucket_delete.meter_rate",
    "riakcs.bucket_get_acl.latency_95",
    "riakcs.bucket_get_acl.latency_99",
    "riakcs.bucket_get_acl.latency_mean",
    "riakcs.bucket_get_acl.latency_median",
    "riakcs.bucket_get_acl.meter_count",
    "riakcs.bucket_get_acl.meter_rate",
    "riakcs.bucket_list_keys.latency_95",
    "riakcs.bucket_list_keys.latency_99",
    "riakcs.bucket_list_keys.latency_mean",
    "riakcs.bucket_list_keys.latency_median",
    "riakcs.bucket_list_keys.meter_count",
    "riakcs.bucket_list_keys.meter_rate",
    "riakcs.bucket_list_pool.overflow",
    "riakcs.bucket_list_pool.size",
    "riakcs.bucket_list_pool.workers",
    "riakcs.bucket_put_acl.latency_95",
    "riakcs.bucket_put_acl.latency_99",
    "riakcs.bucket_put_acl.latency_mean",
    "riakcs.bucket_put_acl.latency_median",
    "riakcs.bucket_put_acl.meter_count",
    "riakcs.bucket_put_acl.meter_rate",
    "riakcs.manifest_siblings_bp_sleep.latency_95",
    "riakcs.manifest_siblings_bp_sleep.latency_99",
    "riakcs.manifest_siblings_bp_sleep.latency_mean",
    "riakcs.manifest_siblings_bp_sleep.latency_median",
    "riakcs.manifest_siblings_bp_sleep.meter_count",
    "riakcs.manifest_siblings_bp_sleep.meter_rate",
    "riakcs.object_delete.latency_95",
    "riakcs.object_delete.latency_99",
    "riakcs.object_delete.latency_mean",
    "riakcs.object_delete.latency_median",
    "riakcs.object_delete.meter_count",
    "riakcs.object_delete.meter_rate",
    "riakcs.object_get_acl.latency_95",
    "riakcs.object_get_acl.latency_99",
    "riakcs.object_get_acl.latency_mean",
    "riakcs.object_get_acl.latency_median",
    "riakcs.object_get_acl.meter_count",
    "riakcs.object_get_acl.meter_rate",
    "riakcs.object_get.latency_95",
    "riakcs.object_get.latency_99",
    "riakcs.object_get.latency_mean",
    "riakcs.object_get.latency_median",
    "riakcs.object_get.meter_count",
    "riakcs.object_get.meter_rate",
    "riakcs.object_head.latency_95",
    "riakcs.object_head.latency_99",
    "riakcs.object_head.latency_mean",
    "riakcs.object_head.latency_median",
    "riakcs.object_head.meter_count",
    "riakcs.object_head.meter_rate",
    "riakcs.object_put_acl.latency_95",
    "riakcs.object_put_acl.latency_99",
    "riakcs.object_put_acl.latency_mean",
    "riakcs.object_put_acl.latency_median",
    "riakcs.object_put_acl.meter_count",
    "riakcs.object_put_acl.meter_rate",
    "riakcs.object_put.latency_95",
    "riakcs.object_put.latency_99",
    "riakcs.object_put.latency_mean",
    "riakcs.object_put.latency_median",
    "riakcs.object_put.meter_count",
    "riakcs.object_put.meter_rate",
    "riakcs.request_pool.overflow",
    "riakcs.request_pool.size",
    "riakcs.request_pool.workers",
    "riakcs.service_get_buckets.latency_95",
    "riakcs.service_get_buckets.latency_99",
    "riakcs.service_get_buckets.latency_mean",
    "riakcs.service_get_buckets.latency_median",
    "riakcs.service_get_buckets.meter_count",
    "riakcs.service_get_buckets.meter_rate",
]


def generate_config_with_creds():
    access_id = run_command(
        [
            "docker",
            "exec",
            "dd-test-riakcs",
            "bash",
            "-c",
            "grep admin_key /etc/riak-cs/advanced.config | cut -d '\"' -f2",
        ],
        capture="out",
    ).stdout.strip()
    access_secret = run_command(
        [
            "docker",
            "exec",
            "dd-test-riakcs",
            "bash",
            "-c",
            "grep admin_secret /etc/riak-cs/advanced.config | cut -d '\"' -f2",
        ],
        capture="out",
    ).stdout.strip()

    config = copy.deepcopy(CONFIG_21)
    config["access_id"] = access_id
    config["access_secret"] = access_secret
    config["is_secure"] = False
    config["s3_root"] = "s3.amazonaws.dev"

    return config


def read_fixture(filename):
    p = os.path.join(HERE, 'fixtures', filename)
    with open(p) as f:
        contents = f.read()

    return contents
