-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.server.js
279 lines (226 loc) · 7.08 KB
/
main.server.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import fs from 'fs';
const tags = JSON.parse(fs.readFileSync(config['dataFolder'] + 'tags.json'));
const history = JSON.parse(fs.readFileSync(config['dataFolder'] + 'history.json'));
function showData(name, dataset) {
console.log(
name.padEnd(20, ' ') + '| Data size: ' +
Object.values(dataset).length + ' / '+
Object.values(dataset).map((elt) => Object.values(elt)).flat(1).length + ' / '+
Object.values(dataset).map((elt) => Object.values(elt)).flat(2).length
);
}
export default class FocusStats {
static init() {
(async () => {
await FocusStats.cleanDatabaseData();
FocusStats.cleanFilesData();
await FocusStats.update();
clearInterval(FocusStats.interval);
FocusStats.interval = setInterval(FocusStats.update, 60 * 60 * 1000); // Update every hour
})();
}
static close() {
clearInterval(FocusStats.interval);
}
static async cleanDatabaseData() {
log('Cleaning database data ...')
for(const oldVal in config['cleaner']['substitutions']) {
const newVal = config['cleaner']['substitutions'][oldVal];
log(` Database - Substitutions "${oldVal}" => "${newVal}"`);
// Rename in history
await Database.execQuery(`
UPDATE focus_stats
SET name = REPLACE(name, $1, $2), exe = REPLACE(exe, $1, $2)
WHERE exe LIKE '%' || $1 || '%'
OR name LIKE '%' || $1 || '%'
`, [oldVal, newVal]);
// Remove tagging, it'll be re-push if not duplicate with right values
await Database.execQuery(`
DELETE FROM focus_stats_tags
WHERE exe LIKE '%' || $1 || '%'
OR name LIKE '%' || $1 || '%'
`, [oldVal]);
}
for(const value of config['cleaner']['keepEndOnly']) {
log(` Database - Keep end only "${value}"`);
// Rename in history
await Database.execQuery(`
UPDATE focus_stats SET exe = $1
WHERE exe LIKE '%' || $1
`, [value]);
await Database.execQuery(`
UPDATE focus_stats SET name = $1
WHERE name LIKE '%' || $1
`, [value]);
// Remove tagging, it'll be re-push if not duplicate with right values
await Database.execQuery(`
DELETE FROM focus_stats_tags
WHERE exe LIKE '%' || $1
OR name LIKE '%' || $1
`, [value]);
}
for(const value of config['cleaner']['keepStartOnly']) {
log(` Database - Keep start only "${value}"`);
// Rename in history
await Database.execQuery(`
UPDATE focus_stats SET exe = $1
WHERE exe LIKE $1 || '%'
`, [value]);
await Database.execQuery(`
UPDATE focus_stats SET name = $1
WHERE name LIKE $1 || '%'
`, [value]);
// Remove tagging, it'll be re-push if not duplicate with right values
await Database.execQuery(`
DELETE FROM focus_stats_tags
WHERE exe LIKE $1 || '%'
OR name LIKE $1 || '%'
`, [value]);
}
log('Cleaned database data !')
}
static cleanString(str) {
for(const value of config['cleaner']['keepEndOnly']) {
if(str.endsWith(value)) {
return value;
}
}
for(const value of config['cleaner']['keepStartOnly']) {
if(str.startsWith(value)) {
return value;
}
}
for(const oldVal in config['cleaner']['substitutions']) {
const newVal = config['cleaner']['substitutions'][oldVal];
str = str.replace(oldVal, newVal);
}
return str;
}
static cleanFilesData() {
log('Cleaning file data ...')
showData('history', history);
showData('tags', tags);
const newHistory = {};
let i = 0;
for(const exe in history) {
log(` File (history) - ${i++} - ${exe}`);
const formattedExe = FocusStats.cleanString(exe);
if(!newHistory[formattedExe]) {
newHistory[formattedExe] = {};
}
for(const name in history[exe]) {
const formattedName = FocusStats.cleanString(name);
if(!newHistory[formattedExe][formattedName]) {
newHistory[formattedExe][formattedName] = [];
}
for(const item of history[exe][name]) {
newHistory[formattedExe][formattedName].push(item);
}
}
}
i = 0;
const newTags = {};
for(const exe in tags) {
log(` File (tags) - ${i++} - ${exe}`);
const formattedExe = FocusStats.cleanString(exe);
for(const name in tags[exe]) {
const formattedName = FocusStats.cleanString(name);
newTags[formattedExe] = newTags[formattedExe] || {};
newTags[formattedExe][formattedName] = Array.from(new Set([...(newTags[formattedExe][formattedName] || []), ...tags[exe][name] ]));
}
}
fs.writeFileSync(config['dataFolder'] + 'history.json', JSON.stringify(newHistory, null, 4))
fs.writeFileSync(config['dataFolder'] + 'tags.json', JSON.stringify(newTags, null, 4))
showData('history', newHistory);
showData('tags', newTags);
log('Cleaned file data !')
}
static async pushHistory() {
log('Saving current focus stats ...', 'info');
const dbEntries = (await Database.execQuery('SELECT date, duration, exe, name FROM focus_stats')).rows;
const dbEntriesMapped = {};
for(const dbEntry of dbEntries) {
dbEntriesMapped[dbEntry.date.getTime()] = dbEntry;
}
const history = JSON.parse(fs.readFileSync(config['dataFolder'] + 'history.json'));
for(const name in history) {
for(const exe in history[name]) {
if(name === '' && exe === '') {
continue;
}
for(const element of history[name][exe]) {
const date = new Date(element.date);
if(typeof dbEntriesMapped[date.getTime()] !== 'undefined') {
if(
dbEntriesMapped[date.getTime()].duration === element.duration &&
dbEntriesMapped[date.getTime()].name === name &&
dbEntriesMapped[date.getTime()].exe === exe) {
continue;
}
await Database.execQuery(
'DELETE FROM focus_stats WHERE date = $1',
[date]
);
}
const [query, values] = Database.buildInsertQuery('focus_stats', {
name,
exe,
date,
duration: element.duration,
});
await Database.execQuery(
query,
values
);
}
}
}
log('Saved current focus stats', 'info');
}
static async pushTags() {
log('Saving tags focus stats assignements', 'info');
// Get already inserted tags
const data = (await Database.execQuery('SELECT * FROM focus_stats_tags')).rows;
// TODO: delete data that has been removed on UI
// We won't do it now because there's now way to remove tags from UI atm
const dbTagsMapped = {};
for(let row of data) {
if(!dbTagsMapped[row.name]) {
dbTagsMapped[row.name] = {};
}
if(!dbTagsMapped[row.name][row.exe]) {
dbTagsMapped[row.name][row.exe] = [];
}
dbTagsMapped[row.name][row.exe].push(row.tag);
}
// Insert tags
const tags = JSON.parse(fs.readFileSync(config['dataFolder'] + 'tags.json'));
for(const name in tags) {
for(const exe in tags[name]) {
for(const tag of tags[name][exe]) {
if(dbTagsMapped[name] && dbTagsMapped[name][exe] && dbTagsMapped[name][exe].includes(tag)) {
continue;
}
const [query, values] = Database.buildInsertQuery('focus_stats_tags', {
name,
exe,
tag
});
await Database.execQuery(
query,
values
);
}
}
}
log('Saved tags focus stats assignements', 'info');
}
static async update() {
try {
await FocusStats.pushHistory();
await FocusStats.pushTags();
} catch(e) {
console.error(e);
}
}
}