Skip to content

Commit

Permalink
Create SearchLogger model
Browse files Browse the repository at this point in the history
Why are these changes being introduced:

* This will simplify logic in GraphQL while making it easier to expand
  the logic necessary to add more complex logic to preprocess terms for
  each source or do any other normalization to incoming data.

Relevant ticket(s):

* Pre-work for: https://mitlibraries.atlassian.net/browse/TCO-113
  • Loading branch information
JPrevost committed Nov 19, 2024
1 parent c342c32 commit c492ce0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
4 changes: 1 addition & 3 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ def nodes(ids:)
end

def log_search_event(search_term:, source_system:)
term = Term.create_or_find_by!(phrase: search_term)
term.calculate_categorizations
term.search_events.create!(source: source_system)
SearchLogger.logevent(search_term, source_system)
end

def lookup_term(search_term:)
Expand Down
13 changes: 13 additions & 0 deletions app/models/search_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

# SearchLogger handles logging of search events including coordination of any preprocessing or normalization
# of data.
class SearchLogger
# Receives a phrase and source and creates a search event. Will find or create a term as needed.
# @return [SearchEvent] the newly created SearchEvent
def self.logevent(phrase, source)
term = Term.create_or_find_by!(phrase:)
term.calculate_categorizations
term.search_events.create!(source:)
end
end
38 changes: 38 additions & 0 deletions test/models/search_logger_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

#
require 'test_helper'

class SearchLoggerTest < ActiveSupport::TestCase
test 'a term is created if it does not exist' do
orig_term_count = Term.count
phrase = 'a term is created if it does not exist'
SearchLogger.logevent(phrase, 'search logger test')

assert_operator(Term.count, :>, orig_term_count)
end

test 'a term is not created if it already exists' do
orig_term_count = Term.count
phrase = Term.first.phrase
SearchLogger.logevent(phrase, 'search logger test')

assert_equal(orig_term_count, Term.count)
end

test 'a new search event is created for an existing term' do
orig_searchevent_count = SearchEvent.count
phrase = Term.first.phrase
SearchLogger.logevent(phrase, 'search logger test')

assert_equal(orig_searchevent_count + 1, SearchEvent.count)
end

test 'a new search event is created for a new term' do
orig_searchevent_count = SearchEvent.count
phrase = 'a new search event is created for a new term'
SearchLogger.logevent(phrase, 'search logger test')

assert_equal(orig_searchevent_count + 1, SearchEvent.count)
end
end

0 comments on commit c492ce0

Please sign in to comment.