Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(History): add option to show stats in different values #2007

Merged
merged 12 commits into from
Nov 9, 2024
Merged
55 changes: 22 additions & 33 deletions src/components/charts/HistoryAllPrintStatusChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@
:option="chartOptions"
:autoresize="true"
:init-options="{ renderer: 'svg' }"
style="height: 200px; width: 100%"></e-chart>
style="height: 200px; width: 100%" />
</template>

<script lang="ts">
import Component from 'vue-class-component'
import { Mixins, Watch } from 'vue-property-decorator'
import { Mixins, Prop, Ref, Watch } from 'vue-property-decorator'
import BaseMixin from '@/components/mixins/base'
import ThemeMixin from '@/components/mixins/theme'
import HistoryStatsMixin from '@/components/mixins/historyStats'
import VueECharts from 'vue-echarts'
import type { ECharts } from 'echarts/core'
import { ECBasicOption } from 'echarts/types/dist/shared.d'
import { ServerHistoryStateAllPrintStatusEntry } from '@/store/server/history/types'
import { formatPrintTime } from '@/plugins/helpers'

@Component({
components: {},
})
export default class HistoryAllPrintStatusChart extends Mixins(BaseMixin, ThemeMixin) {
declare $refs: {
historyAllPrintStatus: any
}
export default class HistoryAllPrintStatusChart extends Mixins(BaseMixin, ThemeMixin, HistoryStatsMixin) {
@Prop({ type: String, default: 'amount' }) valueName!: 'amount' | 'filament' | 'time'
meteyou marked this conversation as resolved.
Show resolved Hide resolved
@Ref('historyAllPrintStatus') historyAllPrintStatus!: typeof VueECharts

get chartOptions(): ECBasicOption {
return {
Expand All @@ -37,6 +38,19 @@ export default class HistoryAllPrintStatusChart extends Mixins(BaseMixin, ThemeM
tooltip: {
trigger: 'item',
borderWidth: 0,
valueFormatter: (value: number) => {
if (this.valueName === 'filament') {
if (value > 1000) return Math.round(value / 1000).toString() + ' m'

return value.toString() + ' mm'
}

if (this.valueName === 'time') {
return formatPrintTime(value, false)
}

return value.toString()
},
},
series: [
{
Expand All @@ -59,33 +73,8 @@ export default class HistoryAllPrintStatusChart extends Mixins(BaseMixin, ThemeM
}
}

get selectedJobs() {
return this.$store.getters['server/history/getSelectedJobs']
}

get allPrintStatusArray() {
return this.$store.getters['server/history/getAllPrintStatusArray']
}

get selectedPrintStatusArray() {
return this.$store.getters['server/history/getSelectedPrintStatusArray']
}

get printStatusArray() {
const output: ServerHistoryStateAllPrintStatusEntry[] = []
const orgArray = this.selectedJobs.length ? this.selectedPrintStatusArray : this.allPrintStatusArray

orgArray.forEach((status: ServerHistoryStateAllPrintStatusEntry) => {
const tmp = { ...status }
tmp.name = status.displayName
output.push(tmp)
})

return output
}

get chart(): ECharts | null {
return this.$refs.historyAllPrintStatus?.chart ?? null
return this.historyAllPrintStatus?.chart ?? null
}

beforeDestroy() {
Expand Down
43 changes: 11 additions & 32 deletions src/components/charts/HistoryAllPrintStatusTable.vue
Original file line number Diff line number Diff line change
@@ -1,47 +1,26 @@
<template>
<v-simple-table>
<tbody>
<tr v-for="status in printStatusArray" :key="status.name">
<td>{{ status.displayName }}</td>
<td class="text-right">{{ status.value }}</td>
</tr>
<history-all-print-status-table-item
v-for="status in printStatusArray"
:key="status.name"
:item="status"
:value-name="valueName" />
</tbody>
</v-simple-table>
</template>

<script lang="ts">
import Component from 'vue-class-component'
import { Mixins } from 'vue-property-decorator'
import { Mixins, Prop } from 'vue-property-decorator'
import BaseMixin from '@/components/mixins/base'
import { ServerHistoryStateAllPrintStatusEntry } from '@/store/server/history/types'
import HistoryStatsMixin from '@/components/mixins/historyStats'
import HistoryAllPrintStatusTableItem from '@/components/charts/HistoryAllPrintStatusTableItem.vue'

@Component({
components: {},
components: { HistoryAllPrintStatusTableItem },
})
export default class HistoryAllPrintStatusTable extends Mixins(BaseMixin) {
get selectedJobs() {
return this.$store.getters['server/history/getSelectedJobs']
}

get allPrintStatusArray() {
return this.$store.getters['server/history/getAllPrintStatusArrayAll']
}

get selectedPrintStatusArray() {
return this.$store.getters['server/history/getSelectedPrintStatusArray']
}

get printStatusArray() {
const output: ServerHistoryStateAllPrintStatusEntry[] = []
const orgArray = this.selectedJobs.length ? this.selectedPrintStatusArray : this.allPrintStatusArray

orgArray.forEach((status: ServerHistoryStateAllPrintStatusEntry) => {
const tmp = { ...status }
tmp.name = status.displayName
output.push(tmp)
})

return output
}
export default class HistoryAllPrintStatusTable extends Mixins(BaseMixin, HistoryStatsMixin) {
@Prop({ type: String, default: 'amount' }) valueName!: 'amount' | 'filament' | 'time'
}
</script>
36 changes: 36 additions & 0 deletions src/components/charts/HistoryAllPrintStatusTableItem.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<template>
meteyou marked this conversation as resolved.
Show resolved Hide resolved
<tr>
<td>{{ item.displayName }}</td>
<td class="text-right">{{ value }}</td>
</tr>
</template>

<script lang="ts">
import Component from 'vue-class-component'
import { Mixins, Prop } from 'vue-property-decorator'
import BaseMixin from '@/components/mixins/base'
import { ServerHistoryStateAllPrintStatusEntry } from '@/store/server/history/types'
import { formatPrintTime } from '@/plugins/helpers'

@Component({
components: {},
})
export default class HistoryAllPrintStatusTableItem extends Mixins(BaseMixin) {
@Prop({ type: Object }) item!: ServerHistoryStateAllPrintStatusEntry
@Prop({ type: String, default: 'amount' }) valueName!: 'amount' | 'filament' | 'time'

get value() {
if (this.valueName === 'filament') {
meteyou marked this conversation as resolved.
Show resolved Hide resolved
if (this.item.value > 1000) return Math.round(this.item.value / 1000).toString() + ' m'

return this.item.value.toString() + ' mm'
meteyou marked this conversation as resolved.
Show resolved Hide resolved
}

if (this.valueName === 'time') {
return formatPrintTime(this.item.value, false)
}

return this.item.value.toString()
}
}
</script>
160 changes: 160 additions & 0 deletions src/components/mixins/historyStats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import Vue from 'vue'
meteyou marked this conversation as resolved.
Show resolved Hide resolved
import Component from 'vue-class-component'
import { ServerHistoryStateAllPrintStatusEntry, ServerHistoryStateJob } from '@/store/server/history/types'
import i18n from '@/plugins/i18n'

@Component
export default class HistoryStatsMixin extends Vue {
meteyou marked this conversation as resolved.
Show resolved Hide resolved
meteyou marked this conversation as resolved.
Show resolved Hide resolved
get allPrintStatusChartData() {
const output: ServerHistoryStateAllPrintStatusEntry[] = []
const hidePrintStatus = this.$store.state.gui.view.history.hidePrintStatus ?? []
const jobs = this.$store.state.server.history.jobs ?? []

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
}

const displayName = i18n.te(`History.StatusValues.${current.status}`, 'en')
? i18n.t(`History.StatusValues.${current.status}`).toString()
: current.status

const itemStyle = {
opacity: 0.9,
color: '#424242',
borderColor: '#1E1E1E',
borderWidth: 2,
borderRadius: 3,
}

switch (current.status) {
case 'completed':
itemStyle['color'] = '#BDBDBD'
break

case 'in_progress':
itemStyle['color'] = '#EEEEEE'
break

case 'cancelled':
itemStyle['color'] = '#616161'
break
}

output.push({
name: current.status,
displayName,
value: 1,
valueFilament: current.filament_used,
valueTime: current.print_duration,
itemStyle,
showInTable: !hidePrintStatus.includes(current.status),
})
})

return output
}

get selectedPrintStatusChartData() {
const output: ServerHistoryStateAllPrintStatusEntry[] = []
const jobs = this.$store.getters['server/history/getSelectedJobs']
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].valueTime += current.print_duration
output[index].valueFilament += current.filament_used
return
}

const displayName = i18n.te(`History.StatusValues.${current.status}`, 'en').toString()
? i18n.t(`History.StatusValues.${current.status}`).toString()
: current.status
const itemStyle = {
opacity: 0.9,
color: '#424242',
borderColor: '#1E1E1E',
borderWidth: 2,
borderRadius: 3,
}

switch (current.status) {
case 'completed':
itemStyle['color'] = '#BDBDBD'
break

case 'in_progress':
itemStyle['color'] = '#EEEEEE'
break

case 'cancelled':
itemStyle['color'] = '#616161'
break
}

output.push({
name: current.status,
displayName,
value: 1,
valueTime: current.print_duration,
valueFilament: current.filament_used,
itemStyle: itemStyle,
showInTable: !hidePrintStatus.includes(current.status),
})
})

return output
}

get printStatusArray() {
let output: ServerHistoryStateAllPrintStatusEntry[] = []
const countSelected = this.$store.getters['server/history/getSelectedJobs'].length
const orgArray = countSelected ? this.selectedPrintStatusChartData : this.allPrintStatusChartData

orgArray.forEach((status: ServerHistoryStateAllPrintStatusEntry) => {
const tmp = { ...status }
tmp.name = status.displayName

if (this.valueName === 'filament') {
tmp.value = status.valueFilament
} else if (this.valueName === 'time') {
tmp.value = status.valueTime
}

output.push(tmp)
})

// group all entries with less than 5% of the total
const totalCount = output.reduce((acc, cur) => acc + cur.value, 0)
const otherLimit = totalCount * 0.05
const others = output.filter((entry) => entry.value < otherLimit)

// no, or only one entry found
if (others.length < 2) return output

const value = others.reduce((acc, cur) => acc + cur.value, 0)
output = output.filter((entry) => entry.value >= otherLimit)
const displayName = i18n.t(`History.StatusValues.Others`).toString() + ` (${others.length})`
output.push({
name: displayName,
displayName,
value,
itemStyle: {
opacity: 0.9,
color: '#616161',
borderColor: '#1E1E1E',
borderWidth: 2,
borderRadius: 3,
},
showInTable: true,
})

return output
}
}
21 changes: 19 additions & 2 deletions src/components/panels/HistoryStatisticsPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
</v-simple-table>
</v-col>
<v-col class="col-12 col-sm-6 col-md-4">
<history-all-print-status-chart v-if="togglePrintStatus === 'chart'" />
<history-all-print-status-table v-else />
<history-all-print-status-chart v-if="togglePrintStatus === 'chart'" :value-name="toggleValue" />
<history-all-print-status-table v-else :value-name="toggleValue" />
<div class="text-center mb-3">
<v-btn-toggle v-model="togglePrintStatus" small mandatory>
<v-btn small value="chart">{{ $t('History.Chart') }}</v-btn>
Expand All @@ -41,6 +41,13 @@
<span>{{ $t('History.LoadCompleteHistory') }}</span>
</v-tooltip>
</div>
<div class="text-center mb-3">
<v-btn-toggle v-model="toggleValue" small mandatory>
<v-btn v-for="option in toggleValueOptions" :key="option.value" small :value="option.value">
{{ option.text }}
</v-btn>
</v-btn-toggle>
</div>
</v-col>
<v-col class="col-12 col-sm-12 col-md-4">
<history-filament-usage v-if="toggleChart === 'filament_usage'" />
Expand Down Expand Up @@ -76,6 +83,16 @@ export default class HistoryStatisticsPanel extends Mixins(BaseMixin, HistoryMix
mdiDatabaseArrowDownOutline = mdiDatabaseArrowDownOutline
formatPrintTime = formatPrintTime

toggleValue = 'amount'

get toggleValueOptions() {
return [
{ text: this.$t('History.Amount'), value: 'amount' },
{ text: this.$t('History.Filament'), value: 'filament' },
{ text: this.$t('History.Time'), value: 'time' },
]
}

get selectedJobs() {
return this.$store.getters['server/history/getSelectedJobs']
}
Expand Down
Loading
Loading