Aggregate Provider¶
When building application with large dataset, it is common to pre-aggregate data on the data management layer and only query for aggregate from precomputed aggregate storage.
MorpFW provides a overrideable aggregate provider API for you to intercept the aggregate mechanism and put your own aggregate logic.
- class morpfw.interfaces.IAggregateProvider¶
- abstract aggregate(query: Optional[dict] = None, group: Optional[dict] = None, order_by: Union[None, list, tuple] = None) list ¶
return aggregation result based on specified rulez query and group
- abstract parse_group(qs: str) dict ¶
Parse query string from group query and convert it into dictionary representation. The dictionary would be passed to aggregate method afterwards, which you can then parse into your backend aggregate query.
- abstract parse_query(qs: str) dict ¶
Parse query string from search query and convert it into rulez query dictionary. The dictionary would be passed to aggregate method afterwards, which you can then parse into your backend search query.
Overriding Aggregate Provider¶
import typing
import morpfw
from .app import App
from .model import PageCollection
class PagesAggregateProvider(morpfw.AggregateProvider):
def aggregate(self, query=None, group=None, order_by=None):
"""search for resources, aggregate and return aggregate result"""
result = []
# do something here
return result
@App.aggregateprovider(model=PageCollection)
def get_aggregateprovider(context):
return PagesAggregateProvider(context)