Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: MITLibraries/tacos
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 32d7dfbfae54891039cb9d9e71bb7f4aa8698a84
Choose a base ref
..
head repository: MITLibraries/tacos
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 4cefcb2039800480daf0e5636d94f67888f92c61
Choose a head ref
Showing with 77 additions and 5 deletions.
  1. +4 −0 Gemfile
  2. +10 −2 Gemfile.lock
  3. +3 −0 README.md
  4. +1 −3 app/graphql/types/query_type.rb
  5. +13 −0 app/models/search_logger.rb
  6. +8 −0 config/initializers/sentry.rb
  7. +38 −0 test/models/search_logger_test.rb
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -69,6 +69,10 @@ gem 'rack-cors'
# Use Redis adapter to run Action Cable in production
# gem "redis", ">= 4.0.1"

# Sentry integration according to their documentation [https://docs.sentry.io/platforms/ruby/guides/rails/]
gem "sentry-ruby"
gem "sentry-rails"

gem 'stringex'

# Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis]
12 changes: 10 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -231,7 +231,7 @@ GEM
marcel (1.0.4)
matrix (0.4.2)
mini_mime (1.1.5)
minitest (5.25.1)
minitest (5.25.2)
msgpack (1.7.2)
multi_json (1.15.0)
net-http (0.5.0)
@@ -338,7 +338,7 @@ GEM
zeitwerk (~> 2.6)
rainbow (3.1.1)
rake (13.2.1)
rdoc (6.7.0)
rdoc (6.8.1)
psych (>= 4.0.0)
regexp_parser (2.9.2)
reline (0.5.11)
@@ -394,6 +394,12 @@ GEM
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
sentry-rails (5.21.0)
railties (>= 5.0)
sentry-ruby (~> 5.21.0)
sentry-ruby (5.21.0)
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
@@ -501,6 +507,8 @@ DEPENDENCIES
rubocop-rails
scout_apm
selenium-webdriver
sentry-rails
sentry-ruby
simplecov
simplecov-lcov
sprockets-rails
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -50,6 +50,9 @@ Lots more [Scout settings](https://scoutapm.com/docs/ruby/configuration#environm
`SCOUT_LOG_LEVEL`: defaults to INFO which is probably fine. Controls verboseness of Scout logs
`SCOUT_NAME`: set a unique name per deployed tier to avoid confusion.

`SENTRY_DSN`: The Sentry-provided key to enable exception logging. Sentry integration is skipped if not present.
`SENTRY_ENV`: Sentry environment for the application. Defaults to 'unknown' if unset.

### Authentication

#### Required in all environments
4 changes: 1 addition & 3 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
@@ -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:)
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
8 changes: 8 additions & 0 deletions config/initializers/sentry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

Sentry.init do |config|
return unless ENV.has_key?('SENTRY_DSN')
config.dsn = ENV.fetch('SENTRY_DSN')
config.breadcrumbs_logger = [:active_support_logger, :http_logger]
config.environment = ENV.fetch('SENTRY_ENV', 'unknown')
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