From 6f724827bd5f54d2eeea222f52af8c178e1198da Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 11 Jun 2018 13:45:32 +0900 Subject: [PATCH] :v: --- locales/ja.yml | 1 + .../app/common/views/widgets/hashtags.vue | 11 ++- src/server/api/endpoints/hashtags/trend.ts | 69 ++++++++++--------- 3 files changed, 48 insertions(+), 33 deletions(-) diff --git a/locales/ja.yml b/locales/ja.yml index 4ce8caaa1..1b4fc1507 100644 --- a/locales/ja.yml +++ b/locales/ja.yml @@ -257,6 +257,7 @@ common/views/widgets/posts-monitor.vue: common/views/widgets/hashtags.vue: title: "ハッシュタグ" + count: "{}人が投稿" common/views/widgets/server.vue: title: "サーバー情報" diff --git a/src/client/app/common/views/widgets/hashtags.vue b/src/client/app/common/views/widgets/hashtags.vue index 9d523f321..f6fd091b7 100644 --- a/src/client/app/common/views/widgets/hashtags.vue +++ b/src/client/app/common/views/widgets/hashtags.vue @@ -9,6 +9,7 @@
#{{ stat.tag }} +

{{ '%i18n:@count%'.replace('{}', stat.usersCount) }}

@@ -83,10 +84,16 @@ root(isDark) > .tag flex 1 + font-size 14px + color isDark ? #9baec8 : #65727b > a - font-size 14px - color isDark ? #9baec8 : #65727b + color inherit + + > p + margin 0 + font-size 75% + opacity 0.7 > .chart height 30px diff --git a/src/server/api/endpoints/hashtags/trend.ts b/src/server/api/endpoints/hashtags/trend.ts index 443d7d203..07317d1db 100644 --- a/src/server/api/endpoints/hashtags/trend.ts +++ b/src/server/api/endpoints/hashtags/trend.ts @@ -18,49 +18,48 @@ module.exports = (params, user) => new Promise(async (res, rej) => { $unwind: '$tags' }, { $group: { - _id: '$tags', - count: { - $sum: 1 - } - } - }, { - $group: { - _id: null, - tags: { - $push: { - tag: '$_id', - count: '$count' - } - } - } - }, { - $project: { - _id: false, - tags: true + _id: { tags: '$tags', userId: '$userId' } } }]) as Array<{ - tags: Array<{ - tag: string; - count: number; - }> + _id: { + tags: string; + userId: any; + } }>; if (data.length == 0) { return res([]); } - const hots = data[0].tags + const tags = []; + + data.map(x => x._id).forEach(x => { + const i = tags.findIndex(tag => tag.name == x.tags); + if (i != -1) { + tags[i].count++; + } else { + tags.push({ + name: x.tags, + count: 1 + }); + } + }); + + const hots = tags .sort((a, b) => b.count - a.count) - .map(tag => tag.tag) + .map(tag => tag.name) .slice(0, 5); - const countPromises: Array> = []; + const countPromises: Array> = []; - for (let i = 0; i < 10; i++) { - // 10分 - const interval = 1000 * 60 * 10; + const range = 20; - countPromises.push(Promise.all(hots.map(tag => Note.count({ + // 10分 + const interval = 1000 * 60 * 10; + + for (let i = 0; i < range; i++) { + + countPromises.push(Promise.all(hots.map(tag => Note.distinct('userId', { tags: tag, createdAt: { $lt: new Date(Date.now() - (interval * i)), @@ -71,9 +70,17 @@ module.exports = (params, user) => new Promise(async (res, rej) => { const countsLog = await Promise.all(countPromises); + const totalCounts: any = await Promise.all(hots.map(tag => Note.distinct('userId', { + tags: tag, + createdAt: { + $gt: new Date(Date.now() - (interval * range)) + } + }))); + const stats = hots.map((tag, i) => ({ tag, - chart: countsLog.map(counts => counts[i]) + chart: countsLog.map(counts => counts[i].length), + usersCount: totalCounts[i].length })); res(stats);