UserOfficeProject / issue-tracker

Shared place for features and bugs from all collaborators.
0 stars 0 forks source link

Copying generic templates is unoptimised #1193

Open simonfernandes opened 1 month ago

simonfernandes commented 1 month ago

When clicking the copy generic template button, we load generic templates and allow the user to select which one to copy a template from. We do this by querying all generic templates but we only send in a question ID to the query, which in the case of certain templates (e.g. Publications that appear in ISIS Direct) returns 10k+ templates, with the number growing every round. We are then doing a number of checks on every template's proposal, which involves fetching user details, before filtering out those that a user doesn't have permission to see. From limited testing I've seen this take anywhere from 30 seconds to over a minute locally with the new Postgres local database, even if the eventual list returned only contains a few templates.

These are the checks that are done on every template's proposal:

    return (
      this.userAuth.isUserOfficer(agent) ||
      this.userAuth.isInstrumentScientist(agent) ||
      this.userAuth.hasGetAccessByToken(agent) ||
      (await this.isMemberOfProposal(agent, proposal)) ||
      (await this.isReviewerOfProposal(agent, proposal.primaryKey)) ||
      (await this.isScientistToProposal(agent, proposal.primaryKey)) ||
      (await this.isInstrumentManagerToProposal(agent, proposal.primaryKey)) ||
      isInternalReviewerOnSomeTechnicalReview ||
      (await this.isChairOrSecretaryOfProposal(agent, proposal.primaryKey)) ||
      (await this.isVisitorOfProposal(agent, proposal.primaryKey)) ||
      (await this.isMemberOfFapProposal(agent, proposal.primaryKey))
    );

Note that if testing this on either local/dev/prod as a developer, isScientistToProposal() probably returns true and you can therefore see every single generic template (i.e. none are filtered out). I initially thought this was the reason for it being slow and dismissed it but it turns out it's unoptimised for everyone.