Navigation Menu

Skip to content

Commit

Permalink
Redshift query access (#3058)
Browse files Browse the repository at this point in the history
* Revert "Revert "Fetch confirmation info from Redshift" (#3046)"

This reverts commit 968f443.

* Fix database.yml
* Fix rubocop lint

Co-authored-by: Albert Wang <=>
  • Loading branch information
yachtcaptain23 committed Feb 24, 2021
1 parent 54aeca6 commit 5f300b9
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions .irbrc
@@ -0,0 +1 @@
IRB.conf[:USE_MULTILINE] = false
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -12,6 +12,8 @@ gem "active_model_serializers", "~> 0.10.0"
# ActiveRecord Session store for server side storage of session data
gem 'activerecord-session_store', '~> 1.1', '>= 1.1.3'

gem 'activerecord6-redshift-adapter', '~> 1.1.3'

# Allowing for URI templates, for HTTP clients
gem 'addressable', '~> 2.6'

Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Expand Up @@ -74,6 +74,9 @@ GEM
multi_json (~> 1.11, >= 1.11.2)
rack (>= 1.5.2, < 3)
railties (>= 4.0)
activerecord6-redshift-adapter (1.1.3)
activerecord (~> 6.0, >= 6.0.0)
pg (>= 0.18)
activestorage (6.0.3.5)
actionpack (= 6.0.3.5)
activejob (= 6.0.3.5)
Expand Down Expand Up @@ -523,6 +526,7 @@ PLATFORMS
DEPENDENCIES
active_model_serializers (~> 0.10.0)
activerecord-session_store (~> 1.1, >= 1.1.3)
activerecord6-redshift-adapter (~> 1.1.3)
addressable (~> 2.6)
attr_encrypted (~> 3.1.0)
autometal-piwik!
Expand Down
19 changes: 19 additions & 0 deletions app/jobs/promo/email_breakdowns_job.rb
Expand Up @@ -13,12 +13,31 @@ def perform(publisher_id)
include_ratios: false
).perform)
new_csv = []
header = true
csv.each do |row|
row = row.dup
row.delete_at(5) # delete the 30-day-confirmation column
row.delete_at(2) # delete Day column
if header
row << "Confirmations received"
header = false
else
row << total_for_date(date: 1.days.ago.to_date,
referral_code: row[0],
country: row[1],
publisher_id: publisher_id)
end
new_csv << row.join(",")
end
publisher = Publisher.find_by(id: publisher_id)
PublisherMailer.promo_breakdowns(publisher, new_csv.join("\n")).deliver_now
end

def total_for_date(date:, referral_code:, country:, publisher_id:)
ReferralDownload.where(finalized: true,
owner_id: publisher_id,
referral_code: referral_code,
country: country,
ymd: date - 30.days).count
end
end
3 changes: 3 additions & 0 deletions app/models/stats_redshift/referral_download.rb
@@ -0,0 +1,3 @@
class ReferralDownload < ApplicationRecord
connects_to database: { writing: :stats_redshift, reading: :stats_redshift }
end
2 changes: 1 addition & 1 deletion config/application.rb
Expand Up @@ -21,7 +21,7 @@ class Application < Rails::Application

config.active_job.queue_adapter = :sidekiq

config.autoload_paths += %W(#{config.root}/app/services/ #{config.root}/lib #{config.root}/app/validators/ #{config.root}/lib/devise #{config.root}/protos)
config.autoload_paths += %W(#{config.root}/app/services/ #{config.root}/lib #{config.root}/app/validators/ #{config.root}/lib/devise #{config.root}/protos #{config.root}/app/models/stats_redshift)

config.exceptions_app = routes

Expand Down
18 changes: 18 additions & 0 deletions config/database.yml
Expand Up @@ -7,6 +7,18 @@ default: &default
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

stats_redshift_default: &stats_redshift_default
adapter: redshift
encoding: utf8
pool: 3
port: <%= ENV["STATS_REDSHIFT_PORT"] %>
host: <%= ENV["STATS_REDSHIFT_HOST"] %>
database: <%= ENV["STATS_REDSHIFT_DBNAME"] %>
username: <%= ENV["STATS_REDSHIFT_USER"] %>
password: <%= ENV["STATS_REDSHIFT_PASSWORD"] %>
replica: true
schema_search_path: "stats"

development:
primary:
<<: *default
Expand All @@ -15,6 +27,8 @@ development:
<<: *default
url: <%= ENV['DATABASE_URL'] || "postgres://postgres@localhost/brave_publishers_dev" %>
replica: true
stats_redshift:
<<: *stats_redshift_default

test:
primary:
Expand All @@ -40,6 +54,8 @@ production:
<<: *default
url: <%= ENV['FOLLOWER_DATABASE_URL'] %>
replica: true
stats_redshift:
<<: *stats_redshift_default

staging:
primary:
Expand All @@ -49,3 +65,5 @@ staging:
<<: *default
url: <%= ENV['DATABASE_URL'] %>
replica: true
stats_redshift:
<<: *stats_redshift_default

0 comments on commit 5f300b9

Please sign in to comment.