Browse Source

feat:删除timer工具类

dev
zhaobing’ 1 year ago
parent
commit
4b7fe0b7bc
  1. 10
      api/app/lib/controllers/analysis/network.js
  2. 139
      api/app/lib/utils/timer.js
  3. 4
      api/config.js

10
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;

139
api/app/lib/utils/timer.js

@ -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;
}
};

4
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: {

Loading…
Cancel
Save