Skip to content

Commit

Permalink
Add meta info to stats
Browse files Browse the repository at this point in the history
  • Loading branch information
daanvdk committed Nov 16, 2023
1 parent a31da8a commit 2d24b50
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions binder/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2982,20 +2982,27 @@ def _get_stat(self, request, queryset, stat, annotations, include_annotations):
group_by = stat['group_by']
except KeyError:
# No group by so just return a simple stat
return queryset.aggregate(result=stat['expr'])['result']
return {
'value': queryset.aggregate(result=stat['expr'])['result'],
'filters': stat.get('filters', {}),
}

group_by = group_by.replace('.', '__')
# The jsonloads/jsondumps is to make sure we can handle different
# types as keys, an example is dates.
return {
jsonloads(jsondumps(key)): value
for key, value in (
queryset
.order_by()
.values(group_by)
.annotate(_binder_stat=stat['expr'])
.values_list(group_by, '_binder_stat')
)
'value': {
# The jsonloads/jsondumps is to make sure we can handle different
# types as keys, an example is dates.
jsonloads(jsondumps(key)): value
for key, value in (
queryset
.order_by()
.values(group_by)
.annotate(_binder_stat=stat['expr'])
.values_list(group_by, '_binder_stat')
)
},
'group_by': group_by,
'filters': stat.get('filters', {}),
}


Expand Down

0 comments on commit 2d24b50

Please sign in to comment.