Created
November 8, 2021 04:49
-
-
Save nebiyuelias1/c61b453d4c14b39df2849c141821c96d to your computer and use it in GitHub Desktop.
Trending factor calculation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_overall_score_trending_factor_queryset(self, queryset): | |
duration = Epoch((F('now') - F('created_at'))) | |
queryset = queryset.annotate( | |
now=Cast(timezone.now(), DateTimeField()), | |
duration=duration, | |
trending_factor=ExpressionWrapper( | |
# the 10 ^ 6 * 3600 * 24 * 30 multiplication is to convert micro second duration to months | |
(F('score__overall_score') * 1_000_000 * 3600 * 24 * 30) / F('duration'), | |
output_field=DecimalField() | |
)).order_by(F('trending_factor').desc(nulls_last=True)) | |
return queryset |
Oh yes, that's a custom-made query expression.
from django.db.models import Func, IntegerField
class Epoch(Func):
template = 'EXTRACT(epoch FROM %(expressions)s)::INTEGER'
output_field = IntegerField()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how do you import the Epoch field