summaryrefslogtreecommitdiff
path: root/src/server/api
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2018-06-11 11:27:55 +0900
committerGitHub <noreply@github.com>2018-06-11 11:27:55 +0900
commit0700be86e218320178b47d6e0327f7e8435bd821 (patch)
tree27ced8befdb8108dcb9093ea6fff3461390bc3b6 /src/server/api
parentMerge pull request #1698 from syuilo/cleanup-dependencies (diff)
parent:v: (diff)
downloadsharkey-0700be86e218320178b47d6e0327f7e8435bd821.tar.gz
sharkey-0700be86e218320178b47d6e0327f7e8435bd821.tar.bz2
sharkey-0700be86e218320178b47d6e0327f7e8435bd821.zip
Merge pull request #1702 from syuilo/hashtags-stats
Hashtags stats
Diffstat (limited to 'src/server/api')
-rw-r--r--src/server/api/endpoints.ts5
-rw-r--r--src/server/api/endpoints/hashtags/trend.ts80
2 files changed, 85 insertions, 0 deletions
diff --git a/src/server/api/endpoints.ts b/src/server/api/endpoints.ts
index 91e5298e73..5f0a020d6f 100644
--- a/src/server/api/endpoints.ts
+++ b/src/server/api/endpoints.ts
@@ -629,6 +629,11 @@ const endpoints: Endpoint[] = [
},
{
+ name: 'hashtags/trend',
+ withCredential: true
+ },
+
+ {
name: 'messaging/history',
withCredential: true,
kind: 'messaging-read'
diff --git a/src/server/api/endpoints/hashtags/trend.ts b/src/server/api/endpoints/hashtags/trend.ts
new file mode 100644
index 0000000000..c888a6cbb5
--- /dev/null
+++ b/src/server/api/endpoints/hashtags/trend.ts
@@ -0,0 +1,80 @@
+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: {
+ $gt: new Date(Date.now() - 1000 * 60 * 60)
+ },
+ 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;
+ }>
+ }>;
+
+ if (data.length == 0) {
+ return res([]);
+ }
+
+ const hots = data[0].tags
+ .sort((a, b) => b.count - a.count)
+ .map(tag => tag.tag)
+ .slice(0, 10);
+
+ const countPromises: Array<Promise<number[]>> = [];
+
+ for (let i = 0; i < 10; i++) {
+ // 10分
+ const interval = 1000 * 60 * 10;
+
+ 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])
+ }));
+
+ res(stats);
+});