Move (re)creation of stats to its own function

This commit is contained in:
valadaptive 2023-12-07 10:04:32 -05:00
parent e9a49b7997
commit d6f5e63d85
2 changed files with 16 additions and 10 deletions

View File

@ -804,7 +804,7 @@ app.post('/getstats', jsonParser, function (request, response) {
*/
app.post('/recreatestats', jsonParser, async function (request, response) {
try {
await statsHelpers.loadStatsFile(DIRECTORIES.chats, DIRECTORIES.characters, true);
await statsHelpers.recreateStats(DIRECTORIES.chats, DIRECTORIES.characters);
return response.sendStatus(200);
} catch (error) {
console.error(error);

View File

@ -139,6 +139,18 @@ async function collectAndCreateStats(chatsPath, charactersPath) {
return finalStats;
}
/**
* Collect and update the stats file.
*
* @param {string} chatsPath - The path to the directory containing the chat files.
* @param {string} charactersPath - The path to the directory containing the character files.
*/
async function recreateStats(chatsPath, charactersPath) {
charStats = await collectAndCreateStats(chatsPath, charactersPath);
await saveStatsToFile();
console.debug('Stats (re)created and saved to file.');
}
/**
* Loads the stats file into memory. If the file doesn't exist or is invalid,
* initializes stats by collecting and creating them for each character.
@ -146,21 +158,14 @@ async function collectAndCreateStats(chatsPath, charactersPath) {
* @param {string} chatsPath - The path to the directory containing the chat files.
* @param {string} charactersPath - The path to the directory containing the character files.
*/
async function loadStatsFile(chatsPath, charactersPath, recreateStats = false) {
if (recreateStats) {
charStats = await collectAndCreateStats(chatsPath, charactersPath); // Call your function to collect and create stats
await saveStatsToFile();
console.debug('Stats recreated and saved to file.');
return true;
}
async function loadStatsFile(chatsPath, charactersPath) {
try {
const statsFileContent = await readFile(statsFilePath, 'utf-8');
charStats = JSON.parse(statsFileContent);
} catch (err) {
// If the file doesn't exist or is invalid, initialize stats
if (err.code === 'ENOENT' || err instanceof SyntaxError) {
charStats = await collectAndCreateStats(chatsPath, charactersPath); // Call your function to collect and create stats
await saveStatsToFile();
recreateStats(chatsPath, charactersPath);
} else {
throw err; // Rethrow the error if it's something we didn't expect
}
@ -425,6 +430,7 @@ function calculateTotalGenTimeAndWordCount(
module.exports = {
saveStatsToFile,
loadStatsFile,
recreateStats,
writeStatsToFileAndExit,
getCharStats,
setCharStats,