# Alert if a result queried from the DB is above a certain thershold.
workflow:
  id: mysql-datetime-monitor
  name: MySQL Datetime Monitor
  description: Monitors time differences in MySQL database entries and alerts via Slack when exceeding one hour threshold.
  triggers:
    - type: interval
      value: 300 # every 5 minutes
  steps:
    - name: get-max-datetime
      provider:
        type: mysql
        config: "{{ providers.mysql-prod }}"
        with:
          # Get max(datetime) from the random table
          query: "SELECT MAX(datetime) FROM demo_table LIMIT 1"
  actions:
    - name: trigger-slack
      condition:
        - name: threshold-condition
          type: threshold
          # datetime_compare(t1, t2) compares t1-t2 and returns the diff in hours
          #   utcnow() returns the local machine datetime in UTC
          #   to_utc() converts a datetime to UTC
          value: keep.datetime_compare(keep.utcnow(), keep.to_utc("{{ steps.this.results[0][0] }}"))
          compare_to: 1 # hours
          compare_type: gt # greater than
      provider:
        type: slack
        config: " {{ providers.slack-demo }} "
        with:
          message: "DB datetime value ({{ actions.trigger-slack.conditions.threshold.0.compare_value }}) is greater than 1! 🚨"
