# Statistics logging

**URL:** <https://www.ruby-forum.com/t/statistics-logging/84992>\
**Category:** Rails\
**Created:** [February 9, 2007, 1:55am UTC](https://www.ruby-forum.com/t/statistics-logging/84992 "2007-02-09T01:55:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![phillyloco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/258eb7/32.png) [@phillyloco](https://www.ruby-forum.com/u/phillyloco)\
**Post date:** [February 9, 2007, 1:55am UTC](https://www.ruby-forum.com/t/statistics-logging/84992/1 "2007-02-09T01:55:37Z")

</div>

Hello,

I am building a web application that will require a lot of statistic  
logging. Each request could generate between 2 and 6 database inserts.  
It seems a bit of an overhead to have this running in the main  
application. Is something like this worth creating a stats logging  
process (with backgroundrb for example). Also, is this something that  
could be saved in a separate database since it’s pretty irrelevant  
from the main collection of data (just to run marketing statistics  
on).

Or am I just overthinking all of this?

Any thoughts on this? Have you dealt with something similar before?

Thanks,  
-carl

–  
EPA Rating: 3000 Lines of Code / Gallon (of coffee)

---

<div class="post-metadata">

**Author:** ![phillyloco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/258eb7/32.png) [@phillyloco](https://www.ruby-forum.com/u/phillyloco)\
**Post date:** [February 9, 2007, 2:04am UTC](https://www.ruby-forum.com/t/statistics-logging/84992/2 "2007-02-09T02:04:37Z")

</div>

On Friday 09 February 2007, Carl L. wrote:

> I am building a web application that will require a lot of statistic  
> logging. Each request could generate between 2 and 6 database  
> inserts. It seems a bit of an overhead to have this running in the  
> main application.

I think your options mainly depend on exactly how the inserts are  
triggered. If there’s any request-related processing involved you may  
have to do it in the controller(s), hopefully in a filter. If you can  
handle the statistic gathering at the model level, then observers may  
be for you. If the statistics are related purely to operations on the  
data, you may even get away with a database trigger.

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![phillyloco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/258eb7/32.png) [@phillyloco](https://www.ruby-forum.com/u/phillyloco)\
**Post date:** [February 9, 2007, 7:47pm UTC](https://www.ruby-forum.com/t/statistics-logging/84992/3 "2007-02-09T19:47:17Z")

</div>

I’d also add that you batch the inserts instead of inserting into the  
database on every logged event.

---

<div class="post-metadata">

**Author:** ![phillyloco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/258eb7/32.png) [@phillyloco](https://www.ruby-forum.com/u/phillyloco)\
**Post date:** [February 9, 2007, 9:59pm UTC](https://www.ruby-forum.com/t/statistics-logging/84992/4 "2007-02-09T21:59:34Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I’d also add that you batch the inserts instead of inserting into the  
> database on every logged event.

Yes, especially if you’re expecting high traffic.

You could store as yaml and possibly avoid the later inserts all  
together. Or store as yaml and have a nice hash ready for your batch.

–  
Chris M.  
Web D.

> **[Chriscodes.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=chriscodes.com)**
>
> Painless, quick delivery of your domain name. Fast and professional service.

---

<div class="post-metadata">

**Author:** ![phillyloco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/258eb7/32.png) [@phillyloco](https://www.ruby-forum.com/u/phillyloco)\
**Post date:** [February 9, 2007, 10:57pm UTC](https://www.ruby-forum.com/t/statistics-logging/84992/5 "2007-02-09T22:57:53Z")

</div>

Could you elaborate on this please?

I would do all my logging throughout each page into some stats  
collector object, then the stats collector object has a write method  
of some sort called in an after\_filter that sends all the inserts at  
once? I would have to write SQL directly I"m guessing or is there a  
way to do batch inserts into multiple tables with AR?

Thanks,  
-carl

On 2/9/07, Chris M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Chris M.  
> Web D.  
> [http://chriscodes.com](http://chriscodes.com)
> 
> >

–  
EPA Rating: 3000 Lines of Code / Gallon (of coffee)

---

<div class="post-metadata">

**Author:** ![phillyloco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/258eb7/32.png) [@phillyloco](https://www.ruby-forum.com/u/phillyloco)\
**Post date:** [February 10, 2007, 5:24am UTC](https://www.ruby-forum.com/t/statistics-logging/84992/6 "2007-02-10T05:24:36Z")

</div>

Carl L. wrote:

> 

Sounds good. Although you lost me at insert to multiple tables at once,  
don’t know enough about what you’re trying to do.  
It really depends on how many stats you’re logging, and the requirements  
for reading them, whether it’s “overthinking” or if my suggestion would  
be to “little”.

Since you said it was irrelevant to your app data, I was thinking just  
to store it in a yaml file, and load/display it from your “marketing  
app”. That way it’s separated. You could do something like

stats = {  
‘user’ =\> ‘Tom’, ‘time’ =\> Time.now  
‘uri’ =\> request[‘REQUEST\_URI’],  
‘browser’ =\> request[‘HTTP\_USER\_AGENT’] }  
stat\_file = File.new(“#{RAILS\_ROOT}/stats/stats.yml”, “w”)  
stat\_file \<\< stats.to\_yaml  
stat\_file.close

Now you’ve got your stats in a simple yaml file that can be read like

yaml\_stats = YAML.load(File.open(“#{RAILS\_ROOT}/stats/stats.yml”))

Then displayed, or manipulated as you wish, and when you wish.  
That’s obviously a really basic example.  
You could do something more interesting like run a BackgrounDRB process  
that loads the yaml file, does your inserts, removes the file, does a  
backflip with a twist, then starts over, etc.  
If the stats must go in a separate table, or separate database all  
together, I’d keep the logic to do that within BackgroundRB, or even a  
separate “stats-for-marketing” rails app. Your main app writes the  
files, and backgrounDRB or other app reads them and writes to the  
database.

Depending on your actual requirements, and if writing to a file is an  
option, you could simply write to a csv file that marketing could open  
in Excel. Then no extra code is needed to read the stats, and you’re on  
your way to finishing early!

HTH

–  
Chris M.  
Web D.

> **[Chriscodes.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=chriscodes.com)**
>
> Painless, quick delivery of your domain name. Fast and professional service.
