From 25d1267a0796a0a4fcd76bbe05f7b6d8bd97dc60 Mon Sep 17 00:00:00 2001 From: Stefan Dej Date: Sun, 19 Dec 2021 23:43:11 +0100 Subject: [PATCH] fix: issue with tempchart/temphistory if the browser go into sleep mode Signed-off-by: Stefan Dej --- src/components/charts/TempChart.vue | 15 ++++++++++--- src/store/printer/tempHistory/actions.ts | 25 ++++++++++++++++------ src/store/printer/tempHistory/index.ts | 2 ++ src/store/printer/tempHistory/mutations.ts | 16 ++++++++++++-- src/store/printer/tempHistory/types.ts | 2 ++ 5 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/components/charts/TempChart.vue b/src/components/charts/TempChart.vue index 9464a3c3..f941ceaa 100644 --- a/src/components/charts/TempChart.vue +++ b/src/components/charts/TempChart.vue @@ -14,7 +14,7 @@ import { convertName } from '@/plugins/helpers' import Component from 'vue-class-component' import {Mixins, Watch} from 'vue-property-decorator' import BaseMixin from '../mixins/base' -import {PrinterTempHistoryStateSerie} from '@/store/printer/tempHistory/types' +import {PrinterTempHistoryStateSerie, PrinterTempHistoryStateSourceEntry} from '@/store/printer/tempHistory/types' import { createComponent } from 'echarts-for-vue' import * as echarts from 'echarts' @@ -243,15 +243,24 @@ export default class TempChart extends Mixins(BaseMixin) { updateChart() { if (this.chart && this.isVisible) { //const t0 = performance.now() + const limitDate = new Date(Date.now() - this.maxHistory * 1000) + let newSource = [...this.source].filter((entry: PrinterTempHistoryStateSourceEntry) => { + return entry.date >= limitDate + }) this.chart?.setOption({ dataset: { - source: this.source + source: newSource }, }) //const t1 = performance.now() - //window.console.debug(t1-t0) + //window.console.debug('calc chart', (t1-t0).toFixed(), newSource.length, this.source.length) + + // reset tempHistory if working sources are smaller than 80% + if (this.source.length > 0 && newSource.length < this.maxHistory * 0.8) { + this.$socket.emit('server.temperature_store', {}, { action: 'printer/tempHistory/init' }) + } } } diff --git a/src/store/printer/tempHistory/actions.ts b/src/store/printer/tempHistory/actions.ts index 23a8b307..2bddbfd5 100644 --- a/src/store/printer/tempHistory/actions.ts +++ b/src/store/printer/tempHistory/actions.ts @@ -15,20 +15,24 @@ import { import {RootState} from '@/store/types' export const actions: ActionTree = { - reset({ commit }) { + reset({ commit, state }) { + if (state.updateSourceInterval !== null) + clearInterval(state.updateSourceInterval) + commit('reset') }, init({ commit, rootGetters, dispatch }, payload) { + window.console.debug('init printer/tempHistory') commit('reset') const now = new Date() const allSensors = rootGetters['printer/getAvailableSensors'] ?? [] const maxHistory = rootGetters['server/getConfig']('server', 'temperature_store_size') || 1200 - if ('requestParams' in payload) delete payload.requestParams - if (payload !== undefined) { + if ('requestParams' in payload) delete payload.requestParams + const objectKeys = Object.keys(payload) // eslint-disable-next-line const importData: any = {} @@ -198,13 +202,20 @@ export const actions: ActionTree = { commit('setInitSeries', series) - setInterval(() => { + const updateSourceInterval = setInterval(() => { dispatch('updateSource') }, datasetInterval) + + commit('setUpdateSourceInterval', updateSourceInterval) } }, - updateSource({ commit, rootState, rootGetters }) { + async updateSource({ commit, rootState, rootGetters, state }) { + /*if (state.timeLastUpdate !== null) { + const t0 = performance.now() + window.console.debug('update Source', t0-state.timeLastUpdate) + }*/ + if (rootState?.printer?.heaters?.available_sensors?.length) { const data: PrinterTempHistoryStateSourceEntry = { date: new Date() @@ -222,10 +233,12 @@ export const actions: ActionTree = { } }) - commit('addToSource', { + await commit('addToSource', { data: data, maxHistory: rootGetters['server/getConfig']('server', 'temperature_store_size') || 1200 }) } + + //commit('saveLastDate', performance.now()) }, } \ No newline at end of file diff --git a/src/store/printer/tempHistory/index.ts b/src/store/printer/tempHistory/index.ts index 5aa038e8..51df7e1a 100644 --- a/src/store/printer/tempHistory/index.ts +++ b/src/store/printer/tempHistory/index.ts @@ -8,6 +8,8 @@ export const getDefaultState = (): PrinterTempHistoryState => { return { source: [], series: [], + timeLastUpdate: null, + updateSourceInterval: null } } diff --git a/src/store/printer/tempHistory/mutations.ts b/src/store/printer/tempHistory/mutations.ts index 660102db..c1e969db 100644 --- a/src/store/printer/tempHistory/mutations.ts +++ b/src/store/printer/tempHistory/mutations.ts @@ -17,8 +17,20 @@ export const mutations: MutationTree = { }, addToSource(state, payload) { - state.source.push(payload.data) - while (state.source.length > payload.maxHistory) state.source.splice(0, 1) + const newSource = [...state.source] + newSource.push(payload.data) + while (newSource.length > payload.maxHistory) newSource.splice(0, 1) + + Vue.set(state, 'source', newSource) + }, + + // function for debugging tempchart update interval (browser sleep) + saveLastDate(state, payload) { + Vue.set(state, 'timeLastUpdate', payload) + }, + + setUpdateSourceInterval(state, payload) { + Vue.set(state, 'updateSourceInterval', payload) }, setColor(state, payload) { diff --git a/src/store/printer/tempHistory/types.ts b/src/store/printer/tempHistory/types.ts index 8a7a3c36..4b040d85 100644 --- a/src/store/printer/tempHistory/types.ts +++ b/src/store/printer/tempHistory/types.ts @@ -1,6 +1,8 @@ export interface PrinterTempHistoryState { source: PrinterTempHistoryStateSourceEntry[] series: PrinterTempHistoryStateSerie[] + timeLastUpdate: number | null + updateSourceInterval: number | null } export interface PrinterTempHistoryStateSerie {