简体   繁体   中英

Django query count unique NEW results per day

I'm keeping statistics of my app in a database in a model that looks like this

class MyStats():
    event_code = django.db.models.PositiveSmallIntegerField()
    timestamp = django.db.models.DateTimeField()
    timestamp_date = django.db.models.DateField()
    device_id = django.db.models.CharField(max_length=32)

I would like to use this data to determine, for each day, how many NEW app installations I have.

I got as far as this:

MyStats.objects.order_by('-timestamp_date').values('timestamp_date').annotate(count_total=Count('device_id', distinct=True))

But what it seems to give me is the amount of unique users per DAY, which is not desired. Any hints?

"New" really means "Occurring after x_timedelta ago":

import datetime

x_timedelta = datetime.timedelta(days=1)

x_timedelta_ago = datetime.datetime.now() - x_timedelta

your_query = MyStats.objects.filter(timestamp_date__gt=x_timedelta_ago)

your_query_distinct = your_query.order_by('-timestamp').annotate(count_total=Count('device_id', distinct=True)).values()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM