Sha256: 93c5407d8d074248e65137d36a306201152f2fd67596071187ecb62534090c35

Contents?: true

Size: 788 Bytes

Versions: 2

Compression:

Stored size: 788 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[:artifacts] || []).map { |artifact_| artifact_[:data] }
        json[:tags] = (json[:tags] || []).map { |tag_| tag_[:name] }
        json
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mihari-1.5.1 lib/mihari/alert_viewer.rb
mihari-1.5.0 lib/mihari/alert_viewer.rb