From a3b500cad105ebfdbb8b92686c3227319d1840a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?zhaobing=E2=80=99?= Date: Wed, 6 Dec 2023 15:14:07 +0800 Subject: [PATCH] feat:fix bugs --- api/app/lib/utils/timer.js | 139 ++++++++++++++++++ api/config.js | 5 + .../sections/network/containers/tableShow.js | 91 +++++++----- 3 files changed, 195 insertions(+), 40 deletions(-) create mode 100644 api/app/lib/utils/timer.js diff --git a/api/app/lib/utils/timer.js b/api/app/lib/utils/timer.js new file mode 100644 index 0000000..84de5ca --- /dev/null +++ b/api/app/lib/utils/timer.js @@ -0,0 +1,139 @@ + +'use strict'; + +const moment = require('moment'); + +let momentToDateTimeString = function (momentDT, format) { + let utcFormat = 'YYYY-MM-DDTHH:mm:ss.SSSZ'; + let formatString = format || utcFormat; + let datetimeString = formatString == utcFormat ? momentDT.toISOString() : momentDT.format(formatString); + return datetimeString; +}; + +let getRangeByPeriod = function (period, format) { + let formatString = format || 'YYYY-MM-DD HH:mm:ss.SSS'; + try { + let range = null, start = null, end = null, momentDT = null; + let now = momentToDateTimeString(moment(), formatString); + switch (period) { + case 'LAST_MONTH': + momentDT = moment().subtract(1, 'months').startOf('month'); + start = momentToDateTimeString(momentDT, formatString); + momentDT = moment().startOf('month'); + end = momentToDateTimeString(momentDT, formatString); + range = { $gte: start, $lt: end }; + break; + case 'THIS_MONTH': + momentDT = moment().startOf('month'); + start = momentToDateTimeString(momentDT, formatString); + range = { $between: [start, now] }; + break; + case 'LAST_WEEK': + momentDT = moment().subtract(1, 'weeks').startOf('isoWeek'); + start = momentToDateTimeString(momentDT, formatString); + momentDT = moment().startOf('isoWeek'); + end = momentToDateTimeString(momentDT, formatString); + range = { $gte: start, $lt: end }; + break; + case 'THIS_WEEK': + momentDT = moment().startOf('isoWeek'); + start = momentToDateTimeString(momentDT, formatString); + range = { $between: [start, now] }; + break; + case 'LAST_DAY': + momentDT = moment().subtract(1, 'days').startOf('day'); + start = momentToDateTimeString(momentDT, formatString); + momentDT = moment().startOf('day'); + end = momentToDateTimeString(momentDT, formatString); + range = { $gte: start, $lt: end }; + break; + case 'THIS_DAY': + momentDT = moment().startOf('day'); + start = momentToDateTimeString(momentDT, formatString); + range = { $between: [start, now] }; + break; + default: + break; + }; + return range; + } catch (err) { + throw err; + } +}; + +let getRange = function (operate, number, period, format, upToNow = false, inquery = true) { + if (!['add', 'subtract'].includes(operate) || !['year', 'month', 'isoWeek', 'week', 'day'].includes(period)) return null; + + let formatString = format || 'YYYY-MM-DD HH:mm:ss.SSS'; + try { + let range = null, start = null, end = null + let periodX = period == 'isoWeek' ? 'weeks' : `${period}s`; + let now = moment().format(formatString); + switch (operate) { + case 'subtract': + start = moment().subtract(number, periodX).startOf(period).format(formatString); + if (upToNow) { + range = inquery ? { $between: [start, now] } : { start, end: now }; + } else { + end = inquery + ? moment().subtract(number - 1, periodX).startOf(period).format(formatString) + : moment().subtract(number, periodX).endOf(period).format(formatString); + range = inquery ? { $gte: start, $lt: end } : { start, end }; + } + break; + case 'add': + start = moment().add(number, periodX).startOf(period).format(formatString); + if (upToNow) { + range = inquery ? { $between: [start, now] } : { start, end: now }; + } else { + end = inquery + ? moment().add(number + 1, periodX).startOf(period).format(formatString) + : moment().add(number, periodX).endOf(period).format(formatString); + range = inquery ? { $gte: start, $lt: end } : { start, end }; + } + break; + default: + start = moment().startOf('day').format(formatString); + range = inquery ? { $between: [start, now] } : { start, end: now }; + break; + }; + return range; + } catch (err) { + throw err; + } +}; + +/** + * 将datetime转换为UTC时间格式 + * @param {datetimeString || Moment} datetime 时间字符串或者Moment对象 + * @returns {string} "YYYY-MM-DDTHH:mm:ss.SSSZ"格式的时间字符串 + */ +let toUTCString = function (datetime) { + let utcString = moment(datetime).toISOString(); + return utcString; +}; + +/** + * 将datetime转换为中国标准时间格式,时间默认精确到秒 + * @param {datetimeString || Moment} datetime 时间字符串或者Moment对象 + * @returns {string} "YYYY-MM-DD HH:mm:ss"格式的时间字符串 + */ +let toCSTString = function (datetime, format) { + let formatString = format || 'YYYY-MM-DD HH:mm:ss'; + let cstString = moment(datetime).format(formatString); + return cstString; +}; + +module.exports = { + entry: (app, router, opts) => { + const timer = { + getRangeByPeriod, + getRange, + toUTCString, + toCSTString + }; + + app.fs = app.fs || {}; + app.fs.timer = timer; + } +}; diff --git a/api/config.js b/api/config.js index 7bfb324..228a93d 100644 --- a/api/config.js +++ b/api/config.js @@ -198,6 +198,10 @@ const product = { port: flags.port || 8080, staticDirs: ['static'], mws: [ + { + entry: require('./utils/timer').entry, + opts: {} + }, { entry: require('@fs/attachment').entry, opts: { @@ -219,6 +223,7 @@ const product = { host: FS_CAMUNDA_HOST, root: FS_CAMUNDA_ROOT }, + exclude: [ // "*", { p: '/attachments/:p', o: 'POST' }, diff --git a/web-network/client/src/sections/network/containers/tableShow.js b/web-network/client/src/sections/network/containers/tableShow.js index 9061532..c8f9242 100644 --- a/web-network/client/src/sections/network/containers/tableShow.js +++ b/web-network/client/src/sections/network/containers/tableShow.js @@ -20,7 +20,7 @@ const Network = props => { const [searchName, setSearchName] = useState('') const [typeList, setTypeList] = useState([]) const [query, setQuery] = useState({ limit: 10, page: 0 }) //页码信息 - + const [deviceId,setDeviceId] = useState([]) const DeviceTypes = { 'DTU': 'DTU', 'gateway': '网关', @@ -91,8 +91,8 @@ const Network = props => { sensorsId.push(id) } } + setDeviceId(deviceIds) dispatch(analysis.getDevicesAlarms(deviceIds, { limit: 5 }, token)) - dispatch(analysis.findDevicesCardStatus({ deviceIds: deviceIds }, token)) setSensorsDataItems(sensorsDataItems) setSensorId(sensorsId) setDeviceData(da) @@ -149,44 +149,55 @@ const Network = props => { } }, [sensorId]) useEffect(() => { - if (deviceData && deviceData.length) { - const dataD = deviceData?.map(p => { - const objRslt = tableData?.find(q => q.sensorId == p.sensorId) - return { - sensorId: objRslt ? objRslt.sensorId : p.sensorId, - sensorName: objRslt ? objRslt.sensorName : p.sensorName, - collectTime: objRslt ? objRslt.collectTime : p.collectTime, - data: objRslt ? objRslt.data : p.data, - deviceType: DeviceTypes[objRslt ? objRslt.deviceType : p.deviceType], - iotCardStatus: - devicesCardStatusList && devicesCardStatusList.length - ? devicesCardStatusList.find(v => v.deviceId == p.sensorId).status === 0 - ? '正常' - : devicesCardStatusList.find(v => v.deviceId == p.sensorId).status === 1 - ? '未激活' - : '停机' - : '--', - status: - deviceListAlarms && deviceListAlarms.length - ? deviceListAlarms?.find(v => v.deviceId == p.sensorId) - ? '异常' - : '正常' - : '--', - option: objRslt ? objRslt.option : p.option, - } - }) - const typeList = dataD.reduce((p, c) => { - let isExist = p.some(q => q.label === c.deviceType) - if (!isExist) { - p.push({ label: c.deviceType, value: c.sensorId }) - } - return p - }, []) - setTypeList(typeList) - setLastData(dataD) - setLastDataCopy(dataD) - } - }, [deviceData]) + if(deviceId&&deviceId.length&&tableData&&tableData.length){ + dispatch(analysis.findDevicesCardStatus({ deviceIds: deviceId }, token)) + .then(res=>{ + if(res.success){ + if (deviceData && deviceData.length&&tableData&&tableData.length) { + const dataD = deviceData?.map(p => { + const objRslt = tableData?.find(q => q.sensorId == p.sensorId) + return { + sensorId: objRslt ? objRslt.sensorId : p.sensorId, + sensorName: objRslt ? objRslt.sensorName : p.sensorName, + collectTime: objRslt ? objRslt.collectTime : p.collectTime, + data: objRslt ? objRslt.data : p.data, + deviceType: DeviceTypes[objRslt ? objRslt.deviceType : p.deviceType], + iotCardStatus: + res.payload.data && res.payload.data.length + ? res.payload.data.find(v => v.deviceId == p.sensorId)?.status === 0 + ? '正常' + : res.payload.data.find(v => v.deviceId == p.sensorId)?.status === 1 + ? '未激活' + : '停机' + : '--', + status: + deviceListAlarms && deviceListAlarms.length + ? deviceListAlarms?.find(v => v.deviceId == p.sensorId) + ? '异常' + : '正常' + : '--', + option: objRslt ? objRslt.option : p.option, + } + }) + const typeList = dataD.reduce((p, c) => { + let isExist = p.some(q => q.label === c.deviceType) + if (!isExist) { + p.push({ label: c.deviceType, value: c.sensorId }) + } + return p + }, []) + setTypeList(typeList) + setLastData(dataD) + setLastDataCopy(dataD) + } + } + + }) + } + + + + }, [deviceData,tableData,deviceId]) // const lastDataCopy=useMemo(()=>{ // return lastData // },[thingId])