Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ jobs:

# run integration tests
pushd tests/rebench-integration
rebench --experiment IntegrationTest rebench.conf
rebench --no-denoise --experiment IntegrationTest rebench.conf

sleep 1
PROJID=$(curl -s http://localhost:33333/ReBenchDB-integration-test/data | grep project-id | grep -o -E '[0-9]+')
EXPID=$(curl -s http://localhost:33333/rebenchdb/dash/$PROJID/data-overview | jq '.data[0].expid')
PROJID=$(curl -sL http://localhost:33333/ReBenchDB-integration-test/data | grep project-id | grep -o -E '[0-9]+')
EXPID=$(curl -sL http://localhost:33333/rebenchdb/dash/$PROJID/data-overview | jq '.data[0].expid')

# Trigger data generation
curl -s http://localhost:33333/ReBenchDB-integration-test/data/$EXPID.json.gz > /dev/null
curl -s http://localhost:33333/ReBenchDB-integration-test/data/$EXPID.csv.gz > /dev/null
curl -sL http://localhost:33333/ReBenchDB-integration-test/data/$EXPID.json.gz > /dev/null
curl -sL http://localhost:33333/ReBenchDB-integration-test/data/$EXPID.csv.gz > /dev/null

sleep 10 # give the server some time to generate the files
# reposses the files to be able to read them
Expand Down
73 changes: 72 additions & 1 deletion src/backend/db/db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ CREATE TABLE Project (
position integer DEFAULT 0,

-- the bases for comparisons that we generate when a experiment is completed
baseBranch varchar
baseBranch varchar,

namespaceId int NOT NULL -- foreign key is added below
);

-- Identifies the specific state of the source, the code, on which
Expand Down Expand Up @@ -200,6 +202,75 @@ CREATE TABLE Timeline (
foreign key (criterion) references Criterion (id)
);

-- User Accounts
CREATE TABLE UserAccount (
id serial primary key,
userName varchar unique, -- has to be a valid `slug`
email varchar unique,
passwordHash varchar,
apiToken char(64) NULL,
createdAt timestamp with time zone DEFAULT now(),
isActive bool NOT NULL DEFAULT true -- deactivated users cannot log in
);

CREATE TABLE UserGroup (
id serial primary key,
groupName varchar unique, -- has to be a valid `slug`
description text,
createdAt timestamp with time zone DEFAULT now(),
createdBy int REFERENCES UserAccount (id)
);

-- A Namespace is either a user account or user group, and ensures that
-- there are no name collisions between users and groups.
CREATE TABLE Namespace (
id serial primary key,
name varchar unique NOT NULL, -- has to be a valid `slug`
type char(1) CHECK (type IN ('u', 'g')),
accountId int NULL REFERENCES UserAccount (id),
groupId int NULL REFERENCES UserGroup (id),

CONSTRAINT eitherAccountOrGroup CHECK (
(accountId IS NOT NULL AND groupId IS NULL AND type = 'u') OR
(accountId IS NULL AND groupId IS NOT NULL AND type = 'g')
)
);

ALTER TABLE Project
ADD FOREIGN KEY (namespaceId) REFERENCES Namespace (id);
ALTER TABLE UserAccount
ADD COLUMN namespaceId int REFERENCES Namespace (id);
ALTER TABLE UserGroup
ADD COLUMN namespaceId int REFERENCES Namespace (id);

CREATE TYPE projectRole AS ENUM ('view', 'edit', 'owner');

CREATE TABLE ProjectPermissions (
projectId int REFERENCES Project (id),
namespaceId int REFERENCES Namespace (id),
role projectRole NOT NULL,
PRIMARY KEY (projectId, namespaceId)
);

-- The admin account owns the legacy group.
-- Because Authentication is not yet implemented, the admin account has no password hash, so it cannot log in.
INSERT INTO UserAccount (userName) VALUES ('admin');
INSERT INTO Namespace (name, type, accountId)
SELECT userName, 'u', id FROM UserAccount WHERE userName = 'admin';
UPDATE UserAccount
SET namespaceId = (SELECT id FROM Namespace WHERE name = 'admin')
WHERE userName = 'admin';

-- Old projects that were created before users and groups existed belong to the legacy group.
INSERT INTO UserGroup (groupName, description, createdBy)
SELECT 'legacy', 'Projects created before users and groups existed', id
FROM UserAccount WHERE userName = 'admin';
INSERT INTO Namespace (name, type, groupId)
SELECT groupName, 'g', id FROM UserGroup WHERE groupName = 'legacy';
UPDATE UserGroup
SET namespaceId = (SELECT id FROM Namespace WHERE name = 'legacy')
WHERE groupName = 'legacy';

-- Used by ReBenchDB's perf-tracker, for self-performance tracking
CREATE PROCEDURE recordAdditionalMeasurement(
aRunId int,
Expand Down
111 changes: 72 additions & 39 deletions src/backend/db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
FullPlotData,
TimelineSuite
} from '../../shared/api.js';
import { robustPath, TotalCriterion } from '../util.js';
import { legacyGroupName, robustPath, TotalCriterion } from '../util.js';
import { assert } from '../logging.js';
import { simplifyCmdline } from '../../shared/util.js';
import type { SummaryStatistics } from '../../shared/stats.js';
Expand Down Expand Up @@ -75,6 +75,11 @@ const measurementDataTableJoins = `
JOIN Criterion ON criterion = criterion.id
JOIN Run ON runId = run.id`;

const selectProjectWithNamespace = `
SELECT p.*, n.name AS namespacename
FROM Project p
JOIN Namespace n ON p.namespaceId = n.id`;

function filterCommitMessage(msg) {
return msg
.replaceAll('\\n', '\n') // resolve new lines
Expand All @@ -96,7 +101,6 @@ export abstract class Database {
protected readonly timelineUpdater: BatchingTimelineUpdater | null;

private readonly queries = {
fetchProjectByName: 'SELECT * FROM Project WHERE name = $1',
insertMeasurementBatchedN: 'GENERATED'
};

Expand Down Expand Up @@ -312,14 +316,20 @@ export abstract class Database {
return cache.get(cacheKey);
}

const result = await this.query(insertUnionFetchQ);
assert(
result.rowCount === 1,
'recordCached insertUnionFetchQ did not return exactly one row. ' +
'rowCount: ${result.rowCount}'
);
cache.set(cacheKey, result.rows[0]);
return result.rows[0];
try {
const result = await this.query(insertUnionFetchQ);
assert(
result.rowCount === 1,
'recordCached insertUnionFetchQ did not return exactly one row. ' +
'rowCount: ${result.rowCount}'
);
cache.set(cacheKey, result.rows[0]);
return result.rows[0];
} catch (e: any) {
e.rebenchdbQuery = insertUnionFetchQ;
console.log({ e });
throw e;
}
}

public async getBenchmarksByProjectId(projectId: number): Promise<
Expand Down Expand Up @@ -477,33 +487,48 @@ export abstract class Database {
});
}

public async recordProject(projectName: string): Promise<Project> {
return this.recordCached(this.projects, projectName, {
name: 'insertOrFetchProjectByName',
text: `WITH insertedRow AS (
INSERT INTO Project (name, slug)
VALUES ($1, regexp_replace($2, '[^0-9a-zA-Z-]', '-', 'g'))
public async recordProject(
namespaceName: string,
projectName: string
): Promise<Project> {
const cacheKey = `${namespaceName}/${projectName}`;
return this.recordCached(this.projects, cacheKey, {
name: 'insertOrFetchProjectByNamespaceAndName',
text: `WITH ns AS (
SELECT id, name FROM Namespace WHERE name = $3
), insertedRow AS (
INSERT INTO Project (name, slug, namespaceId)
SELECT $1,
regexp_replace($2, '[^0-9a-zA-Z-]', '-', 'g'),
ns.id
FROM ns
ON CONFLICT DO NOTHING
RETURNING *
), foundRow AS (
SELECT * FROM insertedRow
UNION ALL
SELECT p.*
FROM Project p
JOIN ns ON p.namespaceId = ns.id
WHERE p.name = $1
)
SELECT * FROM insertedRow
UNION ALL
SELECT * FROM Project WHERE name = $1;`,
values: [projectName, projectName]
SELECT foundRow.*, ns.name AS namespacename
FROM foundRow, ns;`,
values: [projectName, projectName, namespaceName]
});
}

public async getProjectBySlug(
projectNameSlug: string
public async getProjectBySlugs(
namespaceSlug: string,
projectSlug: string
): Promise<Project | undefined> {
const q = {
name: 'fetchProjectBySlugName',
text: `SELECT * FROM Project
WHERE lower($1) = lower(slug)`,
values: [projectNameSlug]
};

const result = await this.query(q);
const result = await this.query({
name: 'fetchProjectByNamespaceAndSlug',
text: `${selectProjectWithNamespace}
WHERE lower(n.name) = lower($1)
AND lower(p.slug) = lower($2)`,
values: [namespaceSlug, projectSlug]
});

if (result.rowCount !== 1) {
return undefined;
Expand All @@ -514,7 +539,7 @@ export abstract class Database {
public async getProjectByExpId(expId: number): Promise<Project | undefined> {
const q = {
name: 'fetchProjectByExpId',
text: `SELECT p.* FROM Project p
text: `${selectProjectWithNamespace}
JOIN Experiment e ON e.projectId = p.id
WHERE e.id = $1`,
values: [expId]
Expand All @@ -528,26 +553,33 @@ export abstract class Database {
return result.rows[0];
}

public async getProjectByName(projectName: string): Promise<Project | null> {
return <Promise<Project | null>>this.getCached(this.projects, projectName, {
name: 'fetchProjectByName',
text: this.queries.fetchProjectByName,
values: [projectName]
public async getProjectByName(
namespaceName: string,
projectName: string
): Promise<Project | null> {
const cacheKey = `${namespaceName}/${projectName}`;
return <Promise<Project | null>>this.getCached(this.projects, cacheKey, {
name: 'fetchProjectByNamespaceAndName',
text: `${selectProjectWithNamespace}
WHERE n.name = $1 AND p.name = $2`,
values: [namespaceName, projectName]
});
}

public async getAllProjects(): Promise<Project[]> {
const result = await this.query({
name: 'fetchAllProjects',
text: 'SELECT * FROM Project ORDER BY position DESC'
text: `${selectProjectWithNamespace}
ORDER BY p.position DESC`
});
return result.rows;
}

public async getProject(projectId: number): Promise<Project | undefined> {
const result = await this.query({
name: 'fetchProjectById',
text: 'SELECT * FROM Project WHERE id = $1',
text: `${selectProjectWithNamespace}
WHERE p.id = $1`,
values: [projectId]
});

Expand Down Expand Up @@ -651,7 +683,8 @@ export abstract class Database {
return <Experiment>this.exps.get(cacheKey);
}

const project = await this.recordProject(data.projectName);
// TODO: use the namespace sent by ReBench
const project = await this.recordProject(legacyGroupName, data.projectName);

return this.recordCached(this.exps, cacheKey, {
name: 'insertOrFetchExperimentByProjectIdAndName',
Expand Down
75 changes: 75 additions & 0 deletions src/backend/db/schema-updates/migration.021.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
-- Adding Support for Users, Groups, and Namespaces
CREATE TABLE UserAccount (
id serial primary key,
userName varchar unique, -- has to be a valid `slug`
email varchar unique,
passwordHash varchar,
apiToken char(64) NULL,
createdAt timestamp with time zone DEFAULT now(),
isActive bool NOT NULL DEFAULT true -- deactivated users cannot log in
);

CREATE TABLE UserGroup (
id serial primary key,
groupName varchar unique, -- has to be a valid `slug`
description text,
createdAt timestamp with time zone DEFAULT now(),
createdBy int REFERENCES UserAccount (id)
);

-- A Namespace is either a user account or user group, and ensures that
-- there are no name collisions between users and groups.
CREATE TABLE Namespace (
id serial primary key,
name varchar unique NOT NULL, -- has to be a valid `slug`
type char(1) CHECK (type IN ('u', 'g')),
accountId int NULL REFERENCES UserAccount (id),
groupId int NULL REFERENCES UserGroup (id),

CONSTRAINT eitherAccountOrGroup CHECK (
(accountId IS NOT NULL AND groupId IS NULL AND type = 'u') OR
(accountId IS NULL AND groupId IS NOT NULL AND type = 'g')
)
);

ALTER TABLE Project
ADD COLUMN namespaceId int REFERENCES Namespace (id);
ALTER TABLE UserAccount
ADD COLUMN namespaceId int REFERENCES Namespace (id);
ALTER TABLE UserGroup
ADD COLUMN namespaceId int REFERENCES Namespace (id);

CREATE TYPE projectRole AS ENUM ('view', 'edit', 'owner');

CREATE TABLE ProjectPermissions (
projectId int REFERENCES Project (id),
namespaceId int REFERENCES Namespace (id),
role projectRole NOT NULL,
PRIMARY KEY (projectId, namespaceId)
);

-- The admin account owns the legacy group.
-- Because Authentication is not yet implemented, the admin account has no password hash, so it cannot log in.
INSERT INTO UserAccount (userName) VALUES ('admin');
INSERT INTO Namespace (name, type, accountId)
SELECT userName, 'u', id FROM UserAccount WHERE userName = 'admin';
UPDATE UserAccount
SET namespaceId = (SELECT id FROM Namespace WHERE name = 'admin')
WHERE userName = 'admin';

-- Old projects that were created before users and groups existed belong to the legacy group.
INSERT INTO UserGroup (groupName, description, createdBy)
SELECT 'legacy', 'Projects created before users and groups existed', id
FROM UserAccount WHERE userName = 'admin';
INSERT INTO Namespace (name, type, groupId)
SELECT groupName, 'g', id FROM UserGroup WHERE groupName = 'legacy';
UPDATE UserGroup
SET namespaceId = (SELECT id FROM Namespace WHERE name = 'legacy')
WHERE groupName = 'legacy';


-- Update all existing projects to belong to the legacy group,
-- and make the namespaceId column non-nullable.
UPDATE Project
SET namespaceId = (SELECT id FROM Namespace WHERE name = 'legacy');
ALTER TABLE Project ALTER COLUMN namespaceId SET NOT NULL;
2 changes: 2 additions & 0 deletions src/backend/db/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export interface Project {
allresults: boolean;
githubnotification: boolean;
basebranch: string;
namespaceid: number;
namespacename: string;
}

export interface Source {
Expand Down
5 changes: 3 additions & 2 deletions src/backend/github/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createAppAuth } from '@octokit/auth-app';
import { existsSync, readFileSync } from 'fs';
import { log } from '../logging.js';
import { BenchmarkCompletion } from '../../shared/api.js';
import { dbConfig, siteConfig } from '../util.js';
import { dbConfig, legacyGroupName, siteConfig } from '../util.js';
import { getSummaryPlotFileName, renderCompare } from '../compare/report.js';

export async function handleReBenchCompletion(
Expand Down Expand Up @@ -50,7 +50,8 @@ async function reportCompletion(
): Promise<void> {
await db.reportCompletion(data);

const project = await db.getProjectByName(data.projectName);
// TODO: use the namespace sent by ReBench
const project = await db.getProjectByName(legacyGroupName, data.projectName);
if (!project) {
throw new Error(`No project with name ${data.projectName} found.`);
}
Expand Down
Loading