Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report duraction when storing artifacts in remote-nx #18

Merged
merged 5 commits into from
Feb 5, 2024
Merged
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
7 changes: 7 additions & 0 deletions .changeset/warm-hats-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@vercel/remote-nx': major
---

Report duraction when storing artifacts in remote-nx. This allows "time saved" to be reported in the Vercel dashboard.

Requires NX v17 or higher.
5 changes: 3 additions & 2 deletions packages/remote-nx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@
"author": "Vercel",
"devDependencies": {
"@babel/core": "^7.22.5",
"@nx/workspace": "^16.3.2",
"@nx/workspace": "^17.2.8",
"@types/yargs": "^17.0.24",
"nx": "^17.2.8",
"tsconfig": "workspace:*",
"typescript": "5.1.3",
"vitest": "^0.32.0"
},
"dependencies": {
"@vercel/remote": "workspace:*",
"chalk": "^4.1.0",
"nx-remotecache-custom": "^4.0.0"
"nx-remotecache-custom": "^17.1.1"
}
}
21 changes: 19 additions & 2 deletions packages/remote-nx/src/setup-sdk.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
import type { Task } from 'nx/src/config/task-graph';
import { initEnv } from 'nx-remotecache-custom';
import { getVercelRemoteCacheClient } from './remote-client';
import type { Readable } from 'stream';
import type { VercelRemoteCacheOptions } from './remote-client';

// eslint-disable-next-line @typescript-eslint/require-await
const setupSDK = async (options: VercelRemoteCacheOptions) => {
const setupSDK = async (options: VercelRemoteCacheOptions, tasks: Task[]) => {
initEnv(options);
const remote = getVercelRemoteCacheClient(options);

return {
name: 'Vercel Remote Cache',
fileExists: (filename: string) => remote.exists(filename).send(),
retrieveFile: (filename: string) => remote.get(filename).stream(),
storeFile: (filename: string, stream: Readable) =>
remote.put(filename).stream(stream),
remote
.put(filename, {
duration: totalDuration(tasks),
})
.stream(stream),
};
};

function totalDuration(tasks: Pick<Task, 'endTime' | 'startTime'>[]) {
let total = 0;
for (const task of tasks) {
if (task.startTime && task.endTime) {
total += task.endTime - task.startTime;
}
}

return total;
}

export { setupSDK };
8 changes: 4 additions & 4 deletions packages/remote-nx/test/setup-sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('sdk', () => {
lifeCycle: {},
};

const client = await setupSDK(options);
const client = await setupSDK(options, []);
expect(client).toBeDefined();
});

Expand All @@ -27,7 +27,7 @@ describe('sdk', () => {
lifeCycle: {},
};

const client = await setupSDK(options);
const client = await setupSDK(options, []);
expect(client.fileExists).toBeInstanceOf(Function);
expect(client.retrieveFile).toBeInstanceOf(Function);
expect(client.storeFile).toBeInstanceOf(Function);
Expand All @@ -40,7 +40,7 @@ describe('sdk', () => {
lifeCycle: {},
};

await expect(setupSDK(options)).rejects.toThrowError(
await expect(setupSDK(options, [])).rejects.toThrowError(
'Missing a Vercel access token',
);
});
Expand All @@ -52,6 +52,6 @@ describe('sdk', () => {
};
// eslint-disable-next-line turbo/no-undeclared-env-vars
process.env.NX_VERCEL_REMOTE_CACHE_TOKEN = 'token';
await expect(setupSDK(options)).resolves.toBeDefined();
await expect(setupSDK(options, [])).resolves.toBeDefined();
});
});
2 changes: 1 addition & 1 deletion packages/remote/src/remote-cache-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface RemoteClientOptions {
export interface RemoteClient {
exists: (hash: string) => ArtifactExistsRequest;
get: (hash: string) => ArtifactGetRequest;
put: (hash: string) => ArtifactPutRequest;
put: (hash: string, options?: ArtifactOptions) => ArtifactPutRequest;
}

export class RemoteClientImpl implements RemoteClient {
Expand Down
Loading
Loading