Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Track usage by month to make it more robust

Daoud Clarkefb1ad2ca28f4
diff --git a/app.json b/app.json
index e036b8a..6fb409a 100644
--- a/app.json
+++ b/app.json
@@ -1,11 +1,4 @@
{
- "cron": [
- {
- "command": "/venv/bin/python /app/manage.py reset_monthly_quotas",
- "schedule": "0 0 1 * *",
- "concurrency_policy": "forbid"
- }
- ],
"scripts": {
"dokku": {
"predeploy": "rm -rf /app/static/* && cp -r /front-end-build/* /app/static/"
diff --git a/mwmbl/background.py b/mwmbl/background.py
index 9ecedd8..658c486 100644
--- a/mwmbl/background.py
+++ b/mwmbl/background.py
@@ -88,12 +88,12 @@ def copy_indexes_continuously():
@background(schedule=0)
def flush_search_counts():
"""
- Sync live Redis monthly search counters to MwmblUser.monthly_search_count
- in the database. Scheduled to run every 10 minutes (600 seconds) via
+ Sync live Redis monthly search counters to UsageBucket records in the
+ database. Scheduled to run every 10 minutes (600 seconds) via
AppConfig.ready().
"""
from django.core.cache import cache
- from mwmbl.models import MwmblUser
+ from mwmbl.models import UsageBucket
from mwmbl.quota import get_all_monthly_keys
for key in get_all_monthly_keys():
@@ -101,8 +101,13 @@ def flush_search_counts():
try:
parts = key.split(":")
user_id = int(parts[2])
+ year = int(parts[3])
+ month = int(parts[4])
count = cache.get(key, default=0)
- MwmblUser.objects.filter(pk=user_id).update(monthly_search_count=count)
+ UsageBucket.objects.update_or_create(
+ user_id=user_id, year=year, month=month,
+ defaults={"count": count},
+ )
except Exception:
logger.exception("Error flushing search count for key %s", key)
diff --git a/mwmbl/models.py b/mwmbl/models.py
index a569514..b64a0f5 100644
--- a/mwmbl/models.py
+++ b/mwmbl/models.py
@@ -25,7 +25,6 @@ class MwmblUser(AbstractUser):
choices=Tier.choices,
default=Tier.FREE,
)
- monthly_search_count = models.IntegerField(default=0)
class UserCuration(models.Model):
@@ -157,6 +156,17 @@ class WasmEvaluationJob(models.Model):
error_message = models.TextField(null=True, blank=True)
+class UsageBucket(models.Model):
+ """Records a user's API usage for a specific calendar month."""
+ user = models.ForeignKey(MwmblUser, on_delete=models.CASCADE)
+ year = models.IntegerField()
+ month = models.IntegerField()
+ count = models.IntegerField(default=0)
+
+ class Meta:
+ unique_together = [('user', 'year', 'month')]
+
+
class SearchResultVote(models.Model):
VOTE_TYPES = {
"upvote": "User upvoted this result",