Better chart generation

This commit is contained in:
syuilo 2018-11-10 23:25:09 +09:00
parent 6167ed4c9f
commit 9c0e990568
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
2 changed files with 33 additions and 16 deletions

View file

@ -155,6 +155,7 @@
"mkdirp": "0.5.1", "mkdirp": "0.5.1",
"mocha": "5.2.0", "mocha": "5.2.0",
"moji": "0.5.1", "moji": "0.5.1",
"moment": "2.22.2",
"mongodb": "3.1.8", "mongodb": "3.1.8",
"monk": "6.0.6", "monk": "6.0.6",
"ms": "2.1.1", "ms": "2.1.1",

View file

@ -2,12 +2,15 @@
* *
*/ */
import * as moment from 'moment';
const nestedProperty = require('nested-property'); const nestedProperty = require('nested-property');
import autobind from 'autobind-decorator'; import autobind from 'autobind-decorator';
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import db from '../db/mongodb'; import db from '../db/mongodb';
import { ICollection } from 'monk'; import { ICollection } from 'monk';
const utc = moment.utc;
export type Obj = { [key: string]: any }; export type Obj = { [key: string]: any };
export type Partial<T> = { export type Partial<T> = {
@ -63,6 +66,19 @@ export default abstract class Chart<T> {
} else { } else {
this.collection.createIndex({ span: -1, date: -1 }, { unique: true }); 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 @autobind
@ -87,12 +103,12 @@ export default abstract class Chart<T> {
@autobind @autobind
private getCurrentDate(): [number, number, number, number] { private getCurrentDate(): [number, number, number, number] {
const now = new Date(); const now = moment().utc();
const y = now.getFullYear(); const y = now.year();
const m = now.getMonth(); const m = now.month();
const d = now.getDate(); const d = now.date();
const h = now.getHours(); const h = now.hour();
return [y, m, d, h]; return [y, m, d, h];
} }
@ -114,15 +130,15 @@ export default abstract class Chart<T> {
const [y, m, d, h] = this.getCurrentDate(); const [y, m, d, h] = this.getCurrentDate();
const current = const current =
span == 'day' ? new Date(y, m, d) : span == 'day' ? utc([y, m, d]) :
span == 'hour' ? new Date(y, m, d, h) : span == 'hour' ? utc([y, m, d, h]) :
null; null;
// 現在(今日または今のHour)のログ // 現在(今日または今のHour)のログ
const currentLog = await this.collection.findOne({ const currentLog = await this.collection.findOne({
group: group, group: group,
span: span, span: span,
date: current date: current.toDate()
}); });
// ログがあればそれを返して終了 // ログがあればそれを返して終了
@ -158,7 +174,7 @@ export default abstract class Chart<T> {
log = await this.collection.insert({ log = await this.collection.insert({
group: group, group: group,
span: span, span: span,
date: current, date: current.toDate(),
data: data data: data
}); });
} catch (e) { } catch (e) {
@ -225,8 +241,8 @@ export default abstract class Chart<T> {
const [y, m, d, h] = this.getCurrentDate(); const [y, m, d, h] = this.getCurrentDate();
const gt = const gt =
span == 'day' ? new Date(y, m, d - range) : span == 'day' ? utc([y, m, d - range]) :
span == 'hour' ? new Date(y, m, d, h - range) : span == 'hour' ? utc([y, m, d, h - range]) :
null; null;
// ログ取得 // ログ取得
@ -234,7 +250,7 @@ export default abstract class Chart<T> {
group: group, group: group,
span: span, span: span,
date: { date: {
$gt: gt $gt: gt.toDate()
} }
}, { }, {
sort: { sort: {
@ -269,17 +285,17 @@ export default abstract class Chart<T> {
// 整形 // 整形
for (let i = (range - 1); i >= 0; i--) { for (let i = (range - 1); i >= 0; i--) {
const current = const current =
span == 'day' ? new Date(y, m, d - i) : span == 'day' ? utc([y, m, d - i]) :
span == 'hour' ? new Date(y, m, d, h - i) : span == 'hour' ? utc([y, m, d, h - i]) :
null; null;
const log = logs.find(l => l.date.getTime() == current.getTime()); const log = logs.find(l => utc(l.date).isSame(current));
if (log) { if (log) {
promisedChart.unshift(Promise.resolve(log.data)); promisedChart.unshift(Promise.resolve(log.data));
} else { } 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)); promisedChart.unshift(this.getTemplate(false, latest ? latest.data : null));
} }
} }