# Graphs over time, where to store data and how to?

**URL:** <https://www.ruby-forum.com/t/graphs-over-time-where-to-store-data-and-how-to/170343>\
**Category:** Rails\
**Created:** [June 16, 2009, 12:01pm UTC](https://www.ruby-forum.com/t/graphs-over-time-where-to-store-data-and-how-to/170343 "2009-06-16T12:01:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![trib4lmaniac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9de053/32.png) [@trib4lmaniac](https://www.ruby-forum.com/u/trib4lmaniac)\
**Post date:** [June 16, 2009, 12:01pm UTC](https://www.ruby-forum.com/t/graphs-over-time-where-to-store-data-and-how-to/170343/1 "2009-06-16T12:01:03Z")

</div>

Hi there, interesting one here. Usually I take a problem like this,  
sit down with MySQL and play about but i’m coming stuck and would like  
to know what you guys think or what you best practices are!

OK, so lets say I’m making a graph on my home page, its a line graph  
and has 12 dots, each is number of hits every-hour.

To log all hits you could have a bit of code that goes in mysql and  
updates that number by one to show all hits, but to hold back data  
what is the best practice, some script to move data down one column or  
im not sure, im stuck to be honest?!

Apologies if this has already been raised, my googling was epic  
failed…

Thanxs Guys, oh since its my first time here, Hi!

Alex B.  
ARBarlow

---

<div class="post-metadata">

**Author:** ![trib4lmaniac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9de053/32.png) [@trib4lmaniac](https://www.ruby-forum.com/u/trib4lmaniac)\
**Post date:** [June 16, 2009, 5:46pm UTC](https://www.ruby-forum.com/t/graphs-over-time-where-to-store-data-and-how-to/170343/2 "2009-06-16T17:46:31Z")

</div>

Alex B. wrote:  
[…]

> OK, so lets say I’m making a graph on my home page, its a line graph  
> and has 12 dots, each is number of hits every-hour.

(This is not really a Rails question…)

So you want hits for the last 12 hours?

> To log all hits you could have a bit of code that goes in mysql and  
> updates that number by one to show all hits, but to hold back data  
> what is the best practice,  
> some script to move data down one column

No need. Just use appropriate criteria in your DB query…something  
like (from memory; untested):

Hit.count :conditions =\> [‘created\_at \> ?’, 12.hours.ago], :group =\>  
‘hour(created\_at)’

Remember, your database will do a lot of the work for you if you’re nice  
to it. (If you use PostgreSQL, it will do even more…but that’s  
another post.)

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![trib4lmaniac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9de053/32.png) [@trib4lmaniac](https://www.ruby-forum.com/u/trib4lmaniac)\
**Post date:** [June 17, 2009, 12:16pm UTC](https://www.ruby-forum.com/t/graphs-over-time-where-to-store-data-and-how-to/170343/3 "2009-06-17T12:16:51Z")

</div>

Ok, so your saying that mysql holds back data automatically? I have  
never heard of this…

> Hit.count :conditions =\> [‘created\_at \> ?’, 12.hours.ago], :group =\>  
> ‘hour(created\_at)’

this piece of code would require multiple rows each with a different  
time, so that would require a table of hits etc. but how would you log  
those into the table ever hour?, day?, week? etc, im talking not only  
about hits but user statistics, profile views for example?

Thanxs for your reply,  
Alex

On Jun 16, 4:46 pm, Marnen Laibow-Koser \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![trib4lmaniac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9de053/32.png) [@trib4lmaniac](https://www.ruby-forum.com/u/trib4lmaniac)\
**Post date:** [June 17, 2009, 12:24pm UTC](https://www.ruby-forum.com/t/graphs-over-time-where-to-store-data-and-how-to/170343/4 "2009-06-17T12:24:31Z")

</div>

2009/6/16 Alex B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi there, interesting one here. Usually I take a problem like this,  
> sit down with MySQL and play about but i’m coming stuck and would like  
> to know what you guys think or what you best practices are!
> 
> OK, so lets say I’m making a graph on my home page, its a line graph  
> and has 12 dots, each is number of hits every-hour.

What is a ‘hit’?

---

<div class="post-metadata">

**Author:** ![trib4lmaniac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9de053/32.png) [@trib4lmaniac](https://www.ruby-forum.com/u/trib4lmaniac)\
**Post date:** [June 17, 2009, 12:39pm UTC](https://www.ruby-forum.com/t/graphs-over-time-where-to-store-data-and-how-to/170343/5 "2009-06-17T12:39:13Z")

</div>

Website hits, visitors…

---

<div class="post-metadata">

**Author:** ![trib4lmaniac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9de053/32.png) [@trib4lmaniac](https://www.ruby-forum.com/u/trib4lmaniac)\
**Post date:** [June 17, 2009, 12:48pm UTC](https://www.ruby-forum.com/t/graphs-over-time-where-to-store-data-and-how-to/170343/6 "2009-06-17T12:48:01Z")

</div>

2009/6/17 Alex B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Website hits, visitors…

Ah, I understand. So is the question (or at least the first part of  
the question) how to log website hits in a Rails app? Is the best way  
to log the data to the database or would it be possible to parse the  
apache (or whatever) log I wonder?

Colin

---

<div class="post-metadata">

**Author:** ![trib4lmaniac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/9de053/32.png) [@trib4lmaniac](https://www.ruby-forum.com/u/trib4lmaniac)\
**Post date:** [June 17, 2009, 5:36pm UTC](https://www.ruby-forum.com/t/graphs-over-time-where-to-store-data-and-how-to/170343/7 "2009-06-17T17:36:24Z")

</div>

Alex B. wrote:

> Ok, so your saying that mysql holds back data automatically? I have  
> never heard of this…

Unless I’m misunderstanding what you mean by “holds back data”, that’s  
what a DB does!

> > Hit.count :conditions =\> [‘created\_at \> ?’, 12.hours.ago], :group =\>  
> > ‘hour(created\_at)’
> 
> this piece of code would require multiple rows each with a different  
> time, so that would require a table of hits etc. but how would you log  
> those into the table ever hour?, day?, week? etc,

There’s an Apache module (can’t recall the name) that will put the  
access logs in a mySQL table instead of a text file…

> im talking not only  
> about hits but user statistics, profile views for example?

This would have to be done in the application. For example, in  
ProfilesController#view, you’d need to insert a stats record into the  
appropriate table (or just increment the view count, depending on your  
DB structure). I wonder if there’s a Rails plugin to makw this easier.

> Thanxs for your reply,  
> Alex

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
