Skip to content

Commit 0b8f475

Browse files
committed
feat: implement aggregation methods with support for grouping and median calculations in Clickhouse, MongoDB, and MySQL connectors
1 parent 63322e7 commit 0b8f475

3 files changed

Lines changed: 12 additions & 14 deletions

File tree

adminforth/dataConnectors/clickhouse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ class ClickhouseConnector extends AdminForthBaseConnector implements IAdminForth
449449
filters: IAdminForthAndOrFilter;
450450
aggregations: { [alias: string]: IAggregationRule };
451451
groupBy?: IGroupByRule;
452-
}): Promise<Array<{ group?: string; [key: string]: any }>> {
452+
}): Promise<any[]> {
453453

454454
const tableName = `${this.dbName}.${resource.table}`;
455455

adminforth/dataConnectors/mongo.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -361,19 +361,15 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
361361
pipeline.push({
362362
$project: {
363363
_id: 0,
364-
group: {
365-
$cond: {
366-
if: { $isNumber: "$_id" },
367-
then: "$_id",
368-
else: {
369-
$cond: {
370-
if: { $toString: "$_id" },
371-
then: { $dateToString: { format: "%Y-%m-%d", date: "$_id", timezone: groupBy?.timezone ?? 'UTC' } },
372-
else: "$_id"
373-
}
364+
group: groupBy?.type === 'date_trunc'
365+
? {
366+
$cond: {
367+
if: { $eq: [{ $type: "$_id" }, "date"] },
368+
then: { $dateToString: { format: "%Y-%m-%d", date: "$_id", timezone: groupBy?.timezone ?? 'UTC' } },
369+
else: "$_id"
374370
}
375371
}
376-
},
372+
: "$_id",
377373
...Object.fromEntries(
378374
Object.keys(groupStage)
379375
.filter(k => k !== '_id')

adminforth/dataConnectors/mysql.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ class MysqlConnector extends AdminForthBaseConnector implements IAdminForthDataS
386386
case 'min': selectParts.push(`MIN(${f}) AS \`${alias}\``); break;
387387
case 'max': selectParts.push(`MAX(${f}) AS \`${alias}\``); break;
388388
case 'median':
389-
selectParts.push(`GROUP_CONCAT(${f}) AS \`${alias}\``);
389+
selectParts.push(`GROUP_CONCAT(${f} ORDER BY ${f} ASC SEPARATOR ',') AS \`${alias}\``);
390390
medianAliases.push(alias);
391391
break;
392392
}
@@ -396,7 +396,9 @@ class MysqlConnector extends AdminForthBaseConnector implements IAdminForthDataS
396396
let query = `SELECT ${selectParts.join(', ')} FROM \`${tableName}\` ${where}`;
397397
if (groupExpr) query += ` GROUP BY ${groupExpr} ORDER BY ${groupExpr} ASC`;
398398

399-
await this.client.execute("SET SESSION group_concat_max_len = 1000000;");
399+
if (medianAliases.length > 0) {
400+
await this.client.execute("SET SESSION group_concat_max_len = 1000000;");
401+
}
400402

401403
const [rows]: any = await this.client.execute(query, filterValues);
402404

0 commit comments

Comments
 (0)