I maintain a Django service that allows online community moderators to review/approve/reject user posts. Right now we measure the average "time to approval" but we need to start measuring the 90th percentile "time to approval" instead. So where we used to say "on average content gets approved in 3.3 hours", we might now say something like "90% of content is approved in 4.2 hours or less".
# Models.py
class Moderation(models.Model):
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
message_id = models.TextField(blank=True, null=True)
class ModerationAction(models.Model):
moderation = models.ForeignKey(Moderation)
action = models.CharField(max_length=50)
created_at = models.DateTimeField(auto_now_add=True)
# stats.py
average_time_to_approve_7days = ModerationAction.objects.filter(
action__in=moderation_actions,
created_at__gte=timezone.now() - timedelta(days=7)
).annotate(
time_to_approve=F('created_at') - F('moderation__created_at')
).values(
'action',
'time_to_approve'
).aggregate(
Avg('time_to_approve')
)['time_to_approve__avg']
# This returns a value like datetime.timedelta(0, 4008, 798824)
My goal: I'm seeking a way to get the 90th percentile time rather than the average time.