# General variables
variable "environment" {
  type        = string
  description = "Name of the environment, e.g. dev, qa, prod"
  default     = "dev"
}

variable "application_family" {
  type        = string
  description = "Application family to which this service belongs"

}

variable "service_name" {
  type        = string
  description = "Service name, without environment prefix"
  default     = "elasticache"
}

variable "teams" {
  type        = set(string)
  description = "A set of Datadog Teams to associate the resources with"
  default     = []
}

variable "evaluation_delay" {
  type        = number
  description = "Time in seconds to wait for logs to stream in from AWS. Prevents false 'no data'"
  default     = 1800
}

variable "engine" {
  type        = string
  description = "Elasticache engine - allowed values: redis, memcached"
  default     = "redis"
  validation {
    condition     = var.engine == "redis" || var.engine == "memcached"
    error_message = "Only redis or memcached are supported"
  }
}

variable "is_redis" {
  type        = bool
  description = "Set to true to create datadog monitoring and dashboards for redis; false for memached"
  default     = true
}

######## Notification Endpoints (START)
variable "notification_endpoints" {
  type        = string
  description = "Endpoints for alerts. Can be email, slack, pagerduty, etc"
}

variable "escalation_notification_endpoints" {
  type        = string
  description = "Endpoints for escalation alerts. Can be email, slack, pagerduty, etc"
}

variable "alert_notification_endpoints" {
  type        = string
  description = "Additional endpoints to send only alert notifications to. Can be email, slack, pagerduty, etc."
  default     = ""
}

variable "no_data_notification_endpoints" {
  type        = string
  description = "Additional endpoints to send only no data notifications to. Can be email, slack, pagerduty, etc."
  default     = ""
}
######## Notification Endpoints (END)


######## Memory Pressure Monitor (START)
variable "memory_pressure_monitor_enabled" {
  type        = bool
  description = "Whether to create a monitor for Memory Pressure (Freeable Memory vs Swap Usage)"
  default     = true
}

variable "memory_pressure_evaluation_window" {
  type        = string
  description = "The evaluation window for calculating memory pressure"
  default     = "last_1h"
}

variable "memory_pressure_critical_number" {
  type        = number
  description = "Threshold for critical level of memory pressure"
  default     = 0
}
######## Memory Pressure Monitor (END)


######## Redis Evictions Anomaly Monitor (START)
variable "evictions_monitor_enabled" {
  type        = bool
  description = "Whether to create a monitor for Evictions"
  default     = true
}

variable "evictions_evaluation_window" {
  type        = string
  description = "The evaluation window for detecting eviction anomalies"
  default     = "last_4h"
}

variable "evictions_anomaly_algorithm" {
  type        = string
  description = "Algorithm used for detecting eviction anomalies"
  default     = "basic"
}

variable "evictions_anomaly_deviations" {
  type        = string
  description = "Control the sensitivity of eviction anomaly detection"
  default     = 2
}

variable "evictions_anomaly_direction" {
  type        = string
  description = "Directionality of eviction anomaly that should trigger an alert"
  default     = "both"
}

variable "evictions_anomaly_count_default_zero" {
  type        = bool
  description = "Interpret lack of a value as 0, for eviction anomaly detection"
  default     = "true"
}

variable "evictions_anomaly_operator" {
  type        = string
  description = "Operator to compare the calculated metric with the threshold for eviction anomaly detection"
  default     = ">="
}

variable "evictions_anomaly_rollup_interval" {
  type        = number
  description = "Interval of time (s) over which eviction anomaly data is aggregated over"
  default     = 60
}

variable "evictions_anomaly_alert_window" {
  type        = string
  description = "Duration of time the eviction metric must be anomalous before the alert triggers"
  default     = "last_15m"
}

variable "evictions_critical_number" {
  type        = number
  description = "Threshold for critical level of eviction anomalies"
  default     = 1
}

variable "evictions_critical_recovery_number" {
  type        = number
  description = "Threshold for recovering from critical level of eviction anomalies"
  default     = 0
}

variable "evictions_warning_number" {
  type        = number
  description = "Threshold for warning level of eviction anomalies"
  default     = 0.8
}

variable "evictions_warning_recovery_number" {
  type        = number
  description = "Threshold for recovering from warning level of eviction anomalies"
  default     = 0.5
}
######## Redis Evictions Anomaly Monitor (END)


######## CPU Utilization Monitor (START)
variable "cpu_utilization_monitor_enabled" {
  type        = bool
  description = "Whether to create a monitor for CPU Utilization"
  default     = true
}

variable "cpu_utilization_evaluation_window" {
  type        = string
  description = "The evaluation window for calculating CPU Utilization"
  default     = "last_1h"
}

variable "cpu_utilization_critical_number" {
  type        = number
  description = "Threshold for critical level of CPU Utilization"
  default     = 90
}

variable "cpu_utilization_critical_recovery_number" {
  type        = number
  description = "Threshold for recovering from a critical level of memory pressure"
  default     = 70
}
######## CPU Utilization Monitor (END)


######## Redis Database Memory Usage Percentage Monitor (START)
variable "database_memory_usage_percentage_monitor_enabled" {
  type        = bool
  description = "Whether to create a monitor for Database Memory Usage Percentage"
  default     = true
}

variable "database_memory_usage_percentage_evaluation_window" {
  type        = string
  description = "The evaluation window for calculating Database Memory Usage Percentage"
  default     = "last_1h"
}

variable "database_memory_usage_percentage_critical_number" {
  type        = number
  description = "Threshold for a critical level of Database Memory Usage Percentage"
  default     = 80
}

variable "database_memory_usage_percentage_critical_recovery_number" {
  type        = number
  description = "Threshold for recovering from a critical level of Database Memory Usage Percentage"
  default     = 70
}

variable "database_memory_usage_percentage_warning_number" {
  type        = number
  description = "Threshold for a warning level of Database Memory Usage Percentage"
  default     = 60
}

variable "database_memory_usage_percentage_warning_recovery_number" {
  type        = number
  description = "Threshold for recovering from a warning level of Database Memory Usage Percentage"
  default     = 50
}
######## Redis Database Memory Usage Percentage Monitor (END)


locals {
  # Only team tags are supported for dashboards
  dashboard_tags = [for team in var.teams : "team:${team}"]

  is_memcached = var.engine == "memached"
  is_redis     = var.engine == "redis"

  tags = flatten([
    "application_family:${var.application_family}",
    "env:${var.environment}",
    "environment:${var.environment}",
    "service:${var.service_name}",
    "service_name:${var.service_name}",
    [for team in var.teams : "team:${team}"],
  ])
}
