forked from coding-blocks/gondor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix n+1 problem for zoom account availability (coding-blocks#82)
* fix n+1 problem for zoom account availability * spead options and reduce statements * replace findOne with findAll * reduce one loop * don't remove some functions * fix conflicts * use findAll and loaderBaseClass * remove redundant code and excludeTopic-->excludeEvent * fix function name
- Loading branch information
1 parent
0d8dc66
commit f923413
Showing
5 changed files
with
81 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
import ZoomAccount from 'Services/ZoomAccount'; | ||
import BaseResolver from 'Graphql/base/Resolver'; | ||
|
||
class ZoomAccountAvailability extends BaseResolver { | ||
resolve = () => | ||
ZoomAccount.findAvailaibilityDuring( | ||
this.parent.id, | ||
this.args.dateTimeRange, | ||
); | ||
resolve = async () => | ||
await this.ctx.loaders.zoomAccountAvailability.load({ | ||
id: this.parent.id, | ||
dateTimeRange: this.args.dateTimeRange, | ||
}); | ||
} | ||
|
||
export default ZoomAccountAvailability.resolver(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import Models from 'Models'; | ||
import BaseLoader from 'Services/BaseModelService/Loader'; | ||
import overlapDateTimeClause from 'Utils/overlapDateTimeClause'; | ||
|
||
export default class Availability extends BaseLoader { | ||
load = async () => { | ||
const accounts = await Models.ZoomAccount.findAll({ | ||
include: [ | ||
{ | ||
model: Models.Resource, | ||
as: 'uses', | ||
include: [ | ||
{ | ||
model: Models.CalendarEvent, | ||
as: 'calendarEvent', | ||
where: { | ||
[Models.Sequelize.Op.or]: this.keys.map( | ||
({ id, dateTimeRange, excludeEvents = [] }) => ({ | ||
[Models.Sequelize.Op.and]: [ | ||
overlapDateTimeClause(dateTimeRange), | ||
{ | ||
'$zoom_account.id$': id, | ||
}, | ||
{ | ||
id: { | ||
[Models.Sequelize.Op.notIn]: excludeEvents, | ||
}, | ||
}, | ||
], | ||
}), | ||
), | ||
}, | ||
required: true, | ||
}, | ||
], | ||
required: true, | ||
}, | ||
], | ||
}); | ||
|
||
return this.keys.map( | ||
(key) => | ||
!accounts.find((account) => this.equateKeyAndAccount(key, account)), | ||
); | ||
}; | ||
|
||
equateKeyAndAccount({ id, excludedEvents = [], dateTimeRange }, account) { | ||
if ( | ||
account.id !== id || | ||
excludedEvents.includes(account['uses.calendarEvent.id']) | ||
) | ||
return false; | ||
|
||
if ( | ||
new Date(account['uses.calendarEvent.start_at']) > | ||
new Date(dateTimeRange.end_at) || | ||
new Date(account['uses.calendarEvent.end_at']) < | ||
new Date(dateTimeRange.start_at) | ||
) | ||
return false; | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters