Skip to content

Commit

Permalink
performance optimation for onFolderDisplayed
Browse files Browse the repository at this point in the history
  • Loading branch information
minutogit committed Dec 12, 2024
1 parent bddd690 commit d6e8d33
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
31 changes: 29 additions & 2 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ let removeOnClassify = true; // Standardwert true
const maxTokenCount = 10000; // Maximale Anzahl der Tokens in der Datenbank.
let tagKeyToNameMap = {}; // Mapping von Tag-Key zu Tag-Name
let tagNameToKeyMap = {}; // Mapping von Tag-Name zu Tag-Key
const lastCheckTimestamps = {}; // Stores the last check timestamp for each folder to optimize performance (in milliseconds)


// translate-Funktion zur Nutzung von i18n
function trans(messageName, placeholders = []) {
Expand Down Expand Up @@ -188,6 +190,14 @@ function initialize() {
}
});

// Sheduler for updateUsageData
messenger.alarms.create("updateUsageDataAlarm", { periodInMinutes: 10 });
messenger.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === "updateUsageDataAlarm") {
updateUsageData();
}
});

})
.catch((error) => {
console.error("Error during initialization:", error);
Expand Down Expand Up @@ -441,9 +451,14 @@ function getMessageTags(messageId) {
* @param {object} folder - Information about the displayed folder.
*/

/**
* Handles the display of a folder and checks for new emails.
* @param {Object} tab - The mail tab object.
* @param {Object} folder - The folder object.
*/
async function onFolderDisplayed(tab, folder) {
updateUsageData();

const CHECK_INTERVAL = 5 * 60 * 1000; // 5 minutes
console.debug("onFolderDisplayed")
if (folder && folder.accountId && folder.path) {
const accountId = folder.accountId;

Expand All @@ -456,6 +471,16 @@ async function onFolderDisplayed(tab, folder) {
try {
const folderKey = `${accountId}:${folder.path}`;

// To optimize performance, skip the check if the last one occurred recently.
const now = Date.now();
// Check the last time this folder was processed
const lastCheck = lastCheckTimestamps[folderKey] || 0;
if (now - lastCheck < CHECK_INTERVAL) {
return;
}
// Update the last check timestamp
lastCheckTimestamps[folderKey] = now;

// Load the last processed date for this folder from storage
let result = await messenger.storage.local.get("folderLastProcessed");
let folderLastProcessed = result.folderLastProcessed || {};
Expand Down Expand Up @@ -553,6 +578,8 @@ async function onFolderDisplayed(tab, folder) {





// learnTagFromMail Funktion
function learnTagFromMail(messageId, tagName, isPositive) {
console.log(`Training with message ID: ${messageId} and tag: ${tagName}`);
Expand Down
2 changes: 1 addition & 1 deletion src/bayes.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function calculateBayesProbability(tokens, data, returnTokenContributions = fals

const probability = Math.exp(logProbPositive - logSumExp); // Normalisierte positive Wahrscheinlichkeit

console.log("Calculated probability:", probability);
console.debug("Calculated probability:", probability);

// Rückgabe der Token-Beiträge, falls aktiviert
if (returnTokenContributions) {
Expand Down
4 changes: 2 additions & 2 deletions src/donation_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ async function verifyDonationCode(cryptedemail, code) {
* Wenn die Checksum nicht mit der berechneten übereinstimmt, wird der usage_counter auf 30 gesetzt.
*/
async function updateUsageData() {
console.debug(`updateUsageData`);
const currentData = await messenger.storage.local.get(['donation_handler']);
const donationHandler = currentData.donation_handler || {};

Expand All @@ -58,7 +57,7 @@ async function updateUsageData() {
const shortHash = await short_sha256(donationHandler.donation_mail);
const isValid = await verifyDonationCode(shortHash, donationHandler.donation_key);
if (isValid) {
console.debug("Gültiger donation_key. updateUsageData.");
console.debug("Valid donation_key found.");
return;
}
}
Expand Down Expand Up @@ -89,6 +88,7 @@ async function updateUsageData() {

// Wenn die Checksum korrekt ist, prüfe, ob ein neuer Tag begonnen hat
if (today != lastCheckDate) {
console.debug(`updateUsageData (new day)`);
const newUsageCounter = (donationHandler.usage_counter || 0) + 1;
const newLastCheckDate = today;

Expand Down
5 changes: 3 additions & 2 deletions src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "PrioMailbox",
"version": "1.3.1",
"version": "1.3.3",
"default_locale": "en",
"description": "PrioMailbox organizes your emails in Thunderbird with intelligent, trainable tags. Important messages are highlighted, while unimportant ones are hidden.",
"icons": {
Expand All @@ -23,7 +23,8 @@
"menus",
"notifications",
"accountsRead",
"accountsFolders"
"accountsFolders",
"alarms"
],
"browser_action": {
"default_popup": "popup/popup.html",
Expand Down
5 changes: 0 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,6 @@ function calculateKnownTokenTypesPercentage(tokens, tokenList) {
const knownUnigramsPercentage = totalUnigrams > 0 ? (knownUnigrams / totalUnigrams) * 100 : 0;
const knownBigramsPercentage = totalBigrams > 0 ? (knownBigrams / totalBigrams) * 100 : 0;

// Log the results for debugging
console.log(`Known unigrams: ${knownUnigramsPercentage.toFixed(2)}% (Known: ${knownUnigrams}, Total: ${totalUnigrams})`);
console.log(`Known bigrams: ${knownBigramsPercentage.toFixed(2)}% (Known: ${knownBigrams}, Total: ${totalBigrams})`);

// Return the percentages
return {
knownUnigramsPercentage,
knownBigramsPercentage
Expand Down

0 comments on commit d6e8d33

Please sign in to comment.