From 4b7fe0b7bc017e91326a622ab35cb6217005e61b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?zhaobing=E2=80=99?= Date: Mon, 18 Dec 2023 10:04:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=88=A0=E9=99=A4timer=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/lib/controllers/analysis/network.js | 10 +- api/app/lib/utils/timer.js | 139 -------------------- api/config.js | 4 - 3 files changed, 4 insertions(+), 149 deletions(-) delete mode 100644 api/app/lib/utils/timer.js diff --git a/api/app/lib/controllers/analysis/network.js b/api/app/lib/controllers/analysis/network.js index a0690bb..700bb4d 100644 --- a/api/app/lib/controllers/analysis/network.js +++ b/api/app/lib/controllers/analysis/network.js @@ -272,7 +272,6 @@ async function findAlarmsDevices (ctx, next) { params.body.query.bool.must[1].terms.state = newState; } let alarms = await client.search(params); - const timer = ctx.app.fs.timer; function filterAlarmMsg (oriMsg) { let msg = []; for (let s of oriMsg) { @@ -280,8 +279,8 @@ async function findAlarmsDevices (ctx, next) { alarmContent: s._source.alarm_content, alarmCount: s._source.alarm_count, deviceId: s._source.source_id, - startTime: timer.toCSTString(s._source.start_time), - endTime: timer.toCSTString(s._source.end_time), + startTime: moment(s._source.start_time).format('YYYY-MM-DD HH:mm:ss'), + endTime: moment(s._source.end_time).format('YYYY-MM-DD HH:mm:ss'), }) } return msg; @@ -395,15 +394,14 @@ async function findAlarmsDevice (ctx, next) { params.body.query.bool.must[1].terms.state = historyState; params.size = 9999; let historyAlarm = await client.search(params); - const timer = ctx.app.fs.timer; function filterAlarmMsg (oriMsg) { let msg = []; for (let s of oriMsg) { msg.push({ alarmContent: s._source.alarm_content, alarmCount: s._source.alarm_count, - startTime: timer.toCSTString(s._source.start_time), - endTime: timer.toCSTString(s._source.end_time), + startTime: moment(s._source.start_time).format('YYYY-MM-DD HH:mm:ss'), + endTime: moment(s._source.end_time).format('YYYY-MM-DD HH:mm:ss'), }) } return msg; diff --git a/api/app/lib/utils/timer.js b/api/app/lib/utils/timer.js deleted file mode 100644 index 84de5ca..0000000 --- a/api/app/lib/utils/timer.js +++ /dev/null @@ -1,139 +0,0 @@ - -'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 ea21e00..c8d5ff0 100644 --- a/api/config.js +++ b/api/config.js @@ -198,10 +198,6 @@ const product = { port: flags.port || 8080, staticDirs: ['static'], mws: [ - { - entry: require('./app/lib/utils/timer').entry, - opts: {} - }, { entry: require('@fs/attachment').entry, opts: {