mirror of
https://github.com/jlengrand/bugsink.git
synced 2026-03-10 08:01:17 +00:00
Count view: async slow counts
when you count, it's usually because there are many, so this extra complication is probaly going to be required
This commit is contained in:
32
bsmain/migrations/0002_cachedmodelcount.py
Normal file
32
bsmain/migrations/0002_cachedmodelcount.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("bsmain", "0001_initial"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="CachedModelCount",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.BigAutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
("app_label", models.CharField(max_length=255)),
|
||||
("model_name", models.CharField(max_length=255)),
|
||||
("count", models.PositiveIntegerField()),
|
||||
("last_updated", models.DateTimeField(auto_now=True)),
|
||||
],
|
||||
options={
|
||||
"unique_together": {("app_label", "model_name")},
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -18,3 +18,15 @@ class AuthToken(models.Model):
|
||||
|
||||
def __str__(self):
|
||||
return f"AuthToken(token={self.token})"
|
||||
|
||||
|
||||
class CachedModelCount(models.Model):
|
||||
"""Model to cache the count of a specific model."""
|
||||
|
||||
app_label = models.CharField(max_length=255)
|
||||
model_name = models.CharField(max_length=255)
|
||||
count = models.PositiveIntegerField(null=False, blank=False)
|
||||
last_updated = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
unique_together = ('app_label', 'model_name')
|
||||
|
||||
26
bsmain/tasks.py
Normal file
26
bsmain/tasks.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from django.apps import apps
|
||||
from django.utils import timezone
|
||||
|
||||
from snappea.decorators import shared_task
|
||||
|
||||
from bugsink.transaction import durable_atomic, immediate_atomic
|
||||
from .models import CachedModelCount
|
||||
|
||||
|
||||
@shared_task
|
||||
def count_model(app_label, model_name):
|
||||
ModelClass = apps.get_model(app_label, model_name)
|
||||
|
||||
# separate transaction for the expensive counting
|
||||
with durable_atomic():
|
||||
count = ModelClass.objects.count()
|
||||
|
||||
with immediate_atomic():
|
||||
CachedModelCount.objects.update_or_create(
|
||||
app_label=app_label,
|
||||
model_name=model_name,
|
||||
defaults={
|
||||
'count': count,
|
||||
'last_updated': timezone.now(),
|
||||
},
|
||||
)
|
||||
Reference in New Issue
Block a user