Skip to content

Commit

Permalink
Improved atVersion.supportedByAutomation field handling (#1182)
Browse files Browse the repository at this point in the history
* First attempt at cache, failing tests

* Simplification getAtVersionWithRequirements

* Import resolver by property name in AtVersion resolver
  • Loading branch information
stalgiag authored Jul 31, 2024
1 parent 945d875 commit 3192435
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 33 deletions.
8 changes: 7 additions & 1 deletion client/components/AddTestToQueueWithConfirmation/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ function AddTestToQueueWithConfirmation({
);

const existingTestPlanReports =
existingTestPlanReportsData?.existingTestPlanVersion?.testPlanReports;
existingTestPlanReportsData?.existingTestPlanVersion?.testPlanReports.filter(
tpr =>
tpr.at?.id === at?.id &&
tpr.browser?.id === browser?.id &&
tpr.minimumAtVersion?.id === minimumAtVersion?.id &&
tpr.exactAtVersion?.id === exactAtVersion?.id
);

let latestOldVersion;
let oldReportToCopyResultsFrom;
Expand Down
18 changes: 1 addition & 17 deletions server/models/AtVersion.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
const {
AT_VERSIONS_SUPPORTED_BY_COLLECTION_JOBS
} = require('../util/constants');

const MODEL_NAME = 'AtVersion';

module.exports = function (sequelize, DataTypes) {
Expand Down Expand Up @@ -32,10 +28,7 @@ module.exports = function (sequelize, DataTypes) {
defaultValue: new Date()
},
supportedByAutomation: {
type: DataTypes.VIRTUAL,
get() {
return Model.isSupportedByAutomation(this.atId, this.name);
}
type: DataTypes.VIRTUAL
}
},
{
Expand All @@ -56,14 +49,5 @@ module.exports = function (sequelize, DataTypes) {
});
};

Model.isSupportedByAutomation = async function (atId, versionName) {
const At = sequelize.models.At;
const at = await At.findByPk(atId);
if (!at) return false;
const supportedVersions =
AT_VERSIONS_SUPPORTED_BY_COLLECTION_JOBS[at.name] || [];
return supportedVersions.includes(versionName);
};

return Model;
};
7 changes: 7 additions & 0 deletions server/resolvers/AtVersion/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const supportedByAutomation = require('./supportedByAutomationResolver');

const AtVersion = {
supportedByAutomation
};

module.exports = AtVersion;
16 changes: 16 additions & 0 deletions server/resolvers/AtVersion/supportedByAutomationResolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { getAtVersionById } = require('../../models/services/AtService');
const {
isSupportedByAutomation
} = require('../../util/isSupportedByAutomation');

const supportedByAutomationResolver = async (parent, _, { transaction }) => {
const atVersion = await getAtVersionById({
id: parent.id,
transaction
});
return isSupportedByAutomation(atVersion.at.id, atVersion.name, {
transaction
});
};

module.exports = supportedByAutomationResolver;
4 changes: 3 additions & 1 deletion server/resolvers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const CollectionJob = require('./CollectionJob');
const TestPlanRun = require('./TestPlanRun');
const Test = require('./Test');
const ScenarioResult = require('./ScenarioResult');
const AtVersion = require('./AtVersion');

const resolvers = {
Query: {
Expand Down Expand Up @@ -97,7 +98,8 @@ const resolvers = {
TestPlanRunOperations,
TestResultOperations,
TestPlanVersionOperations,
CollectionJobOperations
CollectionJobOperations,
AtVersion
};

module.exports = resolvers;
23 changes: 9 additions & 14 deletions server/util/getAtVersionWithRequirements.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ const getAtVersionWithRequirements = async (
);
}

const isMinimumVersionSupported =
await minimumAtVersion.supportedByAutomation;
if (isMinimumVersionSupported) {
if (minimumAtVersion.supportedByAutomation) {
return minimumAtVersion;
}

const matchingAts = await getAtVersions({
const matchingAtVersions = await getAtVersions({
where: {
atId,
releasedAt: { [Op.gte]: minimumAtVersion.releasedAt }
Expand All @@ -35,23 +33,20 @@ const getAtVersionWithRequirements = async (
transaction
});

const supportedAts = await Promise.all(
matchingAts.map(async version => {
const supportedByAutomation = await version.supportedByAutomation;
return supportedByAutomation ? version.toJSON() : null;
})
);

const latestSupportedAt = supportedAts.find(Boolean);
const latestSupportedAtVersion = matchingAtVersions.find(async atv => {
// supportedByAutomation is a computed graphql field,
// so we need to compute directly here
return atv.supportedByAutomation;
});

if (!latestSupportedAt) {
if (!latestSupportedAtVersion) {
throw new Error(
`No suitable AT version found for automation for AT ${atId} ` +
`with minimumAtVersion ${minimumAtVersion?.name}`
);
}

return latestSupportedAt;
return latestSupportedAtVersion;
} catch (error) {
console.error('Error while determining AT version:', error);
throw error;
Expand Down
30 changes: 30 additions & 0 deletions server/util/isSupportedByAutomation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const AtLoader = require('../models/loaders/AtLoader');
const { AT_VERSIONS_SUPPORTED_BY_COLLECTION_JOBS } = require('./constants');

let atIdToNameCache = {};

const isSupportedByAutomation = async function (
atId,
versionName,
{ transaction }
) {
if (Object.keys(atIdToNameCache).length === 0) {
const ats = await AtLoader().getAll({ transaction });
for (const at of ats) {
atIdToNameCache[at.id] = at.name;
}
}
const atName = atIdToNameCache[atId];
if (!atName) {
console.warn(
`Attempt to check if ${versionName} is supported by automation for unknown AT ${atId}`
);
return false;
}
const supportedVersions =
AT_VERSIONS_SUPPORTED_BY_COLLECTION_JOBS[atName] || [];
const isSupported = supportedVersions.includes(versionName);
return isSupported;
};

module.exports = { isSupportedByAutomation };

0 comments on commit 3192435

Please sign in to comment.