Skip to content

Commit 3f150db

Browse files
committed
refactor: remove redundant code
1 parent c972833 commit 3f150db

File tree

5 files changed

+40
-81
lines changed

5 files changed

+40
-81
lines changed

packages/utils/src/lib/process-id.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ export const ID_PATTERNS = Object.freeze({
4747
* Used by: getUniqueInstanceId()
4848
*/
4949
INSTANCE_ID: new RegExp(`^${TIME_ID_BASE.source}\\.\\d+\\.\\d+\\.\\d+$`),
50-
/** @deprecated Use INSTANCE_ID instead */
51-
SHARD_ID: new RegExp(`^${TIME_ID_BASE.source}\\.\\d+\\.\\d+\\.\\d+$`),
52-
/** @deprecated Use TIME_ID instead */
53-
READABLE_DATE: new RegExp(`^${TIME_ID_BASE.source}$`),
5450
} as const);
5551

5652
/**

packages/utils/src/lib/process-id.unit.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,16 @@ describe('ID_PATTERNS', () => {
7777
);
7878

7979
it.each(['20231114-221320-000.12345.1.1'])(
80-
'SHARD_ID should match valid shard ID (deprecated alias): %s',
81-
shardId => {
82-
expect(shardId).toMatch(ID_PATTERNS.SHARD_ID);
80+
'INSTANCE_ID should match valid instance ID: %s',
81+
instanceId => {
82+
expect(instanceId).toMatch(ID_PATTERNS.INSTANCE_ID);
8383
},
8484
);
8585

8686
it.each(['20231114-221320-000'])(
87-
'READABLE_DATE should match valid readable date (deprecated alias): %s',
88-
readableDate => {
89-
expect(readableDate).toMatch(ID_PATTERNS.READABLE_DATE);
87+
'TIME_ID should match valid time ID: %s',
88+
timeId => {
89+
expect(timeId).toMatch(ID_PATTERNS.TIME_ID);
9090
},
9191
);
9292
});

packages/utils/src/lib/profiler/profiler-node.int.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
omitTraceJson,
66
} from '@code-pushup/test-utils';
77
import type { PerformanceEntryEncoder } from '../performance-observer.js';
8-
import { WAL_ID_PATTERNS } from '../wal.js';
8+
import { ID_PATTERNS } from '../process-id.js';
99
import { NodejsProfiler } from './profiler-node.js';
1010
import { entryToTraceEvents } from './trace-file-utils.js';
1111
import type { TraceEvent } from './trace-file.type.js';
@@ -261,7 +261,7 @@ describe('NodeJS Profiler Integration', () => {
261261
const groupIdDir = pathParts.at(-2);
262262
const fileName = pathParts.at(-1);
263263

264-
expect(groupIdDir).toMatch(WAL_ID_PATTERNS.GROUP_ID);
264+
expect(groupIdDir).toMatch(ID_PATTERNS.TIME_ID);
265265
expect(fileName).toMatch(/^trace\.\d{8}-\d{6}-\d{3}(?:\.\d+){3}\.jsonl$/);
266266

267267
const groupIdDirPath = path.dirname(filePath);
@@ -283,7 +283,7 @@ describe('NodeJS Profiler Integration', () => {
283283
const dirPath = path.dirname(filePath);
284284
const groupId = path.basename(dirPath);
285285

286-
expect(groupId).toMatch(WAL_ID_PATTERNS.GROUP_ID);
286+
expect(groupId).toMatch(ID_PATTERNS.TIME_ID);
287287
// eslint-disable-next-line n/no-sync
288288
expect(fs.existsSync(dirPath)).toBeTrue();
289289
// eslint-disable-next-line n/no-sync

packages/utils/src/lib/wal.ts

Lines changed: 15 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import * as fs from 'node:fs';
33
import path from 'node:path';
44
import process from 'node:process';
55
import { threadId } from 'node:worker_threads';
6+
import {
7+
type Counter,
8+
getUniqueInstanceId,
9+
getUniqueTimeId,
10+
} from './process-id.js';
611

712
/**
813
* Codec for encoding/decoding values to/from strings for WAL storage.
@@ -375,6 +380,13 @@ export function setCoordinatorProcess(
375380
// eslint-disable-next-line functional/no-let
376381
let shardCount = 0;
377382

383+
/**
384+
* Simple counter implementation for generating sequential IDs.
385+
*/
386+
const shardCounter: Counter = {
387+
next: () => ++shardCount,
388+
};
389+
378390
/**
379391
* Generates a unique sharded WAL ID based on performance time origin, process ID, thread ID, and instance count.
380392
*/
@@ -383,60 +395,6 @@ function getShardedWalId() {
383395
return `${Math.round(performance.timeOrigin)}.${process.pid}.${threadId}.${++ShardedWal.instanceCount}`;
384396
}
385397

