mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-02-09 08:38:53 +01:00
Move (re)creation of stats to its own function
This commit is contained in:
parent
e9a49b7997
commit
d6f5e63d85
@ -804,7 +804,7 @@ app.post('/getstats', jsonParser, function (request, response) {
|
|||||||
*/
|
*/
|
||||||
app.post('/recreatestats', jsonParser, async function (request, response) {
|
app.post('/recreatestats', jsonParser, async function (request, response) {
|
||||||
try {
|
try {
|
||||||
await statsHelpers.loadStatsFile(DIRECTORIES.chats, DIRECTORIES.characters, true);
|
await statsHelpers.recreateStats(DIRECTORIES.chats, DIRECTORIES.characters);
|
||||||
return response.sendStatus(200);
|
return response.sendStatus(200);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
@ -139,6 +139,18 @@ async function collectAndCreateStats(chatsPath, charactersPath) {
|
|||||||
return finalStats;
|
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,
|
* 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.
|
* 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} chatsPath - The path to the directory containing the chat files.
|
||||||
* @param {string} charactersPath - The path to the directory containing the character files.
|
* @param {string} charactersPath - The path to the directory containing the character files.
|
||||||
*/
|
*/
|
||||||
async function loadStatsFile(chatsPath, charactersPath, recreateStats = false) {
|
async function loadStatsFile(chatsPath, charactersPath) {
|
||||||
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;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
const statsFileContent = await readFile(statsFilePath, 'utf-8');
|
const statsFileContent = await readFile(statsFilePath, 'utf-8');
|
||||||
charStats = JSON.parse(statsFileContent);
|
charStats = JSON.parse(statsFileContent);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// If the file doesn't exist or is invalid, initialize stats
|
// If the file doesn't exist or is invalid, initialize stats
|
||||||
if (err.code === 'ENOENT' || err instanceof SyntaxError) {
|
if (err.code === 'ENOENT' || err instanceof SyntaxError) {
|
||||||
charStats = await collectAndCreateStats(chatsPath, charactersPath); // Call your function to collect and create stats
|
recreateStats(chatsPath, charactersPath);
|
||||||
await saveStatsToFile();
|
|
||||||
} else {
|
} else {
|
||||||
throw err; // Rethrow the error if it's something we didn't expect
|
throw err; // Rethrow the error if it's something we didn't expect
|
||||||
}
|
}
|
||||||
@ -425,6 +430,7 @@ function calculateTotalGenTimeAndWordCount(
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
saveStatsToFile,
|
saveStatsToFile,
|
||||||
loadStatsFile,
|
loadStatsFile,
|
||||||
|
recreateStats,
|
||||||
writeStatsToFileAndExit,
|
writeStatsToFileAndExit,
|
||||||
getCharStats,
|
getCharStats,
|
||||||
setCharStats,
|
setCharStats,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user