Skip to content

Instantly share code, notes, and snippets.

@Electron-libre
Created April 11, 2011 12:26
Show Gist options
  • Save Electron-libre/913430 to your computer and use it in GitHub Desktop.
Save Electron-libre/913430 to your computer and use it in GitHub Desktop.
compute reporting for destination groups
monthly_reports = []
date = '10-10'
Journal.only('_id').where(:account.in => accounts, :date => Regexp.new(date)).each do |j|
j.reload
entries = j.billing_entries.to_a
existing_report_for_account = monthly_reports.find_index do |account|
account[:journal] == j.account
end
if existing_report_for_account
monthly_report = monthly_reports[existing_report_for_account]
else
monthly_report = {:journal => j.account}
end
destinations_groups = {
:total => ['select', '.*'],
:france => ['select', '(france_fixe|france_olo)'],
:france_mobile => ['select', 'france_(gsm|mobile)'],
:etranger => ['reject', 'france']
}
destinations_groups.each do |destination_group, pattern|
unless monthly_report[destination_group]
monthly_report[destination_group] = {}
end
%w(count duration total_cost).each do |indicator|
block = Proc.new { |e| e.destination =~ Regexp.new(pattern[1]) }
value = case indicator
when 'count'
entries.__send__( pattern[0].to_sym, &block ).count
else
entries.__send__( pattern[0].to_sym, &block).sum(&indicator.to_sym)
end
if monthly_report[destination_group][indicator] != nil
monthly_report[destination_group][indicator] += value
else
monthly_report[destination_group][indicator] = value
end
end
end
(existing_report_for_account != nil) && (monthly_reports[existing_report_for_account] = monthly_report) || monthly_reports << monthly_report
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment