-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheDocumentService3.cs
More file actions
341 lines (285 loc) · 10.5 KB
/
CacheDocumentService3.cs
File metadata and controls
341 lines (285 loc) · 10.5 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
Updated C# Service
csharppublic class CacheDocumentService
{
private readonly IMongoCollection<CacheDocument> _collection;
public CacheDocumentService(IMongoDatabase database)
{
_collection = database.GetCollection<CacheDocument>("CacheDocument");
}
public async Task<ServerSideResponse> GetGroupedData(ServerSideRequest request)
{
var currentLevel = request.GroupKeys.Count;
var totalLevels = request.RowGroupCols.Count;
var isLeafLevel = currentLevel >= totalLevels;
List<BsonDocument> results;
int totalCount;
if (isLeafLevel)
{
// Get leaf data from "data" field
results = await GetLeafData(request);
totalCount = await GetLeafDataCount(request);
}
else
{
// Get group data from "data" field
results = await GetGroupData(request, currentLevel);
totalCount = results.Count;
}
return new ServerSideResponse
{
Rows = TransformToGridFormat(results, request, isLeafLevel),
RowCount = totalCount
};
}
private async Task<List<BsonDocument>> GetGroupData(ServerSideRequest request, int level)
{
var pipeline = new List<BsonDocument>();
// Filter out documents where data is null
pipeline.Add(new BsonDocument("$match", new BsonDocument("data", new BsonDocument("$ne", null))));
// Add match stage for parent groups
if (request.GroupKeys.Count > 0)
{
var matchDoc = BuildParentGroupMatch(request);
pipeline.Add(new BsonDocument("$match", matchDoc));
}
// Add filter stage
if (request.FilterModel.Count > 0)
{
var filterDoc = BuildFilterStage(request.FilterModel);
pipeline.Add(new BsonDocument("$match", filterDoc));
}
// Group by current level field from "data"
var groupCol = request.RowGroupCols[level];
var groupDoc = new BsonDocument("$group", new BsonDocument
{
{ "_id", $"$data.{groupCol.Field}" }
});
pipeline.Add(groupDoc);
// Add sort
pipeline.Add(new BsonDocument("$sort", new BsonDocument("_id", 1)));
// Add pagination
if (request.StartRow > 0)
{
pipeline.Add(new BsonDocument("$skip", request.StartRow));
}
var pageSize = request.EndRow - request.StartRow;
if (pageSize > 0)
{
pipeline.Add(new BsonDocument("$limit", pageSize));
}
return await _collection.Aggregate<BsonDocument>(pipeline).ToListAsync();
}
private async Task<List<BsonDocument>> GetLeafData(ServerSideRequest request)
{
var pipeline = new List<BsonDocument>();
// Filter out documents where data is null
pipeline.Add(new BsonDocument("$match", new BsonDocument("data", new BsonDocument("$ne", null))));
// Project the data fields along with cache document fields
pipeline.Add(new BsonDocument("$project", new BsonDocument
{
// Cache document fields
{ "cacheKey", 1 },
{ "gridKey", 1 },
{ "parentId", 1 },
{ "fullPath", 1 },
{ "hashKey", 1 },
{ "documentType", 1 },
{ "createdAt", 1 },
{ "expiresAt", 1 },
{ "metadata", 1 },
// Data fields (flattened from "data" object)
{ "id", "$data.id" },
{ "region", "$data.region" },
{ "pendingChangeType", "$data.pendingChangeType" },
{ "approvalStatus", "$data.approvalStatus" },
{ "updatedBy", "$data.updatedBy" },
{ "updatedOn", "$data.updatedOn" },
{ "algo", "$data.algo" },
{ "controlCategory", "$data.controlCategory" },
{ "productType", "$data.productType" },
{ "productSegment", "$data.productSegment" },
{ "desk", "$data.desk" }
}));
// Match exact group path
if (request.GroupKeys.Count > 0)
{
var matchDoc = BuildExactGroupMatch(request);
pipeline.Add(new BsonDocument("$match", matchDoc));
}
// Add filter stage
if (request.FilterModel.Count > 0)
{
var filterDoc = BuildFilterStage(request.FilterModel);
pipeline.Add(new BsonDocument("$match", filterDoc));
}
// Add sort
if (request.SortModel.Count > 0)
{
var sortDoc = BuildSortStage(request.SortModel);
pipeline.Add(new BsonDocument("$sort", sortDoc));
}
else
{
// Default sort by updatedOn descending
pipeline.Add(new BsonDocument("$sort", new BsonDocument("updatedOn", -1)));
}
// Add pagination
if (request.StartRow > 0)
{
pipeline.Add(new BsonDocument("$skip", request.StartRow));
}
var pageSize = request.EndRow - request.StartRow;
if (pageSize > 0)
{
pipeline.Add(new BsonDocument("$limit", pageSize));
}
return await _collection.Aggregate<BsonDocument>(pipeline).ToListAsync();
}
private BsonDocument BuildParentGroupMatch(ServerSideRequest request)
{
var matchDoc = new BsonDocument();
for (int i = 0; i < request.GroupKeys.Count; i++)
{
var groupCol = request.RowGroupCols[i];
matchDoc[$"data.{groupCol.Field}"] = request.GroupKeys[i];
}
return matchDoc;
}
private BsonDocument BuildExactGroupMatch(ServerSideRequest request)
{
var matchDoc = new BsonDocument();
for (int i = 0; i < request.GroupKeys.Count; i++)
{
var groupCol = request.RowGroupCols[i];
// After projection, the fields are flattened to root level
matchDoc[groupCol.Field] = request.GroupKeys[i];
}
return matchDoc;
}
private async Task<int> GetLeafDataCount(ServerSideRequest request)
{
var pipeline = new List<BsonDocument>();
// Filter out documents where data is null
pipeline.Add(new BsonDocument("$match", new BsonDocument("data", new BsonDocument("$ne", null))));
// Project flattened fields from "data"
pipeline.Add(new BsonDocument("$project", new BsonDocument
{
{ "region", "$data.region" },
{ "controlCategory", "$data.controlCategory" },
{ "productType", "$data.productType" },
{ "desk", "$data.desk" },
{ "id", "$data.id" },
{ "pendingChangeType", "$data.pendingChangeType" },
{ "approvalStatus", "$data.approvalStatus" },
{ "updatedBy", "$data.updatedBy" },
{ "updatedOn", "$data.updatedOn" },
{ "algo", "$data.algo" },
{ "productSegment", "$data.productSegment" }
}));
if (request.GroupKeys.Count > 0)
{
var matchDoc = BuildExactGroupMatch(request);
pipeline.Add(new BsonDocument("$match", matchDoc));
}
if (request.FilterModel.Count > 0)
{
var filterDoc = BuildFilterStage(request.FilterModel);
pipeline.Add(new BsonDocument("$match", filterDoc));
}
pipeline.Add(new BsonDocument("$count", "total"));
var result = await _collection.Aggregate<BsonDocument>(pipeline).FirstOrDefaultAsync();
return result?["total"].AsInt32 ?? 0;
}
private List<object> TransformToGridFormat(
List<BsonDocument> results,
ServerSideRequest request,
bool isLeafLevel)
{
var gridRows = new List<object>();
foreach (var doc in results)
{
if (isLeafLevel)
{
gridRows.Add(TransformLeafRow(doc));
}
else
{
var currentLevel = request.GroupKeys.Count;
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)
{
if (element.Name == "_id") continue;
var value = ConvertBsonValue(element.Value);
row[element.Name] = value;
}
return row;
}
private object TransformGroupRow(BsonDocument doc, ServerSideRequest request, int level)
{
var groupCol = request.RowGroupCols[level];
var groupValue = doc["_id"]?.ToString() ?? "Unknown";
var row = new Dictionary<string, object>
{
[groupCol.Field] = groupValue,
["ag-Grid-AutoColumn"] = groupValue
};
// Add AG-Grid metadata for group rendering
row["__agGridGroupData"] = new
{
isGroup = true,
key = groupValue,
field = groupCol.Field,
level = level,
expanded = false
};
return row;
}
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()
};
}
private BsonDocument BuildFilterStage(Dictionary<string, object> filterModel)
{
var matchDoc = new BsonDocument();
foreach (var filter in filterModel)
{
if (filter.Value is string stringValue && !string.IsNullOrEmpty(stringValue))
{
matchDoc[filter.Key] = new BsonRegularExpression(stringValue, "i");
}
else if (filter.Value != null)
{
matchDoc[filter.Key] = BsonValue.Create(filter.Value);
}
}
return matchDoc;
}
private BsonDocument BuildSortStage(List<SortModel> sortModel)
{
var sortDoc = new BsonDocument();
foreach (var sort in sortModel)
{
sortDoc[sort.ColId] = sort.Sort == "asc" ? 1 : -1;
}
return sortDoc;
}
}