diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2018-08-18 03:52:24 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2018-08-18 03:52:24 +0900 |
| commit | bc34ac82cf4effe2baba8471315ea4a78dae416a (patch) | |
| tree | ad5785ad45b344b88078b239eaef3ee725ded4ec /src/server | |
| parent | Merge pull request #2297 from syuilo/update-readme (diff) | |
| download | sharkey-bc34ac82cf4effe2baba8471315ea4a78dae416a.tar.gz sharkey-bc34ac82cf4effe2baba8471315ea4a78dae416a.tar.bz2 sharkey-bc34ac82cf4effe2baba8471315ea4a78dae416a.zip | |
Show some charts in control panel
Diffstat (limited to 'src/server')
| -rw-r--r-- | src/server/api/endpoints/aggregation/notes.ts | 110 | ||||
| -rw-r--r-- | src/server/api/endpoints/aggregation/posts.ts | 84 | ||||
| -rw-r--r-- | src/server/api/endpoints/aggregation/users.ts | 95 |
3 files changed, 173 insertions, 116 deletions
diff --git a/src/server/api/endpoints/aggregation/notes.ts b/src/server/api/endpoints/aggregation/notes.ts new file mode 100644 index 0000000000..b745c86631 --- /dev/null +++ b/src/server/api/endpoints/aggregation/notes.ts @@ -0,0 +1,110 @@ +import $ from 'cafy'; +import Note from '../../../../models/note'; + +/** + * Aggregate notes + */ +export default (params: any) => new Promise(async (res, rej) => { + // Get 'limit' parameter + const [limit = 365, limitErr] = $.num.optional.range(1, 365).get(params.limit); + if (limitErr) return rej('invalid limit param'); + + const query = [{ + $project: { + renoteId: '$renoteId', + replyId: '$replyId', + user: '$_user', + createdAt: { $add: ['$createdAt', 9 * 60 * 60 * 1000] } // Convert into JST + } + }, { + $project: { + date: { + year: { $year: '$createdAt' }, + month: { $month: '$createdAt' }, + day: { $dayOfMonth: '$createdAt' } + }, + type: { + $cond: { + if: { $ne: ['$renoteId', null] }, + then: 'renote', + else: { + $cond: { + if: { $ne: ['$replyId', null] }, + then: 'reply', + else: 'note' + } + } + } + }, + origin: { + $cond: { + if: { $eq: ['$user.host', null] }, + then: 'local', + else: 'remote' + } + } + } + }, { + $group: { + _id: { + date: '$date', + type: '$type', + origin: '$origin' + }, + count: { $sum: 1 } + } + }, { + $group: { + _id: '$_id.date', + data: { + $addToSet: { + type: '$_id.type', + origin: '$_id.origin', + count: '$count' + } + } + } + }] as any; + + const datas = await Note.aggregate(query); + + datas.forEach((data: any) => { + data.date = data._id; + delete data._id; + + data.localNotes = (data.data.filter((x: any) => x.type == 'note' && x.origin == 'local')[0] || { count: 0 }).count; + data.localRenotes = (data.data.filter((x: any) => x.type == 'renote' && x.origin == 'local')[0] || { count: 0 }).count; + data.localReplies = (data.data.filter((x: any) => x.type == 'reply' && x.origin == 'local')[0] || { count: 0 }).count; + data.remoteNotes = (data.data.filter((x: any) => x.type == 'note' && x.origin == 'remote')[0] || { count: 0 }).count; + data.remoteRenotes = (data.data.filter((x: any) => x.type == 'renote' && x.origin == 'remote')[0] || { count: 0 }).count; + data.remoteReplies = (data.data.filter((x: any) => x.type == 'reply' && x.origin == 'remote')[0] || { count: 0 }).count; + + delete data.data; + }); + + const graph = []; + + for (let i = 0; i < limit; i++) { + const day = new Date(new Date().setDate(new Date().getDate() - i)); + + const data = datas.filter((d: any) => + d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() + )[0]; + + if (data) { + graph.push(data); + } else { + graph.push({ + date: { year: day.getFullYear(), month: day.getMonth() + 1, day: day.getDate() }, + localNotes: 0, + localRenotes: 0, + localReplies: 0, + remoteNotes: 0, + remoteRenotes: 0, + remoteReplies: 0 + }); + } + } + + res(graph); +}); diff --git a/src/server/api/endpoints/aggregation/posts.ts b/src/server/api/endpoints/aggregation/posts.ts deleted file mode 100644 index 629bb19108..0000000000 --- a/src/server/api/endpoints/aggregation/posts.ts +++ /dev/null @@ -1,84 +0,0 @@ -import $ from 'cafy'; -import Note from '../../../../models/note'; - -/** - * Aggregate notes - */ -export default (params: any) => new Promise(async (res, rej) => { - // Get 'limit' parameter - const [limit = 365, limitErr] = $.num.optional.range(1, 365).get(params.limit); - if (limitErr) return rej('invalid limit param'); - - const datas = await Note - .aggregate([ - { $project: { - renoteId: '$renoteId', - replyId: '$replyId', - createdAt: { $add: ['$createdAt', 9 * 60 * 60 * 1000] } // Convert into JST - }}, - { $project: { - date: { - year: { $year: '$createdAt' }, - month: { $month: '$createdAt' }, - day: { $dayOfMonth: '$createdAt' } - }, - type: { - $cond: { - if: { $ne: ['$renoteId', null] }, - then: 'renote', - else: { - $cond: { - if: { $ne: ['$replyId', null] }, - then: 'reply', - else: 'note' - } - } - } - }} - }, - { $group: { _id: { - date: '$date', - type: '$type' - }, count: { $sum: 1 } } }, - { $group: { - _id: '$_id.date', - data: { $addToSet: { - type: '$_id.type', - count: '$count' - }} - } } - ]); - - datas.forEach((data: any) => { - data.date = data._id; - delete data._id; - - data.notes = (data.data.filter((x: any) => x.type == 'note')[0] || { count: 0 }).count; - data.renotes = (data.data.filter((x: any) => x.type == 'renote')[0] || { count: 0 }).count; - data.replies = (data.data.filter((x: any) => x.type == 'reply')[0] || { count: 0 }).count; - - delete data.data; - }); - - const graph = []; - - for (let i = 0; i < limit; i++) { - const day = new Date(new Date().setDate(new Date().getDate() - i)); - - const data = datas.filter((d: any) => - d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() - )[0]; - - if (data) { - graph.push(data); - } else { - graph.push({ - notes: 0, - renotes: 0, - replies: 0 - }); - } - } - - res(graph); -}); diff --git a/src/server/api/endpoints/aggregation/users.ts b/src/server/api/endpoints/aggregation/users.ts index f1e41cf170..2e397545de 100644 --- a/src/server/api/endpoints/aggregation/users.ts +++ b/src/server/api/endpoints/aggregation/users.ts @@ -9,46 +9,77 @@ export default (params: any) => new Promise(async (res, rej) => { const [limit = 365, limitErr] = $.num.optional.range(1, 365).get(params.limit); if (limitErr) return rej('invalid limit param'); - const users = await User - .find({}, { - sort: { - _id: -1 + const query = [{ + $project: { + host: '$host', + createdAt: { $add: ['$createdAt', 9 * 60 * 60 * 1000] } // Convert into JST + } + }, { + $project: { + date: { + year: { $year: '$createdAt' }, + month: { $month: '$createdAt' }, + day: { $dayOfMonth: '$createdAt' } }, - fields: { - _id: false, - createdAt: true, - deletedAt: true + origin: { + $cond: { + if: { $eq: ['$host', null] }, + then: 'local', + else: 'remote' + } } - }); + } + }, { + $group: { + _id: { + date: '$date', + origin: '$origin' + }, + count: { $sum: 1 } + } + }, { + $group: { + _id: '$_id.date', + data: { + $addToSet: { + type: '$_id.type', + origin: '$_id.origin', + count: '$count' + } + } + } + }] as any; - const graph = []; + const datas = await User.aggregate(query); - 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)); + datas.forEach((data: any) => { + data.date = data._id; + delete data._id; - 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)); - // day = day.getTime(); + data.local = (data.data.filter((x: any) => x.origin == 'local')[0] || { count: 0 }).count; + data.remote = (data.data.filter((x: any) => x.origin == 'remote')[0] || { count: 0 }).count; - const total = users.filter(u => - u.createdAt < dayEnd && (u.deletedAt == null || u.deletedAt > dayEnd) - ).length; + delete data.data; + }); + + const graph = []; + + for (let i = 0; i < limit; i++) { + const day = new Date(new Date().setDate(new Date().getDate() - i)); - const created = users.filter(u => - u.createdAt < dayEnd && u.createdAt > dayStart - ).length; + const data = datas.filter((d: any) => + d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() + )[0]; - graph.push({ - total: total, - created: created - }); + if (data) { + graph.push(data); + } else { + graph.push({ + date: { year: day.getFullYear(), month: day.getMonth() + 1, day: day.getDate() }, + local: 0, + remote: 0 + }); + } } res(graph); |