This commit is contained in:
syuilo
2018-06-11 09:11:32 +09:00
parent dc3c80e3ce
commit 2ec25a7729
10 changed files with 185 additions and 43 deletions

View File

@ -628,6 +628,11 @@ const endpoints: Endpoint[] = [
withCredential: true
},
{
name: 'hashtags/trend',
withCredential: true
},
{
name: 'messaging/history',
withCredential: true,

View File

@ -0,0 +1,78 @@
import Note from '../../../../models/note';
/**
* Get trends of hashtags
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// 10分
const interval = 1000 * 60 * 10;
const data = await Note.aggregate([{
$match: {
createdAt: {
$gt: new Date(Date.now() - interval)
},
tags: {
$exists: true,
$ne: []
}
}
}, {
$unwind: '$tags'
}, {
$group: {
_id: '$tags',
count: {
$sum: 1
}
}
}, {
$group: {
_id: null,
tags: {
$push: {
tag: '$_id',
count: '$count'
}
}
}
}, {
$project: {
_id: false,
tags: true
}
}]) as Array<{
tags: Array<{
tag: string;
count: number;
}>
}>;
const hots = data[0].tags
.sort((a, b) => a.count - b.count)
.map(tag => tag.tag)
.slice(0, 10);
const countPromises: Array<Promise<number[]>> = [];
for (let i = 0; i < 10; i++) {
countPromises.push(Promise.all(hots.map(tag => Note.count({
tags: tag,
createdAt: {
$lt: new Date(Date.now() - (interval * i)),
$gt: new Date(Date.now() - (interval * (i + 1)))
}
}))));
}
const countsLog = await Promise.all(countPromises);
const stats = hots.map((tag, i) => ({
tag,
chart: countsLog.map(counts => counts[i])
}));
console.log(stats);
res(stats);
});

View File

@ -1,35 +0,0 @@
import * as websocket from 'websocket';
import Xev from 'xev';
const ev = new Xev();
export default function(request: websocket.request, connection: websocket.connection): void {
const onStats = stats => {
connection.send(JSON.stringify({
type: 'stats',
body: stats
}));
};
connection.on('message', async data => {
const msg = JSON.parse(data.utf8Data);
switch (msg.type) {
case 'requestLog':
ev.once('hashtagsStatsLog:' + msg.id, statsLog => {
connection.send(JSON.stringify({
type: 'statsLog',
body: statsLog
}));
});
ev.emit('requestHashtagsStatsLog', msg.id);
break;
}
});
ev.addListener('hashtagsStats', onStats);
connection.on('close', () => {
ev.removeListener('hashtagsStats', onStats);
});
}

View File

@ -14,7 +14,6 @@ import othelloGameStream from './stream/othello-game';
import othelloStream from './stream/othello';
import serverStatsStream from './stream/server-stats';
import notesStatsStream from './stream/notes-stats';
import hashtagsStatsStream from './stream/hashtags-stats';
import requestsStream from './stream/requests';
import { ParsedUrlQuery } from 'querystring';
import authenticate from './authenticate';
@ -40,11 +39,6 @@ module.exports = (server: http.Server) => {
return;
}
if (request.resourceURL.pathname === '/hashtags-stats') {
hashtagsStatsStream(request, connection);
return;
}
if (request.resourceURL.pathname === '/requests') {
requestsStream(request, connection);
return;