r/howdidtheycodeit • u/WrongFaithlessness83 Hobbyist • Mar 21 '23
Question How do they code 30 day totals?
Say I have an app that simply allows a user to vote on one of 3 squares on the page. (This could be applied to votes, kills, goals, money earned etc.) Then I want to display under each square, how many votes it has gotten in the last 30 days.
The most obvious solution is storing each vote with the date it occurred and then filtering them but that sounds super heavy and slow and also messy.
Is there some sort of clean solution/trick to this sort of thing?
20
Upvotes
6
u/Ignitus1 Mar 21 '23
Another way would be to store the totals for each day. Every day they remove the oldest day. That way there are only 30 totals to sum at any given time.
50
u/Toror Mar 21 '23
That sounds like exactly how they would do it.
Each user responds, that becomes a row in a DB somewhere with the userID, date replied, and choice.
Then if someone wants to see the last 30 days they would likely just have a query that runs once a day to update different time windows like 7 days, 15 days, 30 days, etc so that it doesn't have to run the query each time a user selects that option.
20
u/the_Demongod Mar 21 '23
What you're describing isn't super slow and heavy, unless you're counting millions of votes per second or something. Make it work, then make it fast. Most of the time the naive solution is more than fast enough.
16
u/[deleted] Mar 21 '23
If you have no interest in WHOM voted for what, you could just keep a running total. Perhaps like a list of 30 spots. Each day is the total votes for that answer for that day. And just sum up the spot. On the 31st day, start filling the list from the beginning.
But realistically speaking to prevent double voting you should probably use a DB
12
u/AHeroicLlama Mar 21 '23
Do this! Databases have been optimised so much for exactly this kind of task - done right it will be very very fast
2
u/NUTTA_BUSTAH Mar 21 '23
Any database can handle that query in a millisecond for over a 100 million rows pretty easily. First query might take a bit longer but DB engines are extremely optimized for exactly this.
1
u/HammerBap Mar 21 '23
Basically a window function or a kernel, this is exactly how you do it and has uses in a lot of fields from signal processing, statistics, computer vision, computer graphics, etc..
1
u/-PM_me_your_recipes Mar 21 '23
Essentially, what you said is exactly what they do. They simply pull the data in that date range. But they don't store it in a plain file, that is slow. instead, they store the info in a database like mariadb or postgresql.
Databases are incredibly fast. When you add proper indexing to tables, even doing a query on 30+ million records is pretty trivial and can take a fraction of a second. But when you get to that level, you probably will cache the results and only refresh it every so often so every request isn't trying to run that large query.
Source: I work with large sets of data.
-2
u/Shawn-GT Mar 21 '23
the votes are objects, in the objects you have attributes. The date would be one of those attributes. then you would make a function to call the dates and filter them to not include anything other than the last 30 days.