Sha256: b0528e06fdf0ded7a1bfb291c6f65b803b1c150a98fefc332ca987531dfa71a4

Contents?: true

Size: 807 Bytes

Versions: 9

Compression:

Stored size: 807 Bytes

Contents

# frozen_string_literal: true

module Mihari
  class AlertViewer
    def list(title: nil, source: nil, tag: nil, limit: 5)
      limit = limit.to_i
      raise ArgumentError, "limit should be bigger than zero" unless limit.positive?

      relation = Alert.includes(:tags, :artifacts)
      relation = relation.where(title: title) if title
      relation = relation.where(source: source) if source
      relation = relation.where(tags: { name: tag } ) if tag

      alerts = relation.limit(limit).order(id: :desc)
      alerts.map do |alert|
        json = AlertSerializer.new(alert).as_json
        json[:artifacts] = (json.dig(:artifacts) || []).map { |artifact_| artifact_.dig(:data) }
        json[:tags] = (json.dig(:tags) || []).map { |tag_| tag_.dig(:name) }
        json
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mihari-1.4.1 lib/mihari/alert_viewer.rb
mihari-1.4.0 lib/mihari/alert_viewer.rb
mihari-1.3.2 lib/mihari/alert_viewer.rb
mihari-1.3.1 lib/mihari/alert_viewer.rb
mihari-1.3.0 lib/mihari/alert_viewer.rb
mihari-1.2.1 lib/mihari/alert_viewer.rb
mihari-1.2.0 lib/mihari/alert_viewer.rb
mihari-1.1.1 lib/mihari/alert_viewer.rb
mihari-1.1.0 lib/mihari/alert_viewer.rb