-
Notifications
You must be signed in to change notification settings - Fork 21
feat(scanner): integrate buit-in stats in the response of depWalker #612
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5b6dc92
feat(scanner): integrate built-in stats in the response of depWalker
clemgbld 00c3008
Update workspaces/scanner/src/class/DateProvider.class.ts
clemgbld e869fa1
Update workspaces/scanner/src/class/StatsCollector.class.ts
clemgbld 0d74e79
Update workspaces/scanner/src/class/StatsCollector.class.ts
clemgbld File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@nodesecure/scanner": minor | ||
| --- | ||
|
|
||
| feat(scanner): integrate built-in stats in the response of depWalker |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| export interface DateProvider { | ||
| now(): number; | ||
| oneYearAgo(): Date; | ||
| } | ||
|
|
||
| export class SystemDateProvider implements DateProvider { | ||
| now(): number { | ||
| return Date.now(); | ||
| } | ||
|
|
||
| oneYearAgo(): Date { | ||
| const date = new Date(); | ||
| date.setFullYear(date.getFullYear() - 1); | ||
|
|
||
| return date; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // Import Internal Dependencies | ||
| import { SystemDateProvider, type DateProvider } from "./DateProvider.class.ts"; | ||
| import type { ApiStats, Stats } from "../types.ts"; | ||
|
|
||
| export class StatsCollector { | ||
| #apiCalls: ApiStats[] = []; | ||
| #dateProvider: DateProvider; | ||
| #startedAt: number; | ||
|
|
||
| constructor(dateProvider: DateProvider = new SystemDateProvider()) { | ||
clemgbld marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.#dateProvider = dateProvider; | ||
| this.#startedAt = this.#dateProvider.now(); | ||
| } | ||
|
|
||
| track<T extends () => any>(name: string, fn: T): ReturnType<T> { | ||
| const startedAt = this.#dateProvider.now(); | ||
| try { | ||
| const result = fn(); | ||
| if (result instanceof Promise) { | ||
| return result.finally(() => this.#addApiStat(name, startedAt) | ||
| ) as ReturnType<T>; | ||
| } | ||
|
|
||
| this.#addApiStat(name, startedAt); | ||
|
|
||
| return result; | ||
| } | ||
| catch (err) { | ||
| this.#addApiStat(name, startedAt); | ||
| throw err; | ||
| } | ||
| } | ||
|
|
||
| #addApiStat(name: string, startedAt: number) { | ||
| this.#apiCalls.push({ | ||
| name, | ||
| startedAt, | ||
| executionTime: this.#dateProvider.now() - startedAt | ||
| }); | ||
| } | ||
|
|
||
| getStats(): Stats { | ||
| return { | ||
| startedAt: this.#startedAt, | ||
| executionTime: this.#dateProvider.now() - this.#startedAt, | ||
| apiCalls: this.#apiCalls, | ||
| apiCallsCount: this.#apiCalls.length | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // Import Node.js Dependencies | ||
| import { describe, it } from "node:test"; | ||
| import assert from "node:assert"; | ||
|
|
||
| // Import Internal Dependencies | ||
| import type { DateProvider } from "../src/class/DateProvider.class.ts"; | ||
| import { StatsCollector } from "../src/class/StatsCollector.class.ts"; | ||
|
|
||
| describe("StatsCollectors", () => { | ||
| it("should get the expected global start and execution time", () => { | ||
| const dateProvider = new FakeDateProvider(); | ||
| dateProvider.setNow(1658512000000); | ||
| const statsCollector = new StatsCollector(dateProvider); | ||
| dateProvider.setNow(1658512001000); | ||
| const { startedAt, executionTime } = statsCollector.getStats(); | ||
| assert.strictEqual(startedAt, 1658512000000); | ||
| assert.strictEqual(executionTime, 1000); | ||
| }); | ||
|
|
||
| it("should still record the exexution time if the function being tracked throws", () => { | ||
| const dateProvider = new FakeDateProvider(); | ||
| dateProvider.setNow(1658512000000); | ||
| const statsCollector = new StatsCollector(dateProvider); | ||
| assert.throws(() => { | ||
| statsCollector.track("api/test/1", () => { | ||
| dateProvider.setNow(1658512001000); | ||
| throw new Error("oh no!"); | ||
| }); | ||
| }); | ||
|
|
||
| const { apiCalls, apiCallsCount } = statsCollector.getStats(); | ||
| assert.strictEqual(apiCallsCount, 1); | ||
| assert.deepEqual(apiCalls, [ | ||
| { | ||
| name: "api/test/1", | ||
| startedAt: 1658512000000, | ||
| executionTime: 1000 | ||
| } | ||
|
|
||
| ]); | ||
| }); | ||
|
|
||
| it("should be able to track the start and execution time of external api call", async() => { | ||
| let hasFnOneBeenCalled = false; | ||
| let hasFnTwoBeenCalled = false; | ||
| const dateProvider = new FakeDateProvider(); | ||
| dateProvider.setNow(1658512000000); | ||
| const statsCollector = new StatsCollector(dateProvider); | ||
| dateProvider.setNow(1658512001001); | ||
| const promise = statsCollector.track("api/test/1", () => { | ||
| hasFnOneBeenCalled = true; | ||
|
|
||
| return Promise.resolve(1); | ||
| }); | ||
|
|
||
| dateProvider.setNow(1658512002000); | ||
| const promiseResult = await promise; | ||
|
|
||
| dateProvider.setNow(1658512003000); | ||
| const fnResult = statsCollector.track("api/test/2", () => { | ||
| hasFnTwoBeenCalled = true; | ||
| dateProvider.setNow(1658512004000); | ||
|
|
||
| return null; | ||
| }); | ||
| dateProvider.setNow(1658512005000); | ||
| const { apiCalls, apiCallsCount } = statsCollector.getStats(); | ||
| assert.strictEqual(promiseResult, 1); | ||
| assert.strictEqual(fnResult, null); | ||
| assert.strictEqual(hasFnOneBeenCalled, true); | ||
| assert.strictEqual(hasFnTwoBeenCalled, true); | ||
| assert.strictEqual(apiCallsCount, 2); | ||
| assert.deepEqual(apiCalls, [ | ||
| { | ||
| name: "api/test/1", | ||
| startedAt: 1658512001001, | ||
| executionTime: 999 | ||
| }, | ||
| { | ||
| name: "api/test/2", | ||
| startedAt: 1658512003000, | ||
| executionTime: 1000 | ||
| } | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| class FakeDateProvider implements DateProvider { | ||
| #now: number; | ||
| now(): number { | ||
| return this.#now; | ||
| } | ||
| oneYearAgo(): Date { | ||
| return new Date(Date.now() - (365 * 24 * 60 * 60 * 1000)); | ||
| } | ||
|
|
||
| setNow(now: number) { | ||
| this.#now = now; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.