-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstatsManager.js
More file actions
108 lines (96 loc) · 3.12 KB
/
statsManager.js
File metadata and controls
108 lines (96 loc) · 3.12 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
const fs = require('fs').promises;
const path = require('path');
class StatsManager {
constructor(config = {}, fileSystem = fs) {
this.config = {
statsFile: path.join(__dirname, 'counting_stats.json'),
...config
};
this.fs = fileSystem;
this.stats = {
highestCount: 1,
highestCountTimestamp: null,
totalSuccessfulCounts: 0,
currentCount: 1,
milestones: {},
userStats: {},
mostComplicatedOperation: {
expression: '',
user: null,
complexity: 0
}
};
this.currentCount = this.stats.currentCount;
this.lastUser = null;
// Remove this.currentCount
}
async loadStats() {
try {
const data = await this.fs.readFile(this.config.statsFile, 'utf8');
const loadedStats = JSON.parse(data);
this.stats = {
...loadedStats,
mostComplicatedOperation: loadedStats.mostComplicatedOperation || {
expression: '',
user: null,
complexity: 0
},
userStats: Object.fromEntries(
Object.entries(loadedStats.userStats || {}).map(([userId, userStat]) => [
userId,
{
...userStat,
totalComplexity: userStat.totalComplexity || 0,
countWithComplexity: userStat.countWithComplexity || 0
}
])
)
};
this.currentCount = loadedStats.currentCount || 1;
this.lastUser = loadedStats.lastUser || null;
console.log('Stats loaded successfully');
} catch (error) {
if (error.code === 'ENOENT') {
console.log('No existing stats file found. Starting with default stats.');
} else {
console.error('Error loading stats:', error);
}
}
}
async saveStats() {
try {
const dataToSave = {
...this.stats,
currentCount: this.currentCount,
lastUser: this.lastUser
};
await this.fs.writeFile(this.config.statsFile, JSON.stringify(dataToSave, null, 2));
} catch (error) {
console.error('Error saving stats:', error);
}
}
getStats() {
return this.stats;
}
updateStats(newStats) {
this.stats = {...this.stats, ...newStats};
}
getCurrentCount() {
return this.stats.currentCount;
}
setCurrentCount(count) {
this.currentCount = count;
this.stats.currentCount = count;
}
getLastUser() {
return this.lastUser;
}
setLastUser(user) {
this.lastUser = user;
}
}
// Create and export a singleton instance
const statsManager = new StatsManager();
module.exports = statsManager;
// Also export the class for testing purposes
module.exports.StatsManager = StatsManager;