Skip to content

Instantly share code, notes, and snippets.

@andreyvit
Created July 6, 2011 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreyvit/1066891 to your computer and use it in GitHub Desktop.
Save andreyvit/1066891 to your computer and use it in GitHub Desktop.
summarize_beeline_stats.rb
require 'rubygems'
require 'hpricot'
require 'active_support'
class Array
def sum
inject(0) { |a,b| a + b }
end
end
Item = Struct.new(:group, :date, :amount)
Group = Struct.new(:name, :total_amount, :items)
doc = Hpricot::XML(File.read('/Users/andreyvit/Downloads/1817958474_1f.xls'))
stats = (doc / %q<Row>).collect { |row|
cells = row / 'Cell'
group = (cells[0] / 'Data').text
next unless true ... group == 'Бонус-баланс'
if cells.size < 7 || (cells[1] / 'Data').text == 'Дата звонка'
nil
else
date = DateTime.parse((cells[1] / 'Data').text).strftime('%Y-%m-%d')
sum = (cells[6] / 'Data').text.gsub(',', '.').to_f
Item.new(group, date, sum)
end
}.compact.group_by(&:group).collect do |group, items|
by_date = items.group_by(&:date).collect { |date, date_items| Item.new(group, date, date_items.collect(&:amount).sum) }.sort_by(&:date)
total = by_date.collect(&:amount).sum
Group.new(group, total, by_date)
end
stats.each do |group|
puts
puts "#{group.name} (TOTAL: #{group.total_amount})"
group.items.each { |item| puts sprintf("%10s %5.2f", item.date, item.amount) }
end
puts
stats.each do |group|
puts "#{group.name}: #{group.total_amount}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment