class Mihari::Alert
Public Class Methods
count(artifact_data: nil, description: nil, source: nil, tag_name: nil, title: nil, from_at: nil, to_at: nil)
click to toggle source
Count alerts
@param [String, nil] artifact_data @param [String, nil] description @param [String, nil] source @param [String, nil] tag_name @param [String, nil] title @param [DateTime, nil] from_at @param [DateTime, nil] to_at
@return [Integer]
# File lib/mihari/models/alert.rb, line 72 def count(artifact_data: nil, description: nil, source: nil, tag_name: nil, title: nil, from_at: nil, to_at: nil) relation = build_relation( artifact_data: artifact_data, title: title, description: description, source: source, tag_name: tag_name, from_at: from_at, to_at: to_at ) relation.distinct("alerts.id").count end
search(artifact_data: nil, description: nil, source: nil, tag_name: nil, title: nil, from_at: nil, to_at: nil, limit: 10, page: 1)
click to toggle source
Search alerts
@param [String, nil] artifact_data @param [String, nil] description @param [String, nil] source @param [String, nil] tag_name @param [String, nil] title @param [DateTime, nil] from_at @param [DateTime, nil] to_at @param [Integer, nil] limit @param [Integer, nil] page
@return [Array<Hash>]
# File lib/mihari/models/alert.rb, line 28 def search(artifact_data: nil, description: nil, source: nil, tag_name: nil, title: nil, from_at: nil, to_at: nil, limit: 10, page: 1) limit = limit.to_i raise ArgumentError, "limit should be bigger than zero" unless limit.positive? page = page.to_i raise ArgumentError, "page should be bigger than zero" unless page.positive? offset = (page - 1) * limit relation = build_relation( artifact_data: artifact_data, title: title, description: description, source: source, tag_name: tag_name, from_at: from_at, to_at: to_at ) # TODO: improve queires alert_ids = relation.limit(limit).offset(offset).order(id: :desc).pluck(:id).uniq alerts = includes(:artifacts, :tags).where(id: [alert_ids]).order(id: :desc) alerts.map do |alert| json = Serializers::AlertSerializer.new(alert).as_json json[:artifacts] = json[:artifacts] || [] json[:tags] = json[:tags] || [] json end end
Private Class Methods
build_relation(artifact_data: nil, title: nil, description: nil, source: nil, tag_name: nil, from_at: nil, to_at: nil)
click to toggle source
# File lib/mihari/models/alert.rb, line 87 def build_relation(artifact_data: nil, title: nil, description: nil, source: nil, tag_name: nil, from_at: nil, to_at: nil) relation = self relation = relation.includes(:artifacts, :tags) relation = relation.where(artifacts: { data: artifact_data }) if artifact_data relation = relation.where(tags: { name: tag_name }) if tag_name relation = relation.where(source: source) if source relation = relation.where(title: title) if title relation = relation.filter(description: { like: "%#{description}%" }) if description relation = relation.filter(created_at: { gte: from_at }) if from_at relation = relation.filter(created_at: { lte: to_at }) if to_at relation end