Clear cache and reduce api calls

This commit is contained in:
AlphaX-Projects 2023-07-23 18:19:55 +02:00
parent 1757030eb3
commit 63bd6ac3c4
2 changed files with 109 additions and 116 deletions

View File

@ -7,6 +7,7 @@ import { get, translate, translateUnsafeHTML } from 'lit-translate'
import localForage from 'localforage' import localForage from 'localforage'
import { encryptData, decryptData } from '../lockScreen.js' import { encryptData, decryptData } from '../lockScreen.js'
import { setChatLastSeen } from '../redux/app/app-actions.js' import { setChatLastSeen } from '../redux/app/app-actions.js'
import isElectron from 'is-electron'
const chatLastSeen = localForage.createInstance({ const chatLastSeen = localForage.createInstance({
name: "chat-last-seen", name: "chat-last-seen",
@ -427,7 +428,7 @@ class AppView extends connect(store)(LitElement) {
constructor() { constructor() {
super() super()
this.theme = localStorage.getItem('qortalTheme') ? localStorage.getItem('qortalTheme') : 'light' this.theme = localStorage.getItem('qortalTheme') ? localStorage.getItem('qortalTheme') : 'light'
this.urls = []; this.urls = []
this.nodeType = '' this.nodeType = ''
this.addressInfo = {} this.addressInfo = {}
this.getAllBalancesLoading = false this.getAllBalancesLoading = false
@ -649,6 +650,8 @@ class AppView extends connect(store)(LitElement) {
addTradeBotRoutes(parentEpml) addTradeBotRoutes(parentEpml)
parentEpml.imReady() parentEpml.imReady()
this.clearTheCache()
this.helperMessage = this.renderHelperPass() this.helperMessage = this.renderHelperPass()
this.salt = '' this.salt = ''
@ -1581,14 +1584,14 @@ class AppView extends connect(store)(LitElement) {
} }
const getChatLastSeen = async () => { const getChatLastSeen = async () => {
let items = []; let items = []
await chatLastSeen.iterate(function (value, key, iterationNumber) { await chatLastSeen.iterate(function (value, key, iterationNumber) {
items.push({ key, timestamp: value })
items.push({ key, timestamp: value });
}) })
store.dispatch(setChatLastSeen(items)) store.dispatch(setChatLastSeen(items))
return items; return items
} }
await getOpenTradesBTC() await getOpenTradesBTC()
@ -1603,14 +1606,25 @@ class AppView extends connect(store)(LitElement) {
await appDelay(1000) await appDelay(1000)
await getOpenTradesARRR() await getOpenTradesARRR()
await getChatLastSeen() await getChatLastSeen()
setInterval(() => {
this.clearTheCache()
}, 60000)
} }
shBalanceTicker() { shBalanceTicker() {
const targetDiv = this.shadowRoot.getElementById("theTicker") const targetDiv = this.shadowRoot.getElementById("theTicker")
if (targetDiv.style.display !== "none") { if (targetDiv.style.display !== "none") {
targetDiv.style.display = "none"; targetDiv.style.display = "none"
} else { } else {
targetDiv.style.display = "inline"; targetDiv.style.display = "inline"
}
}
async clearTheCache() {
if (!isElectron()) {
} else {
await window.parent.electronAPI.clearMyCache()
console.clear()
} }
} }

View File

@ -34,10 +34,8 @@ class NodeManagement extends LitElement {
confPeerMessage: { type: String }, confPeerMessage: { type: String },
addMintingAccountMessage: { type: String }, addMintingAccountMessage: { type: String },
removeMintingAccountMessage: { type: String }, removeMintingAccountMessage: { type: String },
tempMintingAccount: { type: Object },
nodeConfig: { type: Object }, nodeConfig: { type: Object },
nodeDomain: { type: String }, nodeDomain: { type: String },
myElementId: { type: String },
theme: { type: String, reflect: true } theme: { type: String, reflect: true }
} }
} }
@ -138,15 +136,13 @@ class NodeManagement extends LitElement {
this.addPeerMessage = "" this.addPeerMessage = ""
this.confPeerMessage = "" this.confPeerMessage = ""
this.addMintingAccountMessage = "" this.addMintingAccountMessage = ""
this.tempMintingAccount = {}
this.config = { this.config = {
user: { user: {
node: {}, node: {},
}, },
}; }
this.nodeConfig = {} this.nodeConfig = {}
this.nodeDomain = "" this.nodeDomain = ""
this.myElementId = ''
this.theme = localStorage.getItem('qortalTheme') ? localStorage.getItem('qortalTheme') : 'light' this.theme = localStorage.getItem('qortalTheme') ? localStorage.getItem('qortalTheme') : 'light'
} }
@ -278,7 +274,7 @@ class NodeManagement extends LitElement {
<br /> <br />
</div> </div>
</div> </div>
`; `
} }
firstUpdated() { firstUpdated() {
@ -311,73 +307,59 @@ class NodeManagement extends LitElement {
// Calculate HH MM SS from Milliseconds... // Calculate HH MM SS from Milliseconds...
const convertMsToTime = (milliseconds) => { const convertMsToTime = (milliseconds) => {
let day, hour, minute, seconds; let day, hour, minute, seconds
seconds = Math.floor(milliseconds / 1000); seconds = Math.floor(milliseconds / 1000)
minute = Math.floor(seconds / 60); minute = Math.floor(seconds / 60)
seconds = seconds % 60; seconds = seconds % 60
hour = Math.floor(minute / 60); hour = Math.floor(minute / 60)
minute = minute % 60; minute = minute % 60
day = Math.floor(hour / 24); day = Math.floor(hour / 24)
hour = hour % 24; hour = hour % 24
if (isNaN(day)) { if (isNaN(day)) {
return "offline"; return "offline"
} }
return day + "d " + hour + "h " + minute + "m"; return day + "d " + hour + "h " + minute + "m"
}; }
const getNodeUpTime = () => { const getNodeUpTime = () => {
parentEpml this.upTime = ""
.request("apiCall", { parentEpml.request("apiCall", { url: `/admin/uptime` }).then((res) => {
url: `/admin/uptime`, this.upTime = convertMsToTime(res)
}) })
.then((res) => { setTimeout(getNodeUpTime, 60000)
this.upTime = ""; }
setTimeout(() => {
this.upTime = convertMsToTime(res);
}, 1);
});
setTimeout(getNodeUpTime, this.config.user.nodeSettings.pingInterval);
};
const updatePeers = () => { const updatePeers = () => {
parentEpml this.peers = []
.request("apiCall", { parentEpml.request("apiCall", { url: `/peers` }).then((res) => {
url: `/peers`, this.peers = res
}) })
.then((res) => { setTimeout(updatePeers, 60000)
setTimeout(() => { }
this.peers = res;
}, 1);
});
setTimeout(updatePeers, this.config.user.nodeSettings.pingInterval);
};
const getNodeConfig = () => { const getNodeConfig = () => {
this.nodeConfig = {}
this.nodeDomain = ""
parentEpml.request("getNodeConfig").then((res) => { parentEpml.request("getNodeConfig").then((res) => {
setTimeout(() => { this.nodeConfig = res
this.nodeConfig = res; const myNode = window.parent.reduxStore.getState().app.nodeConfig.knownNodes[window.parent.reduxStore.getState().app.nodeConfig.node]
}, 1); this.nodeDomain = myNode.domain + ":" + myNode.port
let myNode = window.parent.reduxStore.getState().app.nodeConfig })
.knownNodes[window.parent.reduxStore.getState().app.nodeConfig.node]; setTimeout(getNodeConfig, 60000)
this.nodeDomain = myNode.domain + ":" + myNode.port; }
});
setTimeout(getNodeConfig, 1000);
};
let configLoaded = false let configLoaded = false
parentEpml.ready().then(() => { parentEpml.ready().then(() => {
parentEpml.subscribe("config", async c => { parentEpml.subscribe("config", async c => {
if (!configLoaded) { if (!configLoaded) {
setTimeout(getNodeUpTime, 1); setTimeout(getNodeUpTime, 1)
setTimeout(updatePeers, 1); setTimeout(updatePeers, 1)
setTimeout(this.updateMintingAccounts, 1); setTimeout(this.updateMintingAccounts, 1)
setTimeout(getNodeConfig, 1); setTimeout(getNodeConfig, 1)
configLoaded = true; configLoaded = true
} }
this.config = JSON.parse(c); this.config = JSON.parse(c)
}) })
}) })
parentEpml.imReady() parentEpml.imReady()
@ -386,11 +368,11 @@ class NodeManagement extends LitElement {
changeTheme() { changeTheme() {
const checkTheme = localStorage.getItem('qortalTheme') const checkTheme = localStorage.getItem('qortalTheme')
if (checkTheme === 'dark') { if (checkTheme === 'dark') {
this.theme = 'dark'; this.theme = 'dark'
} else { } else {
this.theme = 'light'; this.theme = 'light'
} }
document.querySelector('html').setAttribute('theme', this.theme); document.querySelector('html').setAttribute('theme', this.theme)
} }
changeLanguage() { changeLanguage() {
@ -421,8 +403,8 @@ class NodeManagement extends LitElement {
}) })
.then((res) => { .then((res) => {
let err3string = get("nodepage.nchange25") let err3string = get("nodepage.nchange25")
parentEpml.request('showSnackBar', `${err3string}` + peerAddress); parentEpml.request('showSnackBar', `${err3string}` + peerAddress)
}); })
} }
removePeer(peerAddress, rowIndex) { removePeer(peerAddress, rowIndex) {
@ -434,9 +416,9 @@ class NodeManagement extends LitElement {
}) })
.then((res) => { .then((res) => {
let err4string = get("nodepage.nchange26") let err4string = get("nodepage.nchange26")
parentEpml.request('showSnackBar', `${err4string}` + peerAddress); parentEpml.request('showSnackBar', `${err4string}` + peerAddress)
this.peers.splice(rowIndex, 1); this.peers.splice(rowIndex, 1)
}); })
} }
stopNode() { stopNode() {
@ -446,9 +428,9 @@ class NodeManagement extends LitElement {
method: "GET" method: "GET"
}) })
.then((res) => { .then((res) => {
let err7string = get("nodepage.nchange32") let err7string = get("nodepage.nchange32")
parentEpml.request('showSnackBar', `${err7string}`); parentEpml.request('showSnackBar', `${err7string}`)
}); })
} }
restartNode() { restartNode() {
@ -458,19 +440,18 @@ class NodeManagement extends LitElement {
method: "GET" method: "GET"
}) })
.then((res) => { .then((res) => {
let err7string = get("nodepage.nchange34") let err7string = get("nodepage.nchange34")
parentEpml.request('showSnackBar', `${err7string}`); parentEpml.request('showSnackBar', `${err7string}`)
}); })
} }
onPageNavigation(pageUrl) { onPageNavigation(pageUrl) {
parentEpml.request("setPageUrl", pageUrl); parentEpml.request("setPageUrl", pageUrl)
} }
addPeer(e) { addPeer(e) {
this.addPeerLoading = true; this.addPeerLoading = true
const addPeerAddress = this.shadowRoot.querySelector("#addPeerAddress") const addPeerAddress = this.shadowRoot.querySelector("#addPeerAddress").value
.value;
parentEpml parentEpml
.request("apiCall", { .request("apiCall", {
@ -479,18 +460,16 @@ class NodeManagement extends LitElement {
body: addPeerAddress, body: addPeerAddress,
}) })
.then((res) => { .then((res) => {
this.addPeerMessage = res.message; this.addPeerMessage = res.message
this.addPeerLoading = false; this.addPeerLoading = false
}); })
} }
addMintingAccount(e) { addMintingAccount(e) {
this.addMintingAccountLoading = true; this.addMintingAccountLoading = true
this.addMintingAccountMessage = "Loading..."; this.addMintingAccountMessage = "Loading..."
this.addMintingAccountKey = this.shadowRoot.querySelector( this.addMintingAccountKey = this.shadowRoot.querySelector("#addMintingAccountKey").value
"#addMintingAccountKey"
).value;
parentEpml parentEpml
.request("apiCall", { .request("apiCall", {
@ -500,28 +479,28 @@ class NodeManagement extends LitElement {
}) })
.then((res) => { .then((res) => {
if (res === true) { if (res === true) {
this.updateMintingAccounts(); this.updateMintingAccounts()
this.addMintingAccountKey = ""; this.addMintingAccountKey = ""
this.addMintingAccountMessage = this.renderErr1Text(); this.addMintingAccountMessage = this.renderErr1Text()
this.addMintingAccountLoading = false; this.addMintingAccountLoading = false
} else { } else {
this.addMintingAccountKey = ""; this.addMintingAccountKey = ""
this.addMintingAccountMessage = this.renderErr2Text(); // Corrected an error here thanks to crow (-_-) this.addMintingAccountMessage = this.renderErr2Text() // Corrected an error here thanks to crow (-_-)
this.addMintingAccountLoading = false; this.addMintingAccountLoading = false
} }
}); })
} }
updateMintingAccounts() { updateMintingAccounts() {
parentEpml.request("apiCall", { this.mintingAccounts = []
url: `/admin/mintingaccounts`, parentEpml.request("apiCall", { url: `/admin/mintingaccounts` }).then((res) => {
}).then((res) => { this.mintingAccounts = res
setTimeout(() => this.mintingAccounts = res, 1); })
}); console.clear()
} }
removeMintingAccount(publicKey) { removeMintingAccount(publicKey) {
this.removeMintingAccountLoading = true; this.removeMintingAccountLoading = true
parentEpml.request("apiCall", { parentEpml.request("apiCall", {
url: `/admin/mintingaccounts?apiKey=${this.getApiKey()}`, url: `/admin/mintingaccounts?apiKey=${this.getApiKey()}`,
@ -529,28 +508,28 @@ class NodeManagement extends LitElement {
body: publicKey, body: publicKey,
}).then((res) => { }).then((res) => {
if (res === true) { if (res === true) {
this.updateMintingAccounts(); this.updateMintingAccounts()
this.removeMintingAccountLoading = false; this.removeMintingAccountLoading = false
let err5string = get("nodepage.nchange29") let err5string = get("nodepage.nchange29")
parentEpml.request('showSnackBar', `${err5string}`); parentEpml.request('showSnackBar', `${err5string}`)
} else { } else {
this.removeMintingAccountLoading = false; this.removeMintingAccountLoading = false
let err6string = get("nodepage.nchange30") let err6string = get("nodepage.nchange30")
parentEpml.request('showSnackBar', `${err6string}`); parentEpml.request('showSnackBar', `${err6string}`)
} }
}); })
} }
getApiKey() { getApiKey() {
const myNode = window.parent.reduxStore.getState().app.nodeConfig.knownNodes[window.parent.reduxStore.getState().app.nodeConfig.node]; const myNode = window.parent.reduxStore.getState().app.nodeConfig.knownNodes[window.parent.reduxStore.getState().app.nodeConfig.node]
let apiKey = myNode.apiKey; let apiKey = myNode.apiKey
return apiKey; return apiKey
} }
isEmptyArray(arr) { isEmptyArray(arr) {
if (!arr) return true; if (!arr) return true
return arr.length === 0; return arr.length === 0
} }
} }
window.customElements.define("node-management", NodeManagement); window.customElements.define("node-management", NodeManagement)