django-hitcount icon indicating copy to clipboard operation
django-hitcount copied to clipboard

Feature request: Show hits from yesterday, lastweek ...

Open sowinski opened this issue 7 years ago • 4 comments

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

sowinski avatar Jun 27 '17 18:06 sowinski

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.

thornomad avatar Jul 07 '17 21:07 thornomad

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

sowinski avatar Jul 29 '21 13:07 sowinski

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?

sowinski avatar Mar 15 '22 18:03 sowinski

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()

Ione03 avatar Mar 23 '22 03:03 Ione03