{{- if and .Values.metrics.enabled .Values.metrics.prometheusRule.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: {{ include "tc.common.names.fullname" . }}
  labels:
    {{- include "tc.common.labels" . | nindent 4 }}
    {{- with .Values.metrics.prometheusRule.labels }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
spec:
  groups:
    - name: {{ include "tc.common.names.fullname" . }}
      rules:
        - alert: SpeedtestExporterAbsent
          annotations:
            description: Speedtest Exporter has disappeared from Prometheus target discovery.
            summary: Speedtest Exporter is down.
          expr: |
            absent(up{job=~".*{{ include "tc.common.names.fullname" . }}.*"} == 1)
          for: {{ trimAll "m" .Values.metrics.serviceMonitor.interval | add 15 }}m
          labels:
            severity: critical
        - alert: SpeedtestSlowInternetDownload
          annotations:
            description: Internet download speed is averaging {{ "{{ humanize $value }}" }} Mbps.
            summary: SpeedTest slow internet download.
          expr: |
            avg_over_time(speedtest_download_bits_per_second{job=~".*{{ include "tc.common.names.fullname" . }}.*"}[4h])
              < {{ .Values.metrics.prometheusRule.downloadLimit }}
          for: 0m
          labels:
            severity: warning
        - alert: SpeedtestSlowInternetUpload
          annotations:
            description: Internet upload speed is averaging {{ "{{ humanize $value }}" }} Mbps.
            summary: SpeedTest slow internet upload.
          expr: |
            avg_over_time(speedtest_upload_bits_per_second{job=~".*{{ include "tc.common.names.fullname" . }}.*"}[4h])
              < {{ .Values.metrics.prometheusRule.uploadLimit }}
          for: 0m
          labels:
            severity: warning
        - alert: SpeedtestHighPingLatency
          annotations:
            description: Internet ping latency is averaging {{ "{{ humanize $value }}" }} ms.
            summary: SpeedTest high ping latency.
          expr: |
            avg_over_time(speedtest_ping_latency_milliseconds{job=~".*{{ include "tc.common.names.fullname" . }}.*"}[4h])
              > {{ .Values.metrics.prometheusRule.pingLimit }}
          for: 0m
          labels:
            severity: warning
        - alert: SpeedtestHighJitterLatency
          annotations:
            description: Internet jitter latency is averaging {{ "{{ humanize $value }}" }} ms.
            summary: SpeedTest high jitter latency.
          expr: |
            avg_over_time(speedtest_jitter_latency_milliseconds{job=~".*{{ include "tc.common.names.fullname" . }}.*"}[4h])
              > {{ .Values.metrics.prometheusRule.jitterLimit }}
          for: 0m
          labels:
            severity: warning
        {{- with .Values.metrics.prometheusRule.rules }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
{{- end }}
