Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby
- require 'csv'
- require 'date'
- require 'pathname'
- CSV::Converters.update({ force_date: lambda {|f| Date.strptime(f, '%Y/%m/%d') rescue f } })
- in_dir = '/home/data'
- out_dir = '/home/ranking'
- today = Date.today
- CSV.open(
- Pathname(out_dir).join((today - 1).strftime('%Y%m%d') + '.csv'), 'w',
- :headers => %w(RANKING USER TOTAL), :write_headers => true
- ) {|csv|
- Pathname(in_dir).children.select {|e| e.fnmatch('*.csv', File::FNM_CASEFOLD) }
- .map {|e|
- {
- user: e.basename(e.extname).to_s,
- total: CSV.read(
- e,
- :converters => [:force_date, :integer],
- :headers => :first_row,
- :return_headers => false
- ).select {|a| today - a['DATE'] == 1 }
- .inject(0) {|s, a| s += a['DATA1'] + a['DATA2'] }
- }
- }.sort_by {|e| -e[:total] }
- .each_with_index {|e, i| csv << [i + 1, e[:user], e[:total]] }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement