Skip to content

Instantly share code, notes, and snippets.

@kwilcox
Created November 3, 2008 16:10
Show Gist options
  • Save kwilcox/21898 to your computer and use it in GitHub Desktop.
Save kwilcox/21898 to your computer and use it in GitHub Desktop.
def dayify(symbol,countsymbol,sumsymbol = nil)
# This is for calculating things like emails sent. Each time an update email is sent,
# one row is added to the Analytic table with the 'symbol'. Each analytic run, the past
# day of those symbols are rolled into one Analytic called 'countsymbol'. If there is a
# quantity specifed as 'newsymbol' then the values are aggregated and saved as 'newsymbol'.
# In the case of update emails, the number of updates is the aggregate, the number of emails
# is the count.
# Sample call for update emails: dayify(:update_email_sent,:daily_update_emails,:updates_in_daily_emails)
# Sample call for invite emails: dayify(:invite_email_sent,:daily_invite_emails)
z = Analytic.find_by_sql("select sum(value) as sum, count(value) as count, date_trunc('day',created_at) as x from analytics WHERE resource = '#{symbol.to_s}' AND created_at < date_trunc('day',now()) group by date_trunc('day',created_at)")
z.each { |r|
Analytic.store(countsymbol, r.count, r.x + 1.day)
if sumsymbol
Analytic.store(sumsymbol, r.sum, r.x + 1.day)
end
}
Analytic.delete_all "resource => #{symbol.to_s} AND created_at < date_trunc('day',now())"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment