From fbeecb0169abe6e86882c5c19640fa9114c7e49b Mon Sep 17 00:00:00 2001 From: Stefan Dej Date: Sat, 9 Nov 2024 01:11:53 +0100 Subject: [PATCH] feat(History): add option to show stats in different values (#2007) * refactor: refactor getters in HistoryAllPrintStatus Signed-off-by: Stefan Dej * feat: add option to switch the stats between amount, filament and time Signed-off-by: Stefan Dej * feat: add option to switch the stats between amount, filament and time Signed-off-by: Stefan Dej * refactor: extract HistoryStatsValueNames to history types Signed-off-by: Stefan Dej * refactor: remove complexity in historyStatsMixin Signed-off-by: Stefan Dej * refactor: extract some logic into a separate methods Signed-off-by: Stefan Dej * refactor: rename option amount to jobs Signed-off-by: Stefan Dej * fix: fix status groups in print history filter Signed-off-by: Stefan Dej * fix: fix decimals in filament length output of HistoryAllPrintStatusTableItem Signed-off-by: Stefan Dej * refactor: remove unused imports Signed-off-by: Stefan Dej * fix: add minAngle to chart Signed-off-by: Stefan Dej --------- Signed-off-by: Stefan Dej --- .../charts/HistoryAllPrintStatusChart.vue | 63 ++++---- .../charts/HistoryAllPrintStatusTable.vue | 44 ++---- .../charts/HistoryAllPrintStatusTableItem.vue | 36 +++++ src/components/mixins/historyStats.ts | 124 ++++++++++++++++ src/components/panels/HistoryListPanel.vue | 10 +- .../panels/HistoryStatisticsPanel.vue | 29 +++- src/locales/en.json | 2 + src/store/server/history/getters.ts | 134 +----------------- src/store/server/history/types.ts | 4 + 9 files changed, 236 insertions(+), 210 deletions(-) create mode 100644 src/components/charts/HistoryAllPrintStatusTableItem.vue create mode 100644 src/components/mixins/historyStats.ts diff --git a/src/components/charts/HistoryAllPrintStatusChart.vue b/src/components/charts/HistoryAllPrintStatusChart.vue index 0b32dbac4..e2a3927d5 100644 --- a/src/components/charts/HistoryAllPrintStatusChart.vue +++ b/src/components/charts/HistoryAllPrintStatusChart.vue @@ -5,25 +5,27 @@ :option="chartOptions" :autoresize="true" :init-options="{ renderer: 'svg' }" - style="height: 200px; width: 100%"> + style="height: 200px; width: 100%" /> diff --git a/src/components/charts/HistoryAllPrintStatusTableItem.vue b/src/components/charts/HistoryAllPrintStatusTableItem.vue new file mode 100644 index 000000000..eca1159e5 --- /dev/null +++ b/src/components/charts/HistoryAllPrintStatusTableItem.vue @@ -0,0 +1,36 @@ + + + diff --git a/src/components/mixins/historyStats.ts b/src/components/mixins/historyStats.ts new file mode 100644 index 000000000..e761a993e --- /dev/null +++ b/src/components/mixins/historyStats.ts @@ -0,0 +1,124 @@ +import Vue from 'vue' +import Component from 'vue-class-component' +import { + HistoryStatsValueNames, + ServerHistoryStateAllPrintStatusEntry, + ServerHistoryStateJob, +} from '@/store/server/history/types' +import i18n from '@/plugins/i18n' + +@Component +export default class HistoryStatsMixin extends Vue { + valueName!: HistoryStatsValueNames + + get allPrintStatusChartData() { + return this.getChartData(this.$store.state.server.history.jobs ?? []) + } + + get selectedPrintStatusChartData() { + return this.getChartData(this.$store.getters['server/history/getSelectedJobs']) + } + + private getStatusColor(status: string) { + const colorMap: Record = { + completed: '#BDBDBD', + in_progress: '#EEEEEE', + cancelled: '#616161', + default: '#424242', + } + + return colorMap[status] ?? colorMap.default + } + + private getLocalizedStatusName(status: string) { + return i18n.te(`History.StatusValues.${status}`, 'en') + ? i18n.t(`History.StatusValues.${status}`).toString() + : status + } + + private getChartData(jobs: ServerHistoryStateJob[]) { + const output: ServerHistoryStateAllPrintStatusEntry[] = [] + const hidePrintStatus = this.$store.state.gui.view.history.hidePrintStatus ?? [] + + jobs.forEach((current: ServerHistoryStateJob) => { + const index = output.findIndex((element) => element.name === current.status) + if (index !== -1) { + output[index].value += 1 + output[index].valueFilament += current.filament_used + output[index].valueTime += current.print_duration + return + } + + output.push({ + name: current.status, + displayName: this.getLocalizedStatusName(current.status), + value: 1, + valueFilament: current.filament_used, + valueTime: current.print_duration, + itemStyle: { + opacity: 0.9, + color: this.getStatusColor(current.status), + borderColor: '#1E1E1E', + borderWidth: 2, + borderRadius: 3, + }, + showInTable: !hidePrintStatus.includes(current.status), + }) + }) + + return output + } + + private groupSmallEntries( + entries: ServerHistoryStateAllPrintStatusEntry[], + threshold: number + ): ServerHistoryStateAllPrintStatusEntry[] { + const totalCount = entries.reduce((acc, cur) => acc + cur.value, 0) + const otherLimit = totalCount * threshold + const others = entries.filter((entry) => entry.value < otherLimit) + + if (others.length < 2) return entries + + const value = others.reduce((acc, cur) => acc + cur.value, 0) + const remaining = entries.filter((entry) => entry.value >= otherLimit) + const displayName = i18n.t(`History.StatusValues.Others`).toString() + ` (${others.length})` + + remaining.push({ + name: displayName, + displayName, + value, + valueFilament: 0, + valueTime: 0, + itemStyle: { + opacity: 0.9, + color: '#616161', + borderColor: '#1E1E1E', + borderWidth: 2, + borderRadius: 3, + }, + showInTable: true, + }) + + return remaining + } + + get printStatusArray() { + const countSelected = this.$store.getters['server/history/getSelectedJobs'].length + const orgArray = countSelected ? this.selectedPrintStatusChartData : this.allPrintStatusChartData + + return orgArray.map((status) => ({ + ...status, + name: status.displayName, + value: + this.valueName === 'filament' + ? status.valueFilament + : this.valueName === 'time' + ? status.valueTime + : status.value, + })) + } + + get groupedPrintStatusArray() { + return this.groupSmallEntries(this.printStatusArray, 0.05) + } +} diff --git a/src/components/panels/HistoryListPanel.vue b/src/components/panels/HistoryListPanel.vue index f25ac602f..eca41eb34 100644 --- a/src/components/panels/HistoryListPanel.vue +++ b/src/components/panels/HistoryListPanel.vue @@ -89,11 +89,8 @@ @change="showPrintJobs = !showPrintJobs" /> -