diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 835cf416..343f213a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/src/backend/db/db.sql b/src/backend/db/db.sql index 189b2012..5db133c9 100644 --- a/src/backend/db/db.sql +++ b/src/backend/db/db.sql @@ -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 @@ -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, diff --git a/src/backend/db/db.ts b/src/backend/db/db.ts index c95d31be..c8f31fbd 100644 --- a/src/backend/db/db.ts +++ b/src/backend/db/db.ts @@ -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'; @@ -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 @@ -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' }; @@ -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< @@ -477,33 +487,48 @@ export abstract class Database { }); } - public async recordProject(projectName: string): Promise { - 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 { + 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 { - 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; @@ -514,7 +539,7 @@ export abstract class Database { public async getProjectByExpId(expId: number): Promise { 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] @@ -528,18 +553,24 @@ export abstract class Database { return result.rows[0]; } - public async getProjectByName(projectName: string): Promise { - return >this.getCached(this.projects, projectName, { - name: 'fetchProjectByName', - text: this.queries.fetchProjectByName, - values: [projectName] + public async getProjectByName( + namespaceName: string, + projectName: string + ): Promise { + const cacheKey = `${namespaceName}/${projectName}`; + return >this.getCached(this.projects, cacheKey, { + name: 'fetchProjectByNamespaceAndName', + text: `${selectProjectWithNamespace} + WHERE n.name = $1 AND p.name = $2`, + values: [namespaceName, projectName] }); } public async getAllProjects(): Promise { 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; } @@ -547,7 +578,8 @@ export abstract class Database { public async getProject(projectId: number): Promise { const result = await this.query({ name: 'fetchProjectById', - text: 'SELECT * FROM Project WHERE id = $1', + text: `${selectProjectWithNamespace} + WHERE p.id = $1`, values: [projectId] }); @@ -651,7 +683,8 @@ export abstract class Database { return 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', diff --git a/src/backend/db/schema-updates/migration.021.sql b/src/backend/db/schema-updates/migration.021.sql new file mode 100644 index 00000000..9f766184 --- /dev/null +++ b/src/backend/db/schema-updates/migration.021.sql @@ -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; diff --git a/src/backend/db/types.ts b/src/backend/db/types.ts index eebaf4b7..fec63aca 100644 --- a/src/backend/db/types.ts +++ b/src/backend/db/types.ts @@ -72,6 +72,8 @@ export interface Project { allresults: boolean; githubnotification: boolean; basebranch: string; + namespaceid: number; + namespacename: string; } export interface Source { diff --git a/src/backend/github/github.ts b/src/backend/github/github.ts index 48bb7efd..37bfef76 100644 --- a/src/backend/github/github.ts +++ b/src/backend/github/github.ts @@ -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( @@ -50,7 +50,8 @@ async function reportCompletion( ): Promise { 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.`); } diff --git a/src/backend/main/index.html b/src/backend/main/index.html index 5e896141..a02fcb5d 100644 --- a/src/backend/main/index.html +++ b/src/backend/main/index.html @@ -31,13 +31,14 @@

Execute and document benchmarks reproducibly.

{% for (const p of it.projects) { %}
{%= p.name %}
+ href="/{%= p.namespacename %}/{%= p.slug %}/data">{%= p.name %}
{% if (p.showchanges) { %}
Changes
@@ -50,7 +51,7 @@
Changes
if (p.allresults) { %}
{% } %} - Timeline + Timeline
{% } diff --git a/src/backend/project/project-data.html b/src/backend/project/project-data.html index 4905e0f6..7ef8a35b 100644 --- a/src/backend/project/project-data.html +++ b/src/backend/project/project-data.html @@ -4,6 +4,7 @@ + ReBench: {%= it.project.name %} {%- include('header.html', { rebenchVersion: it.rebenchVersion }) %} diff --git a/src/backend/project/project.html b/src/backend/project/project.html index 59be1cb6..5b68d556 100644 --- a/src/backend/project/project.html +++ b/src/backend/project/project.html @@ -35,13 +35,14 @@

{%= it.description%}

{%= it.name %}
+ href="/{%= it.namespacename %}/{%= it.slug %}/data">{%= it.name %}
{% if (it.showchanges) { %}
Changes
@@ -55,7 +56,7 @@
Changes
{% } %}
- Timeline + Timeline
diff --git a/src/backend/project/project.ts b/src/backend/project/project.ts index fa30cb30..bee4dda4 100644 --- a/src/backend/project/project.ts +++ b/src/backend/project/project.ts @@ -10,16 +10,28 @@ import { } from '../perf-tracker.js'; import { getExpData } from './data-export.js'; import { Database } from '../db/db.js'; -import { rebenchVersion, robustPath } from '../../backend/util.js'; +import { + legacyGroupName, + rebenchVersion, + robustPath +} from '../../backend/util.js'; import { Source } from '../db/types.js'; +/** Supports URLs that predate namespaces. */ +export function redirectToLegacyNamespace(ctx: ParameterizedContext): void { + ctx.redirect(`/${legacyGroupName}${ctx.url}`); +} + const projectHtml = prepareTemplate(robustPath('backend/project/project.html')); export async function renderProjectPage( ctx: ParameterizedContext, db: Database ): Promise { - const project = await db.getProjectBySlug(ctx.params.projectSlug); + const project = await db.getProjectBySlugs( + ctx.params.namespaceSlug, + ctx.params.projectSlug + ); if (project) { ctx.body = projectHtml({ ...project, rebenchVersion }); ctx.type = 'html'; @@ -57,7 +69,10 @@ export async function renderProjectDataPage( ctx: ParameterizedContext, db: Database ): Promise { - const project = await db.getProjectBySlug(ctx.params.projectSlug); + const project = await db.getProjectBySlugs( + ctx.params.namespaceSlug, + ctx.params.projectSlug + ); if (project) { ctx.body = projectDataTpl({ project, rebenchVersion }); ctx.type = 'html'; diff --git a/src/backend/timeline/timeline.ts b/src/backend/timeline/timeline.ts index 631aa140..d4376e6f 100644 --- a/src/backend/timeline/timeline.ts +++ b/src/backend/timeline/timeline.ts @@ -41,7 +41,10 @@ export async function renderTimeline( ctx: ParameterizedContext, db: Database ): Promise { - const project = await db.getProjectBySlug(ctx.params.projectSlug); + const project = await db.getProjectBySlugs( + ctx.params.namespaceSlug, + ctx.params.projectSlug + ); if (project) { ctx.body = timelineTpl({ diff --git a/src/backend/util.ts b/src/backend/util.ts index d69bee1a..f63d80ba 100644 --- a/src/backend/util.ts +++ b/src/backend/util.ts @@ -28,10 +28,10 @@ const __dirname = getDirname(import.meta.url); * Get a robust path, relative to the source directory. */ export const robustPath = __dirname.includes('dist/') - ? function (path) { + ? function (path: string) { return resolve(`${__dirname}/../../../src/${path}`); } - : function (path) { + : function (path: string) { return resolve(`${__dirname}/../${path}`); }; @@ -39,10 +39,10 @@ export const robustPath = __dirname.includes('dist/') * Get a robust path in the compiled source directory. */ export const robustSrcPath = __dirname.includes('dist/') - ? function (path) { + ? function (path: string) { return `${__dirname}/../${path}`; } - : function (path) { + : function (path: string) { return `${__dirname}/../../dist/src/${path}`; }; @@ -67,6 +67,9 @@ const reportsUrl = process.env.REPORTS_URL || '/static/reports'; const staticUrl = process.env.STATIC_URL || '/static'; const publicUrl = process.env.PUBLIC_URL || _rebench_dev; +// Backwards Compatibility Support for users/groups +export const legacyGroupName = process.env.RDB_LEGACY_GROUP_NAME || 'legacy'; + // configuration for data export is a little more involved, // because the database might run elsewhere, but may produce // data files, which we need to be able to serve, at least in the dev mode. diff --git a/src/frontend/project-data.ts b/src/frontend/project-data.ts index 64699dc6..397202f2 100644 --- a/src/frontend/project-data.ts +++ b/src/frontend/project-data.ts @@ -3,6 +3,7 @@ import { renderProjectDataOverview } from './render.js'; const projectId = $('#project-id').attr('value'); const projectSlug = $('#project-slug').attr('value'); +const namespaceSlug = $('#namespace-slug').attr('value'); const dataOverviewP = apiFetch('/rebenchdb/dash/:projectId/data-overview', { projectId }); @@ -10,5 +11,5 @@ const dataOverviewP = apiFetch('/rebenchdb/dash/:projectId/data-overview', { $(async () => { const dataOverviewResponse = await dataOverviewP; const data = dataOverviewResponse.data; - renderProjectDataOverview(data, projectSlug); + renderProjectDataOverview(data, namespaceSlug, projectSlug); }); diff --git a/src/frontend/render.ts b/src/frontend/render.ts index c21a7c2a..93d4de6d 100644 --- a/src/frontend/render.ts +++ b/src/frontend/render.ts @@ -59,10 +59,11 @@ function formatCommitMessages(messages) { export function renderProjectDataOverview( data: any[], + namespaceSlug: string, projectSlug: string ): void { const tBody = $('#data-overview'); - const pSlug = projectSlug; + const dataUrl = `/${namespaceSlug}/${projectSlug}/data`; let hasDesc = false; @@ -89,8 +90,8 @@ export function renderProjectDataOverview( ${row.measurements} ${row.expid} - JSON, - CSV + JSON, + CSV `); tBody.find('a[href="#"]').on('click', expandMessage); @@ -137,6 +138,7 @@ function setHref(event, projectId: string, isBaseline: boolean) { // every time a commit is clicked, check to see if both left and right // commit are defined. set link if that is true const baseJQ = $(`#p${projectId}-baseline`); + const namespaceSlug = baseJQ.data('namespace-slug'); const projectSlug = baseJQ.data('project-slug'); const clicked = $(event.currentTarget).data('hash'); @@ -157,7 +159,7 @@ function setHref(event, projectId: string, isBaseline: boolean) { $(`#p${projectId}-compare`).attr( 'href', - `/${projectSlug}/compare/${baseline}..${change}` + `/${namespaceSlug}/${projectSlug}/compare/${baseline}..${change}` ); } diff --git a/src/index.ts b/src/index.ts index c86e9601..611376b7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,6 +21,7 @@ import { } from './backend/main/main.js'; import { getSourceAsJson, + redirectToLegacyNamespace, renderDataExport, renderProjectDataPage, renderProjectPage @@ -117,25 +118,15 @@ router.post('/runners', koaBody({ urlencoded: true }), async (ctx) => renderRunners(ctx, runnerCache, pipelinesCache) ); -router.get('/:projectSlug', async (ctx) => renderProjectPage(ctx, db)); -defineRoute('/:projectSlug/source/:sourceId', router, db, getSourceAsJson); -router.get('/:projectSlug/timeline', async (ctx) => renderTimeline(ctx, db)); -router.get('/:projectSlug/data', async (ctx) => renderProjectDataPage(ctx, db)); -router.get('/:projectSlug/data/:expIdAndExtension', async (ctx) => { - if ( - ctx.header['X-Purpose'] === 'preview' || - ctx.header['Purpose'] === 'prefetch' || - ctx.header['X-Moz'] === 'prefetch' - ) { - ctx.set('Cache-Control', 'must-revalidate'); - ctx.status = 425; // HTTP Code for 'Too Early' - return; - } - return renderDataExport(ctx, db); -}); -router.get('/:projectSlug/compare/:baseline..:change', async (ctx) => - renderComparePage(ctx, db) -); +if (DEV) { + router.get(`${siteConfig.staticUrl}/*filename`, serveStaticResource); + router.get(`/shared/*filename`, serveStaticSharedResource); + router.get(`/src/frontend/*filename`, serveViewJs); + router.get( + `${siteConfig.reportsUrl}/:change/figure-html/:filename`, + serveReport + ); +} // TODO: rename this to say that this endpoint gets the last 100 measurements // for the project @@ -189,16 +180,6 @@ router.post( deleteCachedReport ); -if (DEV) { - router.get(`${siteConfig.staticUrl}/*filename`, serveStaticResource); - router.get(`/shared/*filename`, serveStaticSharedResource); - router.get(`/src/frontend/*filename`, serveViewJs); - router.get( - `${siteConfig.reportsUrl}/:change/figure-html/:filename`, - serveReport - ); -} - // curl -X OPTIONS http://localhost:33333/rebenchdb/results -i router.options('/rebenchdb/results', reportResultApiVersion); @@ -217,6 +198,51 @@ router.put('/rebenchdb/completion', koaBody(), async (ctx) => handleReBenchCompletion(ctx, db) ); +// all the generic routes with variables need to come after +// the fixed special-purpose routes. Otherwise, the generic +// routes will match first. + +// These routes are for legacy URLs that predate namespaces. +router.get('/:projectSlug', redirectToLegacyNamespace); +router.get('/:projectSlug/timeline', redirectToLegacyNamespace); +router.get('/:projectSlug/data', redirectToLegacyNamespace); +router.get('/:projectSlug/data/:expIdAndExtension', redirectToLegacyNamespace); +router.get( + '/:projectSlug/compare/:baseline..:change', + redirectToLegacyNamespace +); + +defineRoute('/:projectSlug/source/:sourceId', router, db, getSourceAsJson); + +router.get('/:namespaceSlug/:projectSlug', async (ctx) => + renderProjectPage(ctx, db) +); +router.get('/:namespaceSlug/:projectSlug/timeline', async (ctx) => + renderTimeline(ctx, db) +); +router.get('/:namespaceSlug/:projectSlug/data', async (ctx) => + renderProjectDataPage(ctx, db) +); +router.get( + '/:namespaceSlug/:projectSlug/data/:expIdAndExtension', + async (ctx) => { + if ( + ctx.header['X-Purpose'] === 'preview' || + ctx.header['Purpose'] === 'prefetch' || + ctx.header['X-Moz'] === 'prefetch' + ) { + ctx.set('Cache-Control', 'must-revalidate'); + ctx.status = 425; // HTTP Code 425 Too Early + return; + } + return renderDataExport(ctx, db); + } +); +router.get( + '/:namespaceSlug/:projectSlug/compare/:baseline..:change', + async (ctx) => renderComparePage(ctx, db) +); + app.use(router.routes()); app.use(router.allowedMethods()); diff --git a/src/shared/api.ts b/src/shared/api.ts index 3bf2ba52..8e655aaf 100644 --- a/src/shared/api.ts +++ b/src/shared/api.ts @@ -118,7 +118,7 @@ export interface Environment { /** * Settings reported by `rebench-denoise`. */ - denoise?: Record; + denoise?: Record | null; } export interface BenchmarkData { diff --git a/tests/backend/project/project.test.ts b/tests/backend/project/project.test.ts index ccab8671..876ec620 100644 --- a/tests/backend/project/project.test.ts +++ b/tests/backend/project/project.test.ts @@ -17,7 +17,9 @@ describe('renderProjectDataPage', () => { showchanges: true, allresults: true, githubnotification: false, - basebranch: 'base' + basebranch: 'base', + namespaceid: 1, + namespacename: 'legacy' }; const tpl = prepareTemplate( diff --git a/tests/data/expected-results/main/index.html b/tests/data/expected-results/main/index.html index fb97bf84..54ddf450 100644 --- a/tests/data/expected-results/main/index.html +++ b/tests/data/expected-results/main/index.html @@ -46,13 +46,14 @@

Execute and document benchmarks reproducibly.

Small Example Project
+href="/legacy/Small-Example-Project/data">Small Example Project
Changes
@@ -61,7 +62,7 @@
Changes
Compare -Timeline +Timeline
diff --git a/tests/data/expected-results/project/project-data.html b/tests/data/expected-results/project/project-data.html index df2ca261..5fca5945 100644 --- a/tests/data/expected-results/project/project-data.html +++ b/tests/data/expected-results/project/project-data.html @@ -4,6 +4,7 @@ + ReBench: Test Project