Class: Mihari::Alert

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/mihari/models/alert.rb

Class Method Summary collapse

Class Method Details

.count(filter) ⇒ Integer

Count alerts

Parameters:

  • artifact_data (String, nil)

Returns:

  • (Integer)


54
55
56
57
# File 'lib/mihari/models/alert.rb', line 54

def count(filter)
  relation = build_relation(filter)
  relation.distinct("alerts.id").count
end

.search(filter) ⇒ Array<Alert>

Search alerts

Parameters:

Returns:

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mihari/models/alert.rb', line 19

def search(filter)
  limit = filter.limit.to_i
  raise ArgumentError, "limit should be bigger than zero" unless limit.positive?

  page = filter.page.to_i
  raise ArgumentError, "page should be bigger than zero" unless page.positive?

  offset = (page - 1) * limit

  relation = build_relation(filter.without_pagination)

  alert_ids = relation.limit(limit).offset(offset).order(id: :desc).pluck(:id).uniq
  eager_load(
    {
      artifacts: [
        :autonomous_system,
        :geolocation,
        :whois_record,
        :dns_records,
        :reverse_dns_names,
        :cpes,
        :ports
      ]
    },
    :tags
  ).where(id: [alert_ids]).order(id: :desc)
end