From 1d0702a63c79628aa070f09de95cd03ceca39cb8 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Mon, 16 Jan 2017 11:03:14 -0800 Subject: [PATCH] Restore notifications --- lib/searchkick/index.rb | 25 +++++++++++++++++++------ lib/searchkick/logging.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++-- lib/searchkick/model.rb | 2 +- 3 files changed, 65 insertions(+), 9 deletions(-) diff --git a/lib/searchkick/index.rb b/lib/searchkick/index.rb index b48189d..9eeddf5 100644 --- a/lib/searchkick/index.rb +++ b/lib/searchkick/index.rb @@ -66,26 +66,31 @@ module Searchkick alias_method :swap, :promote # record based + # use helpers for notifications def store(record) - bulk_index([record]) + bulk_index_helper([record]) end def remove(record) - bulk_delete([record]) + bulk_delete_helper([record]) + end + + def update_record(record, method_name) + bulk_update_helper([record], method_name) end def bulk_delete(records) - bulk_delete_helper(records.reject { |r| r.id.blank? }) + bulk_delete_helper(records) end def bulk_index(records) - Searchkick.indexer.queue(records.map { |r| {index: record_data(r).merge(data: search_data(r))} }) + bulk_index_helper(records) end alias_method :import, :bulk_index def bulk_update(records, method_name) - Searchkick.indexer.queue(records.map { |r| {update: record_data(r).merge(data: {doc: search_data(r, method_name)})} }) + bulk_update_helper(records, method_name) end def record_data(r) @@ -475,8 +480,16 @@ module Searchkick end end + def bulk_index_helper(records) + Searchkick.indexer.queue(records.map { |r| {index: record_data(r).merge(data: search_data(r))} }) + end + def bulk_delete_helper(records) - Searchkick.indexer.queue(records.map { |r| {delete: record_data(r)} }) + Searchkick.indexer.queue(records.reject { |r| r.id.blank? }.map { |r| {delete: record_data(r)} }) + end + + def bulk_update_helper(records, method_name) + Searchkick.indexer.queue(records.map { |r| {update: record_data(r).merge(data: {doc: search_data(r, method_name)})} }) end def redis diff --git a/lib/searchkick/logging.rb b/lib/searchkick/logging.rb index b763815..84f528e 100644 --- a/lib/searchkick/logging.rb +++ b/lib/searchkick/logging.rb @@ -16,10 +16,53 @@ module Searchkick end module IndexWithInstrumentation + def store(record) + event = { + name: "#{record.searchkick_klass.name} Store", + id: search_id(record) + } + if Searchkick.callbacks_value == :bulk + super + else + ActiveSupport::Notifications.instrument("request.searchkick", event) do + super + end + end + end + + def remove(record) + name = record && record.searchkick_klass ? "#{record.searchkick_klass.name} Remove" : "Remove" + event = { + name: name, + id: search_id(record) + } + if Searchkick.callbacks_value == :bulk + super + else + ActiveSupport::Notifications.instrument("request.searchkick", event) do + super + end + end + end + + def update_record(record, method_name) + event = { + name: "#{record.searchkick_klass.name} Update", + id: search_id(record) + } + if Searchkick.callbacks_value == :bulk + super + else + ActiveSupport::Notifications.instrument("request.searchkick", event) do + super + end + end + end + def bulk_index(records) if records.any? event = { - name: "#{records.first.searchkick_klass.name} Index", + name: "#{records.first.searchkick_klass.name} Import", count: records.size } event[:id] = search_id(records.first) if records.size == 1 @@ -51,7 +94,7 @@ module Searchkick end end - def bulk_delete_helper(records) + def bulk_delete(records) if records.any? event = { name: "#{records.first.searchkick_klass.name} Delete", diff --git a/lib/searchkick/model.rb b/lib/searchkick/model.rb index 7c19633..882bbc8 100644 --- a/lib/searchkick/model.rb +++ b/lib/searchkick/model.rb @@ -120,7 +120,7 @@ module Searchkick end else if method_name - self.class.searchkick_index.bulk_update([self], method_name) + self.class.searchkick_index.update_record(self, method_name) else self.class.searchkick_index.reindex_record(self) end -- libgit2 0.21.0