2018-08-24 07:23:04 +09:00
|
|
|
import Stats, { IStats } from '../../../models/stats';
|
2018-08-19 00:27:23 +09:00
|
|
|
|
|
|
|
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
|
|
|
|
|
2018-08-25 08:35:41 +09:00
|
|
|
function migrateStats(stats: IStats[]) {
|
|
|
|
stats.forEach(stat => {
|
|
|
|
const isOldData = stat.users.local.inc == null;
|
|
|
|
|
|
|
|
if (!isOldData) return;
|
|
|
|
|
|
|
|
stat.users.local.inc = (stat as any).users.local.diff;
|
|
|
|
stat.users.local.dec = 0;
|
|
|
|
stat.users.remote.inc = (stat as any).users.remote.diff;
|
|
|
|
stat.users.remote.dec = 0;
|
|
|
|
stat.notes.local.inc = (stat as any).notes.local.diff;
|
|
|
|
stat.notes.local.dec = 0;
|
|
|
|
stat.notes.remote.inc = (stat as any).notes.remote.diff;
|
|
|
|
stat.notes.remote.dec = 0;
|
|
|
|
stat.drive.local.incCount = (stat as any).drive.local.diffCount;
|
|
|
|
stat.drive.local.decCount = 0;
|
|
|
|
stat.drive.local.incSize = (stat as any).drive.local.diffSize;
|
|
|
|
stat.drive.local.decSize = 0;
|
|
|
|
stat.drive.remote.incCount = (stat as any).drive.remote.diffCount;
|
|
|
|
stat.drive.remote.decCount = 0;
|
|
|
|
stat.drive.remote.incSize = (stat as any).drive.remote.diffSize;
|
|
|
|
stat.drive.remote.decSize = 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-19 00:27:23 +09:00
|
|
|
export const meta = {
|
|
|
|
};
|
|
|
|
|
|
|
|
export default (params: any) => new Promise(async (res, rej) => {
|
2018-08-24 14:55:58 +09:00
|
|
|
const daysRange = 30;
|
|
|
|
const hoursRange = 30;
|
2018-08-23 16:36:23 +09:00
|
|
|
|
2018-08-19 00:27:23 +09:00
|
|
|
const now = new Date();
|
|
|
|
const y = now.getFullYear();
|
|
|
|
const m = now.getMonth();
|
|
|
|
const d = now.getDate();
|
2018-08-23 16:36:23 +09:00
|
|
|
const h = now.getHours();
|
2018-08-19 00:27:23 +09:00
|
|
|
|
2018-08-23 16:36:23 +09:00
|
|
|
const [statsPerDay, statsPerHour] = await Promise.all([
|
|
|
|
Stats.find({
|
|
|
|
span: 'day',
|
|
|
|
date: {
|
|
|
|
$gt: new Date(y, m, d - daysRange)
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
sort: {
|
|
|
|
date: -1
|
|
|
|
},
|
|
|
|
fields: {
|
|
|
|
_id: 0
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
Stats.find({
|
|
|
|
span: 'hour',
|
|
|
|
date: {
|
|
|
|
$gt: new Date(y, m, d, h - hoursRange)
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
sort: {
|
|
|
|
date: -1
|
|
|
|
},
|
|
|
|
fields: {
|
|
|
|
_id: 0
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
]);
|
2018-08-19 00:27:23 +09:00
|
|
|
|
2018-08-25 08:35:41 +09:00
|
|
|
// 後方互換性のため
|
|
|
|
migrateStats(statsPerDay);
|
|
|
|
migrateStats(statsPerHour);
|
|
|
|
|
2018-08-23 16:36:23 +09:00
|
|
|
const format = (src: IStats[], span: 'day' | 'hour') => {
|
|
|
|
const chart: Array<Omit<Omit<IStats, '_id'>, 'span'>> = [];
|
2018-08-19 00:27:23 +09:00
|
|
|
|
2018-08-23 16:36:23 +09:00
|
|
|
const range =
|
|
|
|
span == 'day' ? daysRange :
|
|
|
|
span == 'hour' ? hoursRange :
|
|
|
|
null;
|
2018-08-19 00:27:23 +09:00
|
|
|
|
2018-08-23 16:36:23 +09:00
|
|
|
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) :
|
|
|
|
null;
|
2018-08-19 00:27:23 +09:00
|
|
|
|
2018-08-23 16:36:23 +09:00
|
|
|
const stat = src.find(s => s.date.getTime() == current.getTime());
|
|
|
|
|
|
|
|
if (stat) {
|
|
|
|
chart.unshift(stat);
|
|
|
|
} else { // 隙間埋め
|
|
|
|
const mostRecent = src.find(s => s.date.getTime() < current.getTime());
|
|
|
|
if (mostRecent) {
|
2018-08-24 14:55:58 +09:00
|
|
|
chart.unshift({
|
|
|
|
date: current,
|
|
|
|
users: {
|
|
|
|
local: {
|
|
|
|
total: mostRecent.users.local.total,
|
2018-08-25 08:35:41 +09:00
|
|
|
inc: 0,
|
|
|
|
dec: 0
|
2018-08-24 14:55:58 +09:00
|
|
|
},
|
|
|
|
remote: {
|
|
|
|
total: mostRecent.users.remote.total,
|
2018-08-25 08:35:41 +09:00
|
|
|
inc: 0,
|
|
|
|
dec: 0
|
2018-08-24 14:55:58 +09:00
|
|
|
}
|
|
|
|
},
|
|
|
|
notes: {
|
|
|
|
local: {
|
|
|
|
total: mostRecent.notes.local.total,
|
2018-08-25 08:35:41 +09:00
|
|
|
inc: 0,
|
|
|
|
dec: 0,
|
2018-08-24 14:55:58 +09:00
|
|
|
diffs: {
|
|
|
|
normal: 0,
|
|
|
|
reply: 0,
|
|
|
|
renote: 0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
remote: {
|
|
|
|
total: mostRecent.notes.remote.total,
|
2018-08-25 08:35:41 +09:00
|
|
|
inc: 0,
|
|
|
|
dec: 0,
|
2018-08-24 14:55:58 +09:00
|
|
|
diffs: {
|
|
|
|
normal: 0,
|
|
|
|
reply: 0,
|
|
|
|
renote: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
drive: {
|
|
|
|
local: {
|
|
|
|
totalCount: mostRecent.drive.local.totalCount,
|
|
|
|
totalSize: mostRecent.drive.local.totalSize,
|
2018-08-25 08:35:41 +09:00
|
|
|
incCount: 0,
|
|
|
|
incSize: 0,
|
|
|
|
decCount: 0,
|
|
|
|
decSize: 0
|
2018-08-24 14:55:58 +09:00
|
|
|
},
|
|
|
|
remote: {
|
|
|
|
totalCount: mostRecent.drive.remote.totalCount,
|
|
|
|
totalSize: mostRecent.drive.remote.totalSize,
|
2018-08-25 08:35:41 +09:00
|
|
|
incCount: 0,
|
|
|
|
incSize: 0,
|
|
|
|
decCount: 0,
|
|
|
|
decSize: 0
|
2018-08-24 14:55:58 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-08-23 16:36:23 +09:00
|
|
|
} else {
|
|
|
|
chart.unshift({
|
|
|
|
date: current,
|
|
|
|
users: {
|
|
|
|
local: {
|
|
|
|
total: 0,
|
2018-08-25 08:35:41 +09:00
|
|
|
inc: 0,
|
|
|
|
dec: 0
|
2018-08-23 16:36:23 +09:00
|
|
|
},
|
|
|
|
remote: {
|
|
|
|
total: 0,
|
2018-08-25 08:35:41 +09:00
|
|
|
inc: 0,
|
|
|
|
dec: 0
|
2018-08-19 00:27:23 +09:00
|
|
|
}
|
|
|
|
},
|
2018-08-23 16:36:23 +09:00
|
|
|
notes: {
|
|
|
|
local: {
|
|
|
|
total: 0,
|
2018-08-25 08:35:41 +09:00
|
|
|
inc: 0,
|
|
|
|
dec: 0,
|
2018-08-23 16:36:23 +09:00
|
|
|
diffs: {
|
|
|
|
normal: 0,
|
|
|
|
reply: 0,
|
|
|
|
renote: 0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
remote: {
|
|
|
|
total: 0,
|
2018-08-25 08:35:41 +09:00
|
|
|
inc: 0,
|
|
|
|
dec: 0,
|
2018-08-23 16:36:23 +09:00
|
|
|
diffs: {
|
|
|
|
normal: 0,
|
|
|
|
reply: 0,
|
|
|
|
renote: 0
|
|
|
|
}
|
2018-08-19 00:27:23 +09:00
|
|
|
}
|
|
|
|
},
|
2018-08-23 16:36:23 +09:00
|
|
|
drive: {
|
|
|
|
local: {
|
|
|
|
totalCount: 0,
|
|
|
|
totalSize: 0,
|
2018-08-25 08:35:41 +09:00
|
|
|
incCount: 0,
|
|
|
|
incSize: 0,
|
|
|
|
decCount: 0,
|
|
|
|
decSize: 0
|
2018-08-23 16:36:23 +09:00
|
|
|
},
|
|
|
|
remote: {
|
|
|
|
totalCount: 0,
|
|
|
|
totalSize: 0,
|
2018-08-25 08:35:41 +09:00
|
|
|
incCount: 0,
|
|
|
|
incSize: 0,
|
|
|
|
decCount: 0,
|
|
|
|
decSize: 0
|
2018-08-23 16:36:23 +09:00
|
|
|
}
|
2018-08-19 00:27:23 +09:00
|
|
|
}
|
2018-08-23 16:36:23 +09:00
|
|
|
});
|
|
|
|
}
|
2018-08-19 00:27:23 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 16:36:23 +09:00
|
|
|
chart.forEach(x => {
|
|
|
|
delete (x as any).span;
|
|
|
|
});
|
2018-08-19 00:42:09 +09:00
|
|
|
|
2018-08-23 16:36:23 +09:00
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
|
|
|
res({
|
|
|
|
perDay: format(statsPerDay, 'day'),
|
|
|
|
perHour: format(statsPerHour, 'hour')
|
|
|
|
});
|
2018-08-19 00:27:23 +09:00
|
|
|
});
|