diff --git a/app/models/metrics/algorithms.rb b/app/models/metrics/algorithms.rb index 387c95b..0353144 100644 --- a/app/models/metrics/algorithms.rb +++ b/app/models/metrics/algorithms.rb @@ -48,8 +48,9 @@ def generate(month = nil) else count_matches(SearchEvent.includes(:term)) end - Metrics::Algorithms.create(month:, doi: matches[:doi], issn: matches[:issn], isbn: matches[:isbn], - lcsh: matches[:lcsh], pmid: matches[:pmid], journal_exact: matches[:journal_exact], + Metrics::Algorithms.create(month:, citation: matches[:citation], doi: matches[:doi], issn: matches[:issn], + isbn: matches[:isbn], lcsh: matches[:lcsh], pmid: matches[:pmid], + journal_exact: matches[:journal_exact], suggested_resource_exact: matches[:suggested_resource_exact], unmatched: matches[:unmatched]) end @@ -78,11 +79,25 @@ def count_matches(events) # @return does not return anything (the same matches Hash is passed in each loop but not explicitly sent back) def event_matches(event, matches) ids = match_standard_identifiers(event, matches) + citation = match_citations(event, matches) journal_exact = process_journals(event, matches) suggested_resource_exact = process_suggested_resources(event, matches) lcshs = match_lcsh(event, matches) - matches[:unmatched] += 1 if ids.detections.blank? && lcshs.detections.blank? && journal_exact.count.zero? && suggested_resource_exact.count.zero? + matches[:unmatched] += 1 if ids.detections.blank? && !citation.detection? && lcshs.detections.blank? && journal_exact.count.zero? && suggested_resource_exact.count.zero? + end + + # Checks for Citation matches + # + # @param event [SearchEvent] an individual search event to check for matches + # @param matches [Hash] a Hash that keeps track of how many of each algorithm we match, to which this method will + # add. + # @return [Boolean] an indication of whether a citation was detector or not. + def match_citations(event, matches) + result = Detector::Citation.new(event.term.phrase) + + matches[:citation] += 1 if result.detection? + result end # Checks for LCSH matches diff --git a/db/migrate/20241011181823_add_citation_to_metrics_algorithms.rb b/db/migrate/20241011181823_add_citation_to_metrics_algorithms.rb new file mode 100644 index 0000000..9e8d8de --- /dev/null +++ b/db/migrate/20241011181823_add_citation_to_metrics_algorithms.rb @@ -0,0 +1,5 @@ +class AddCitationToMetricsAlgorithms < ActiveRecord::Migration[7.1] + def change + add_column :metrics_algorithms, :citation, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index c20d917..3a9f440 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_10_01_205152) do +ActiveRecord::Schema[7.1].define(version: 2024_10_11_181823) do create_table "categories", force: :cascade do |t| t.string "name" t.text "description" @@ -94,6 +94,7 @@ t.integer "journal_exact" t.integer "suggested_resource_exact" t.integer "lcsh" + t.integer "citation" end create_table "search_events", force: :cascade do |t| diff --git a/test/fixtures/search_events.yml b/test/fixtures/search_events.yml index 2060684..f0f35a6 100644 --- a/test/fixtures/search_events.yml +++ b/test/fixtures/search_events.yml @@ -22,6 +22,9 @@ old_month_pmid: term: pmid_38908367 source: test created_at: <%= 1.year.ago %> +current_month_citation: + term: citation + source: test current_month_issn: term: issn_1075_8623 source: test diff --git a/test/fixtures/terms.yml b/test/fixtures/terms.yml index ad38915..a3258c1 100644 --- a/test/fixtures/terms.yml +++ b/test/fixtures/terms.yml @@ -27,7 +27,7 @@ doi: phrase: '10.1016/j.physio.2010.12.004' isbn_9781319145446: - phrase: 'Sadava, D. E., D. M. Hillis, et al. Life: The Science of Biology. 11th ed. W. H. Freeman, 2016. ISBN: 9781319145446' + phrase: 'Sadava, D. E., D. M. Hillis, et al. Life The Science of Biology. 11th ed. W. H. Freeman, 2016. ISBN: 9781319145446' journal_nature_medicine: phrase: 'nature medicine' diff --git a/test/models/metrics/algorithms_test.rb b/test/models/metrics/algorithms_test.rb index 7428096..959ab80 100644 --- a/test/models/metrics/algorithms_test.rb +++ b/test/models/metrics/algorithms_test.rb @@ -21,6 +21,12 @@ class Algorithms < ActiveSupport::TestCase # Monthlies + test 'citation counts are included in monthly aggregation' do + aggregate = Metrics::Algorithms.new.generate(DateTime.now) + + assert_equal 1, aggregate.citation + end + test 'dois counts are included in monthly aggregation' do aggregate = Metrics::Algorithms.new.generate(DateTime.now) @@ -85,6 +91,11 @@ class Algorithms < ActiveSupport::TestCase # drop all searchevents to make math easier and minimize fragility over time as more fixtures are created SearchEvent.delete_all + citation_expected_count = rand(1...100) + citation_expected_count.times do + SearchEvent.create(term: terms(:citation), source: 'test') + end + doi_expected_count = rand(1...100) doi_expected_count.times do SearchEvent.create(term: terms(:doi), source: 'test') @@ -117,6 +128,7 @@ class Algorithms < ActiveSupport::TestCase aggregate = Metrics::Algorithms.new.generate(DateTime.now) + assert_equal citation_expected_count, aggregate.citation assert_equal doi_expected_count, aggregate.doi assert_equal issn_expected_count, aggregate.issn assert_equal isbn_expected_count, aggregate.isbn @@ -126,6 +138,12 @@ class Algorithms < ActiveSupport::TestCase end # Total + test 'citation counts are included in total aggregation' do + aggregate = Metrics::Algorithms.new.generate + + assert_equal 1, aggregate.citation + end + test 'dois counts are included in total aggregation' do aggregate = Metrics::Algorithms.new.generate @@ -178,6 +196,11 @@ class Algorithms < ActiveSupport::TestCase # drop all searchevents to make math easier and minimize fragility over time as more fixtures are created SearchEvent.delete_all + citation_expected_count = rand(1...100) + citation_expected_count.times do + SearchEvent.create(term: terms(:citation), source: 'test') + end + doi_expected_count = rand(1...100) doi_expected_count.times do SearchEvent.create(term: terms(:doi), source: 'test') @@ -215,6 +238,7 @@ class Algorithms < ActiveSupport::TestCase aggregate = Metrics::Algorithms.new.generate + assert_equal citation_expected_count, aggregate.citation assert_equal doi_expected_count, aggregate.doi assert_equal issn_expected_count, aggregate.issn assert_equal isbn_expected_count, aggregate.isbn