-
Notifications
You must be signed in to change notification settings - Fork 20
Examples
schacon edited this page Sep 12, 2010
·
6 revisions
The following examples are from the MungerData tutorial pages.
result = AdAirings.find(:all) report = Munger::Report.from_data(result).process puts Munger::Render.to_text(report)
will result in:
|airtime | airdate | clicks | advert | ------------------------------------------ |15 | 2008-01-01 | 301 | spot 1 | |30 | 2008-01-02 | 199 | spot 1 | |30 | 2008-01-03 | 234 | spot 1 | |15 | 2008-01-04 | 342 | spot 1 | |30 | 2008-01-01 | 172 | spot 2 | |15 | 2008-01-02 | 217 | spot 2 | |90 | 2008-01-03 | 1023 | spot 2 | |30 | 2008-01-04 | 321 | spot 2 | |60 | 2008-01-01 | 512 | spot 3 | |30 | 2008-01-02 | 813 | spot 3 | |15 | 2008-01-03 | 333 | spot 3 |
result = AdAirings.find(:all) data = Munger::Data.load_data(result) new_columns = data.pivot('airdate', 'advert', 'clicks') report = Munger::Report.from_data(data).columns([:advert] + new_columns.sort).process puts Munger::Render.to_text(report)
|advert | 2008-01-01 | 2008-01-02 | 2008-01-03 | 2008-01-04 | -------------------------------------------------------------- |Spot 1 | 301 | 199 | 234 | 342 | |Spot 2 | 172 | 217 | 1023 | 321 | |Spot 3 | 512 | 813 | 333 | |
result = AdAirings.find(:all) data = Munger::Data.load_data(result) data.add_columns([:advert, :rate]) do |row| rate = (row.clicks / row.airtime) [row.advert.capitalize, rate] end report = Munger::Report.from_data(data) report.sort('airtime').subgroup('airtime') report.aggregate(Proc.new {|arr| arr.inject(0) {|total, i| i * i + (total - 30) }} => :airtime, :sum => :rate) puts Munger::Render.to_text(report)
|Spot | Rate | Air Date | Airtime | ---------------------------------------- |Spot 2 | 14 | 2008-01-02 | 15 | |Spot 1 | 20 | 2008-01-01 | 15 | |Spot 3 | 22 | 2008-01-03 | 15 | |Spot 1 | 22 | 2008-01-04 | 15 | | | 78 | | 780 | |Spot 2 | 5 | 2008-01-01 | 30 | |Spot 1 | 6 | 2008-01-02 | 30 | |Spot 1 | 7 | 2008-01-03 | 30 | |Spot 2 | 10 | 2008-01-04 | 30 | |Spot 3 | 27 | 2008-01-02 | 30 | | | 55 | | 4350 | |Spot 3 | 8 | 2008-01-01 | 60 | | | 8 | | 3570 | |Spot 2 | 11 | 2008-01-03 | 90 | | | 11 | | 8070 | | | 152 | | 16770 |