386-
/**
387-
* Generates a human-readable shard ID.
388-
* This ID is unique per process/thread/shard combination and used in the file name.
389-
* Format: readable-timestamp.pid.threadId.shardCount
390-
* Example: "20240101-120000-000.12345.1.1"
391-
* Becomes file: trace.20240101-120000-000.12345.1.1.log
392-
*/
393-
export function getShardId(): string {
394-
const timestamp = Math.round(performance.timeOrigin + performance.now());
395-
const readableTimestamp = sortableReadableDateString(`${timestamp}`);
396-
return `${readableTimestamp}.${process.pid}.${threadId}.${++shardCount}`;
397-
}
398-
399-
/**
400-
* Generates a human-readable sharded group ID.
401-
* This ID is a globally unique, sortable, human-readable date string per run.
402-
* Used directly as the folder name to group shards.
403-
* Format: yyyymmdd-hhmmss-ms
404-
* Example: "20240101-120000-000"
405-
*/
406-
export function getShardedGroupId(): string {
407-
return sortableReadableDateString(
408-
`${Math.round(performance.timeOrigin + performance.now())}`,
409-
);
410-
}
411-
412-
/**
413-
* Regex patterns for validating WAL ID formats
414-
*/
415-
export const WAL_ID_PATTERNS = {
416-
/** Readable date format: yyyymmdd-hhmmss-ms */
417-
READABLE_DATE: /^\d{8}-\d{6}-\d{3}$/,
418-
/** Group ID format: yyyymmdd-hhmmss-ms */
419-
GROUP_ID: /^\d{8}-\d{6}-\d{3}$/,
420-
/** Shard ID format: readable-date.pid.threadId.count */
421-
SHARD_ID: /^\d{8}-\d{6}-\d{3}(?:\.\d+){3}$/,
422-
} as const;
423-
424-
export function sortableReadableDateString(timestampMs: string): string {
425-
const timestamp = Number.parseInt(timestampMs, 10);
426-
const date = new Date(timestamp);
427-
const MILLISECONDS_PER_SECOND = 1000;
428-
const yyyy = date.getFullYear();
429-
const mm = String(date.getMonth() + 1).padStart(2, '0');
430-
const dd = String(date.getDate()).padStart(2, '0');
431-
const hh = String(date.getHours()).padStart(2, '0');
432-
const min = String(date.getMinutes()).padStart(2, '0');
433-
const ss = String(date.getSeconds()).padStart(2, '0');
434-
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
435-
const ms = String(timestamp % MILLISECONDS_PER_SECOND).padStart(3, '0');
436-
437-
return `${yyyy}${mm}${dd}-${hh}${min}${ss}-${ms}`;
438-
}
439-
440398
/**
441399
* Ensures a directory exists, creating it recursively if necessary using sync methods.
442400
* @param dirPath - The directory path to ensure exists
@@ -491,7 +449,7 @@ export function getShardedFinalPath<T extends object | string = object>(opt: {
491449
export class ShardedWal<T extends object | string = object> {
492450
static instanceCount = 0;
493451
readonly #id: string = getShardedWalId();
494-
readonly groupId = getShardedGroupId();
452+
readonly groupId = getUniqueTimeId();
495453
readonly #format: WalFormat<T>;
496454
readonly #dir: string = process.cwd();
497455
readonly #isCoordinator: boolean;
@@ -511,7 +469,7 @@ export class ShardedWal<T extends object | string = object> {
511469
coordinatorIdEnvVar: string;
512470
}) {
513471
const { dir, format, groupId, coordinatorIdEnvVar } = opt;
514-
this.groupId = groupId ?? getShardedGroupId();
472+
this.groupId = groupId ?? getUniqueTimeId();
515473
if (dir) {
516474
this.#dir = dir;
517475
}
@@ -531,7 +489,7 @@ export class ShardedWal<T extends object | string = object> {
531489
return this.#isCoordinator;
532490
}
533491

534-
shard(shardId: string = getShardId()) {
492+
shard(shardId: string = getUniqueInstanceId(shardCounter)) {
535493
return new WriteAheadLogFile({
536494
file: getShardedPath({
537495
dir: this.#dir,

packages/utils/src/lib/wal.unit.test.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import { vol } from 'memfs';
22
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
3+
import {
4+
ID_PATTERNS,
5+
getUniqueInstanceId,
6+
getUniqueTimeId,
7+
} from './process-id.js';
38
import { SHARDED_WAL_COORDINATOR_ID_ENV_VAR } from './profiler/constants.js';
49
import {
510
type Codec,
611
type InvalidEntry,
712
ShardedWal,
8-
WAL_ID_PATTERNS,
913
WriteAheadLogFile,
1014
createTolerantCodec,
1115
filterValidRecords,
12-
getShardId,
13-
getShardedGroupId,
1416
isCoordinatorProcess,
1517
parseWalFormat,
1618
recoverFromContent,
@@ -506,15 +508,18 @@ describe('stringCodec', () => {
506508

507509
describe('getShardId', () => {
508510
it('should generate shard ID with readable timestamp', () => {
509-
const result = getShardId();
511+
const counter = { next: () => 1 };
512+
const result = getUniqueInstanceId(counter);
510513

511-
expect(result).toMatch(WAL_ID_PATTERNS.SHARD_ID);
514+
expect(result).toMatch(ID_PATTERNS.INSTANCE_ID);
512515
expect(result).toStartWith('20231114-221320-000.');
513516
});
514517

515518
it('should generate different shard IDs for different calls', () => {
516-
const result1 = getShardId();
517-
const result2 = getShardId();
519+
let count = 0;
520+
const counter = { next: () => ++count };
521+
const result1 = getUniqueInstanceId(counter);
522+
const result2 = getUniqueInstanceId(counter);
518523

519524
expect(result1).not.toBe(result2);
520525
expect(result1).toStartWith('20231114-221320-000.');
@@ -555,15 +560,15 @@ describe('getShardId', () => {
555560

556561
describe('getShardedGroupId', () => {
557562
it('should work with mocked timeOrigin', () => {
558-
const result = getShardedGroupId();
563+
const result = getUniqueTimeId();
559564

560565
expect(result).toBe('20231114-221320-000');
561-
expect(result).toMatch(WAL_ID_PATTERNS.GROUP_ID);
566+
expect(result).toMatch(ID_PATTERNS.TIME_ID);
562567
});
563568

564569
it('should be idempotent within same process', () => {
565-
const result1 = getShardedGroupId();
566-
const result2 = getShardedGroupId();
570+
const result1 = getUniqueTimeId();
571+
const result2 = getUniqueTimeId();
567572

568573
expect(result1).toBe(result2);
569574
});

0 commit comments

Comments
 (0)