Created
August 9, 2013 15:31
-
-
Save ifyouseewendy/6194569 to your computer and use it in GitHub Desktop.
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
DATES = ["2012-01-01", | |
"2012-02-01", | |
"2012-03-01", | |
"2012-04-01", | |
"2012-05-01", | |
"2012-06-01", | |
"2012-07-01", | |
"2012-08-01", | |
"2012-09-01", | |
"2012-10-01", | |
"2012-11-01", | |
"2012-12-01", | |
"2013-01-01", | |
"2013-02-01", | |
"2013-03-01", | |
"2013-04-01", | |
"2013-05-01", | |
"2013-06-01", | |
"2013-07-01", | |
"2013-08-01"] | |
# STEP = 5 | |
STEP = 10000 | |
def commander | |
threads = [] | |
# rounds = 28 / STEP | |
rounds = App.count / STEP | |
t= Time.now | |
puts "=="*30+" COMMANDER ROAR! "+"=="*30 | |
puts t | |
(0..rounds).each do |n| | |
threads << Thread.new(n) { siyu_job(n) } | |
end | |
threads.map(&:join) | |
puts "=="*30+" COMMANDER TIRED! "+"=="*30 | |
puts (Time.now-t).to_s | |
end | |
def siyu_job(num) | |
ff = File.new("tmp/siyu.data-#{num.to_s}", 'w') | |
fa = File.new("tmp/siyu.failed-#{num.to_s}", 'w') | |
header = "AppName\tAppKey\tUserName\tCreateDate\tPlatform\tAppType\tTotalUser\tTotalLaunch\t"+DATES.join("\t")+"\t"+DATES.join("\t")+"\t"+DATES.join("\t") | |
ff.puts header | |
i = 0 | |
t= Time.now | |
puts "="*20+" JOB #{num} " | |
puts t | |
# u = User.where(:name => 'chenyukun').first | |
# u.apps.skip(num*STEP).limit(STEP).each do |ap| | |
App.skip(num*STEP).limit(STEP).each do |ap| | |
begin | |
next if [User.admin_user, User.recycle_user].include? ap.user | |
ac = ap.app_counter | |
total_user = ac.try(:installations) || 'wendi-set-null' | |
total_launch = ac.try(:launches) || 'wendi-set-null' | |
ins = Installation.new(ap._id.to_s, '2012-01-01', '2013-08-08').get_monthly.try(:last) || [] | |
act = ActiveUser.new(ap._id.to_s, '2012-01-01', '2013-08-08').get_monthly.try(:last) || [] | |
lau = Launch.new(ap._id.to_s, '2012-01-01', '2013-08-08').get_monthly.try(:last) || [] | |
ins_count = ins.count | |
act_count = act.count | |
lau_count = lau.count | |
(DATES.count - ins_count).times { ins.unshift '-' } | |
(DATES.count - act_count).times { act.unshift '-' } | |
(DATES.count - lau_count).times { lau.unshift '-' } | |
str = "#{ap.name}\t#{ap._id.to_s}\t#{ap.user.name || 'wendi-set-null'}\t#{ap.created_at.to_date.to_s}\t#{ap.platform || 'wendi-set-null'}\t#{ap.category || 'wendi-set-null'}\t#{total_user}\t#{total_launch}\t"+ins.join("\t")+"\t"+act.join("\t")+"\t"+lau.join("\t") | |
ff.puts str | |
i += 1 | |
puts i if i%1000 == 0 | |
rescue => e | |
print '!' | |
fa.puts ap._id.to_s | |
end | |
end | |
puts "=="*20+" JOB #{num} END " | |
puts (Time.now-t).to_s + " s" | |
ff.close | |
fa.close | |
end | |
commander |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment