iceshrimp-legacy/src/server/api/endpoints/hashtags/trend.ts

120 lines
3 KiB
TypeScript
Raw Normal View History

2018-06-11 02:11:32 +02:00
import Note from '../../../../models/note';
2018-06-11 18:41:17 +02:00
/*
a分間のユニーク投稿数が今からa分前b分前の間のユニーク投稿数のn倍以上5
稿稿稿稿1
*/
const rangeA = 1000 * 60 * 10; // 10分
2018-06-11 18:43:56 +02:00
const rangeB = 1000 * 60 * 40; // 40分
const coefficient = 1.5; // 「n倍」の部分
2018-06-11 18:41:17 +02:00
2018-06-11 02:11:32 +02:00
/**
* Get trends of hashtags
*/
2018-06-11 06:49:53 +02:00
module.exports = () => new Promise(async (res, rej) => {
2018-06-11 18:41:17 +02:00
//#region 1. 直近Aの内に投稿されたハッシュタグ(とユーザーのペア)を集計
2018-06-11 02:11:32 +02:00
const data = await Note.aggregate([{
$match: {
createdAt: {
2018-06-11 18:41:17 +02:00
$gt: new Date(Date.now() - rangeA)
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 18:41:17 +02:00
//#endregion
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 = [];
2018-06-11 18:41:17 +02:00
// カウント
2018-06-11 06:45:32 +02:00
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
});
}
});
2018-06-11 18:41:17 +02:00
//#region 2. 1で取得したそれぞれのタグについて、「直近a分間のユニーク投稿数が今からa分前今からb分前の間のユニーク投稿数のn倍以上」かどうかを判定する
const hotsPromises = tags.map(async tag => {
const passedCount = (await Note.distinct('userId', {
2018-06-11 18:45:58 +02:00
tags: tag.name,
2018-06-11 18:41:17 +02:00
createdAt: {
$lt: new Date(Date.now() - rangeA),
$gt: new Date(Date.now() - rangeB)
}
}) as any).length;
2018-06-11 18:48:29 +02:00
if (tag.count > (passedCount * coefficient)) {
2018-06-11 18:41:17 +02:00
return tag;
} else {
return null;
}
});
//#endregion
const hots = (await Promise.all(hotsPromises))
.filter(x => x != null)
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 18:41:17 +02:00
//#region 2で話題と判定されたタグそれぞれについて過去の投稿数グラフを取得する
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 18:41:17 +02:00
//#endregion
2018-06-11 06:45:32 +02:00
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);
});