2018-06-11 02:11:32 +02:00
|
|
|
import Note from '../../../../models/note';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get trends of hashtags
|
|
|
|
*/
|
|
|
|
module.exports = (params, user) => new Promise(async (res, rej) => {
|
|
|
|
const data = await Note.aggregate([{
|
|
|
|
$match: {
|
|
|
|
createdAt: {
|
2018-06-11 04:24:29 +02:00
|
|
|
$gt: new Date(Date.now() - 1000 * 60 * 60)
|
2018-06-11 02:11:32 +02:00
|
|
|
},
|
|
|
|
tags: {
|
|
|
|
$exists: true,
|
|
|
|
$ne: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
$unwind: '$tags'
|
|
|
|
}, {
|
|
|
|
$group: {
|
2018-06-11 06:45:32 +02:00
|
|
|
_id: { tags: '$tags', userId: '$userId' }
|
2018-06-11 02:11:32 +02:00
|
|
|
}
|
|
|
|
}]) as Array<{
|
2018-06-11 06:45:32 +02:00
|
|
|
_id: {
|
|
|
|
tags: string;
|
|
|
|
userId: any;
|
|
|
|
}
|
2018-06-11 02:11:32 +02:00
|
|
|
}>;
|
|
|
|
|
2018-06-11 04:24:29 +02:00
|
|
|
if (data.length == 0) {
|
|
|
|
return res([]);
|
|
|
|
}
|
|
|
|
|
2018-06-11 06:45:32 +02:00
|
|
|
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
|
2018-06-11 04:27:21 +02:00
|
|
|
.sort((a, b) => b.count - a.count)
|
2018-06-11 06:45:32 +02:00
|
|
|
.map(tag => tag.name)
|
2018-06-11 05:52:47 +02:00
|
|
|
.slice(0, 5);
|
2018-06-11 02:11:32 +02:00
|
|
|
|
2018-06-11 06:45:32 +02:00
|
|
|
const countPromises: Array<Promise<any[]>> = [];
|
2018-06-11 02:11:32 +02:00
|
|
|
|
2018-06-11 06:45:32 +02:00
|
|
|
const range = 20;
|
2018-06-11 04:24:29 +02:00
|
|
|
|
2018-06-11 06:45:32 +02:00
|
|
|
// 10分
|
|
|
|
const interval = 1000 * 60 * 10;
|
|
|
|
|
|
|
|
for (let i = 0; i < range; i++) {
|
|
|
|
|
|
|
|
countPromises.push(Promise.all(hots.map(tag => Note.distinct('userId', {
|
2018-06-11 02:11:32 +02:00
|
|
|
tags: tag,
|
|
|
|
createdAt: {
|
|
|
|
$lt: new Date(Date.now() - (interval * i)),
|
|
|
|
$gt: new Date(Date.now() - (interval * (i + 1)))
|
|
|
|
}
|
|
|
|
}))));
|
|
|
|
}
|
|
|
|
|
|
|
|
const countsLog = await Promise.all(countPromises);
|
|
|
|
|
2018-06-11 06:45:32 +02:00
|
|
|
const totalCounts: any = await Promise.all(hots.map(tag => Note.distinct('userId', {
|
|
|
|
tags: tag,
|
|
|
|
createdAt: {
|
|
|
|
$gt: new Date(Date.now() - (interval * range))
|
|
|
|
}
|
|
|
|
})));
|
|
|
|
|
2018-06-11 02:11:32 +02:00
|
|
|
const stats = hots.map((tag, i) => ({
|
|
|
|
tag,
|
2018-06-11 06:45:32 +02:00
|
|
|
chart: countsLog.map(counts => counts[i].length),
|
|
|
|
usersCount: totalCounts[i].length
|
2018-06-11 02:11:32 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
res(stats);
|
|
|
|
});
|