# Alert description: this alert will trigger if the disk defects is over 50%, 40% or 30%.
# Alert breakdown:
# 1. Read the disk status from postgres (select * from disk)
# 2. For each disk, check if the disk defects is over 50% (major), 40% (medium) or 30% (minor).
# 3. If the disk defects is over the threshold, insert a new row to the alert table with the disk name and the disk defects.
workflow:
  id: disk-defect-tracker
  name: Disk Defect Tracker
  description: Monitors disk defects and creates tiered alerts in PostgreSQL based on defect percentage thresholds.
  triggers:
    - type: interval
      value: 60
  steps:
    - name: check-disk-defects
      provider:
        type: postgres
        config: "{{ providers.postgres-server }}"
        with:
          query: "select * from disk"
  actions:
    - name: push-alert-to-postgres
      foreach: "{{steps.check-disk-defects.results}}"
      condition:
        - name: threshold-condition
          type: threshold
          value: " {{ foreach.value[13] }} " # disk defect is the 13th column
          compare_to: 50, 40, 30
          level: major, medium, minor
      provider:
        type: postgres
        config: "{{ providers.postgres-server }}"
        with:
          query: >-
            INSERT INTO alert (alert_level, alert_message)
            VALUES ('{{ foreach.level }}', 'Disk defects: {{ foreach.value[13] }} | Disk name: {{ foreach.value[1] }}')
  providers:
    postgres-server:
      description: The postgres server (sql)
      authentication:
        username: "{{ env.POSTGRES_USER }}"
        password: "{{ env.POSTGRES_PASSWORD }}"
        database: "{{ env.POSTGRES_DATABASE }}"
        host: "{{ env.POSTGRES_HOST }}"
