Owain wrote in post #1015933:
My application is gradually being refined and it is becoming necessary
to provide some statistics. I don't really want to add all of these
into the main application logic since it is not critical. I would
like to "observe" a bunch of models and classes and increment and
decrement counters. So rather than have a bit of hideous AR finds to
show me the number of orders by month I can observe the AR callbacks
on event and increment "August 2011 Orders". I would also need to be
able to reset via a Rake task or something similar.
Has someone come across a Gem that provides this sort of functionality
before I go and write it?
Unless there was a requirement for these statistics to be absolutely
up-to-date at all times, I don't think I would track them with every
change to the models being tracked. Instead, I would probably create a
background process to periodically update the counts and store them in a
separate "statistics" table. Essentially "mining" the statistical
information rather than tracking it directly. This also provides an
opportunity to flatten some of the relational data, which can
dramatically improve query performance for reporting purposes.
Consider the example you mentioned; It seems likely that users would be
interested in "August 2011 Orders" sometime in September, or later. It
would also be likely that they would want to compare August with the
results from August of the prior year.
Using data mining techniques these statistics could be provided
retroactively, and it would also be possible to update the statistics on
a completely separate process or even a separate server leaving your
primary application servers free to serve user requests.
There are several gems that provide the infrastructure to build
something like what I described. For example Github created their own
solution, which they have open sourced called Resque:
This would also provide you the rake task for resetting, which would
just enqueue another Resque job that you create.