# (C) Datadog, Inc. 2020-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
CHECK_NAME = 'scylla'
NAMESPACE = 'scylla.'

# fmt: off
INSTANCE_METRIC_GROUP_MAP = {
    'scylla.alien': [
        'scylla.alien.receive_batch_queue_length',
        'scylla.alien.total_received_messages',
        'scylla.alien.total_sent_messages',
    ],
    'scylla.batchlog_manager': [
        'scylla.batchlog_manager.total_write_replay_attempts',
    ],
    'scylla.cache': [
        'scylla.cache.active_reads',
        'scylla.cache.bytes_total',
        'scylla.cache.bytes_used',
        'scylla.cache.concurrent_misses_same_key',
        'scylla.cache.mispopulations',
        'scylla.cache.partition_evictions',
        'scylla.cache.partition_hits',
        'scylla.cache.partition_insertions',
        'scylla.cache.partition_merges',
        'scylla.cache.partition_misses',
        'scylla.cache.partition_removals',
        'scylla.cache.partitions',
        'scylla.cache.pinned_dirty_memory_overload',
        'scylla.cache.reads',
        'scylla.cache.reads_with_misses',
        'scylla.cache.row_evictions',
        'scylla.cache.row_hits',
        'scylla.cache.row_insertions',
        'scylla.cache.row_misses',
        'scylla.cache.row_removals',
        'scylla.cache.rows',
        'scylla.cache.rows_dropped_from_memtable',
        'scylla.cache.rows_merged_from_memtable',
        'scylla.cache.rows_processed_from_memtable',
        'scylla.cache.sstable_partition_skips',
        'scylla.cache.sstable_reader_recreations',
        'scylla.cache.sstable_row_skips',
        'scylla.cache.static_row_insertions',
    ],
    'scylla.commitlog': [
        'scylla.commitlog.alloc',
        'scylla.commitlog.allocating_segments',
        'scylla.commitlog.bytes_written',
        'scylla.commitlog.cycle',
        'scylla.commitlog.disk_total_bytes',
        'scylla.commitlog.flush',
        'scylla.commitlog.flush_limit_exceeded',
        'scylla.commitlog.memory_buffer_bytes',
        'scylla.commitlog.pending_allocations',
        'scylla.commitlog.pending_flushes',
        'scylla.commitlog.requests_blocked_memory',
        'scylla.commitlog.segments',
        'scylla.commitlog.slack',
        'scylla.commitlog.unused_segments',
    ],
    'scylla.compaction_manager': [
        'scylla.compaction_manager.compactions',
    ],
    'scylla.cql': [
        'scylla.cql.authorized_prepared_statements_cache_evictions',
        'scylla.cql.authorized_prepared_statements_cache_size',
        'scylla.cql.batches',
        'scylla.cql.batches_pure_logged',
        'scylla.cql.batches_pure_unlogged',
        'scylla.cql.batches_unlogged_from_logged',
        'scylla.cql.deletes',
        'scylla.cql.filtered_read_requests',
        'scylla.cql.filtered_rows_dropped_total',
        'scylla.cql.filtered_rows_matched_total',
        'scylla.cql.filtered_rows_read_total',
        'scylla.cql.inserts',
        'scylla.cql.prepared_cache_evictions',
        'scylla.cql.prepared_cache_memory_footprint',
        'scylla.cql.prepared_cache_size',
        'scylla.cql.reads',
        'scylla.cql.reverse_queries',
        'scylla.cql.rows_read',
        'scylla.cql.secondary_index_creates',
        'scylla.cql.secondary_index_drops',
        'scylla.cql.secondary_index_reads',
        'scylla.cql.secondary_index_rows_read',
        'scylla.cql.statements_in_batches',
        'scylla.cql.unpaged_select_queries',
        'scylla.cql.updates',
        'scylla.cql.user_prepared_auth_cache_footprint',
    ],
    'scylla.database': [
        'scylla.database.active_reads',
        'scylla.database.active_reads_memory_consumption',
        'scylla.database.clustering_filter_count',
        'scylla.database.clustering_filter_fast_path_count',
        'scylla.database.clustering_filter_sstables_checked',
        'scylla.database.clustering_filter_surviving_sstables',
        'scylla.database.counter_cell_lock_acquisition',
        'scylla.database.counter_cell_lock_pending',
        'scylla.database.dropped_view_updates',
        'scylla.database.large_partition_exceeding_threshold',
        'scylla.database.multishard_query_failed_reader_saves',
        'scylla.database.multishard_query_failed_reader_stops',
        'scylla.database.multishard_query_unpopped_bytes',
        'scylla.database.multishard_query_unpopped_fragments',
        'scylla.database.paused_reads',
        'scylla.database.paused_reads_permit_based_evictions',
        'scylla.database.querier_cache_drops',
        'scylla.database.querier_cache_lookups',
        'scylla.database.querier_cache_memory_based_evictions',
        'scylla.database.querier_cache_misses',
        'scylla.database.querier_cache_population',
        'scylla.database.querier_cache_resource_based_evictions',
        'scylla.database.querier_cache_time_based_evictions',
        'scylla.database.queued_reads',
        'scylla.database.requests_blocked_memory',
        'scylla.database.requests_blocked_memory_current',
        'scylla.database.short_data_queries',
        'scylla.database.short_mutation_queries',
        'scylla.database.sstable_read_queue_overloads',
        'scylla.database.total_reads',
        'scylla.database.total_reads_failed',
        'scylla.database.total_result_bytes',
        'scylla.database.total_view_updates_failed_local',
        'scylla.database.total_view_updates_failed_remote',
        'scylla.database.total_view_updates_pushed_local',
        'scylla.database.total_view_updates_pushed_remote',
        'scylla.database.total_writes',
        'scylla.database.total_writes_failed',
        'scylla.database.total_writes_timedout',
        'scylla.database.view_building_paused',
        'scylla.database.view_update_backlog',
    ],
    'scylla.execution_stages': [
        'scylla.execution_stages.function_calls_enqueued',
        'scylla.execution_stages.function_calls_executed',
        'scylla.execution_stages.tasks_preempted',
        'scylla.execution_stages.tasks_scheduled',
    ],
    'scylla.gossip': [
        'scylla.gossip.heart_beat',
    ],
    'scylla.hints': [
        'scylla.hints.for_views_manager_corrupted_files',
        'scylla.hints.for_views_manager_discarded',
        'scylla.hints.for_views_manager_dropped',
        'scylla.hints.for_views_manager_errors',
        'scylla.hints.for_views_manager_sent',
        'scylla.hints.for_views_manager_size_of_hints_in_progress',
        'scylla.hints.for_views_manager_written',
        'scylla.hints.manager_corrupted_files',
        'scylla.hints.manager_discarded',
        'scylla.hints.manager_dropped',
        'scylla.hints.manager_errors',
        'scylla.hints.manager_sent',
        'scylla.hints.manager_size_of_hints_in_progress',
        'scylla.hints.manager_written',
    ],
    'scylla.httpd': [
        'scylla.httpd.connections_current',
        'scylla.httpd.connections_total',
        'scylla.httpd.read_errors',
        'scylla.httpd.reply_errors',
        'scylla.httpd.requests_served',
    ],
    'scylla.io_queue': [
        'scylla.io_queue.delay',
        'scylla.io_queue.queue_length',
        'scylla.io_queue.shares',
        'scylla.io_queue.total_bytes',
        'scylla.io_queue.total_operations',
    ],
    'scylla.lsa': [
        'scylla.lsa.free_space',
        'scylla.lsa.large_objects_total_space_bytes',
        'scylla.lsa.memory_allocated',
        'scylla.lsa.memory_compacted',
        'scylla.lsa.non_lsa_used_space_bytes',
        'scylla.lsa.occupancy',
        'scylla.lsa.segments_compacted',
        'scylla.lsa.segments_migrated',
        'scylla.lsa.small_objects_total_space_bytes',
        'scylla.lsa.small_objects_used_space_bytes',
        'scylla.lsa.total_space_bytes',
        'scylla.lsa.used_space_bytes',
    ],
    'scylla.memory': [
        'scylla.memory.allocated_memory',
        'scylla.memory.cross_cpu_free_operations',
        'scylla.memory.dirty_bytes',
        'scylla.memory.free_memory',
        'scylla.memory.free_operations',
        'scylla.memory.malloc_live_objects',
        'scylla.memory.malloc_operations',
        'scylla.memory.reclaims_operations',
        'scylla.memory.regular_dirty_bytes',
        'scylla.memory.regular_virtual_dirty_bytes',
        'scylla.memory.streaming_dirty_bytes',
        'scylla.memory.streaming_virtual_dirty_bytes',
        'scylla.memory.system_dirty_bytes',
        'scylla.memory.system_virtual_dirty_bytes',
        'scylla.memory.total_memory',
        'scylla.memory.virtual_dirty_bytes',
    ],
    'scylla.memtables': [
        'scylla.memtables.pending_flushes',
        'scylla.memtables.pending_flushes_bytes',
    ],
    'scylla.node': [
        'scylla.node.operation_mode'
    ],
    'scylla.query_processor': [
        'scylla.query_processor.queries',
        'scylla.query_processor.statements_prepared',
    ],
    'scylla.reactor': [
        'scylla.reactor.aio_bytes_read',
        'scylla.reactor.aio_bytes_write',
        'scylla.reactor.aio_errors',
        'scylla.reactor.aio_reads',
        'scylla.reactor.aio_writes',
        'scylla.reactor.cpp_exceptions',
        'scylla.reactor.cpu_busy_ms',
        'scylla.reactor.cpu_steal_time_ms',
        'scylla.reactor.fstream_read_bytes',
        'scylla.reactor.fstream_read_bytes_blocked',
        'scylla.reactor.fstream_reads',
        'scylla.reactor.fstream_reads_ahead_bytes_discarded',
        'scylla.reactor.fstream_reads_aheads_discarded',
        'scylla.reactor.fstream_reads_blocked',
        'scylla.reactor.fsyncs',
        'scylla.reactor.io_queue_requests',
        'scylla.reactor.io_threaded_fallbacks',
        'scylla.reactor.logging_failures',
        'scylla.reactor.polls',
        'scylla.reactor.tasks_pending',
        'scylla.reactor.tasks_processed',
        'scylla.reactor.timers_pending',
        'scylla.reactor.utilization',
    ],
    'scylla.scheduler': [
        'scylla.scheduler.queue_length',
        'scylla.scheduler.runtime_ms',
        'scylla.scheduler.shares',
        'scylla.scheduler.tasks_processed',
        'scylla.scheduler.time_spent_on_task_quota_violations_ms',
    ],
    'scylla.sstables': [
        'scylla.sstables.capped_local_deletion_time',
        'scylla.sstables.capped_tombstone_deletion_time',
        'scylla.sstables.cell_tombstone_writes',
        'scylla.sstables.cell_writes',
        'scylla.sstables.index_page_blocks',
        'scylla.sstables.index_page_hits',
        'scylla.sstables.index_page_misses',
        'scylla.sstables.partition_reads',
        'scylla.sstables.partition_seeks',
        'scylla.sstables.partition_writes',
        'scylla.sstables.range_partition_reads',
        'scylla.sstables.range_tombstone_writes',
        'scylla.sstables.row_reads',
        'scylla.sstables.row_writes',
        'scylla.sstables.single_partition_reads',
        'scylla.sstables.sstable_partition_reads',
        'scylla.sstables.static_row_writes',
        'scylla.sstables.tombstone_writes',
    ],
    'scylla.storage': [
        'scylla.storage.proxy.coordinator_background_read_repairs',
        'scylla.storage.proxy.coordinator_background_reads',
        'scylla.storage.proxy.coordinator_background_replica_writes_failed_local_node',
        'scylla.storage.proxy.coordinator_background_write_bytes',
        'scylla.storage.proxy.coordinator_background_writes',
        'scylla.storage.proxy.coordinator_background_writes_failed',
        'scylla.storage.proxy.coordinator_canceled_read_repairs',
        'scylla.storage.proxy.coordinator_completed_reads_local_node',
        'scylla.storage.proxy.coordinator_current_throttled_base_writes',
        'scylla.storage.proxy.coordinator_current_throttled_writes',
        'scylla.storage.proxy.coordinator_foreground_read_repair',
        'scylla.storage.proxy.coordinator_foreground_reads',
        'scylla.storage.proxy.coordinator_foreground_writes',
        'scylla.storage.proxy.coordinator_last_mv_flow_control_delay',
        'scylla.storage.proxy.coordinator_queued_write_bytes',
        'scylla.storage.proxy.coordinator_range_timeouts',
        'scylla.storage.proxy.coordinator_range_unavailable',
        'scylla.storage.proxy.coordinator_read_errors_local_node',
        'scylla.storage.proxy.coordinator_read_latency.count',
        'scylla.storage.proxy.coordinator_read_latency.sum',
        'scylla.storage.proxy.coordinator_read_repair_write_attempts_local_node',
        'scylla.storage.proxy.coordinator_read_retries',
        'scylla.storage.proxy.coordinator_read_timeouts',
        'scylla.storage.proxy.coordinator_read_unavailable',
        'scylla.storage.proxy.coordinator_reads_local_node',
        'scylla.storage.proxy.coordinator_speculative_data_reads',
        'scylla.storage.proxy.coordinator_speculative_digest_reads',
        'scylla.storage.proxy.coordinator_throttled_writes',
        'scylla.storage.proxy.coordinator_total_write_attempts_local_node',
        'scylla.storage.proxy.coordinator_write_errors_local_node',
        'scylla.storage.proxy.coordinator_write_latency.count',
        'scylla.storage.proxy.coordinator_write_latency.sum',
        'scylla.storage.proxy.coordinator_write_timeouts',
        'scylla.storage.proxy.coordinator_write_unavailable',
        'scylla.storage.proxy.replica_cross_shard_ops',
        'scylla.storage.proxy.replica_forwarded_mutations',
        'scylla.storage.proxy.replica_forwarding_errors',
        'scylla.storage.proxy.replica_reads',
        'scylla.storage.proxy.replica_received_counter_updates',
        'scylla.storage.proxy.replica_received_mutations',
    ],
    'scylla.streaming': [
        'scylla.streaming.total_incoming_bytes',
        'scylla.streaming.total_outgoing_bytes',
    ],
    'scylla.thrift': [
        'scylla.thrift.current_connections',
        'scylla.thrift.served',
        'scylla.thrift.thrift_connections',
    ],
    'scylla.tracing': [
        'scylla.tracing.active_sessions',
        'scylla.tracing.cached_records',
        'scylla.tracing.dropped_records',
        'scylla.tracing.dropped_sessions',
        'scylla.tracing.flushing_records',
        'scylla.tracing.keyspace_helper_bad_column_family_errors',
        'scylla.tracing.keyspace_helper_tracing_errors',
        'scylla.tracing.pending_for_write_records',
        'scylla.tracing.trace_errors',
        'scylla.tracing.trace_records_count',
    ],
    'scylla.transport': [
        'scylla.transport.cql_connections',
        'scylla.transport.current_connections',
        'scylla.transport.requests_blocked_memory',
        'scylla.transport.requests_blocked_memory_current',
        'scylla.transport.requests_served',
        'scylla.transport.requests_serving',
    ],
}
# fmt: on


