Better chart generation
This commit is contained in:
parent
3ab2daa6ad
commit
b664dc97a9
2 changed files with 33 additions and 16 deletions
|
@ -155,6 +155,7 @@
|
|||
"mkdirp": "0.5.1",
|
||||
"mocha": "5.2.0",
|
||||
"moji": "0.5.1",
|
||||
"moment": "2.22.2",
|
||||
"mongodb": "3.1.8",
|
||||
"monk": "6.0.6",
|
||||
"ms": "2.1.1",
|
||||
|
|
|
@ -2,12 +2,15 @@
|
|||
* チャートエンジン
|
||||
*/
|
||||
|
||||
import * as moment from 'moment';
|
||||
const nestedProperty = require('nested-property');
|
||||
import autobind from 'autobind-decorator';
|
||||
import * as mongo from 'mongodb';
|
||||
import db from '../db/mongodb';
|
||||
import { ICollection } from 'monk';
|
||||
|
||||
const utc = moment.utc;
|
||||
|
||||
export type Obj = { [key: string]: any };
|
||||
|
||||
export type Partial<T> = {
|
||||
|
@ -63,6 +66,19 @@ export default abstract class Chart<T> {
|
|||
} else {
|
||||
this.collection.createIndex({ span: -1, date: -1 }, { unique: true });
|
||||
}
|
||||
|
||||
//#region 後方互換性のため
|
||||
this.collection.find({ span: 'day' }, { fields: { _id: true, date: true } }).then(logs => {
|
||||
logs.forEach(log => {
|
||||
this.collection.update({ _id: log._id }, { $set: { date: utc(log.date).hour(0).toDate() } });
|
||||
});
|
||||
});
|
||||
this.collection.find({ span: 'hour' }, { fields: { _id: true, date: true } }).then(logs => {
|
||||
logs.forEach(log => {
|
||||
this.collection.update({ _id: log._id }, { $set: { date: utc(log.date).toDate() } });
|
||||
});
|
||||
});
|
||||
//#endregion
|
||||
}
|
||||
|
||||
@autobind
|
||||
|
@ -87,12 +103,12 @@ export default abstract class Chart<T> {
|
|||
|
||||
@autobind
|
||||
private getCurrentDate(): [number, number, number, number] {
|
||||
const now = new Date();
|
||||
const now = moment().utc();
|
||||
|
||||
const y = now.getFullYear();
|
||||
const m = now.getMonth();
|
||||
const d = now.getDate();
|
||||
const h = now.getHours();
|
||||
const y = now.year();
|
||||
const m = now.month();
|
||||
const d = now.date();
|
||||
const h = now.hour();
|
||||
|
||||
return [y, m, d, h];
|
||||
}
|
||||
|
@ -114,15 +130,15 @@ export default abstract class Chart<T> {
|
|||
const [y, m, d, h] = this.getCurrentDate();
|
||||
|
||||
const current =
|
||||
span == 'day' ? new Date(y, m, d) :
|
||||
span == 'hour' ? new Date(y, m, d, h) :
|
||||
span == 'day' ? utc([y, m, d]) :
|
||||
span == 'hour' ? utc([y, m, d, h]) :
|
||||
null;
|
||||
|
||||
// 現在(今日または今のHour)のログ
|
||||
const currentLog = await this.collection.findOne({
|
||||
group: group,
|
||||
span: span,
|
||||
date: current
|
||||
date: current.toDate()
|
||||
});
|
||||
|
||||
// ログがあればそれを返して終了
|
||||
|
@ -158,7 +174,7 @@ export default abstract class Chart<T> {
|
|||
log = await this.collection.insert({
|
||||
group: group,
|
||||
span: span,
|
||||
date: current,
|
||||
date: current.toDate(),
|
||||
data: data
|
||||
});
|
||||
} catch (e) {
|
||||
|
@ -225,8 +241,8 @@ export default abstract class Chart<T> {
|
|||
const [y, m, d, h] = this.getCurrentDate();
|
||||
|
||||
const gt =
|
||||
span == 'day' ? new Date(y, m, d - range) :
|
||||
span == 'hour' ? new Date(y, m, d, h - range) :
|
||||
span == 'day' ? utc([y, m, d - range]) :
|
||||
span == 'hour' ? utc([y, m, d, h - range]) :
|
||||
null;
|
||||
|
||||
// ログ取得
|
||||
|
@ -234,7 +250,7 @@ export default abstract class Chart<T> {
|
|||
group: group,
|
||||
span: span,
|
||||
date: {
|
||||
$gt: gt
|
||||
$gt: gt.toDate()
|
||||
}
|
||||
}, {
|
||||
sort: {
|
||||
|
@ -269,17 +285,17 @@ export default abstract class Chart<T> {
|
|||
// 整形
|
||||
for (let i = (range - 1); i >= 0; i--) {
|
||||
const current =
|
||||
span == 'day' ? new Date(y, m, d - i) :
|
||||
span == 'hour' ? new Date(y, m, d, h - i) :
|
||||
span == 'day' ? utc([y, m, d - i]) :
|
||||
span == 'hour' ? utc([y, m, d, h - i]) :
|
||||
null;
|
||||
|
||||
const log = logs.find(l => l.date.getTime() == current.getTime());
|
||||
const log = logs.find(l => utc(l.date).isSame(current));
|
||||
|
||||
if (log) {
|
||||
promisedChart.unshift(Promise.resolve(log.data));
|
||||
} else {
|
||||
// 隙間埋め
|
||||
const latest = logs.find(l => l.date.getTime() < current.getTime());
|
||||
const latest = logs.find(l => utc(l.date).isBefore(current));
|
||||
promisedChart.unshift(this.getTemplate(false, latest ? latest.data : null));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue