forked from night-slayer18/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.ts
More file actions
131 lines (111 loc) · 3.08 KB
/
timer.ts
File metadata and controls
131 lines (111 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Timer storage for tracking solve times - workspace-aware
import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs';
import { dirname } from 'path';
import { workspaceStorage } from './workspaces.js';
interface SolveTimeEntry {
problemId: string;
title: string;
difficulty: string;
solvedAt: string;
durationSeconds: number;
timerMinutes: number;
}
interface TimerSchema {
solveTimes: Record<string, SolveTimeEntry[]>;
activeTimer: {
problemId: string;
title: string;
difficulty: string;
startedAt: string;
durationMinutes: number;
} | null;
}
function getTimerPath(): string {
return workspaceStorage.getTimerPath();
}
function loadTimer(): TimerSchema {
const path = getTimerPath();
if (existsSync(path)) {
return JSON.parse(readFileSync(path, 'utf-8'));
}
return { solveTimes: {}, activeTimer: null };
}
function saveTimer(data: TimerSchema): void {
const timerPath = getTimerPath();
const dir = dirname(timerPath);
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
writeFileSync(timerPath, JSON.stringify(data, null, 2) + '\n');
}
export const timerStorage = {
startTimer(problemId: string, title: string, difficulty: string, durationMinutes: number): void {
const data = loadTimer();
data.activeTimer = {
problemId,
title,
difficulty,
startedAt: new Date().toISOString(),
durationMinutes,
};
saveTimer(data);
},
getActiveTimer() {
return loadTimer().activeTimer;
},
stopTimer(): { durationSeconds: number } | null {
const data = loadTimer();
const active = data.activeTimer;
if (!active) return null;
const startedAt = new Date(active.startedAt);
const now = new Date();
const durationSeconds = Math.floor((now.getTime() - startedAt.getTime()) / 1000);
data.activeTimer = null;
saveTimer(data);
return { durationSeconds };
},
recordSolveTime(
problemId: string,
title: string,
difficulty: string,
durationSeconds: number,
timerMinutes: number
): void {
const data = loadTimer();
if (!data.solveTimes[problemId]) {
data.solveTimes[problemId] = [];
}
data.solveTimes[problemId].push({
problemId,
title,
difficulty,
solvedAt: new Date().toISOString(),
durationSeconds,
timerMinutes,
});
saveTimer(data);
},
getSolveTimes(problemId: string): SolveTimeEntry[] {
const data = loadTimer();
return data.solveTimes[problemId] ?? [];
},
getAllSolveTimes(): Record<string, SolveTimeEntry[]> {
return loadTimer().solveTimes ?? {};
},
getStats(): { totalProblems: number; totalTime: number; avgTime: number } {
const solveTimes = loadTimer().solveTimes ?? {};
let totalProblems = 0;
let totalTime = 0;
for (const times of Object.values(solveTimes)) {
totalProblems += times.length;
for (const t of times) {
totalTime += t.durationSeconds;
}
}
return {
totalProblems,
totalTime,
avgTime: totalProblems > 0 ? Math.floor(totalTime / totalProblems) : 0,
};
},
};