Repository navigation
Expand file tree
/
Copy pathSSRM.ts
More file actions
452 lines (377 loc) · 13.3 KB
/
Copy pathSSRM.ts
File metadata and controls
452 lines (377 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
difference client vs server SSRM ag-grid Row Column Grouping angular
In AG-Grid's Server-Side Row Model (SSRM), row and column grouping can be handled either on the client-side or server-side, each with distinct implications for performance and implementation.
Client-Side Grouping in SSRM
How it works:
The server returns flat data to AG-Grid
AG-Grid performs the grouping logic in the browser
All group operations (expand/collapse, aggregations) happen client-side
Pros:
Simpler server implementation - just return raw data
Rich interactive features work out-of-the-box
Good for smaller datasets (thousands of rows)
Cons:
Performance degrades with large datasets
All data must be loaded to perform grouping
Memory consumption increases significantly
Angular Implementation:
typescript
gridOptions: GridOptions = {
rowModelType: 'serverSide',
rowGroupPanelShow: 'always',
groupDefaultExpanded: 1,
columnDefs: [
{ field: 'country', rowGroup: true, hide: true },
{ field: 'sport', rowGroup: true, hide: true },
{ field: 'athlete' },
{ field: 'gold', aggFunc: 'sum' }
],
onGridReady: (params) => {
const datasource: IServerSideDatasource = {
getRows: (params: IServerSideGetRowsParams) => {
// Server returns flat data, AG-Grid handles grouping
this.httpClient.get('/api/data', { params: this.buildParams(params) })
.subscribe(data => {
params.success({
rowData: data.rows,
rowCount: data.totalCount
});
});
}
};
params.api.setServerSideDatasource(datasource);
}
};
Server-Side Grouping in SSRM
How it works:
The server receives grouping instructions from AG-Grid
Server performs grouping, aggregation, and pagination
AG-Grid receives pre-grouped data structure
Pros:
Handles massive datasets efficiently
Lower memory footprint on client
Faster initial load times
Leverages database optimization (indexes, etc.)
Cons:
Complex server-side implementation required
Must handle group expand/collapse requests
Server must maintain grouping state
Angular Implementation:
typescript
gridOptions: GridOptions = {
rowModelType: 'serverSide',
serverSideStoreType: 'partial',
rowGroupPanelShow: 'always',
columnDefs: [
{ field: 'country', rowGroup: true, hide: true },
{ field: 'sport', rowGroup: true, hide: true },
{ field: 'athlete' },
{ field: 'gold', aggFunc: 'sum' }
],
onGridReady: (params) => {
const datasource: IServerSideDatasource = {
getRows: (params: IServerSideGetRowsParams) => {
// Build request with grouping information
const request = {
startRow: params.request.startRow,
endRow: params.request.endRow,
rowGroupCols: params.request.rowGroupCols,
groupKeys: params.request.groupKeys,
valueCols: params.request.valueCols
};
this.httpClient.post('/api/grouped-data', request)
.subscribe(data => {
params.success({
rowData: data.rows, // Pre-grouped data
rowCount: data.rowCount
});
});
}
};
params.api.setServerSideDatasource(datasource);
}
};
Server-Side Implementation Considerations
For server-side grouping, your API needs to handle:
csharp
// C# Example - handling the grouping request
public class GroupedDataRequest
{
public int StartRow { get; set; }
public int EndRow { get; set; }
public List<ColumnVO> RowGroupCols { get; set; }
public List<string> GroupKeys { get; set; }
public List<ColumnVO> ValueCols { get; set; }
}
// MongoDB aggregation pipeline example
public async Task<GroupedDataResponse> GetGroupedData(GroupedDataRequest request)
{
var pipeline = new List<BsonDocument>();
// Build grouping stages based on request.RowGroupCols
foreach (var groupCol in request.RowGroupCols)
{
pipeline.Add(new BsonDocument("$group", new BsonDocument
{
{ "_id", $"${groupCol.Field}" },
{ "count", new BsonDocument("$sum", 1) }
}));
}
// Execute aggregation
var results = await collection.Aggregate<BsonDocument>(pipeline).ToListAsync();
return new GroupedDataResponse
{
Rows = TransformToGridFormat(results),
RowCount = results.Count
};
}
Recommendation
For your architecture:
Use client-side grouping for datasets < 10,000 rows or when you need rapid prototyping
Use server-side grouping for large datasets, especially when leveraging MongoDB's powerful aggregation framework for optimal performance
The choice often depends on your data volume, server capabilities, and the complexity you're willing to handle on the backend.
define TransformToGridFormat
Here's a comprehensive implementation of TransformToGridFormat for converting MongoDB aggregation results into AG-Grid's expected format for server-side row grouping:
csharp
public class GroupedDataResponse
{
public List<object> Rows { get; set; }
public int RowCount { get; set; }
}
public class GridRowData
{
public string Group { get; set; }
public Dictionary<string, object> Data { get; set; }
public bool IsGroup { get; set; }
public int ChildCount { get; set; }
public string[] GroupKeys { get; set; }
}
private List<object> TransformToGridFormat(
List<BsonDocument> mongoResults,
GroupedDataRequest request)
{
var gridRows = new List<object>();
// Determine the current grouping level based on GroupKeys
var currentLevel = request.GroupKeys?.Count ?? 0;
var totalLevels = request.RowGroupCols?.Count ?? 0;
var isLeafLevel = currentLevel >= totalLevels;
foreach (var doc in mongoResults)
{
if (isLeafLevel)
{
// Leaf level - return actual data rows
gridRows.Add(TransformLeafRow(doc));
}
else
{
// Group level - return group headers
gridRows.Add(TransformGroupRow(doc, request, currentLevel));
}
}
return gridRows;
}
private object TransformLeafRow(BsonDocument doc)
{
var row = new Dictionary<string, object>();
foreach (var element in doc.Elements)
{
var key = element.Name;
var value = element.Value;
// Convert BSON types to .NET types
row[key] = ConvertBsonValue(value);
}
return row;
}
private object TransformGroupRow(BsonDocument doc, GroupedDataRequest request, int level)
{
var groupCol = request.RowGroupCols[level];
var groupValue = doc["_id"].ToString();
var childCount = doc.Contains("count") ? doc["count"].AsInt32 : 0;
// Build the group keys array (parent groups + current group)
var groupKeys = new List<string>();
if (request.GroupKeys != null)
{
groupKeys.AddRange(request.GroupKeys);
}
groupKeys.Add(groupValue);
var groupRow = new Dictionary<string, object>
{
// The field being grouped by should contain the group value
[groupCol.Field] = groupValue,
// AG-Grid specific properties for group rows
["ag-Grid-AutoColumn"] = groupValue,
// Add aggregated values if they exist
};
// Add aggregated values from MongoDB results
AddAggregatedValues(groupRow, doc, request.ValueCols);
// Add AG-Grid group metadata
groupRow["__agGridGroupData"] = new
{
isGroup = true,
key = groupValue,
field = groupCol.Field,
childCount = childCount,
level = level,
expanded = false // Initially collapsed
};
return groupRow;
}
private void AddAggregatedValues(
Dictionary<string, object> groupRow,
BsonDocument doc,
List<ColumnVO> valueCols)
{
if (valueCols == null) return;
foreach (var valueCol in valueCols)
{
var aggFieldName = GetAggregatedFieldName(valueCol);
if (doc.Contains(aggFieldName))
{
groupRow[valueCol.Field] = ConvertBsonValue(doc[aggFieldName]);
}
}
}
private string GetAggregatedFieldName(ColumnVO valueCol)
{
// Map AG-Grid aggregation function to MongoDB field name
return valueCol.AggFunc switch
{
"sum" => $"{valueCol.Field}_sum",
"avg" => $"{valueCol.Field}_avg",
"min" => $"{valueCol.Field}_min",
"max" => $"{valueCol.Field}_max",
"count" => $"{valueCol.Field}_count",
_ => valueCol.Field
};
}
private object ConvertBsonValue(BsonValue bsonValue)
{
return bsonValue.BsonType switch
{
BsonType.String => bsonValue.AsString,
BsonType.Int32 => bsonValue.AsInt32,
BsonType.Int64 => bsonValue.AsInt64,
BsonType.Double => bsonValue.AsDouble,
BsonType.Decimal128 => bsonValue.AsDecimal,
BsonType.Boolean => bsonValue.AsBoolean,
BsonType.DateTime => bsonValue.AsDateTime,
BsonType.ObjectId => bsonValue.AsObjectId.ToString(),
BsonType.Null => null,
_ => bsonValue.ToString()
};
}
Enhanced MongoDB Aggregation Pipeline
Here's an updated aggregation method that properly handles multi-level grouping:
csharp
public async Task<GroupedDataResponse> GetGroupedData(GroupedDataRequest request)
{
var pipeline = new List<BsonDocument>();
// Apply filters first (if any)
if (request.FilterModel != null)
{
pipeline.Add(BuildFilterStage(request.FilterModel));
}
// Determine grouping level
var currentLevel = request.GroupKeys?.Count ?? 0;
var totalLevels = request.RowGroupCols?.Count ?? 0;
if (currentLevel < totalLevels)
{
// We're at a group level - need to create group aggregation
pipeline.AddRange(BuildGroupingPipeline(request, currentLevel));
}
else
{
// We're at leaf level - return filtered data
pipeline.AddRange(BuildLeafPipeline(request));
}
// Execute pipeline
var results = await _collection.Aggregate<BsonDocument>(pipeline).ToListAsync();
return new GroupedDataResponse
{
Rows = TransformToGridFormat(results, request),
RowCount = results.Count
};
}
private List<BsonDocument> BuildGroupingPipeline(GroupedDataRequest request, int level)
{
var pipeline = new List<BsonDocument>();
var groupCol = request.RowGroupCols[level];
// Match stage for parent group keys
if (request.GroupKeys != null && request.GroupKeys.Count > 0)
{
var matchConditions = new BsonDocument();
for (int i = 0; i < request.GroupKeys.Count; i++)
{
var parentCol = request.RowGroupCols[i];
matchConditions[parentCol.Field] = request.GroupKeys[i];
}
pipeline.Add(new BsonDocument("$match", matchConditions));
}
// Group stage
var groupStage = new BsonDocument("$group", new BsonDocument
{
{ "_id", $"${groupCol.Field}" },
{ "count", new BsonDocument("$sum", 1) }
});
// Add aggregations for value columns
if (request.ValueCols != null)
{
foreach (var valueCol in request.ValueCols)
{
var aggFieldName = GetAggregatedFieldName(valueCol);
var aggExpression = valueCol.AggFunc switch
{
"sum" => new BsonDocument("$sum", $"${valueCol.Field}"),
"avg" => new BsonDocument("$avg", $"${valueCol.Field}"),
"min" => new BsonDocument("$min", $"${valueCol.Field}"),
"max" => new BsonDocument("$max", $"${valueCol.Field}"),
"count" => new BsonDocument("$sum", 1),
_ => new BsonDocument("$first", $"${valueCol.Field}")
};
groupStage["$group"][aggFieldName] = aggExpression;
}
}
pipeline.Add(groupStage);
// Sort stage
pipeline.Add(new BsonDocument("$sort", new BsonDocument("_id", 1)));
// Pagination
if (request.StartRow > 0)
{
pipeline.Add(new BsonDocument("$skip", request.StartRow));
}
if (request.EndRow > request.StartRow)
{
pipeline.Add(new BsonDocument("$limit", request.EndRow - request.StartRow));
}
return pipeline;
}
private List<BsonDocument> BuildLeafPipeline(GroupedDataRequest request)
{
var pipeline = new List<BsonDocument>();
// Match exact group path for leaf nodes
if (request.GroupKeys != null && request.GroupKeys.Count > 0)
{
var matchConditions = new BsonDocument();
for (int i = 0; i < request.GroupKeys.Count; i++)
{
var groupCol = request.RowGroupCols[i];
matchConditions[groupCol.Field] = request.GroupKeys[i];
}
pipeline.Add(new BsonDocument("$match", matchConditions));
}
// Pagination for leaf data
if (request.StartRow > 0)
{
pipeline.Add(new BsonDocument("$skip", request.StartRow));
}
if (request.EndRow > request.StartRow)
{
pipeline.Add(new BsonDocument("$limit", request.EndRow - request.StartRow));
}
return pipeline;
}
This implementation handles:
Multi-level grouping with proper parent-child relationships
Aggregated values (sum, avg, min, max, count) at group levels
Pagination at both group and leaf levels
Type conversion from BSON to .NET types
AG-Grid specific metadata for proper rendering
The key is understanding that AG-Grid expects different data structures for group rows vs. leaf rows, and the grouping level is determined by the GroupKeys array length.