Skip to content

Commit

Permalink
Fixed error when creating teams
Browse files Browse the repository at this point in the history
  • Loading branch information
just3ws committed Nov 15, 2014
1 parent 07fa50d commit 73fe695
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 632 deletions.
278 changes: 0 additions & 278 deletions Rakefile
Expand Up @@ -4,281 +4,3 @@ require 'rake'
Coderwall::Application.load_tasks

task default: :spec

namespace :team do
task migrate: :environment do
puts '--- Beginning team migration ---'
success = true
begin
Team.each do |team|
begin
puts ">>> Migrating #{team.id}"
TeamMigratorJob.new.perform(team.id.to_s)
rescue => ex
success = false
puts "[#{team.id.to_s}] #{ex} >>\n#{ex.backtrace.join("\n ")}"
end
end
ensure
puts "--- #{success ? 'Successful' : 'Unsuccessful'} team migration ---"
end
end

#
# IMPORTANT: pending_join_requests is a STRING array in Postgres but an INTEGER array in MongoDB.
# IMPORTANT: pending_join_requests is an array of User#id values
#

task verify: :environment do
#ActiveRecord::Base.logger = nil
#Mongoid.logger = nil
#Moped.logger = nil

PgTeam.find_each(batch_size: 100) do |pg_team|
begin
mongo_id = pg_team.mongo_id
mongo_team = Team.find(mongo_id)

# Ignoring:
# - updated_at

puts 'TEAM'

neq(:slug, pg_team, mongo_team, false)

neq_string(:pending_join_requests, pg_team, pg_team.pending_join_requests.map(&:to_i).sort.join(', '), mongo_team, mongo_team.pending_join_requests.map(&:to_i).sort.join(', '), false)

%i(score size total mean median).each do |attr|
neq_dec(attr, pg_team, mongo_team, false)
end

%i(about achievement_count analytics benefit_description_1 benefit_description_2 benefit_description_3 benefit_name_1 benefit_name_2 benefit_name_3 big_image big_quote blog_feed branding country_id created_at endorsement_count facebook featured_banner_image featured_links_title github github_organization_name headline hide_from_featured highlight_tags hiring_tagline interview_steps invited_emails link_to_careers_page location monthly_subscription name number_of_jobs_to_show office_photos organization_way organization_way_name organization_way_photo our_challenge paid_job_posts premium preview_code reason_description_1 reason_description_2 reason_description_3 reason_name_1 reason_name_2 reason_name_3 size stack_list twitter upcoming_events upgraded_at valid_jobs website why_work_image your_impact youtube_url).each do |attr|
neq(attr, pg_team, mongo_team)
end

# TODO: Account
if mongo_team.account.present? && pg_team.account.blank?
puts "account | pg:#{pg_team.id} | mongo:#{mongo_team.id}| The account was not migrated."
end

if mongo_team.account.present? && pg_team.account.present?
check_plans = %i(stripe_card_token stripe_customer_token admin_id).map do |attr|
neq(attr, pg_team.account, mongo_team.account)
end.any? { |x| !x }

# TODO: Plans
if check_plans
left = pg_team.account.plans.pluck(:id).sort
right = mongo_team.account.plan_ids.sort

if left != right
puts "account.plans | pg:#{pg_team.id} | mongo:#{mongo_team.id}| #{left} != #{right}"
end
end
end

#puts 'LOCATIONS'

#pg_team_locations = pg_team.locations
#mongo_team_locations = mongo_team.team_locations

#if mongo_team_locations.count != pg_team_locations.count
#puts "locations | pg:#{pg_team.id} | mongo:#{mongo_team.id}| #{mongo_team_locations.count} != #{pg_team_locations.count}"
#end

## Ignoring:
## - points_of_interest
#pg_team.locations.each do |pg_team_location|
#mongo_team_location = mongo_team.team_locations.select { |tl| tl.name == pg_team_location.name }.first

#%i(address city country description name state_code).each do |attr|
#neq(attr, pg_team_location, mongo_team_location, false)
#end
#end


#puts 'LINKS'

pg_team_links = pg_team.links
mongo_team_links = mongo_team.featured_links

if mongo_team_links.count != pg_team_links.count
puts "links | pg:#{pg_team.id} | mongo:#{mongo_team.id}| #{mongo_team_links.count} != #{pg_team_links.count}"
end

pg_team_links.each do |pg_team_link|
mongo_team_link = mongo_team_links.select { |tl| tl.name == pg_team_link.name }.first

%i(url name).each do |attr|
neq(attr, pg_team_link, mongo_team_link, false)
end
end

#puts 'MEMBERS'

if pg_team.members.count != mongo_team.team_members.count
puts "members | pg:#{pg_team.id} | mongo:#{mongo_team.id}| #{pg_team.members.count} < #{mongo_team.team_members.count}"
end


#puts 'JOBS'

#pg_team.jobs.each do |pg_team_job|
#mongo_team_job = Team.where(id: pg_team_job.team_document_id.to_s).first

#neq(:name, pg_team_job, mongo_team_job, false)
#end

#puts 'FOLLOWERS'

