Sha256: 8b7db220aeab10f9420053bcb9c981d36d7ef5242baa41a37cf7803b00128a3b

Contents?: true

Size: 1.46 KB

Versions: 9

Compression:

Stored size: 1.46 KB

Contents

module NdrError
  # Module to help with searching through fingerprints / logs.
  module Finder
    def search(keywords)
      scope = Fingerprint.latest_first
      return scope unless keywords && keywords.any?

      # Fetch the collection of fingerprint matches:
      records = scope.filter_by_keywords(keywords).to_a

      # Add to that fingerprints with log records that matched the search:
      log_print_ids = Log.not_deleted.filter_by_keywords(keywords).pluck(:error_fingerprintid)
      scope.where(error_fingerprintid: log_print_ids).find_in_batches do |batch|
        records.concat batch
      end

      order(records)
    end

    # Proxy to paginate fingerprint results, filtering them
    # if search keywords have been supplied.
    def paginate(keywords, page)
      search(keywords).paginate(page: page, per_page: Fingerprint.per_page)
    end

    # Sends finds through to the fingerprint resource.
    def find(id)
      Fingerprint.find(id)
    end

    private

    # Intelligent search order:
    #
    #   Bring records that have matched multiple times
    #   to the front, but attempt to maintain the
    #   created_at DESC order within groups.
    #
    def order(records)
      grouped = records.group_by { |record| records.count(record) }.each do |_count, group|
        group.sort! do |a, b|
          [b.updated_at, b.error_fingerprintid] <=> [a.updated_at, a.error_fingerprintid]
        end
      end

      grouped.values.flatten.uniq
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ndr_error-2.4.1 lib/ndr_error/finder.rb
ndr_error-2.4.0 lib/ndr_error/finder.rb
ndr_error-2.3.2 lib/ndr_error/finder.rb
ndr_error-2.3.1 lib/ndr_error/finder.rb
ndr_error-2.3.0 lib/ndr_error/finder.rb
ndr_error-2.2.0 lib/ndr_error/finder.rb
ndr_error-2.1.0 lib/ndr_error/finder.rb
ndr_error-2.0.3 lib/ndr_error/finder.rb
ndr_error-2.0.2 lib/ndr_error/finder.rb