Sha256: 3c5fa82d0dec7511d34aeb5e40448df4ce7f9b1f3e84b11f514dd1b515a89390

Contents?: true

Size: 1.54 KB

Versions: 6

Compression:

Stored size: 1.54 KB

Contents

require 'rest-client'

module InsightsCloud
  module Async
    class InsightsGenerateNotifications < ::Actions::EntryAction
      # cache blueprint on class level, so it won't be reloaded on subsequent calls
      def self.blueprint
        @blueprint ||= NotificationBlueprint.find_by(name: 'insights_satellite_hits')
      end

      def run
        add_satellite_notifications
      end

      def add_satellite_notifications
        hits_count = InsightsHit.where(host_id: foreman_host.id).count

        # Remove stale notifications
        blueprint.notifications.destroy_all

        if hits_count > 0
          add_notification(hits_count)
        end
      end

      private

      def logger
        action_logger
      end

      def foreman_host
        ForemanRhCloud.foreman_host
      end

      def blueprint
        self.class.blueprint
      end

      def add_notification(hits_count)
        Notification.create!(
          initiator: User.anonymous_admin,
          audience: ::Notification::AUDIENCE_ADMIN,
          message: UINotifications::StringParser.new(blueprint.message, { hits_count: hits_count }).to_s,
          notification_blueprint: blueprint,
          actions: {
            links: [
              {
                href: Rails.application.routes.url_helpers.foreman_rh_cloud_insights_cloud_path(search: "hostname=#{foreman_host.name}"),
                title: _('Fix host'),
              },
            ],
          }
        )
      end

      def rescue_strategy_for_self
        Dynflow::Action::Rescue::Fail
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
foreman_rh_cloud-10.0.3 lib/insights_cloud/async/insights_generate_notifications.rb
foreman_rh_cloud-11.0.3 lib/insights_cloud/async/insights_generate_notifications.rb
foreman_rh_cloud-11.0.2 lib/insights_cloud/async/insights_generate_notifications.rb
foreman_rh_cloud-11.0.1 lib/insights_cloud/async/insights_generate_notifications.rb
foreman_rh_cloud-11.0.0 lib/insights_cloud/async/insights_generate_notifications.rb
foreman_rh_cloud-10.0.2 lib/insights_cloud/async/insights_generate_notifications.rb