django-hitcount
django-hitcount copied to clipboard
Feature request: Show hits from yesterday, lastweek ...
Hi,
how can I check hits for a specific time period? Right now, it is only possible to count the hits from now back to a certain time period.
Get total hits for an object over a certain time period:
{% get_hit_count for [object] within ["days=1"] %}
Regards
Good idea! Would need to be able to do it on a specific date or a range of dates or some basic querying. I like it!
Would be happy to consider a PR for such a feature. I am not actively developing this project these days; just trying to keep it up and running.
I use this peace of code for general statistics per Day with some filter for a specific kind of objects.
from hitcount.models import Hit from django.contrib.contenttypes.models import ContentType content_type = ContentType.objects.get(app_label="searchroom", model="buildingcontactdetails")
Hit.objects.filter(hitcount__content_type=content_type).annotate(created_date=TruncDate('created')).values('created_date').annotate(sum=Count('created_date')).values('created_date', 'sum').order_by('created_date')
This show the hits per day
I ended up with this code. But it is super slow.
Building.objects.filter(user=self.request.user)
.annotate(get_hits_lastmonth=Coalesce(Subquery(hitcount_30.values('myhits')[:1], output_field=IntegerField()), 0)) \
.annotate(get_hits_lastweek=Coalesce(Subquery(hitcount_7.values('myhits')[:1], output_field=IntegerField()), 0)) \
.annotate(get_hits_yesterday=Coalesce(Subquery(hitcount_1.values('myhits')[:1], output_field=IntegerField()), 0))
I am using this request to show in my users dashboard some activities in the last day, week and month.
Any Idea how to optimize this?
I use this peace of code:
today = datetime.datetime.now()
hit_today = Hit.objects.filter(created__year=today.year, created__month=today.month, \
created__day=today.day).count()