summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-06-11 09:11:32 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-06-11 09:11:32 +0900
commit2ec25a7729e8d2fa6734bdf25ffb5a1b35ca2d5b (patch)
tree8e50814af7140caf4c8ad9ec2f69a59b8bb88a23 /src/server/api/endpoints
parentwip (diff)
downloadsharkey-2ec25a7729e8d2fa6734bdf25ffb5a1b35ca2d5b.tar.gz
sharkey-2ec25a7729e8d2fa6734bdf25ffb5a1b35ca2d5b.tar.bz2
sharkey-2ec25a7729e8d2fa6734bdf25ffb5a1b35ca2d5b.zip
wip
Diffstat (limited to 'src/server/api/endpoints')
-rw-r--r--src/server/api/endpoints/hashtags/trend.ts78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/server/api/endpoints/hashtags/trend.ts b/src/server/api/endpoints/hashtags/trend.ts
new file mode 100644
index 0000000000..3b95e1fa4c
--- /dev/null
+++ b/src/server/api/endpoints/hashtags/trend.ts
@@ -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);
+});