pg_team.followers.each do |pg_team_follower|
mongo_team_follower = Team.where(id: pg_team_follower.mongo_id.to_s).first
# admins
# editors
%i(
about
achievement_count
analytics
benefit_description_1
benefit_description_2
benefit_description_3
benefit_name_1
benefit_name_2
benefit_name_3
big_image
big_quote
blog_feed
branding
country_id
created_at
endorsement_count
facebook
featured_banner_image
featured_links_title
github_organization_name
headline
hide_from_featured
highlight_tags
hiring_tagline
interview_steps
invited_emails
link_to_careers_page
location
monthly_subscription
name
number_of_jobs_to_show
office_photos
organization_way
organization_way_name
organization_way_photo
our_challenge
paid_job_posts
premium
preview_code
reason_description_1
reason_description_2
reason_description_3
reason_name_1
reason_name_2
reason_name_3
slug
stack_list
twitter
upcoming_events
upgraded_at
valid_jobs
website
why_work_image
your_impact
youtube_url
).each do |attr|
neq(attr, pg_team_follower, mongo_team_follower, false)
end
neq_string(:pending_join_requests, pg_team_follower, pg_team_follower.pending_join_requests.map(&:to_i).sort.join(', '), mongo_team_follower, mongo_team_follower.pending_join_requests.map(&:to_i).sort.join(', '), false)

neq_string(:avatar, pg_team_follower, pg_team_follower.avatar.url, mongo_team_follower, mongo_team_follower.avatar.url, false)

%i(score size total mean median).each do |attr|
neq_dec(attr, pg_team_follower, mongo_team_follower, false)
end
end

# TODO: Pending Requests
end
end
end

def neq(attr, pg, mongo, fail_if_neq=true)
left = pg.send(attr)
right = mongo.send(attr)

if left != right
puts "#{attr} | pg:#{pg.id} | mongo:#{mongo.id}| #{left} != #{right}"
true
else
false
end
rescue => ex
print_neq_error(ex)
end

def neq_string(attr, pg, left, mongo, right, fail_if_neq=true)
if left != right
puts "#{attr} | pg:#{pg.id} | mongo:#{mongo.id}| #{left} != #{right}"
true
else
false
end
rescue => ex
print_neq_error(ex)
end

def neq_dec(attr, pg, mongo, fail_if_neq=true)
scale = 7

left = pg.send(attr).to_d.round(scale)
right = mongo.send(attr).to_d.round(scale)


if left != right
puts "#{attr} | pg:#{pg.id} | mongo:#{mongo.id}| #{left} != #{right}"
true
else
false
end
rescue => ex
print_neq_error(ex)
end

def print_neq_error(ex)
puts '*'*80
puts
puts ex
puts
puts '-'*80
puts
ap ex.backtrace
puts
puts '*'*80
end

task counts: :environment do
pg_team_count = PgTeam.count
puts "PgTeam.count=#{pg_team_count}"
team_count = Team.count
puts "Team.count=#{team_count}"
puts "Unmigrated teams count=#{(team_count - pg_team_count)}"
end


task unmigrated: :environment do
unmigrated_teams = []

Team.all.each do |team|
unmigrated_teams << team.id.to_s unless PgTeam.where(mongo_id: team.id.to_s).exists?
end

puts "Unmigrated teams count=#{unmigrated_teams.count}"
puts "Unmigrated Teams=%w(#{unmigrated_teams.join(' ')})"
end
end
Expand Up @@ -5,7 +5,7 @@
%img.team-avatar{:bind => {:src => 'team.avatar'}}
%span=hb 'team.name'
%td.members.span3
=hb 'each team.team_members' do
=hb 'each team.members' do
%a{:bind => {:href => 'profile_path' }}
%img.thumb{:bind => {:src => 'avatar'}}
=hb 'if team.has_more_than_min_members' do
Expand Down
10 changes: 7 additions & 3 deletions app/controllers/teams_controller.rb
Expand Up @@ -7,9 +7,11 @@ class TeamsController < ApplicationController

def index
current_user.seen(:teams) if signed_in?
@featured_teams = Rails.cache.fetch(Team::FEATURED_TEAMS_CACHE_KEY, :expires_in => 4.hours) do
Team.featured.sort_by(&:relevancy).reject { |team| team.jobs.count == 0 }.reverse!
end
#@featured_teams = Rails.cache.fetch(Team::FEATURED_TEAMS_CACHE_KEY, expires_in: 4.hours) do
@featured_teams = Team.featured.sort_by(&:relevancy).reject do |team|
team.jobs.count == 0
end.reverse!
#end
@teams = []
end

Expand Down Expand Up @@ -62,6 +64,7 @@ def show
format.json do
options = { :expires_in => 5.minutes }
options[:force] = true if !show_params[:refresh].blank?
Team
response = Rails.cache.fetch(['v1', 'team', show_params[:id], :json], options) do
begin
@team = team_from_params(slug: show_params[:slug], id: show_params[:id])
Expand Down Expand Up @@ -309,6 +312,7 @@ def page_based_on_rank(rank)
end

def job_public_ids
Opportunity
Rails.cache.fetch('all-jobs-public-ids', :expires_in => 1.hour) { Opportunity.select(:public_id).group('team_id, created_at, public_id').map(&:public_id) }
end

Expand Down
6 changes: 3 additions & 3 deletions app/jobs/process_team_job.rb
Expand Up @@ -7,12 +7,12 @@ def perform(process_type, team_id)
team = Team.find(team_id)
case process_type
when 'recalculate'
if team.team_members.size <= 0
if team.members.size <= 0
team.destroy
Redis.current.zrem(Team::LEADERBOARD_KEY, team.id.to_s)
else
team.recalculate!
if team.team_members.size < 3
if team.members.size < 3
Redis.current.zrem(Team::LEADERBOARD_KEY, team.id.to_s)
else
Redis.current.zadd(Team::LEADERBOARD_KEY, team.score.to_f, team.id.to_s)
Expand All @@ -24,4 +24,4 @@ def perform(process_type, team_id)
end
end
end
end
end
14 changes: 0 additions & 14 deletions app/jobs/team_migrator_batch_job.rb

This file was deleted.

0 comments on commit 73fe695

Please sign in to comment.