Sha256: 74095b797daa41d52410c0537c46d30a9c75f54190d77840c262812a496e67ae

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module Motor
  module Alerts
    module_function

    def send_alert(alert)
      if alert.preferences[:adapter] == 'slack'
        send_slack_alert(alert)
      else
        send_email_alert(alert)
      end
    end

    def send_email_alert(alert)
      alert.to_emails.split(',').each do |email|
        if email.include?('@')
          Motor::AlertsMailer.alert_email(alert, email: email).deliver_now!
        else
          send_query_email_alerts(alert, email.gsub(/[{}]/, ''))
        end
      end
    end

    def send_query_email_alerts(alert, column_name)
      result = Queries::RunQuery.call(alert.query)

      column_index = result.columns.find_index { |c| c[:name] == column_name }

      emails = result.data.map { |row| row[column_index] }.uniq

      emails.each do |email|
        Motor::AlertsMailer.alert_email(alert, email: email).deliver_now!
      end
    end

    def send_slack_alert(alert)
      slack_users = Slack::Client.load_users(limit: Slack::ITEMS_LIMIT)['members']

      alert.preferences[:slack_conversation_ids].each do |conversation_id|
        user = slack_users.find { |u| u['id'] == conversation_id }

        Motor::Alerts::SlackSender.call(alert, conversation_id, slack_user: user)
      end
    end
  end
end

require_relative './alerts/scheduler'
require_relative './alerts/scheduled_alerts_cache'
require_relative './alerts/persistance'
require_relative './alerts/slack_sender'

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
motor-admin-0.4.3 lib/motor/alerts.rb
motor-admin-0.4.2 lib/motor/alerts.rb
motor-admin-0.4.0 lib/motor/alerts.rb