-
Notifications
You must be signed in to change notification settings - Fork 313
Updated Popular Protips emailer with New Relic promotion option #189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cf49296
Only force logging to STDOUT in Heroku production. It's just annoying…
just3ws 6316853
[WIP#335] Update old digest email to send monthly popular protips
just3ws 1592cd2
Added worker and schedule for running the popular protips emailer
just3ws 7b8fc06
Updated the popular protip email to include a message about Assembly
just3ws b19f556
Restore the call to render the new relic partial
just3ws 6f6d4e7
Restored the New Relic integration
just3ws 5289d5c
Revert "Removed New Relic T-Shirt offer leftover file"
just3ws f29ae49
Conditionally display the NEW_RELIC_PROMOTION via ENV variable
just3ws ae2a0b4
Removed redundant configuration for STDOUT logging
just3ws File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class MailPreview < MailView | ||
USERNAME = 'just3ws' | ||
|
||
def popular_protips | ||
from = 60.days.ago | ||
to = 0.days.ago | ||
user = User.with_username(USERNAME) | ||
protips = ProtipMailer::Queries.popular_protips(from, to) | ||
ProtipMailer.popular_protips(user, protips, from, to).deliver | ||
end | ||
|
||
def old_weekly_digest | ||
WeeklyDigestMailer.weekly_digest(USERNAME) | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
class ProtipMailer < ActionMailer::Base | ||
include ActionView::Helpers::TextHelper | ||
|
||
add_template_helper(UsersHelper) | ||
add_template_helper(ProtipsHelper) | ||
add_template_helper(ApplicationHelper) | ||
|
||
default_url_options[:host] = 'coderwall.com' | ||
default_url_options[:only_path] = false | ||
default from: '"Coderwall" <support@coderwall.com>' | ||
|
||
SPAM_NOTICE = "You're receiving this email because you signed up for Coderwall. We hate spam and make an effort to keep notifications to a minimum. To change your notification preferences, you can update your email settings here: http://coderwall.com/settings#email or immediately unsubscribe by clicking this link %unsubscribe_url%" | ||
STARS = { | ||
protip_upvotes: 'pro tip upvotes', | ||
followers: 'followers', | ||
endorsements: 'endorsements', | ||
protips_count: 'protips' | ||
} | ||
ACTIVITY_SUBJECT_PREFIX = '[Coderwall]' | ||
|
||
################################################################################# | ||
def popular_protips(user, protips, from, to) | ||
fail 'Protips are required.' if protips.nil? || protips.empty? | ||
fail 'User is required.' unless user | ||
fail 'From date is required.' unless from | ||
fail 'To date is required.' unless to | ||
|
||
headers['X-Mailgun-Campaign-Id'] = 'protip_mailer-popular_protips' | ||
|
||
@user = user | ||
@protips = protips | ||
@team, @job = get_team_and_job_for(@user) | ||
@issue = campaign_params | ||
|
||
stars = @user.following_users.where('last_request_at > ?', 1.month.ago) | ||
@star_stat = star_stat_for_this_week | ||
@star_stat_string = STARS[@star_stat] | ||
|
||
@most = star_stats(stars).sort_by do |star| | ||
-star[@star_stat] | ||
end.first | ||
@most = nil if @most && (@most[@star_stat] <= 0) | ||
|
||
mail(to: @user.email, subject: "#{ACTIVITY_SUBJECT_PREFIX} Popular Protips on Coderwall") | ||
rescue Exception => ex | ||
abort_delivery(ex) | ||
end | ||
################################################################################# | ||
|
||
def abort_delivery(ex) | ||
Rails.logger.error("[ProtipMailer.popular_protips] Aborted email '#{ex}' >>\n#{ex.backtrace.join("\n ")}") | ||
end | ||
|
||
def campaign_params | ||
{ | ||
utm_campaign: 'coderwall-popular_protips', | ||
utm_content: Date.today.midnight, | ||
utm_medium: 'email' | ||
} | ||
end | ||
|
||
def star_stat_for_this_week | ||
STARS.keys[week_of_the_month % 4] | ||
end | ||
|
||
def star_stats(stars, since=1.week.ago) | ||
stars.collect { |star| star.activity_stats(since, true) }.each_with_index.map { |stat, index| stat.merge(user: stars[index]) } | ||
end | ||
|
||
def week_of_the_month | ||
Date.today.cweek - Date.today.at_beginning_of_month.cweek | ||
end | ||
|
||
def get_team_and_job_for(user) | ||
if user.team.try(:hiring?) | ||
[user.team, user.team.jobs.sample] | ||
else | ||
teams = teams_for_user(user) | ||
teams.each do |team| | ||
best_job = team.best_positions_for(user).detect do |job| | ||
job.team_document_id == user.team_document_id | ||
end | ||
return [team, best_job] unless best_job.nil? | ||
end | ||
end | ||
[nil, nil] | ||
end | ||
|
||
def teams_for_user(user) | ||
Team.most_relevant_featured_for(user).select do |team| | ||
team.hiring? | ||
end | ||
end | ||
|
||
module Queries | ||
def self.popular_protips(from, to) | ||
search_results = ProtipMailer::Queries.search_for_popular_protips(from, to) | ||
public_ids = search_results.map { |protip| protip['public_id'] } | ||
|
||
Protip.eager_load(:user, :comments).where('public_id in (?)', public_ids) | ||
end | ||
|
||
def self.search_for_popular_protips(from, to, max_results=10) | ||
url = "#{ENV['ELASTICSEARCH_URL']}/#{ENV['ELASTICSEARCH_PROTIPS_INDEX']}/_search" | ||
query = { | ||
'query' => { | ||
'bool' => { | ||
'must' => [ | ||
{ | ||
'range' => { | ||
'protip.created_at' => { | ||
'from' => from.strftime('%Y-%m-%d'), | ||
'to' => to.strftime('%Y-%m-%d') | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
'size' => max_results, | ||
'sort' => [ | ||
{ | ||
'protip.popular_score' => { | ||
'order' => 'desc' | ||
} | ||
} | ||
] | ||
} | ||
response = RestClient.post(url, MultiJson.dump(query), content_type: :json, accept: :json) | ||
# TODO: check for response code | ||
MultiJson.load(response.body)['hits']['hits'].map { |protip| protip['_source'] } | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WTF ?