summaryrefslogtreecommitdiff
path: root/src/api/endpoints
diff options
context:
space:
mode:
authorこぴなたみぽ <Syuilotan@yahoo.co.jp>2017-08-12 20:23:21 +0900
committerGitHub <noreply@github.com>2017-08-12 20:23:21 +0900
commit115c3d668c68af6fca16704ca34b8ab4dd8d624c (patch)
tree7e9e1da8431e4ed6fb4cc47be0e9f19aeb2cb10f /src/api/endpoints
parentchore(package): update @types/chai-http to version 3.0.2 (diff)
parentMerge pull request #685 from syuilo/greenkeeper/@types/chai-4.0.3 (diff)
downloadmisskey-115c3d668c68af6fca16704ca34b8ab4dd8d624c.tar.gz
misskey-115c3d668c68af6fca16704ca34b8ab4dd8d624c.tar.bz2
misskey-115c3d668c68af6fca16704ca34b8ab4dd8d624c.zip
Merge branch 'master' into greenkeeper/@types/chai-http-3.0.2
Diffstat (limited to 'src/api/endpoints')
-rw-r--r--src/api/endpoints/aggregation/posts.ts90
-rw-r--r--src/api/endpoints/aggregation/users.ts58
-rw-r--r--src/api/endpoints/stats.ts48
3 files changed, 196 insertions, 0 deletions
diff --git a/src/api/endpoints/aggregation/posts.ts b/src/api/endpoints/aggregation/posts.ts
new file mode 100644
index 0000000000..48ee225129
--- /dev/null
+++ b/src/api/endpoints/aggregation/posts.ts
@@ -0,0 +1,90 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import Post from '../../models/post';
+
+/**
+ * Aggregate posts
+ *
+ * @param {any} params
+ * @return {Promise<any>}
+ */
+module.exports = params => new Promise(async (res, rej) => {
+ // Get 'limit' parameter
+ const [limit = 365, limitErr] = $(params.limit).optional.number().range(1, 365).$;
+ if (limitErr) return rej('invalid limit param');
+
+ const datas = await Post
+ .aggregate([
+ { $project: {
+ repost_id: '$repost_id',
+ reply_to_id: '$reply_to_id',
+ created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST
+ }},
+ { $project: {
+ date: {
+ year: { $year: '$created_at' },
+ month: { $month: '$created_at' },
+ day: { $dayOfMonth: '$created_at' }
+ },
+ type: {
+ $cond: {
+ if: { $ne: ['$repost_id', null] },
+ then: 'repost',
+ else: {
+ $cond: {
+ if: { $ne: ['$reply_to_id', null] },
+ then: 'reply',
+ else: 'post'
+ }
+ }
+ }
+ }}
+ },
+ { $group: { _id: {
+ date: '$date',
+ type: '$type'
+ }, count: { $sum: 1 } } },
+ { $group: {
+ _id: '$_id.date',
+ data: { $addToSet: {
+ type: '$_id.type',
+ count: '$count'
+ }}
+ } }
+ ]);
+
+ datas.forEach(data => {
+ data.date = data._id;
+ delete data._id;
+
+ data.posts = (data.data.filter(x => x.type == 'post')[0] || { count: 0 }).count;
+ data.reposts = (data.data.filter(x => x.type == 'repost')[0] || { count: 0 }).count;
+ data.replies = (data.data.filter(x => 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 =>
+ 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({
+ posts: 0,
+ reposts: 0,
+ replies: 0
+ });
+ }
+ }
+
+ res(graph);
+});
diff --git a/src/api/endpoints/aggregation/users.ts b/src/api/endpoints/aggregation/users.ts
new file mode 100644
index 0000000000..9eb2d035ec
--- /dev/null
+++ b/src/api/endpoints/aggregation/users.ts
@@ -0,0 +1,58 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import User from '../../models/user';
+
+/**
+ * Aggregate users
+ *
+ * @param {any} params
+ * @return {Promise<any>}
+ */
+module.exports = params => new Promise(async (res, rej) => {
+ // Get 'limit' parameter
+ const [limit = 365, limitErr] = $(params.limit).optional.number().range(1, 365).$;
+ if (limitErr) return rej('invalid limit param');
+
+ const users = await User
+ .find({}, {
+ _id: false,
+ created_at: true,
+ deleted_at: true
+ }, {
+ sort: { created_at: -1 }
+ });
+
+ 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));
+ // day = day.getTime();
+
+ const total = users.filter(u =>
+ u.created_at < dayEnd && (u.deleted_at == null || u.deleted_at > dayEnd)
+ ).length;
+
+ const created = users.filter(u =>
+ u.created_at < dayEnd && u.created_at > dayStart
+ ).length;
+
+ graph.push({
+ total: total,
+ created: created
+ });
+ }
+
+ res(graph);
+});
diff --git a/src/api/endpoints/stats.ts b/src/api/endpoints/stats.ts
new file mode 100644
index 0000000000..a6084cd17a
--- /dev/null
+++ b/src/api/endpoints/stats.ts
@@ -0,0 +1,48 @@
+/**
+ * Module dependencies
+ */
+import Post from '../models/post';
+import User from '../models/user';
+
+/**
+ * @swagger
+ * /stats:
+ * post:
+ * summary: Show the misskey's statistics
+ * responses:
+ * 200:
+ * description: Success
+ * schema:
+ * type: object
+ * properties:
+ * posts_count:
+ * description: count of all posts of misskey
+ * type: number
+ * users_count:
+ * description: count of all users of misskey
+ * type: number
+ *
+ * default:
+ * description: Failed
+ * schema:
+ * $ref: "#/definitions/Error"
+ */
+
+/**
+ * Show the misskey's statistics
+ *
+ * @param {any} params
+ * @return {Promise<any>}
+ */
+module.exports = params => new Promise(async (res, rej) => {
+ const postsCount = await Post
+ .count();
+
+ const usersCount = await User
+ .count();
+
+ res({
+ posts_count: postsCount,
+ users_count: usersCount
+ });
+});