Clean old beacons and compact DB.

This commit is contained in:
Mashintime 2023-06-14 18:03:10 -04:00
parent 8f308d9adc
commit a65d2d52de
2 changed files with 51 additions and 6 deletions

View file

@ -165,7 +165,9 @@ window.addEventListener("DOMContentLoaded", () => {
"user_info_website", "user_info_website",
"user_info_comments", "user_info_comments",
]; ];
document.getElementById("btndbClean").addEventListener("click", () => {
dbClean();
});
users users
.find({ .find({
selector: { selector: {
@ -819,7 +821,7 @@ ipcRenderer.on("action-new-msg-received", (event, arg) => {
add_obj_to_database(obj); add_obj_to_database(obj);
update_chat_obj_by_uuid(obj.uuid); update_chat_obj_by_uuid(obj.uuid);
// handle beacon // handle ping-ack
} else if (item.ping == "acknowledge") { } else if (item.ping == "acknowledge") {
obj.timestamp = parseInt(item.timestamp); obj.timestamp = parseInt(item.timestamp);
obj.dxcallsign = item.dxcallsign; obj.dxcallsign = item.dxcallsign;
@ -2848,3 +2850,41 @@ function showOsPopUp(title, message)
const NOTIFICATION_BODY = message; const NOTIFICATION_BODY = message;
new Notification(NOTIFICATION_TITLE, { body: NOTIFICATION_BODY }); new Notification(NOTIFICATION_TITLE, { body: NOTIFICATION_BODY });
} }
//Function to clean old beacons and optimize database
async function dbClean()
{
//Only keep the most x latest days of beacons
let beaconKeep = 7;
let timestampPurge = Math.floor((Date.now()- beaconKeep * 24*60*60*1000) / 1000) ;
if (confirm("Delete beacons older than " + beaconKeep + " days and compact database?")) {
} else {
return;
}
await db.find({
selector: {
timestamp: {$lt: timestampPurge},
type: "beacon",
}
})
.then(async function (result) {
console.log("Purging " + result.docs.length + " beacons received before " + timestampPurge);
result.docs.forEach(async function (item) {
await db.get(item._id)
.then(async function (doc) {
await db.remove(doc)
})
.catch(function (err) {
console.log(err);
});
});
})
.catch(function (err) {
console.log(err);
});
//Compact database
await db.compact();
window.alert("Database maintenance is complete.");
}

View file

@ -264,9 +264,14 @@
Help Help
</button> </button>
</li> </li>
<li>
<button
class="dropdown-item"
id="btndbClean"
>
Clean Database
</button>
</li>
</ul> </ul>
<span <span