1
0
mirror of https://github.com/misskey-dev/misskey synced 2025-01-02 16:03:12 +09:00
misskey/src/server/api/endpoints/aggregation/users.ts

56 lines
1.3 KiB
TypeScript
Raw Normal View History

2017-08-12 15:17:03 +09:00
import $ from 'cafy';
2018-03-29 20:32:18 +09:00
import User from '../../../../models/user';
2017-08-12 15:17:03 +09:00
/**
* Aggregate users
*/
2018-07-06 02:58:29 +09:00
export default (params: any) => new Promise(async (res, rej) => {
2017-08-12 15:17:03 +09:00
// Get 'limit' parameter
2018-07-05 23:36:07 +09:00
const [limit = 365, limitErr] = $.num.optional.range(1, 365).get(params.limit);
2017-08-12 15:17:03 +09:00
if (limitErr) return rej('invalid limit param');
const users = await User
.find({}, {
2018-02-04 14:52:33 +09:00
sort: {
_id: -1
},
fields: {
_id: false,
2018-03-29 14:48:47 +09:00
createdAt: true,
deletedAt: true
2018-02-04 14:52:33 +09:00
}
2017-08-12 15:17:03 +09:00
});
const graph = [];
for (let i = 0; i < limit; i++) {
let dayStart = new Date(new Date().setDate(new Date().getDate() - i));
dayStart = new Date(dayStart.setMilliseconds(0));
dayStart = new Date(dayStart.setSeconds(0));
dayStart = new Date(dayStart.setMinutes(0));
dayStart = new Date(dayStart.setHours(0));
let dayEnd = new Date(new Date().setDate(new Date().getDate() - i));
dayEnd = new Date(dayEnd.setMilliseconds(999));
dayEnd = new Date(dayEnd.setSeconds(59));
dayEnd = new Date(dayEnd.setMinutes(59));
dayEnd = new Date(dayEnd.setHours(23));
2017-08-12 15:17:03 +09:00
// day = day.getTime();
const total = users.filter(u =>
2018-03-29 14:48:47 +09:00
u.createdAt < dayEnd && (u.deletedAt == null || u.deletedAt > dayEnd)
).length;
const created = users.filter(u =>
2018-03-29 14:48:47 +09:00
u.createdAt < dayEnd && u.createdAt > dayStart
2017-08-12 15:17:03 +09:00
).length;
graph.push({
total: total,
created: created
2017-08-12 15:17:03 +09:00
});
}
res(graph);
});