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)


52
53
54
55
# File 'lib/mihari/models/rule.rb', line 52

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

.search(filter) ⇒ Array<Rule>

Search rules

Parameters:

Returns:

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mihari/models/rule.rb', line 29

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

#tagsObject



17
18
19
# File 'lib/mihari/models/rule.rb', line 17

def tags
  (data["tags"] || []).map { |tag| { name: tag } }
end

#yamlObject



13
14
15
# File 'lib/mihari/models/rule.rb', line 13

def yaml
  data.to_yaml
end