I asked a question on SO yesterday titled Deciding and implementing a trending algorithm in Django. A lot of people suggested a simple thing like averages (exponential, weighted, etc.) I have a model called Book and another called Readers:
class Book(models.Model):
name = models.charField()
class Reader(models.Model):
date = models.DateField()
book = models.ForeignKey(Book)
reader_count = models.PostiveIntegerField()
A simple structure. New books are added every day and the reader count for each book is added every day too. i.e. a book will have a reader count for the day for each day, multiple records.
I need to calculated the averages for the Books for the current week, the current month and the current year. Apart from the current data, I'd like to preserve the historical data too.
If i tried querying this kind of data from the DB, it would take a big hit. Wouldn't it. Besides, I'm trying to implement this system using simple averages to begin with but later on, I'd like to have the flexibility of changing my computational method. I have two options -
one, where I can partially update the data in another table which stores the computed data every time a new
Reader
record is added.two, where I could rebuild the aggregated data every night through a script for the current day/week/month.
Here's some sample data and the results.
Book Date Count
---- ---------- -----
AAAA 01.01.2012 10
AAAA 02.01.2012 20
AAAA 03.01.2012 30
AAAA 04.01.2012 30
AAAA 05.01.2012 40
AAAA 06.01.2012 10
AAAA 07.01.2012 25
AAAA 08.01.2012 15
AAAA 09.01.2012 10
The reader count average for Week #1 is: 23.5. The reader count average for Week #2 (which isn this case would be the current week) is: 12.5 ..and for the current month and year would be 21.1
HTH.
In order to give any of these a shot, I'd like to build a system to store the data. I need to store the averages on a daily, weekly, and monthly basis. However I'm very lost with what kind of a table structure I should implement? I'd like to not re-invent the wheel if possible so if any of you know about any packages that allow me to accomplish this, it would be great.
Thanks.