INSTANCE_DEFAULT_GROUPS = [
    'scylla.cache',
    'scylla.compaction_manager',
    'scylla.gossip',
    'scylla.node',
    'scylla.reactor',
    'scylla.storage',
    'scylla.streaming',
    'scylla.transport',
]


INSTANCE_ADDITIONAL_GROUPS = [
    'scylla.alien',
    'scylla.batchlog_manager',
    'scylla.commitlog',
    'scylla.cql',
    'scylla.database',
    'scylla.execution_stages',
    'scylla.hints',
    'scylla.httpd',
    'scylla.io_queue',
    'scylla.lsa',
    'scylla.memory',
    'scylla.memtables',
    'scylla.query_processor',
    'scylla.scheduler',
    'scylla.sstables',
    'scylla.thrift',
    'scylla.tracing',
]


# expand the lists into a single list of metrics
def get_metrics(metric_groups):
    """Given a list of metric groups, return single consolidated list"""
    return sorted(m for g in metric_groups for m in INSTANCE_METRIC_GROUP_MAP[g])


INSTANCE_DEFAULT_METRICS = get_metrics(INSTANCE_DEFAULT_GROUPS)
INSTANCE_ADDITIONAL_METRICS = get_metrics(INSTANCE_ADDITIONAL_GROUPS)
