implement always and hash sync

This commit is contained in:
Arthur Lu 2023-07-12 22:37:08 +00:00
parent e83cde0db7
commit d25ce49ede
4 changed files with 71 additions and 32 deletions

26
scripts/clientsync.js Normal file
View File

@ -0,0 +1,26 @@
import { requestAPI } from "./utils.js";
export async function setupClientSync (scheme, rate, callback) {
if (scheme === "always") {
callback();
window.setInterval(callback, rate);
}
else if (scheme === "hash") {
const newHash = (await requestAPI("/sync/hash")).data;
localStorage.setItem("sync-current-hash", newHash);
callback();
window.setInterval(async () => {
const newHash = (await requestAPI("/sync/hash")).data;
if (localStorage.getItem("sync-current-hash") !== newHash) {
localStorage.setItem("sync-current-hash", newHash);
callback();
}
}, rate);
}
else if (scheme === "interrupt") {
}
else {
}
}

View File

@ -1,37 +1,24 @@
import { requestPVE, requestAPI, goToPage, goToURL, instancesConfig, nodesConfig, setTitleAndHeader } from "./utils.js"; import { requestPVE, requestAPI, goToPage, goToURL, instancesConfig, nodesConfig, setTitleAndHeader } from "./utils.js";
import { alert, dialog } from "./dialog.js"; import { alert, dialog } from "./dialog.js";
import { PVE } from "../vars.js"; import { PVE } from "../vars.js";
import { setupClientSync } from "./clientsync.js"
window.addEventListener("DOMContentLoaded", init); window.addEventListener("DOMContentLoaded", init);
let currentHash;
const refreshRate = 5000;
async function init () { async function init () {
setTitleAndHeader(); setTitleAndHeader();
const cookie = document.cookie; const cookie = document.cookie;
if (cookie === "") { if (cookie === "") {
goToPage("login.html"); goToPage("login.html");
} }
currentHash = (await requestAPI("/sync/hash")).data;
await populateInstances();
const addInstanceBtn = document.querySelector("#instance-add"); const addInstanceBtn = document.querySelector("#instance-add");
addInstanceBtn.addEventListener("click", handleInstanceAdd); addInstanceBtn.addEventListener("click", handleInstanceAdd);
window.setInterval(async () => { setupClientSync(localStorage.getItem("sync-scheme"), Number(localStorage.getItem("sync-rate")) * 1000, populateInstances);
const newHash = (await requestAPI("/sync/hash")).data;
if (currentHash !== newHash) {
currentHash = newHash;
populateInstances();
}
}, refreshRate);
} }
async function populateInstances () { async function populateInstances () {
const newHash = (await requestAPI("/sync/hash")).data;
currentHash = newHash;
const resources = await requestPVE("/cluster/resources", "GET"); const resources = await requestPVE("/cluster/resources", "GET");
const instanceContainer = document.getElementById("instance-container"); const instanceContainer = document.getElementById("instance-container");
const instances = []; const instances = [];

View File

@ -0,0 +1,22 @@
window.addEventListener("DOMContentLoaded", init);
function init () {
let scheme = localStorage.getItem("sync-scheme");
if (scheme) {
document.querySelector(`#sync-${scheme}`).checked = true;
}
let rate = localStorage.getItem("sync-rate");
if (rate) {
document.querySelector(`#sync-rate`).value = rate;
}
document.querySelector("#settings").addEventListener("submit", handleSaveSettings, false);
}
function handleSaveSettings (event) {
event.preventDefault();
const form = new FormData(document.querySelector("#settings"));
console.log(form.get("sync-scheme"))
localStorage.setItem("sync-scheme", form.get("sync-scheme"));
localStorage.setItem("sync-rate", form.get("sync-rate"));
window.location.reload();
}

View File

@ -10,6 +10,16 @@
<link rel="stylesheet" href="css/nav.css"> <link rel="stylesheet" href="css/nav.css">
<link rel="stylesheet" href="css/form.css"> <link rel="stylesheet" href="css/form.css">
<script src="scripts/settings.js" type="module"></script> <script src="scripts/settings.js" type="module"></script>
<style>
label {
display: flex;
height: fit-content;
width: 100%;
align-items: center;
justify-content: left;
column-gap: 10px;
}
</style>
</head> </head>
<body> <body>
<header> <header>
@ -26,35 +36,29 @@
<main> <main>
<section class="w3-container"> <section class="w3-container">
<h2>Settings</h2> <h2>Settings</h2>
<form> <form id = "settings">
<div class="w3-card w3-padding"> <div class="w3-card w3-padding">
<h3>Synchronization Settings</h3> <h3>Synchronization Settings</h3>
<fieldset> <fieldset>
<legend>App Sync Method</legend> <legend>App Sync Method</legend>
<div class="input-grid mobile-linear" style="grid-template-columns: auto auto auto 1fr;"> <div>
<input class="w3-radio" type="radio" id="sync-always" name="sync-scheme" value="always"> <label><input class="w3-radio" type="radio" id="sync-always" name="sync-scheme" value="always" required><span>Always Sync</span></label>
<label for="sync-always">Always Sync</label> <p>App will periodically synchronize with Proxmox. High resource usage.</p>
<p>App will periodically synchronize with Proxmox</p> <label><input class="w3-radio" type="radio" id="sync-hash" name="sync-scheme" value="hash" required>Check For Sync</label>
<p>High resource usage</p> <p>App will periodically synchronize only if there have been change. Medium resource usage.</p>
<input class="w3-radio" type="radio" id="sync-hash" name="sync-scheme" value="always"> <label><input class="w3-radio" type="radio" id="sync-interrupt" name="sync-scheme" value="interrupt" required>Sync When Needed</label>
<label for="sync-hash">Check For Sync</label> <p>App will react to changes and synchronize when changes are made. Low resource usage.</p>
<p>App will periodically synchronize only if there have been change</p>
<p>Medium resource usage.</p>
<input class="w3-radio" type="radio" id="sync-interrupt" name="sync-scheme" value="always">
<label for="sync-interrupt">Sync When Needed</label>
<p>App will react to changes and synchronize when changes are made.</p>
<p>Low resource usage.</p>
</div> </div>
</fieldset> </fieldset>
<fieldset> <fieldset>
<legend>App Sync Frequency</legend> <legend>App Sync Frequency</legend>
<div class="input-grid" style="grid-template-columns: auto auto 1fr;"> <div class="input-grid" style="grid-template-columns: auto auto 1fr;">
<p>Sync every</p><input class="w3-input w3-border" type="number" id="sync-rate" name="sync-rate" min="1"><p>Second(s)</p> <p>Sync every</p><input class="w3-input w3-border" type="number" id="sync-rate" name="sync-rate" min="1" required><p>Second(s)</p>
</div> </div>
</fieldset> </fieldset>
</div> </div>
<div class="w3-container w3-center" id="form-actions"> <div class="w3-container w3-center" id="form-actions">
<button class="w3-button w3-margin" id="exit" type="button">SAVE</button> <button class="w3-button w3-margin" id="save" type="submit">SAVE</button>
</div> </div>
</form> </form>
</section> </section>