Class: Mihari::Rule

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.count(filter) ⇒ Integer

Count alerts

Parameters:

Returns:

  • (Integer)


58
59
60
61
# File 'lib/mihari/models/rule.rb', line 58

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

.search(filter) ⇒ Array<Rule>

Search rules

Parameters:

Returns:

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mihari/models/rule.rb', line 35

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)

  # TODO: improve queires
  rule_ids = relation.limit(limit).offset(offset).order(created_at: :desc).pluck(:id).uniq
  where(id: [rule_ids]).order(created_at: :desc)
end

Instance Method Details

#symbolized_dataObject



9
10
11
# File 'lib/mihari/models/rule.rb', line 9

def symbolized_data
  @symbolized_data ||= data.deep_symbolize_keys
end

#to_hHash

Returns a hash representative

Returns:

  • (Hash)


18
19
20
21
22
23
24
25
# File 'lib/mihari/models/rule.rb', line 18

def to_h
  {
    id: id,
    yaml: yaml || data.to_yaml,
    created_at: created_at,
    updated_at: updated_at
  }
end