def get_group_stats(): """Get group message stats per day""" message_models = Message.__subclasses__() time_window = 10 start_time = timezone.now() - timezone.timedelta(days=time_window) initial_message_counts = { (timezone.now() - timezone.timedelta(days=days_num)).date(): 0 for days_num in range(time_window) } group_stats = {} for message_model in message_models: annotated_model = (message_model.objects.filter( sent_at__gt=start_time).annotate(date=TruncDay("sent_at")).values( "date", "group_room_id").annotate(count=Count("id"))) for entry in annotated_model: room_id = entry["group_room_id"] date = entry["date"].date() count = entry["count"] if room_id not in group_stats: group_stats[room_id] = dict(initial_message_counts) group_stats[room_id][date] += count return group_stats