## About `nostr_event_store`'s Aggregation --- ### What is Aggregation? Aggregation is a mechanism for grouping, statistically analyzing, and aggregating event data. It allows you to perform statistical, trend analysis, and ranking operations on events based on different fields (such as author, event type, tags, time, etc.). --- ### What can Aggregation do? - Count the total number of events or the number of groups - Analyze trends by time binning (e.g., daily, hourly) - Group by multiple dimensions such as author, event type, and tags - Query the activity level or distribution under a specific author, tag, or topic --- ### Specific Examples (including parameters and calling methods) #### 1. Count active users and their posts on a specific day ``` q := &types.AggregationQuery{ Filter: &types.QueryFilter{ Since: uint32(dayStart.Unix()), Until: uint32(dayEnd.Unix()) - 1, }, GroupBy: []types.GroupByField{types.GroupByAuthor, types.GroupByKind}, AggFunc: types.AggCount, OrderDesc: true, Limit: 1000, } entries, err := s.client.QueryAggregation(ctx, q) ``` --- #### 2. Statistics on daily event trends ``` q := &types.AggregationQuery{ Filter: &types.QueryFilter{ Since: timeWindow.StartUnix, Until: timeWindow.EndUnix - 1, Kinds: kinds, }, GroupBy: []types.GroupByField{types.GroupByTimeBucket}, AggFunc: types.AggCount, TimeBucketSeconds: 86400, // A day OrderDesc: false, } entries, err := s.client.QueryAggregation(ctx, q) ``` --- #### 3. Statistics on popular tags ``` q := &types.AggregationQuery{ Filter: &types.QueryFilter{ Since: timeWindow.StartUnix, Until: timeWindow.EndUnix - 1, }, GroupBy: []types.GroupByField{types.GroupByTagValue}, AggFunc: types.AggCount, TagName: "t", OrderDesc: true, Limit: 20, } entries, err := s.client.QueryAggregation(ctx, q) ``` --- #### 4. Statistics on the most active authors under a certain topic(KOL) ``` q := &types.AggregationQuery{ Filter: &types.QueryFilter{ Since: timeWindow.StartUnix, Until: timeWindow.EndUnix - 1, Tags: map[string][]string{"t": {"A topic"}}, }, GroupBy: []types.GroupByField{types.GroupByTagValue, types.GroupByAuthor}, AggFunc: types.AggCount, TagName: "t", OrderDesc: true, Limit: 20, } entries, err := s.client.QueryAggregation(ctx, q) ``` --- #### 5. Statistical analysis of author activity trends on a specific topic ``` q := &types.AggregationQuery{ Filter: &types.QueryFilter{ Since: timeWindow.StartUnix, Until: timeWindow.EndUnix - 1, Authors: [][32]byte{pub}, Tags: map[string][]string{"t": {"A topic"}}, }, GroupBy: []types.GroupByField{types.GroupByAuthor, types.GroupByTagValue, types.GroupByTimeBucket}, AggFunc: types.AggCount, TagName: "t", TimeBucketSeconds: 86400, OrderDesc: false, } entries, err := s.client.QueryAggregation(ctx, q) ``` --- These parameters and calling methods can be flexibly combined to meet various statistical and analytical needs. The core is to construct an `AggregationQuery` and then use the `QueryAggregation` method to initiate an aggregation query. --- These are some relay data statistics functions that I developed based on this Aggregation feature. image Project link: #nostr_event_store