|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class Detector |
| 4 | + class MlCitation |
| 5 | + attr_reader :detections |
| 6 | + |
| 7 | + # For now the initialize method just needs to consult the external lambda. |
| 8 | + # |
| 9 | + # @param phrase String. Often a `Term.phrase`. |
| 10 | + # @return Nothing intentional. Data is written to Hash `@detections` during processing. |
| 11 | + def initialize(phrase) |
| 12 | + return unless self.class.expected_env? |
| 13 | + |
| 14 | + response = fetch(phrase) |
| 15 | + @detections = response unless response == 'Error' |
| 16 | + end |
| 17 | + |
| 18 | + def detection? |
| 19 | + @detections == true |
| 20 | + end |
| 21 | + |
| 22 | + # expected_env? confirms that all three required environment variables are defined. It is provided for the Term |
| 23 | + # model to check prior to calling because this is still an optional extension to TACOS. If this method returns |
| 24 | + # false, the Term model will fall back to the initial citation detector. |
| 25 | + # |
| 26 | + # @return Boolean |
| 27 | + def self.expected_env? |
| 28 | + Rails.logger.error('No lambda URL defined') if lambda_url.nil? |
| 29 | + |
| 30 | + Rails.logger.error('No lambda path defined') if lambda_path.nil? |
| 31 | + |
| 32 | + Rails.logger.error('No lambda secret defined') if lambda_secret.nil? |
| 33 | + |
| 34 | + [lambda_url, lambda_path, lambda_secret].all?(&:present?) |
| 35 | + end |
| 36 | + |
| 37 | + # The record method runs a supplied term through the detector via its initialize method, which consults the lambda. |
| 38 | + # If a positive result is received, a Detection is registered. |
| 39 | + # |
| 40 | + # @param term [Term] |
| 41 | + # @return nil |
| 42 | + def self.record(term) |
| 43 | + result = Detector::MlCitation.new(term.phrase) |
| 44 | + return unless result.detection? |
| 45 | + |
| 46 | + # Detections are registered to the "MlCitation" detector for now, but may end up replacing the "Citation" detector |
| 47 | + # in a future step. |
| 48 | + Detection.find_or_create_by( |
| 49 | + term:, |
| 50 | + detector: Detector.where(name: 'MlCitation').first, |
| 51 | + detector_version: ENV.fetch('DETECTOR_VERSION', 'unset') |
| 52 | + ) |
| 53 | + |
| 54 | + nil |
| 55 | + end |
| 56 | + |
| 57 | + # lambda_path reads and returns the value of one environment variable. |
| 58 | + # |
| 59 | + # @note This is a public class method because the entire class ends up getting called in both class and instance |
| 60 | + # contexts, due to how detectors are built. The ideal state would be a private method, but that would require |
| 61 | + # changing how the class calls itself via the fetch method. |
| 62 | + # |
| 63 | + # @see Detector::MlCitation.expected_env? |
| 64 | + # @see Detector::MlCitation.fetch |
| 65 | + # @return String or nil |
| 66 | + def self.lambda_path |
| 67 | + ENV.fetch('DETECTOR_LAMBDA_PATH', nil) |
| 68 | + end |
| 69 | + |
| 70 | + # lambda_secret reads and returns the value of one environment variable. |
| 71 | + # |
| 72 | + # @note This is a public class method because the entire class ends up getting called in both class and instance |
| 73 | + # contexts, due to how detectors are built. The ideal state would be a private method, but that would require |
| 74 | + # changing how the class calls itself via the fetch method. |
| 75 | + # |
| 76 | + # @see Detector::MlCitation.expected_env? |
| 77 | + # @see Detector::MlCitation.fetch |
| 78 | + # @return String or nil |
| 79 | + def self.lambda_secret |
| 80 | + ENV.fetch('DETECTOR_LAMBDA_CHALLENGE_SECRET', nil) |
| 81 | + end |
| 82 | + |
| 83 | + # lambda_url reads and returns the value of one environment variable. |
| 84 | + # |
| 85 | + # @note This is a public class method because the entire class ends up getting called in both class and instance |
| 86 | + # contexts, due to how detectors are built. The ideal state would be a private method, but that would require |
| 87 | + # changing how the class calls itself via the fetch method. |
| 88 | + # |
| 89 | + # @see Detector::MlCitation.expected_env? |
| 90 | + # @see Detector::MlCitation.fetch |
| 91 | + # @return String or nil |
| 92 | + def self.lambda_url |
| 93 | + ENV.fetch('DETECTOR_LAMBDA_URL', nil) |
| 94 | + end |
| 95 | + |
| 96 | + private |
| 97 | + |
| 98 | + # define_lambda connects to the detector lambda. |
| 99 | + # |
| 100 | + # @return Faraday connection |
| 101 | + def define_lambda |
| 102 | + Faraday.new( |
| 103 | + url: self.class.lambda_url, |
| 104 | + params: {} |
| 105 | + ) |
| 106 | + end |
| 107 | + |
| 108 | + # define_payload defines the Hash that will be sent to the lambda. |
| 109 | + # |
| 110 | + # @return Hash |
| 111 | + def define_payload(phrase) |
| 112 | + { |
| 113 | + action: 'predict', |
| 114 | + features: extract_features(phrase), |
| 115 | + challenge_secret: self.class.lambda_secret |
| 116 | + } |
| 117 | + end |
| 118 | + |
| 119 | + # extract_features passes the search phrase through the citation detector, and massages the resulting features object |
| 120 | + # to correspond with what the lambda expects. |
| 121 | + # |
| 122 | + # @return Hash |
| 123 | + def extract_features(phrase) |
| 124 | + features = Detector::Citation.new(phrase).features |
| 125 | + features[:apa] = features.delete :apa_volume_issue |
| 126 | + features[:year] = features.delete :year_parens |
| 127 | + features.delete :characters |
| 128 | + features |
| 129 | + end |
| 130 | + |
| 131 | + # Fetch handles the communication with the detector lambda: defining the connection, building the payload, and any |
| 132 | + # error handling with the response. |
| 133 | + # |
| 134 | + # @return Boolean or 'Error' |
| 135 | + def fetch(phrase) |
| 136 | + lambda = define_lambda |
| 137 | + payload = define_payload(phrase) |
| 138 | + |
| 139 | + response = lambda.post(self.class.lambda_path, payload.to_json) |
| 140 | + |
| 141 | + if response.status == 200 |
| 142 | + JSON.parse(response.body)['response'] == 'true' |
| 143 | + else |
| 144 | + Rails.logger.error(response.body) |
| 145 | + Sentry.set_extras({ body: response.body }) |
| 146 | + Sentry.capture_message('Non-200 response received from detector lambda') |
| 147 | + |
| 148 | + 'Error' |
| 149 | + end |
| 150 | + end |
| 151 | + end |
| 152 | +end |
0 commit comments