DRF ViewSet for getting aggregations
Project description
DRF Mixin for getting aggregations
List of possible aggregations:
count
sum
average
minimum
maximum
percentile (work only on PostgreSQL)
percent
Additional features
grouping by multiple fields
grouping by date fields with the required precision (from a second to a year)
display of top-N records, with the ability to show the rest of the records as a single group
Installing
For installing use pip
$ pip install drf-aggregation
Usage
Create a ViewSet using a ViewSet from a package or by adding a mixin to an existing one
from drf_aggregation.mixins import AggregationMixin
from drf_aggregation.viewsets import AggregationViewSet
class MyCustomUserViewSet(AggregationMixin, GenericViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
class UserViewSet(AggregationViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
Register url
from drf_aggregation.routers import AggregationRouter
aggregation_router = AggregationRouter()
aggregation_router.register("user", UserViewSet)
urlpatterns = [
path("my/custom/endpoint", UserViewSet.as_view({"get": "aggregation"})),
]
urlpatterns += aggregation_router.urls
Get aggregations
# Get the number of users /user/aggregation?aggregation=count # Total cost of orders /order/aggregation?aggregation=sum&aggregationField=price # Earliest registration date /user/aggregation?aggregation=minimum&aggregationField=date_joined # Last travel date /trip/aggregation?aggregation=maximum&aggregationField=duration # Median salary /position/aggregation?aggregation=percentile&aggregationField=salary&percentile=0.5 # Number of tickets by state /ticket/aggregation?aggregation=count&groupByFields=state # Top 5 ticket executors /ticket/aggregation?aggregation=count&groupByFields=assigned_to&limit=5&limitByField=assigned_to&order=desc # Percentage of open tickets by service /ticket/aggregation?aggregation=percent&groupByFields=service&additionalFilter={"type":"operator","operator":{"attribute":"state","operator":"=","value":"open"}} # Life expectancy depending on the year of birth /person/aggregation?aggregation=average&aggregationField=age&annotations={"birth_year":{"field":"birth_date","kind":"year"}}&groupByFields=birth_year
Query parameters
aggregation - aggregation type, one of:
count
sum
average
minimum
maximum
percentile
percent - return two additional values: “numerator” and “denominator”
aggregationField - mandatory for aggregations: sum, average, minimum, maximum, percentile
percentile - from 0 to 1, mandatory for percentile
outputType - currently only accepts “floats” to properly aggregate integer fields, used for percentile only
additionalFilter - filter parser is used from package drf-complex-filter, mandatory for percent
The following additional options are available for all aggregation types
groupBy - comma-separated list of fields, used to group the result by one or more fields, mandatory if limit is set
annotations - additional annotations for truncating date fields, using Trunc method from Django, format see examples above
limit - limits the output to the number of groups of records passed
limitByField - field for selecting the values that will remain, mandatory if limit is set
order - sorting direction of values: “asc” or “desc”
showOther - show groups not included in the top by one category or not
otherGroupName - label for a group with records not included in the top
Supported field types
Aggregations are available on the following field types:
IntegerField
FloatField
DateField - only minimum and maximum
DateTimeField - only minimum and maximum
DurationField
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for drf_aggregation-0.5.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 859f6ef9ddafec1710cc90f18197b6b17e5a8cb0cc78b65832680ca94c605e00 |
|
MD5 | f1dceace23c0e398ae22e1b509e544c3 |
|
BLAKE2b-256 | 116ab336af4297d88a4ca95636fdf0ba188f38318c78d442578b237ca61e021e |