123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- 'use strict';
- const moment = require('moment');
- const utils = require('../util/utils');
- module.exports = {
- initV: 0,
- initC: 0,
- kv: {
- 0: {
- unit: 'days', label: (moment, format) => moment.format(format || 'YYYY年MM月DD日'),
- test: moment => { return { year: moment.year(), month: moment.month() + 1, day: moment.date() }; },
- },
- 1: {
- unit: 'month', label: (moment, format) => moment.format(format || 'YYYY年MM月'),
- test: moment => { return { year: moment.year(), month: moment.month() + 1 }; },
- },
- 2: {
- unit: 'year', label: (moment, format) => moment.format(format || 'YYYY年'),
- test: moment => { return { year: moment.year() }; },
- },
- },
- getInitArr(kv, cond) {
- return Object.keys(kv).map(item => {
- return { count: this.initC, value: this.initV, key: item, label: kv[item], ...cond };
- });
- },
- getKeyArr(data, key) {
- return data.map(item => {
- let value = this.initV;
- let count = this.initC;
- if (Array.isArray(item.value)) {
- const obj = item.value.find(item => item._id == key);
- value = obj ? obj.value : this.initV;
- count = obj ? obj.count : this.initC;
- }
- return { label: item.label, value, count };
- });
- },
- commonMergeData(startTime, endTime, type, data, config) {
- let array = this.range2(startTime, endTime, type, config);
- if (data) {
- const arr = data.map(item => {
- return this.getMomentObj(type, item, config);
- }).concat(array);
- const res = new Map();
- array = arr.filter(a => !res.has(a.label) && res.set(a.label, 1))
- .sort((a, b) => parseInt(a.time) - parseInt(b.time));
- }
- return array;
- },
- commonMergeData1(startTime, endTime, type, data, config) {
- let array = this.range2(startTime, endTime, type, config);
- if (data) {
- const arr = data.map(item => {
- return this.getMomentObj(type, { ...item, count: item.interface_exception_count }, config);
- }).concat(array);
- const res = new Map();
- array = arr.filter(a => !res.has(a.label) && res.set(a.label, 1))
- .sort((a, b) => parseInt(a.time) - parseInt(b.time));
- }
- return array;
- },
- commonMergeData2(startTime, endTime, type, data, config) {
- let array = this.range2(startTime, endTime, type, config);
- if (data) {
- const arr = data.map(item => {
- return this.getMomentObj(type, { ...item, count: item.sys_count }, config);
- }).concat(array);
- const res = new Map();
- array = arr.filter(a => !res.has(a.label) && res.set(a.label, 1))
- .sort((a, b) => parseInt(a.time) - parseInt(b.time));
- }
- return array;
- },
- getMomentObj(type, item, config) { // config k可以是0,1,2对应的formatter或者count 对应数值的字段
- let momentObj;
- let year;
- let month;
- let day;
- switch (type) {
- case 0:
- year = item._id.year;
- month = item._id.month;
- day = item._id.day;
- break;
- case 1:
- year = item._id.year;
- month = item._id.month;
- break;
- case 2:
- year = item._id.year;
- break;
- }
- momentObj = moment().set('year', year || 0).set('month', month ? month - 1 : 0)
- .set('date', day || 1)
- .set('hour', 0)
- .set('minute', 0)
- .set('second', 0)
- .set('millisecond', 0);
- console.log(momentObj, 'momentObj');
- let formatter;
- let countStr;
- const valueObj = {};
- let count = item.count;
- if (config) {
- formatter = config[type];
- countStr = config.count;
- if (config.value) {
- config.value.forEach(str => {
- valueObj[str] = item[str].toString().indexOf('.') != -1 ? parseFloat(item[str].toFixed(2)) : item[str];
- });
- }
- }
- if (countStr) {
- if (utils.isFunction(countStr)) {
- count = countStr(item);
- } else {
- count = item[countStr];
- }
- }
- console.log(count, '我是count');
- return {
- label: this.kv[type].label(momentObj, formatter),
- value: count ? (count.toString().indexOf('.') != -1 ? parseFloat(count.toFixed(2)) : count) : count,
- ...valueObj,
- time: momentObj.valueOf(),
- };
- },
- range2(s1, s2, type, config) {
- if (s1 == '' || s2 == '') {
- return [];
- }
- const arr = [];
- const start = moment(s1);
- const end = moment(s2);
- console.log("start",start);
- console.log("end",end);
- let isLoop = start.isBefore(end);
- while (isLoop) {
- // if (arr.length == 0){
- // end = end.add(1, this.kv[type].unit);
- // }
- let formatter;
- const iV = this.initV;
- const valueObj = {};
- if (config) {
- formatter = config[type];
- if (config.value) {
- config.value.forEach(item => {
- valueObj[item] = iV;
- });
- }
- }
- arr.push({
- label: this.kv[type].label(start, formatter), value: iV,
- ...valueObj, time: start.valueOf(),
- });
- start.add(1, this.kv[type].unit);
- if (!start.isBefore(end, this.kv[type].unit)) {
- isLoop = false;
- }
- }
- return arr;
- },
- rangeTest(s1, s2, type, config) {
- if (s1 == '' || s2 == '') {
- return [];
- }
- const arr = [];
- const start = moment(s1);
- const end = moment(s2);
- console.log("start",start);
- console.log("end",end);
- let isLoop = start.isBefore(end);
- while (isLoop) {
- // if (arr.length == 0){
- // end = end.add(1, this.kv[type].unit);
- // }
- let formatter;
- const valueObj = {};
- if (config) {
- formatter = config[type];
- if (config.value) {
- config.value.forEach(item => {
- valueObj[item] = utils.randomNumC();
- });
- }
- }
- arr.push({
- _id: this.kv[type].test(start, formatter), count: utils.randomNumC(),
- ...valueObj, time: start.valueOf(),
- });
- start.add(1, this.kv[type].unit);
- if (!start.isBefore(end, this.kv[type].unit)) {
- isLoop = false;
- }
- }
- return arr;
- },
- rangeTestAdd(s1, s2, type, config) {
- if (s1 == '' || s2 == '') {
- return [];
- }
- const arr = [];
- const start = moment(s1);
- const end = moment(s2);
- let isLoop = start.isBefore(end);
- let num = 0;
- while (isLoop) {
- // if (arr.length == 0){
- // end = end.add(1, this.kv[type].unit);
- // }
- let formatter;
- const valueObj = {};
- num += utils.randomNumC();
- if (config) {
- formatter = config[type];
- if (config.value) {
- config.value.forEach(item => {
- valueObj[item] = num;
- num += utils.randomNumC();
- });
- }
- }
- arr.push({
- _id: this.kv[type].test(start, formatter), count: num,
- ...valueObj, time: start.valueOf(),
- });
- start.add(1, this.kv[type].unit);
- if (!start.isBefore(end, this.kv[type].unit)) {
- isLoop = false;
- }
- }
- return arr;
- },
- rangeTest2(s1, s2, type, kv) {
- if (s1 == '' || s2 == '') {
- return [];
- }
- const arr = [];
- const start = moment(s1);
- const end = moment(s2);
- let isLoop = start.isBefore(end);
- while (isLoop) {
- // if (arr.length == 0){
- // end = end.add(1, this.kv[type].unit);
- // }
- arr.push({
- _id: this.kv[type].test(start), data: Object.keys(kv).map(item => {
- return { count: utils.randomNumC(), _id: item };
- }),
- time: start.valueOf(),
- });
- start.add(1, this.kv[type].unit);
- if (!start.isBefore(end, this.kv[type].unit)) {
- isLoop = false;
- }
- }
- return arr;
- },
- rangeTest3(s1, s2, type, kv) {
- if (s1 == '' || s2 == '') {
- return [];
- }
- const arr = [];
- const start = moment(s1);
- const end = moment(s2);
- let isLoop = start.isBefore(end);
- while (isLoop) {
- // if (arr.length == 0){
- // end = end.add(1, this.kv[type].unit);
- // }
- arr.push({
- _id: this.kv[type].test(start), data: Object.keys(kv).map(item => {
- return { count: utils.randomNumC(), dsmCount: utils.randomNumC(), _id: item };
- }),
- time: start.valueOf(),
- });
- start.add(1, this.kv[type].unit);
- if (!start.isBefore(end, this.kv[type].unit)) {
- isLoop = false;
- }
- }
- return arr;
- },
- getQuery(s1, s2, type) {
- if (s1 == '' || s2 == '') {
- return {};
- }
- const startTime = moment(s1).valueOf();
- const endTime = moment(s2).add(1, this.kv[type].unit).valueOf();
- return { startTime, endTime, type };
- },
- getDefaultQuery(type) {
- let startTime = 0;
- let endTime = 0;
- switch (type) {
- case 0:
- // eslint-disable-next-line no-case-declarations
- const s1 = moment().subtract(1, 'day').set('date', 1)
- .set('hour', 0)
- .set('minute', 0)
- .set('second', 0)
- .set('millisecond', 0);
- startTime = s1.valueOf();
- // eslint-disable-next-line no-case-declarations
- const e1 = s1.add(1, 'month');
- endTime = e1.valueOf();
- break;
- case 1:
- // eslint-disable-next-line no-case-declarations
- const s2 = moment().subtract(1, 'day').set('month', 0)
- .set('date', 1)
- .set('hour', 0)
- .set('minute', 0)
- .set('second', 0)
- .set('millisecond', 0);
- startTime = s2.valueOf();
- // eslint-disable-next-line no-case-declarations
- const e2 = s2.add(1, 'year');
- endTime = e2.valueOf();
- break;
- case 1.1:
- // eslint-disable-next-line no-case-declarations
- const s21 = moment().subtract(1, 'day').set('month', 0)
- .set('date', 1)
- .set('hour', 0)
- .set('minute', 0)
- .set('second', 0)
- .set('millisecond', 0);
- startTime = s21.valueOf();
- // eslint-disable-next-line no-case-declarations
- const e21 = s21.add(1, 'month');
- endTime = e21.valueOf();
- type = 1;
- break;
- case 1.5:
- // eslint-disable-next-line no-case-declarations
- const s22 = moment().subtract(1, 'day').set('month', 0)
- .set('date', 1)
- .set('hour', 0)
- .set('minute', 0)
- .set('second', 0)
- .set('millisecond', 0);
- startTime = s22.valueOf();
- // eslint-disable-next-line no-case-declarations
- const e22 = s22.add(3, 'month');
- endTime = e22.valueOf();
- type = 1;
- break;
- case 2:
- // eslint-disable-next-line no-case-declarations
- const s3 = moment().subtract(1, 'day').set('month', 0)
- .set('date', 1)
- .set('hour', 0)
- .set('minute', 0)
- .set('second', 0)
- .set('millisecond', 0);
- endTime = s3.add(1, 'year').valueOf();
- // eslint-disable-next-line no-case-declarations
- const e3 = s3.subtract(3, 'year');
- startTime = e3.valueOf();
- break;
- }
- return { startTime, endTime, type };
- },
- };
|