From e1c71917b331fd2f0ab5e2dd0480d0a19fa4b51c Mon Sep 17 00:00:00 2001 From: Stefan Marr Date: Sat, 16 Mar 2024 09:47:43 +0000 Subject: [PATCH] Use USING for expId Signed-off-by: Stefan Marr --- src/backend/compare/compare.ts | 2 +- src/backend/db/db.sql | 4 +-- src/backend/db/db.ts | 47 +++++++++++++++++----------------- src/backend/db/types.ts | 2 +- src/backend/main/main.ts | 6 ++--- tests/backend/db/db.test.ts | 8 +++--- 6 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/backend/compare/compare.ts b/src/backend/compare/compare.ts index 88a56592..66d415f7 100644 --- a/src/backend/compare/compare.ts +++ b/src/backend/compare/compare.ts @@ -100,7 +100,7 @@ async function getMeasurements( Measurement JOIN Trial ON trialId = Trial.id JOIN Source USING (sourceId) - JOIN Experiment ON Trial.expId = Experiment.id + JOIN Experiment USING (expId) JOIN Criterion ON criterion = criterion.id JOIN Run ON runId = run.id JOIN Project ON Project.id = Experiment.projectId diff --git a/src/backend/db/db.sql b/src/backend/db/db.sql index ff35a4e0..8b8ae44e 100644 --- a/src/backend/db/db.sql +++ b/src/backend/db/db.sql @@ -71,7 +71,7 @@ CREATE TABLE Source ( -- To identify experiments, we use a name. -- Optionally, a more elaborated description can be provided for documentation. CREATE TABLE Experiment ( - id serial primary key, + expId serial primary key, name varchar NOT NULL, projectId smallint, @@ -111,7 +111,7 @@ CREATE TABLE Trial ( -- functionally dependent on startTime in the intended scenarios. unique (username, envId, expId, startTime), - foreign key (expId) references Experiment (id), + foreign key (expId) references Experiment (expId), foreign key (envId) references Environment (envId), foreign key (sourceId) references Source (sourceId) ); diff --git a/src/backend/db/db.ts b/src/backend/db/db.ts index c7c2d1a9..38830090 100644 --- a/src/backend/db/db.ts +++ b/src/backend/db/db.ts @@ -61,7 +61,7 @@ const measurementDataColumns = ` const measurementDataTableJoins = ` Measurement JOIN Trial ON trialId = Trial.id - JOIN Experiment ON expId = Experiment.id + JOIN Experiment USING (expId) JOIN Source USING (sourceId) JOIN Criterion USING (critId) JOIN Run ON runId = run.id`; @@ -217,7 +217,7 @@ export abstract class Database { s.commitMessage, s.authorName FROM Project p JOIN Experiment e USING (projectId) - JOIN Trial t ON e.id = t.expId + JOIN Trial t USING (expId) JOIN Source s USING (sourceId) WHERE lower(p.slug) = lower($1) AND s.commitid = $2 @@ -336,7 +336,7 @@ export abstract class Database { r.executor as execName FROM Project p JOIN Experiment exp USING (projectId) - JOIN Trial t ON t.expId = exp.id + JOIN Trial t USING (expId) JOIN Source src USING (sourceId) JOIN Environment env USING (envId) JOIN Timeline tl ON tl.trialId = t.id @@ -443,7 +443,8 @@ export abstract class Database { exp: Experiment ): Promise { const e = data.env; - const cacheKey = `${e.userName}-${env.envid}-${data.startTime}-${exp.id}`; + // eslint-disable-next-line max-len + const cacheKey = `${e.userName}-${env.envid}-${data.startTime}-${exp.expid}`; if (this.trials.has(cacheKey)) { return this.trials.get(cacheKey); @@ -458,7 +459,7 @@ export abstract class Database { text: `SELECT * FROM Trial WHERE username = $1 AND envId = $2 AND startTime = $3 AND expId = $4`, - values: [e.userName, env.envid, data.startTime, exp.id] + values: [e.userName, env.envid, data.startTime, exp.expid] }, { name: 'insertTrial', @@ -468,7 +469,7 @@ export abstract class Database { values: [ e.manualRun, data.startTime, - exp.id, + exp.expid, e.userName, env.envid, source.sourceid, @@ -584,13 +585,13 @@ export abstract class Database { text: `SELECT DISTINCT s.*, min(t.startTime) as firstStart FROM Source s JOIN Trial t USING (sourceId) - JOIN Experiment e ON e.id = t.expId + JOIN Experiment e USING (expId) JOIN Project p USING (projectId) WHERE p.name = $1 AND s.branchOrTag = p.baseBranch AND s.commitId <> $2 AND p.baseBranch IS NOT NULL - GROUP BY e.id, s.sourceId + GROUP BY e.expId, s.sourceId ORDER BY firstStart DESC LIMIT 1`, values: [projectName, currentCommitId] @@ -612,7 +613,7 @@ export abstract class Database { text: `SELECT DISTINCT s.* FROM Source s JOIN Trial t USING (sourceId) - JOIN Experiment e ON e.id = t.expId + JOIN Experiment e USING (expId) JOIN Project p USING (projectId) WHERE p.name = $1 AND s.id = $2 LIMIT 1`, @@ -634,9 +635,9 @@ export abstract class Database { name: 'fetchSourceByProjectNameExpName', text: `SELECT DISTINCT s.* FROM Source s - JOIN Trial t USING (sourceId) - JOIN Experiment e ON e.id = t.expId - JOIN Project p USING (projectId) + JOIN Trial t USING (sourceId) + JOIN Experiment e USING (expId) + JOIN Project p USING (projectId) WHERE p.name = $1 AND e.name = $2`, values: [projectName, experimentName] }); @@ -824,7 +825,7 @@ export abstract class Database { throw new Error(`Could not record completion without endTime`); } - await this.recordExperimentCompletion(exp.id, data.endTime); + await this.recordExperimentCompletion(exp.expid, data.endTime); return exp; } @@ -1231,7 +1232,7 @@ export abstract class Database { text: `SELECT DISTINCT branchOrTag, s.commitId FROM Source s JOIN Trial tr USING (sourceId) - JOIN Experiment e ON tr.expId = e.id + JOIN Experiment e USING (expId) JOIN Project p USING (projectId) WHERE p.name = $1 AND @@ -1284,8 +1285,8 @@ export abstract class Database { commitId, runId FROM ProfileData pd JOIN Trial ON pd.trialId = Trial.id - JOIN Experiment e ON trial.expId = e.id - JOIN Source USING (sourceId) + JOIN Experiment e USING (expId) + JOIN Source USING (sourceId) JOIN Run ON runId = run.id WHERE (commitId = $1 OR commitId = $2) @@ -1305,14 +1306,14 @@ export abstract class Database { const q = { name: 'fetchLatestBenchmarksForProject', text: `WITH LatestExperiment AS ( - SELECT exp.id as expId, max(t.startTime) as newest + SELECT exp.expId, max(t.startTime) as newest FROM Project p JOIN Experiment exp USING (projectId) - JOIN Trial t ON t.expId = exp.id + JOIN Trial t USING (expId) JOIN Source src USING (sourceId) WHERE p.projectId = $1 AND p.baseBranch = src.branchOrTag - GROUP BY exp.id + GROUP BY exp.expId ORDER BY newest DESC LIMIT 1 ) @@ -1328,12 +1329,12 @@ export abstract class Database { r.extraArgs FROM Project p JOIN Experiment exp USING (projectId) - JOIN Trial t ON t.expId = exp.id + JOIN Trial t USING (expId) JOIN Source src USING (sourceId) JOIN Environment env USING (envId) JOIN Timeline tl ON tl.trialId = t.id JOIN Run r ON tl.runId = r.id - JOIN LatestExperiment le ON exp.id = le.expId + JOIN LatestExperiment le USING (expId) WHERE p.projectId = $1 AND p.baseBranch = src.branchOrTag @@ -1508,7 +1509,7 @@ export abstract class Database { FROM Timeline ti JOIN Trial tr ON tr.id = ti.trialId JOIN Source s USING (sourceId) - JOIN Experiment e ON tr.expId = e.id + JOIN Experiment e USING (expId) JOIN Project p USING (projectId) JOIN Run r ON r.id = ti.runId JOIN Criterion c USING (critId) @@ -1540,7 +1541,7 @@ export abstract class Database { FROM Timeline ti JOIN Trial tr ON tr.id = ti.trialId JOIN Source s USING (sourceId) - JOIN Experiment e ON tr.expId = e.id + JOIN Experiment e USING (expId) JOIN Project p USING (projectId) JOIN Run r ON r.id = ti.runId JOIN Criterion c USING (critId) diff --git a/src/backend/db/types.ts b/src/backend/db/types.ts index fb294823..8d067e8e 100644 --- a/src/backend/db/types.ts +++ b/src/backend/db/types.ts @@ -80,7 +80,7 @@ export interface Source { } export interface Experiment { - id: number; + expid: number; name: string; projectid: number; diff --git a/src/backend/main/main.ts b/src/backend/main/main.ts index 722d822c..47d64a0c 100644 --- a/src/backend/main/main.ts +++ b/src/backend/main/main.ts @@ -73,7 +73,7 @@ export async function getLast100Measurements( ) FROM Measurement m JOIN Trial t ON m.trialId = t.id - JOIN Experiment e ON t.expId = e.id + JOIN Experiment e USING (expId) JOIN Run r ON m.runId = r.id JOIN Criterion c USING (critId) WHERE projectId = $1 AND @@ -163,8 +163,8 @@ export async function getChanges( text: ` SELECT commitId, branchOrTag, p.projectId, repoURL, commitMessage, max(startTime) as experimentTime FROM experiment - JOIN Trial ON expId = experiment.id - JOIN Source USING (sourceId) + JOIN Trial USING (expId) + JOIN Source USING (sourceId) JOIN Project p USING (projectId) WHERE p.projectId = $1 GROUP BY commitId, branchOrTag, p.projectId, repoURL, commitMessage diff --git a/tests/backend/db/db.test.ts b/tests/backend/db/db.test.ts index c3a97b4e..06aea9f6 100644 --- a/tests/backend/db/db.test.ts +++ b/tests/backend/db/db.test.ts @@ -60,15 +60,15 @@ describe('Record Trial', () => { it('but, recording the same for another experiment, should', async () => { basicTestData.experimentName += ' 2'; const exp2 = await db.recordExperiment(basicTestData); - expect(exp2.id).not.toEqual(exp.id); + expect(exp2.expid).not.toEqual(exp.expid); const result = await db.recordTrial(basicTestData, env, exp2); - expect(result.expid).toEqual(exp2.id); + expect(result.expid).toEqual(exp2.expid); const tResult = await db.query({ text: 'SELECT * FROM Trial' }); expect(tResult.rowCount).toEqual(2); - expect(tResult.rows[0].expid).toEqual(exp.id); - expect(tResult.rows[1].expid).toEqual(exp2.id); + expect(tResult.rows[0].expid).toEqual(exp.expid); + expect(tResult.rows[1].expid).toEqual(exp2.expid); }); });