-
Notifications
You must be signed in to change notification settings - Fork 23
feat: implement aggregation methods with support for grouping and med… #577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
63322e7
0b8f475
235814e
ed5ae7e
a1f7db1
70366c6
2b534b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,7 +1,7 @@ | ||||||
| import dayjs from 'dayjs'; | ||||||
| import { MongoClient } from 'mongodb'; | ||||||
| import { Decimal128, Double } from 'bson'; | ||||||
| import { IAdminForthDataSourceConnector, IAdminForthSingleFilter, IAdminForthAndOrFilter, AdminForthResource } from '../types/Back.js'; | ||||||
| import { IAdminForthDataSourceConnector, IAdminForthSingleFilter, IAdminForthAndOrFilter, AdminForthResource, IAggregationRule, IGroupByRule, IGroupByDateTrunc, IGroupByField } from '../types/Back.js'; | ||||||
| import AdminForthBaseConnector from './baseConnector.js'; | ||||||
| import { afLogger } from '../modules/logger.js'; | ||||||
| import { AdminForthDataTypes, AdminForthFilterOperators, AdminForthSortDirections, } from '../types/Common.js'; | ||||||
|
|
@@ -305,6 +305,108 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS | |||||
| .filter((f) => (f as IAdminForthSingleFilter).insecureRawSQL === undefined) | ||||||
| .map((f) => this.getFilterQuery(resource, f))); | ||||||
| } | ||||||
|
|
||||||
| async getAggregateWithOriginalTypes({ resource, filters, aggregations, groupBy }: { | ||||||
| resource: AdminForthResource; | ||||||
| filters: IAdminForthAndOrFilter; | ||||||
| aggregations: { [alias: string]: IAggregationRule }; | ||||||
| groupBy?: IGroupByRule; | ||||||
| }): Promise<Array<{ group?: string, [key: string]: any }>> { | ||||||
|
kulikp1 marked this conversation as resolved.
|
||||||
|
|
||||||
| const collection = this.client.db().collection(resource.table); | ||||||
|
|
||||||
| const match = filters?.subFilters?.length ? this.getFilterQuery(resource, filters) : {}; | ||||||
|
|
||||||
| let groupId: any = null; | ||||||
|
|
||||||
| if (groupBy?.type === 'field') { | ||||||
| const g = groupBy as IGroupByField; | ||||||
| groupId = `$${g.field}`; | ||||||
| } | ||||||
|
|
||||||
| if (groupBy?.type === 'date_trunc') { | ||||||
| const g = groupBy as IGroupByDateTrunc; | ||||||
| const tz = g.timezone ?? 'UTC'; | ||||||
| const dateTruncSpec: any = { | ||||||
| date: `$${g.field}`, | ||||||
| unit: g.truncation, | ||||||
| timezone: tz, | ||||||
| }; | ||||||
| if (g.truncation === 'week') { | ||||||
| dateTruncSpec.startOfWeek = 'Mon'; | ||||||
| } | ||||||
| groupId = { $dateTrunc: dateTruncSpec }; | ||||||
| } | ||||||
|
|
||||||
| const groupStage: Record<string, any> = { | ||||||
| _id: groupId, | ||||||
| }; | ||||||
|
|
||||||
| for (const [alias, rule] of Object.entries(aggregations)) { | ||||||
| switch (rule.operation) { | ||||||
| case 'count': groupStage[alias] = { $sum: 1 }; break; | ||||||
| case 'sum': groupStage[alias] = { $sum: { $toDouble: `$${rule.field}` } }; break; | ||||||
| case 'avg': groupStage[alias] = { $avg: { $toDouble: `$${rule.field}` } }; break; | ||||||
| case 'min': groupStage[alias] = { $min: { $toDouble: `$${rule.field}` } }; break; | ||||||
| case 'max': groupStage[alias] = { $max: { $toDouble: `$${rule.field}` } }; break; | ||||||
| case 'median': groupStage[alias] = { $push: { $toDouble: `$${rule.field}` } }; break; | ||||||
|
||||||
| case 'median': groupStage[alias] = { $push: { $toDouble: `$${rule.field}` } }; break; | |
| case 'median': groupStage[alias] = { $median: { input: { $toDouble: `$${rule.field}` }, method: 'approximate' } }; break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is important
Uh oh!
There was an error while loading. Please reload this page.