Skip to content

Commit

Permalink
Refactors to Metrics::Algorithms
Browse files Browse the repository at this point in the history
Moves both types of metrics on algorithms to use a single class and
associated database table.

Creating the namespace `Metrics` should allow us to better organize code
associated with various metrics into a common space that is both
intuitive and useful.

Relevant ticket(s):
https://mitlibraries.atlassian.net/browse/TCO-17
  • Loading branch information
JPrevost committed Jul 10, 2024
1 parent 1f33926 commit 044b1a0
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 337 deletions.
36 changes: 0 additions & 36 deletions app/models/aggregate_match.rb

This file was deleted.

31 changes: 0 additions & 31 deletions app/models/concerns/match_counter.rb

This file was deleted.

72 changes: 72 additions & 0 deletions app/models/metrics/algorithms.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: metrics_algorithms
#
# id :integer not null, primary key
# month :date
# doi :integer
# issn :integer
# isbn :integer
# pmid :integer
# unmatched :integer
# created_at :datetime not null
# updated_at :datetime not null
#
module Metrics
# Algorithms aggregates statistics for matches for all SearchEvents
class Algorithms < ApplicationRecord
self.table_name = 'metrics_algorithms'

# generate metrics data about SearchEvents matches
#
# @note This is expected to only be run once per month per type of aggregation (once with no month supplied, once
# with a month supplied), ideally at the beginning of the following month to ensure as
# accurate as possible statistics. Running further from the month in question will work, but matches will use the
# current versions of all algorithms which may not match the algorithm in place during the month the SearchEvent
# occurred.
# @note We don't currently prevent this running more than once per month per type of aggregation.
# @param month [DateTime] A DateTime object within the `month` to be generated. Defaults to nil will runs is how
# total algorithm statistics are created.
# @example
# # Generate metrics for all SearchEvents
# Metrics::Algorithms.new.generate
#
# # Generate metrics for all SearchEvents last month
# Metrics::Algorithms.new.generate(1.month.ago)
# @return [Metrics::Algorithms] The created Metrics::Algorithms object.
def generate(month = nil)
matches = if month.present?
count_matches(SearchEvent.single_month(month).includes(:term))
else
count_matches(SearchEvent.all.includes(:term))
end
Metrics::Algorithms.create(month:, doi: matches[:doi], issn: matches[:issn], isbn: matches[:isbn],
pmid: matches[:pmid], unmatched: matches[:unmatched])
end

# Counts matches supplied events
#
# @note We currently only have StandardIdentifiers to match. As we add new algorithms, this method will need to
# expand to handle additional match types.
# @param events [Array of SearchEvents] An array of SearchEvents to check for matches.
# @return [Hash] A Hash with keys for each known algorithm and the count of matched SearchEvents.
def count_matches(events)
matches = Hash.new(0)
known_ids = %i[unmatched pmid isbn issn doi]

events.each do |event|
ids = StandardIdentifiers.new(event.term.phrase)

matches[:unmatched] += 1 if ids.identifiers.blank?

known_ids.each do |id|
matches[id] += 1 if ids.identifiers[id].present?
end
end

matches
end
end
end
38 changes: 0 additions & 38 deletions app/models/monthly_match.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class CreateMonthlyMatches < ActiveRecord::Migration[7.1]
class CreateMetricsAlgorithms < ActiveRecord::Migration[7.1]
def change
create_table :monthly_matches do |t|
create_table :metrics_algorithms do |t|
t.date :month
t.integer :doi
t.integer :issn
Expand Down
12 changes: 0 additions & 12 deletions db/migrate/20240621132150_create_aggregate_matches.rb

This file was deleted.

14 changes: 2 additions & 12 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 0 additions & 23 deletions test/fixtures/aggregate_matches.yml

This file was deleted.

24 changes: 0 additions & 24 deletions test/fixtures/monthly_matches.yml

This file was deleted.

79 changes: 0 additions & 79 deletions test/models/aggregate_match_test.rb

This file was deleted.

Loading

0 comments on commit 044b1a0

Please sign in to comment.