Now that we can filter our queries, let's extract meaningful insights from the data. Insight counts documents across the current query and optional per-field query filters. Think of it like a kitchen display system that shows the chef exactly how many active orders or topping requests are waiting.
First, define the Insight class in your constant file. Each field uses the accumulate option as an Akan document query filter for counting:
Let's break down the count filter patterns used:
🍦{}
Counts all orders that match the current query. Leave accumulate as an empty object for the base count.
🍓{ toppings: "strawberry" }
Adds a field filter before counting. Because toppings is an array field, this counts orders whose toppings include "strawberry".
Add dictionary entries for the insight fields to enable proper labeling in the UI:
Now let's create a View component to display the aggregated insights in a beautiful dashboard layout:
The View component displays each insight metric in a responsive grid. The chef can quickly see how much yogurt to prepare and which toppings are most popular.
Now create a Zone component that connects the View to the store. Zone components handle data fetching and state management:
Key feature of the Zone component:
🔗st.slice[slice.sliceName].use.icecreamOrderInsight(): Auto-generated hook that retrieves the aggregated insight data for the specified slice. The framework handles all the aggregation pipeline execution.
Finally, add the Insight Zone to your page to display real-time aggregated statistics:
Now when users filter orders by status, the insight dashboard automatically updates to show aggregated statistics for only those filtered orders. This is incredibly powerful for real-time operational decisions!
🎉 What You've Accomplished:
- ✓ Created dynamic Query Makers with searchable parameters
- ✓ Learned how to define Insight classes with Akan document query filters
- ✓ Built View components to display aggregated statistics
- ✓ Connected Zone components to auto-generated store hooks
- ✓ Integrated insights with filtered queries for real-time analytics
In the next tutorial, we'll explore how to relate data between different models. This will allow you to create rich relationships like associating orders with customers, linking products to categories, and building complex data graphs.