-
-
Notifications
You must be signed in to change notification settings - Fork 291
Add contributions report query and allow filtering by order transaction charge date #11342
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
base: main
Are you sure you want to change the base?
Conversation
gustavlrsn
left a comment
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.
Looking good! Added a question about possible performance issues
| if (args.chargedDateFrom || args.chargedDateTo) { | ||
| where[Op.and].push( | ||
| sequelize.where(sequelize.literal(`COALESCE("Subscription"."lastChargedAt", "Order"."createdAt")`), { | ||
| [Op.gte]: args.chargedDateFrom, | ||
| }), | ||
| ); | ||
| } | ||
| if (args.chargedDateTo) { | ||
| where[Op.and].push( | ||
| sequelize.where(sequelize.literal(`COALESCE("Subscription"."lastChargedAt", "Order"."createdAt")`), { | ||
| [Op.lte]: args.chargedDateTo, | ||
| }), | ||
| sequelize.literal( | ||
| SequelizeUtils.formatNamedParameters( | ||
| `EXISTS ( | ||
| SELECT 1 FROM "Transactions" t | ||
| WHERE t."OrderId" = "Order"."id" AND | ||
| ${args.chargedDateFrom ? `COALESCE(t."clearedAt", t."createdAt") >= :chargedDateFrom AND` : ''} | ||
| ${args.chargedDateTo ? `COALESCE(t."clearedAt", t."createdAt") <= :chargedDateTo AND` : ''} | ||
| t."kind" in ('CONTRIBUTION', 'ADDED_FUNDS') AND | ||
| t."type" = 'CREDIT' AND | ||
| NOT t."isRefund" AND | ||
| t."RefundTransactionId" IS NULL AND | ||
| t."deletedAt" IS NULL | ||
| LIMIT 1 | ||
| )`, | ||
| { | ||
| chargedDateFrom: args.chargedDateFrom, | ||
| chargedDateTo: args.chargedDateTo, | ||
| }, | ||
| 'postgres', | ||
| ), | ||
| ), |
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.
What's the performance impact of this subquery?
If there is not already an index for this, maybe we could add one, or maybe use the existing condition when you only supply chargedDateFrom without chargedDateTo
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.
I tested the generated query on prod and the result is not great (~5s), I am trying something else I will update this PR with a better query strategy.
server/graphql/v2/object/Host.ts
Outdated
| dateTo: moment(args.dateTo).utc().toISOString(), | ||
| dateFrom: moment(args.dateFrom).utc().toISOString(), |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
server/lib/sequelize-cte.ts
Outdated
| identifierField: leftKey, | ||
| } as unknown as Association<Model, Model>, | ||
| _pseudo: true, | ||
| required: true, |
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 should be optional, the filtering nature of this include should be kept within the main query and not implicitly defined by the helper function.
| ${args.chargedDateFrom ? `COALESCE(t."clearedAt", t."createdAt") >= :chargedDateFrom AND` : ''} | ||
| ${args.chargedDateTo ? `COALESCE(t."clearedAt", t."createdAt") <= :chargedDateTo AND` : ''} |
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.
You can use the helper ifStr.
server/graphql/v2/object/Host.ts
Outdated
| dateTo: moment(args.dateTo).utc().toISOString(), | ||
| dateFrom: moment(args.dateFrom).utc().toISOString(), | ||
| }, |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
| ${ifStr(args.chargedDateFrom, `COALESCE(t."clearedAt", t."createdAt") >= :chargedDateFrom AND`)} | ||
| ${ifStr(args.chargedDateTo, `COALESCE(t."clearedAt", t."createdAt") <= :chargedDateTo AND`)} | ||
| t."type" = 'CREDIT' AND | ||
| NOT t."isRefund" AND | ||
| t."RefundTransactionId" IS NULL AND | ||
| t."deletedAt" IS NULL | ||
| ${ifStr(host?.id, `AND t."HostCollectiveId" = :hostCollectiveId`)} |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
Related opencollective/opencollective#8477
Resolve opencollective/opencollective#8498
Resolve opencollective/opencollective#8500