diff --git a/app/models/feedback.rb b/app/models/feedback.rb index 231c390..77f4af7 100644 --- a/app/models/feedback.rb +++ b/app/models/feedback.rb @@ -20,8 +20,23 @@ def result_ratings_attributes=(attributes) def save return false unless valid? - # TODO: Implement me! - Rails.logger.info "Feedback submitted: #{inspect}" - true + dataset = Google::Cloud::Bigquery.new.dataset(Rails.configuration.bigquery_dataset) + table = dataset.table(Rails.configuration.bigquery_table) + + data = to_bigquery_row + table.insert(data) + Rails.logger.info "Feedback submitted: #{data.inspect}" + end + + def to_bigquery_row + { + query_id: SecureRandom.uuid, + anonymised_user_id: "not-yet-available", + timestamp: Time.zone.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%3NZ"), + search_query:, + result_ratings: result_ratings.map(&:to_bigquery_data), + suggested_url:, + comments:, + } end end diff --git a/app/models/result_rating.rb b/app/models/result_rating.rb index f501d6b..c721783 100644 --- a/app/models/result_rating.rb +++ b/app/models/result_rating.rb @@ -15,4 +15,21 @@ def self.rating_options end def id = content_id + + def to_bigquery_data + { + content_id:, + url:, + position: position + 1, # have it 1-indexed in BigQuery + rating:, + } + end + + def url + if link.start_with?("/") + Plek.new.website_root + link + else + link + end + end end