This commit is contained in:
syuilo 2018-06-11 13:45:32 +09:00
parent c3e375e8a5
commit 6f724827bd
3 changed files with 48 additions and 33 deletions

View file

@ -257,6 +257,7 @@ common/views/widgets/posts-monitor.vue:
common/views/widgets/hashtags.vue: common/views/widgets/hashtags.vue:
title: "ハッシュタグ" title: "ハッシュタグ"
count: "{}人が投稿"
common/views/widgets/server.vue: common/views/widgets/server.vue:
title: "サーバー情報" title: "サーバー情報"

View file

@ -9,6 +9,7 @@
<div v-for="stat in stats" :key="stat.tag"> <div v-for="stat in stats" :key="stat.tag">
<div class="tag"> <div class="tag">
<router-link :to="`/tags/${ stat.tag }`">#{{ stat.tag }}</router-link> <router-link :to="`/tags/${ stat.tag }`">#{{ stat.tag }}</router-link>
<p>{{ '%i18n:@count%'.replace('{}', stat.usersCount) }}</p>
</div> </div>
<x-chart class="chart" :src="stat.chart"/> <x-chart class="chart" :src="stat.chart"/>
</div> </div>
@ -83,11 +84,17 @@ root(isDark)
> .tag > .tag
flex 1 flex 1
> a
font-size 14px font-size 14px
color isDark ? #9baec8 : #65727b color isDark ? #9baec8 : #65727b
> a
color inherit
> p
margin 0
font-size 75%
opacity 0.7
> .chart > .chart
height 30px height 30px

View file

@ -18,49 +18,48 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
$unwind: '$tags' $unwind: '$tags'
}, { }, {
$group: { $group: {
_id: '$tags', _id: { tags: '$tags', userId: '$userId' }
count: {
$sum: 1
}
}
}, {
$group: {
_id: null,
tags: {
$push: {
tag: '$_id',
count: '$count'
}
}
}
}, {
$project: {
_id: false,
tags: true
} }
}]) as Array<{ }]) as Array<{
tags: Array<{ _id: {
tag: string; tags: string;
count: number; userId: any;
}> }
}>; }>;
if (data.length == 0) { if (data.length == 0) {
return res([]); 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) .sort((a, b) => b.count - a.count)
.map(tag => tag.tag) .map(tag => tag.name)
.slice(0, 5); .slice(0, 5);
const countPromises: Array<Promise<number[]>> = []; const countPromises: Array<Promise<any[]>> = [];
const range = 20;
for (let i = 0; i < 10; i++) {
// 10分 // 10分
const interval = 1000 * 60 * 10; const interval = 1000 * 60 * 10;
countPromises.push(Promise.all(hots.map(tag => Note.count({ for (let i = 0; i < range; i++) {
countPromises.push(Promise.all(hots.map(tag => Note.distinct('userId', {
tags: tag, tags: tag,
createdAt: { createdAt: {
$lt: new Date(Date.now() - (interval * i)), $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 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) => ({ const stats = hots.map((tag, i) => ({
tag, tag,
chart: countsLog.map(counts => counts[i]) chart: countsLog.map(counts => counts[i].length),
usersCount: totalCounts[i].length
})); }));
res(stats); res(stats);