Today I was working on the stats generation for the homeworks and I noticed that it took a ridiculous amount of time to pull the aggregated stats for any user.
The problem seemed to be that I had to run a bunch of sql calls in order to get the information to build each Stats block. It seemed unreasonable that I would have to compute this on the fly whenever the user requested it. I cannot loop over all the user's answers every time they want to see their stats.
To solve this I decided to create a new Stat model and use polymorphism to deal with the different elements that would need statistics.
For each question we have several data points that we collect and they have a heirarchy. These are considered to be Statable (polymorphism)
After each exam the the data points from the questions are collected and computed into the "stats". Then a new entry in the Stats table is created with all this information. Here I will take advantage of the queue system in heroku and send these data computing jobs to run there.
Now with this new setup I can pull aggregate data easily by running a simple sql query. More importantly I also now have data for specific time periods. It would have been much harder to see progression over time of a particular user because I would have needed to use the dates on the exams. Now I just get the stats from a range of dates and I can even filter it to specific data points.
No comments:
Post a Comment