Rails Console Graphing Lib

Now that I'm working on a site with actual real users, I've become quite "stats hungry." I find myself constantly checking the database to see how many new users there are, how many new items of this or that type have been created, how many users have uninstalled my app, etc. Up till now I've been querying the DB directly with SQL. However, it is difficult to get a good sense of what the trends are when just looking at long lists of records, or counts of records.

Thus with a little research and some glue code, I was able to put together this library (to be required in environment.rb), which will let you generate histograms of counts of Rails models. Following is an example.

Let's say I want to see how many comments there have been in the past several hours:

>> puts histogram(:comments, :maxlen => 30, :showval => true)
2007-08-23 20:00:00 :***** (3)
2007-08-23 19:00:00 :** (2)
2007-08-23 18:00:00 :************ (6)
2007-08-23 17:00:00 :********* (5)
2007-08-23 16:00:00 :***** (3)
2007-08-23 15:00:00 :********************* (10)
2007-08-23 14:00:00 :**************************** (13)
2007-08-23 13:00:00 :** (2)
2007-08-23 12:00:00 :***** (3)
2007-08-23 11:00:00 :******* (4)
2007-08-23 10:00:00 :******* (4)
2007-08-23 09:00:00 :** (2)
2007-08-23 08:00:00 :******* (4)
2007-08-23 07:00:00 :********* (5)
2007-08-23 06:00:00 :************ (6)
2007-08-23 05:00:00 :***** (3)
2007-08-23 04:00:00 :********* (5)
2007-08-23 03:00:00 :****************** (9)
2007-08-23 02:00:00 :******* (4)
2007-08-23 01:00:00 :************** (7)
2007-08-23 00:00:00 :** (2)
2007-08-22 23:00:00 : (1)
2007-08-22 22:00:00 : (1)
2007-08-22 21:00:00 :****************** (9)
=> nil
>>

Concise Code Leaves a Small Footprint

Something I realized today is that it is a good idea to make your code as concise as possible, even if you end up not using it.

Let me explain.  Earlier this week I'd written a nice helper method to automatically generate a row of tabs at the top of my web app.  Yesterday I completely threw it away.  I wondered momentarily this morning if I'd wasted my time constructing the helper method that generated the tabs in one easy step.  In fact, I didn't waste my time.  If I had not taken the time to write the helper method, I would have had to remove serveral lines of html in multiple files instead of just a couple of calls to the helper method.  In many cases removing code can be just as complex as adding it, so the benefit of conciseness remains even if the concise code is removed.