diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2018-06-12 02:46:54 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2018-06-12 02:46:54 +0900 |
| commit | ed67e3506b5244e1b79cbb8b5de6951ff9592e42 (patch) | |
| tree | c196a5ea804ed96adb6dda462a827108e55a3fd5 /src/server/api/endpoints | |
| parent | :v: (diff) | |
| download | sharkey-ed67e3506b5244e1b79cbb8b5de6951ff9592e42.tar.gz sharkey-ed67e3506b5244e1b79cbb8b5de6951ff9592e42.tar.bz2 sharkey-ed67e3506b5244e1b79cbb8b5de6951ff9592e42.zip | |
:v:
Diffstat (limited to 'src/server/api/endpoints')
| -rw-r--r-- | src/server/api/endpoints/hashtags/trend.ts | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/src/server/api/endpoints/hashtags/trend.ts b/src/server/api/endpoints/hashtags/trend.ts index efd74a21e0..df7addab98 100644 --- a/src/server/api/endpoints/hashtags/trend.ts +++ b/src/server/api/endpoints/hashtags/trend.ts @@ -10,6 +10,8 @@ const rangeB = 1000 * 60 * 120; // 2時間 const coefficient = 1.5; // 「n倍」の部分 const requiredUsers = 3; // 最低何人がそのタグを投稿している必要があるか +const max = 5; + /** * Get trends of hashtags */ @@ -43,7 +45,7 @@ module.exports = () => new Promise(async (res, rej) => { return res([]); } - let tags = []; + const tags = []; // カウント data.map(x => x._id).forEach(x => { @@ -59,10 +61,10 @@ module.exports = () => new Promise(async (res, rej) => { }); // 最低要求投稿者数を下回るならカットする - tags = tags.filter(tag => tag.count >= requiredUsers); + const limitedTags = tags.filter(tag => tag.count >= requiredUsers); //#region 2. 1で取得したそれぞれのタグについて、「直近a分間のユニーク投稿数が今からa分前~今からb分前の間のユニーク投稿数のn倍以上」かどうかを判定する - const hotsPromises = tags.map(async tag => { + const hotsPromises = limitedTags.map(async tag => { const passedCount = (await Note.distinct('userId', { tags: tag.name, createdAt: { @@ -71,7 +73,7 @@ module.exports = () => new Promise(async (res, rej) => { } }) as any).length; - if (tag.count > (passedCount * coefficient)) { + if (tag.count >= (passedCount * coefficient)) { return tag; } else { return null; @@ -79,13 +81,24 @@ module.exports = () => new Promise(async (res, rej) => { }); //#endregion - const hots = (await Promise.all(hotsPromises)) + // タグを人気順に並べ替え + let hots = (await Promise.all(hotsPromises)) .filter(x => x != null) .sort((a, b) => b.count - a.count) .map(tag => tag.name) - .slice(0, 5); + .slice(0, max); + + //#region 3. もし上記の方法でのトレンド抽出の結果、求められているタグ数に達しなければ「ただ単に現在投稿数が多いハッシュタグ」に切り替える + if (hots.length < max) { + hots = hots.concat(tags + .filter(tag => hots.indexOf(tag.name) == -1) + .sort((a, b) => b.count - a.count) + .map(tag => tag.name) + .slice(0, max - hots.length)); + } + //#endregion - //#region 2で話題と判定されたタグそれぞれについて過去の投稿数グラフを取得する + //#region 2(または3)で話題と判定されたタグそれぞれについて過去の投稿数グラフを取得する const countPromises: Array<Promise<any[]>> = []; const range = 20; |