I’ve been trying to find a good and repeatable way of doing metrics in Django. When I say metrics I mean something like: “User played X amount of games in month Y.”
This has been a big struggle. We’ve yet to figure a good way of doing this.
Our requirements are:
- no 3rd party service (we need to write and read these values)
- no secondary database (postgresql should do just nicely)
- Django ORM needs to work properly
If a good solution presents itself we are more than willing to waiver these requirements. But still they are the assumptions we are working on. Onto the problem itself.
The problem
When we first developed this we came up with the following solution.
1 | class GameVelocity(models.Model): |
We now know that this wasn’t the best approach but at the time it made perfect sense for us. This approach leads to many inconsistencies and very hard to read code because you have to pass down all of the parameters (frequency, date….).
The solutions
I can see two main solutions for this:
1 | class Metric(models.Model): |
This would make us have one row for each metric. This would make the class much much simpler but the filters on the database would be harder to do.
Another solution (which I I’m more inclined to) is:
1 | class MonthlyMetric(models.Model): |
In this one we remove the frequency field and put it on the model name. Because when we are filtering metrics we usually are always on the same frequency. You don’t mix monthly metrics with weekly ones.
During my investigation I found some interesting articles but none of them did it for me. Here are some likns.
How to turn Django Admin into a lightweight dashboard{:target=”_blank”}
Django App Metrics{:target=”_blank”}
Things You Must Know About Django Admin As Your App Gets Bigger{:target=”_blank”}
Questions:
- Do you have any idea on how to do this in a cleaner way? Or this just a complex problem and a complex solution is always needed?
- Any books or blogs I should be reading?
- Anyone you know that is a master at this stuff I can reach out to?