From b1f1fa8a26d6add04e1ea57cc923b5379d4f784d Mon Sep 17 00:00:00 2001 From: BlipRanger <1860540+BlipRanger@users.noreply.github.com> Date: Mon, 17 Jul 2023 19:32:21 -0400 Subject: [PATCH] Change the way we count non-user msgs --- public/script.js | 11 +++++---- public/scripts/RossAscends-mods.js | 11 ++++++--- server.js | 1 + statsHelpers.js | 38 ++++++++++++++++-------------- 4 files changed, 36 insertions(+), 25 deletions(-) diff --git a/public/script.js b/public/script.js index f1e94d89a..944fd24a6 100644 --- a/public/script.js +++ b/public/script.js @@ -1309,7 +1309,7 @@ function addOneMessage(mes, { type = "normal", insertAfter = null, scroll = true const momentDate = timestampToMoment(mes.send_date); const timestamp = momentDate.isValid() ? momentDate.format('LL LT') : ''; - statMesProcess(mes, type); + if (mes?.extra?.display_text) { @@ -3712,15 +3712,17 @@ function calculateGenTime(gen_started, gen_finished) { return endDate - startDate; } -function updateStats() { - const response = fetch('/updatestats', { +async function updateStats() { + const response = await fetch('/updatestats', { method: 'POST', headers: getRequestHeaders(), body: JSON.stringify(charStats), }); + if (response.status !== 200) { console.error('Failed to update stats'); + console.log(response).status; } } @@ -3756,7 +3758,7 @@ function saveReply(type, getMessage, this_mes_is_name, title) { chat[chat.length - 1]['is_user'])) { type = 'normal'; } - console.log(chat); + const generationFinished = new Date(); const img = extractImageFromMessage(getMessage); getMessage = img.getMessage; @@ -3852,6 +3854,7 @@ function saveReply(type, getMessage, this_mes_is_name, title) { extra: JSON.parse(JSON.stringify(chat[chat.length - 1]["extra"])), }; } + statMesProcess(chat[chat.length - 1], type); return { type, getMessage }; } diff --git a/public/scripts/RossAscends-mods.js b/public/scripts/RossAscends-mods.js index 4da217610..b551a85e0 100644 --- a/public/scripts/RossAscends-mods.js +++ b/public/scripts/RossAscends-mods.js @@ -13,6 +13,7 @@ import { menu_type, max_context, saveSettingsDebounced, + charStats, } from "../script.js"; @@ -71,6 +72,7 @@ const observer = new MutationObserver(function (mutations) { RA_checkOnlineStatus(); } else if (mutation.target.parentNode === SelectedCharacterTab) { setTimeout(RA_CountCharTokens, 200); + setTimeout(AA_CountCharTime, 200); } }); }); @@ -107,8 +109,9 @@ function waitForElement(querySelector, timeout) { //humanize character generation time export function humanizeGenTime(character) { + let stat = charStats[character.avatar] //convert time_spent to humanized format of "_ Hours, _ Minutes, _ Seconds" from milliseconds - let time_spent = character.total_gen_time + let time_spent = stat.total_gen_time time_spent = Math.floor(time_spent / 1000); let seconds = time_spent % 60; time_spent = Math.floor(time_spent / 60); @@ -144,8 +147,9 @@ export function AA_CountCharTime(chid){ //get character stats from the server //get total word counts - let user_words = selected_character.stats.user_word_count; - let char_words = selected_character.stats.word_count; + let stat = charStats[selected_character.avatar] + let user_words = stat.user_word_count; + let char_words = stat.non_user_word_count; @@ -155,6 +159,7 @@ export function AA_CountCharTime(chid){ $("#result_info").append(`
Total words: ${user_words} / ${char_words}
`); } + else { console.debug("AA_CountCharTime -- no valid char found, closing."); } } // Device detection diff --git a/server.js b/server.js index 97a088c7f..0d48a9ecb 100644 --- a/server.js +++ b/server.js @@ -1297,6 +1297,7 @@ app.post("/getstats", jsonParser, function (request, response) { * */ app.post("/updatestats", jsonParser, function (request, response) { + console.log(request.body) if (!request.body) return response.sendStatus(400); statsHelpers.setCharStats(request.body); return response.sendStatus(200); diff --git a/statsHelpers.js b/statsHelpers.js index 13b2afd50..a4386157f 100644 --- a/statsHelpers.js +++ b/statsHelpers.js @@ -18,6 +18,7 @@ const crypto = require('crypto'); let charStats = {}; +let lastSaveTimestamp = 0; const statsFilePath = 'public/stats.json'; @@ -34,7 +35,8 @@ async function collectAndCreateStats(chatsPath, charactersPath) { for (let stat of statsArr) { finalStats = { ...finalStats, ...stat } } - + // tag with timestamp on when stats were generated + finalStats.timestamp = Date.now(); return finalStats; } @@ -57,9 +59,15 @@ async function loadStatsFile(chatsPath, charactersPath) { } // Function to save the stats to file + async function saveStatsToFile() { - console.debug('Saving stats to file...'); - await writeFile(statsFilePath, JSON.stringify(charStats)); + if (charStats.timestamp > lastSaveTimestamp) { + console.debug('Saving stats to file...'); + await writeFile(statsFilePath, JSON.stringify(charStats)); + lastSaveTimestamp = Date.now(); + } else { + //console.debug('Stats have not changed since last save. Skipping file write.'); + } } async function writeStatsToFileAndExit(charStats) { @@ -170,6 +178,7 @@ function getCharStats() { function setCharStats(stats) { charStats = stats; + charStats.timestamp = Date.now(); } @@ -219,27 +228,20 @@ function calculateTotalGenTimeAndWordCount(char_dir, chat, uniqueGenStartTimes) } if (json.mes) { - if (!firstNonUserMsgSkipped || json.is_user) { - let wordCount = countWordsInString(json.mes); - json.is_user ? userWordCount += wordCount : nonUserWordCount += wordCount; - json.is_user ? userMsgCount++ : nonUserMsgCount++; - } - if (!json.is_user) { - firstNonUserMsgSkipped = true; - } + let wordCount = countWordsInString(json.mes); + json.is_user ? userWordCount += wordCount : nonUserWordCount += wordCount; + json.is_user ? userMsgCount++ : nonUserMsgCount++; } if (json.swipes && json.swipes.length > 1) { totalSwipeCount += json.swipes.length - 1; // Subtract 1 to not count the first swipe for (let i = 1; i < json.swipes.length; i++) { // Start from the second swipe let swipeText = json.swipes[i]; - if (!firstNonUserMsgSkipped || json.is_user) { - let wordCount = countWordsInString(swipeText); - json.is_user ? userWordCount += wordCount : nonUserWordCount += wordCount; - } - if (!json.is_user) { - firstNonUserMsgSkipped = true; - } + + let wordCount = countWordsInString(swipeText); + json.is_user ? userWordCount += wordCount : nonUserWordCount += wordCount; + json.is_user ? userMsgCount++ : nonUserMsgCount++; + } }