Created
May 15, 2011 09:22
-
-
Save sevos/972995 to your computer and use it in GitHub Desktop.
This file contains 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
class DailyStatsReport < KPI::Report | |
def users | |
result 'Users', User.count, :description => 'Total users count' | |
end | |
def premium_users | |
result 'Premium users', User.premium.count | |
end | |
def premium_users_percentage | |
result 'Premium users', premium_users.to_f / users * 100, :unit => '%' | |
end | |
def income | |
result 'Total income', Order.paid.sum(:total), :unit => 'EUR' | |
end | |
def daily_income | |
result 'Daily income', Order.paid.where(:created_at => the_day).sum(:total), :unit => 'EUR' | |
end | |
private | |
def the_day | |
@yesterday ||= begin | |
yesterday = (time - 1.day) | |
yesterday.beginning_of_day..yesterday.end_of_day | |
end | |
end | |
end |
This file contains 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
report = DailyStatsReport.new |
This file contains 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
> report.users | |
=> #<KPI::Entry:0x10177f630 @name="Users", @unit=nil, @description="Total users count", @value=874> |
This file contains 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
> report.entries.map(&:value) | |
=> [874, 14, 138.6, 19.8] |
This file contains 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
report = DailyStatsReport.new(:time => 1.day.ago) |
This file contains 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
report_today = DailyStatsReport.new | |
report_yesterday = DailyStatsReport.new(:time => 1.day.ago) | |
diff_report = KPI::MergedReport.new(report_today, report_yesterday) do |entry_today, entry_yesterday| | |
KPI::Entry.new '$$ (change)', entry_today.value - entry_yesterday.value | |
end |
This file contains 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
> diff_report.premium_users | |
=> #<KPI::Entry:0x11577e319 @name="Premium users (change)", @unit=nil, @description="", @value=2> |
This file contains 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
avg_report = KPI::MergedReport.new(*week_reports) do |*entries| | |
KPI::Entry.new '$$ (avg)', entries.map(&:value).inject(:+).to_f / entries.size | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment