Is frequency capping algorithm possible with map/reduce?
I have a problem with creating a map/reduce algorithm in CouchBase. Let's assume my documents have three fields: user_id, time and qualifier.
I need to count all qualified users (simple field equality check), but capping the number of user_id occurrences within each hour. For example, if some user appears 100 times in the same hour, but limit is set to 10, then only 10 occurrences should count. So essentially it's a three-stage algorithm:
1. Group all qualified users by user_id and timestamp rounded down to the whole hour.
2. Reduce each group by counting and then apply the cap.
3. Sum all counts from all groups into a single number.
There are two problems here: how to pass the limit to the view and how to actually implement the cap?