Skip to content
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

HPC-8486: Respect provided version and allow skipping validation #140

Merged
merged 4 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions src/lib/data/attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,16 @@ export const getAllAttachments = async ({
database,
planId,
types,
version,
planEntities,
governingEntities,
log,
skipValidation = false,
}: {
database: Database;
planId: PlanId;
types: AttachmentType[];
version: 'latest';
version: 'latest' | 'current';
/**
* A map of plan entities, which **must** include any entity that is the
* parent of the given attachment.
Expand All @@ -126,6 +128,13 @@ export const getAllAttachments = async ({
*/
governingEntities: MapOfGoverningEntities;
log: SharedLogContext;
/**
* Skip validation when fetching data from tables which have
* JSON columns, as those are expensive to verify.
*
* Also, skip type-checking attachment values against their types.
*/
skipValidation?: boolean;
}): Promise<AttachmentResults> => {
const attachmentPrototypesById = await findAndOrganizeObjectsByUniqueProperty(
database.attachmentPrototype,
Expand All @@ -137,6 +146,7 @@ export const getAllAttachments = async ({
[Op.IN]: types,
},
},
skipValidation,
}),
'id'
);
Expand All @@ -146,6 +156,9 @@ export const getAllAttachments = async ({
type: {
[Op.IN]: types,
},
...(version === 'latest'
? { latestVersion: true }
: { currentVersion: true }),
},
});
const attachmentVersionsByAttachmentId =
Expand All @@ -154,11 +167,14 @@ export const getAllAttachments = async ({
(t) =>
t.find({
where: {
latestVersion: true,
attachmentId: {
[Op.IN]: attachments.map((pa) => pa.id),
},
...(version === 'latest'
? { latestVersion: true }
: { currentVersion: true }),
},
skipValidation,
}),
'attachmentId'
);
Expand Down Expand Up @@ -200,11 +216,22 @@ export const getAllAttachments = async ({
})
);
}
const data = typeCheckAttachmentData({
attachment,
attachmentVersion,
log,
});

let data: AttachmentData;
if (!skipValidation) {
data = typeCheckAttachmentData({
attachment,
attachmentVersion,
log,
});
} else {
// Here, we sacrifice type-safety for speed, if `skipValidation` is enabled
data = {
type: attachment.type,
value: attachmentVersion.value,
} as unknown as AttachmentData;
}

result.set(attachment.id, {
id: attachment.id,
customRef,
Expand Down
9 changes: 8 additions & 1 deletion src/lib/data/globalClusters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ import { Op } from '../../db/util/conditions';
export const getAllGlobalClustersForPlan = async ({
database,
planId,
version,
}: {
database: Database;
planId: PlanId;
version: 'current' | 'latest';
}) => {
const governingEntities = await database.governingEntity.find({
where: { planId },
where: {
planId,
...(version === 'latest'
? { latestVersion: true }
: { currentVersion: true }),
},
});

const globalClusterAssociations =
Expand Down
17 changes: 15 additions & 2 deletions src/lib/data/governingEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ export type MapOfGoverningEntities = AnnotatedMap<
export const getAllGoverningEntitiesForPlan = async ({
database,
planId,
version,
prototypes,
skipValidation = false,
}: {
database: Database;
planId: PlanId;
version: 'latest';
version: 'latest' | 'current';
/**
* A map of prototypes that **must** include the prototype for governing
* entities of this plan.
Expand All @@ -40,10 +42,18 @@ export const getAllGoverningEntitiesForPlan = async ({
EntityPrototypeId,
InstanceDataOfModel<Database['entityPrototype']>
>;
/**
* Skip validation when fetching data from tables which have
* JSON columns, as those are expensive to verify
*/
skipValidation?: boolean;
}): Promise<MapOfGoverningEntities> => {
const ges = await database.governingEntity.find({
where: {
planId,
...(version === 'latest'
? { latestVersion: true }
: { currentVersion: true }),
},
});

Expand All @@ -52,11 +62,14 @@ export const getAllGoverningEntitiesForPlan = async ({
(t) =>
t.find({
where: {
latestVersion: true,
governingEntityId: {
[Op.IN]: ges.map((ge) => ge.id),
},
...(version === 'latest'
? { latestVersion: true }
: { currentVersion: true }),
},
skipValidation,
}),
'governingEntityId'
);
Expand Down
17 changes: 15 additions & 2 deletions src/lib/data/planEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ export const getAndValidateAllPlanEntities = async ({
database,
planId,
governingEntities,
version,
prototypes,
allowMissingPlanEntities,
skipValidation = false,
}: {
database: Database;
planId: PlanId;
Expand All @@ -51,7 +53,7 @@ export const getAndValidateAllPlanEntities = async ({
* generating references
*/
governingEntities: MapOfGoverningEntities;
version: 'latest';
version: 'latest' | 'current';
/**
* A map of prototypes that **must** include the prototype for governing
* entities of this plan.
Expand All @@ -70,10 +72,18 @@ export const getAndValidateAllPlanEntities = async ({
* This can happen for example when another entity is soft-deleted.
*/
allowMissingPlanEntities?: boolean;
/**
* Skip validation when fetching data from tables which have
* JSON columns, as those are expensive to verify
*/
skipValidation?: boolean;
}): Promise<ValidatedPlanEntities> => {
const planEntities = await database.planEntity.find({
where: {
planId,
...(version === 'latest'
? { latestVersion: true }
: { currentVersion: true }),
},
});
const planEntityIDs = new Set(planEntities.map((pe) => pe.id));
Expand All @@ -83,11 +93,14 @@ export const getAndValidateAllPlanEntities = async ({
(t) =>
t.find({
where: {
latestVersion: true,
planEntityId: {
[Op.IN]: planEntityIDs,
},
...(version === 'latest'
? { latestVersion: true }
: { currentVersion: true }),
},
skipValidation,
}),
'planEntityId'
);
Expand Down
7 changes: 7 additions & 0 deletions src/lib/data/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ export const getProjectBudgetsByOrgAndCluster = async <
projects,
log,
ignoreInconsistentBudgets,
skipValidation = false,
}: {
database: Database;
projects: Map<ProjectId, Data>;
Expand All @@ -273,6 +274,11 @@ export const getProjectBudgetsByOrgAndCluster = async <
* to for example calculate a plan's overall requirements.
*/
ignoreInconsistentBudgets?: true;
/**
* Skip validation when fetching data from tables which have
* JSON columns, as those are expensive to verify
*/
skipValidation?: boolean;
}): Promise<Map<ProjectId, ProjectBudgetSegmentBreakdown[]>> => {
const projectVersionIds = [...projects.values()].map(
(p) => p.projectVersion.id
Expand All @@ -298,6 +304,7 @@ export const getProjectBudgetsByOrgAndCluster = async <
[Op.IN]: segments.map((s) => s.id),
},
},
skipValidation,
});

const breakdownsBySegment = groupObjectsByProperty(
Expand Down