Skip to content

Commit 8426265

Browse files
Copilothotlong
andcommitted
Fix analytics service implementation and tests
- Fixed field naming issue in aggregation pipeline - Use short names internally and map to full cube.field names in results - Fixed sorting to use short field names - Added driver.connect() call in tests to load initial data - All 18 tests now passing Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 056f32a commit 8426265

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

packages/plugins/driver-memory/src/memory-analytics.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ describe('MemoryAnalyticsService', () => {
2929
}
3030
});
3131

32+
// Connect the driver to load initial data
33+
await driver.connect();
34+
3235
// Define cubes
3336
const cubes: Cube[] = [
3437
{

packages/plugins/driver-memory/src/memory-analytics.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,27 +140,28 @@ export class MemoryAnalyticsService implements IAnalyticsService {
140140

141141
pipeline.push({ $group: groupStage });
142142

143-
// Stage 4: $project to reshape results
143+
// Stage 4: $project to reshape results (use short names, we'll fix them later)
144144
const projectStage: Record<string, any> = { _id: 0 };
145145
if (query.dimensions && query.dimensions.length > 0) {
146146
for (const dim of query.dimensions) {
147147
const dimName = this.getShortName(dim);
148-
projectStage[dim] = `$_id.${dimName}`;
148+
projectStage[dimName] = `$_id.${dimName}`;
149149
}
150150
}
151151
if (query.measures && query.measures.length > 0) {
152152
for (const measure of query.measures) {
153153
const measureName = this.getShortName(measure);
154-
projectStage[measure] = `$${measureName}`;
154+
projectStage[measureName] = `$${measureName}`;
155155
}
156156
}
157157
pipeline.push({ $project: projectStage });
158158

159-
// Stage 5: $sort
159+
// Stage 5: $sort (use short names)
160160
if (query.order && Object.keys(query.order).length > 0) {
161161
const sortStage: Record<string, any> = {};
162162
for (const [field, direction] of Object.entries(query.order)) {
163-
sortStage[field] = direction === 'asc' ? 1 : -1;
163+
const shortName = this.getShortName(field);
164+
sortStage[shortName] = direction === 'asc' ? 1 : -1;
164165
}
165166
pipeline.push({ $sort: sortStage });
166167
}
@@ -175,7 +176,34 @@ export class MemoryAnalyticsService implements IAnalyticsService {
175176

176177
// Execute the aggregation pipeline
177178
const tableName = this.extractTableName(cube.sql);
178-
const rows = await this.driver.aggregate(tableName, pipeline);
179+
const rawRows = await this.driver.aggregate(tableName, pipeline);
180+
181+
// Rename fields from short names to full cube.field names
182+
const rows = rawRows.map(row => {
183+
const renamedRow: Record<string, unknown> = {};
184+
185+
// Rename dimensions
186+
if (query.dimensions) {
187+
for (const dim of query.dimensions) {
188+
const shortName = this.getShortName(dim);
189+
if (shortName in row) {
190+
renamedRow[dim] = row[shortName];
191+
}
192+
}
193+
}
194+
195+
// Rename measures
196+
if (query.measures) {
197+
for (const measure of query.measures) {
198+
const shortName = this.getShortName(measure);
199+
if (shortName in row) {
200+
renamedRow[measure] = row[shortName];
201+
}
202+
}
203+
}
204+
205+
return renamedRow;
206+
});
179207

180208
// Build field metadata
181209
const fields: Array<{ name: string; type: string }> = [];

0 commit comments

Comments
 (0)