Skip to content

Commit

Permalink
Use USING for expId
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Marr <git@stefan-marr.de>
  • Loading branch information
smarr committed Mar 16, 2024
1 parent 61103ca commit e1c7191
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/backend/compare/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/backend/db/db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
);
Expand Down
47 changes: 24 additions & 23 deletions src/backend/db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -443,7 +443,8 @@ export abstract class Database {
exp: Experiment
): Promise<Trial> {
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 <Trial>this.trials.get(cacheKey);
Expand All @@ -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',
Expand All @@ -468,7 +469,7 @@ export abstract class Database {
values: [
e.manualRun,
data.startTime,
exp.id,
exp.expid,
e.userName,
env.envid,
source.sourceid,
Expand Down Expand Up @@ -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]
Expand All @@ -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`,
Expand All @@ -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]
});
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/backend/db/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export interface Source {
}

export interface Experiment {
id: number;
expid: number;

name: string;
projectid: number;
Expand Down
6 changes: 3 additions & 3 deletions src/backend/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tests/backend/db/db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down

0 comments on commit e1c7191

Please sign in to comment.