From 85dbb9e173d18a68e41f13060d8287523849518c Mon Sep 17 00:00:00 2001 From: cnouguier Date: Thu, 20 Jun 2024 00:19:16 +0200 Subject: [PATCH] feat: KDataTable must display number using the default unit (close #882) --- core/client/components/chart/KDataTable.vue | 14 +++++++++---- core/client/units.js | 6 ++++-- core/client/utils/index.js | 4 +++- core/client/utils/utils.data.js | 22 +++++++++++++++++++++ 4 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 core/client/utils/utils.data.js diff --git a/core/client/components/chart/KDataTable.vue b/core/client/components/chart/KDataTable.vue index ff1465829..9ea7837e9 100644 --- a/core/client/components/chart/KDataTable.vue +++ b/core/client/components/chart/KDataTable.vue @@ -30,11 +30,11 @@ import _ from 'lodash' import moment from 'moment' import Papa from 'papaparse' import { ref, watch } from 'vue' -import { downloadAsBlob } from '../../utils' + import { downloadAsBlob, convertTimeSerie } from '../../utils' import { useSchema } from '../../composables' -import { Units } from '../../units' -import { Time } from '../../time' -import { i18n } from '../../i18n' +import { Units } from '../../units.js' +import { Time } from '../../time.js' +import { i18n } from '../../i18n.js' // Props const props = defineProps({ @@ -71,6 +71,7 @@ const height = ref(0) // Used to store template compilers per field const compilers = {} const exportCompilers = {} +let propertiesToConvert = [] // Watch watch(() => props.tables, update) @@ -81,11 +82,14 @@ async function update () { await compile(props.schema) columns.value = [] const invisibleColumns = [] + propertiesToConvert = [] _.forOwn(schema.value.properties, (value, key) => { const type = _.get(value, 'type') // FIXME: allow for custom representation of complex objects if (type === 'object') return const label = _.get(value, 'field.label', _.get(value, 'field.helper', key)) + const convertToDefaultUnit = _.get(value, 'field.defaultUnit', false) + if (convertToDefaultUnit) propertiesToConvert.push(key) const visible = _.get(value, 'field.visible', true) if (!visible) invisibleColumns.push(key) const formatter = _.has(value, 'field.formatter') ? _.get(props.formatters, value.field.formatter) : null @@ -131,6 +135,7 @@ async function update () { rows.value = [] for (const table of props.tables) { const data = await table.data + convertTimeSerie(data, table.variable, propertiesToConvert) rows.value = rows.value.concat(data) } } @@ -143,6 +148,7 @@ async function exportData (options = {}) { for (let i = 0; i < props.tables.length; i++) { const table = props.tables[i] const data = await table.data + await convertTimeSerie(data, table.variable, propertiesToConvert) for (const item of data) { const row = {} _.forOwn(schema.value.properties, (value, key) => { diff --git a/core/client/units.js b/core/client/units.js index 7b1e7e197..27fa0cb5d 100644 --- a/core/client/units.js +++ b/core/client/units.js @@ -322,10 +322,12 @@ export const Units = { // If target unit is not specified will use default unit (if any) for source unit convert (value, sourceUnit, targetUnit) { if (_.isNil(value)) { - logger.warn('[KDK] cannont convert an nil value') + logger.warn('[KDK] cannont convert a nil value') return - } + } if (value === Number.MIN_VALUE || value === Number.MAX_VALUE) return value + // If target unit is same as source unit does nothing + if (targetUnit === sourceUnit) return value // If target unit is not given use default one if (!targetUnit) targetUnit = this.getDefaultUnit(sourceUnit) // Check if the target unit does exist diff --git a/core/client/utils/index.js b/core/client/utils/index.js index 150a27362..811080e8a 100644 --- a/core/client/utils/index.js +++ b/core/client/utils/index.js @@ -6,8 +6,10 @@ import { Notify, Dialog, Loading, exportFile } from 'quasar' import { defineAsyncComponent, markRaw } from 'vue' export * from './utils.account.js' +export * from './utils.actions.js' export * from './utils.colors.js' export * from './utils.content.js' +export * from './utils.data.js' export * from './utils.locale.js' export * from './utils.platform.js' export * from './utils.push.js' @@ -15,7 +17,7 @@ export * from './utils.pwa.js' export * from './utils.shapes.js' export * from './utils.session.js' export * from './utils.time.js' -export * from './utils.actions.js' + Notify.setDefaults({ position: 'bottom-left', diff --git a/core/client/utils/utils.data.js b/core/client/utils/utils.data.js new file mode 100644 index 000000000..6ba911501 --- /dev/null +++ b/core/client/utils/utils.data.js @@ -0,0 +1,22 @@ +import _ from 'lodash' +import { Units } from '../units.js' + +export function convertData (data, valuePaths, sourceUnit, targetUnit) { + if (!Array.isArray(valuePaths)) valuePaths = [valuePaths] + _.forEach(data, document => { + _.forEach(valuePaths, valuePath => { + const value = _.get(document, valuePath) + if (value) _.set(document, valuePath, Units.convert(value, sourceUnit, targetUnit)) + }) + }) +} + +export function convertTimeSerie (data, variable, valuePaths) { + if (!Array.isArray(valuePaths)) valuePaths = [valuePaths] + const unit = variable.unit + if (unit) { + const targetUnit = Units.getDefaultUnit(unit) + convertData(data, valuePaths, unit, targetUnit) + variable.unit = targetUnit + } +} \ No newline at end of file