From feaad2d6ca14e3c353b820c7803bdd25c3e2ffea Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Wed, 8 Mar 2017 19:01:08 -0700 Subject: [PATCH] Added loader, graphql --- graph/loaders/metrics.js | 49 ++++++++++++++++++++++++++++++++++- graph/resolvers/asset.js | 6 ++++- graph/resolvers/root_query.js | 4 +++ graph/typeDefs.graphql | 21 ++++++++++++--- 4 files changed, 75 insertions(+), 5 deletions(-) diff --git a/graph/loaders/metrics.js b/graph/loaders/metrics.js index a6fe5afe4..d3c602de8 100644 --- a/graph/loaders/metrics.js +++ b/graph/loaders/metrics.js @@ -4,6 +4,52 @@ const {objectCacheKeyFn} = require('./util'); const ActionModel = require('../../models/action'); +/** + * Returns the assets which have had comments made within the last time period. + */ +const getCommentActivityMetrics = ({loaders: {Assets, Comments}}, {from, to, limit}) => { + let assetMetrics = []; + + return Comments.aggregate([ + {$match: { + parent_id: null, + created_at: { + $gt: from, + $lt: to + } + }}, + {$group: { + _id: '$asset_id', + commentCount: { + $sum: 1 + } + }}, + {$project: { + _id: false, + asset_id: '$_id', + commentCount: '$commentCount' + }}, + {$sort: { + commentCount: -1 + }}, + {$limit: limit} + ]) + .then((results) => { + results = assetMetrics; + + return Assets.getByID.loadMany(results.map((result) => result.asset_id)); + }) + .then((assets) => assets.map((asset, i) => { + + // We're leveraging the fact that the comments returned by the aggregation + // query are in the request order that we just made, it's what the + // Assets.getByID loader does. + asset.commentCount = assetMetrics[i].commentCount; + + return asset; + })); +}; + /** * Returns a list of assets with action metadata included on the models. */ @@ -211,7 +257,8 @@ module.exports = (context) => ({ get: ({from, to, sort, limit}) => getAssetMetrics(context, {from, to, sort, limit}) }, Comments: { - get: ({from, to, sort, limit}) => getCommentMetrics(context, {from, to, sort, limit}) + get: ({from, to, sort, limit}) => getCommentMetrics(context, {from, to, sort, limit}), + getActivity: ({from, to, limit}) => getCommentActivityMetrics(context, {from, to, limit}), } } }); diff --git a/graph/resolvers/asset.js b/graph/resolvers/asset.js index 2077b9e7a..c0cffd1ae 100644 --- a/graph/resolvers/asset.js +++ b/graph/resolvers/asset.js @@ -10,7 +10,11 @@ const Asset = { parent_id: null }); }, - commentCount({id}, _, {loaders: {Comments}}) { + commentCount({id, commentCount}, _, {loaders: {Comments}}) { + if (commentCount) { + return commentCount; + } + return Comments.countByAssetID.load(id); }, settings({settings = null}, _, {loaders: {Settings}}) { diff --git a/graph/resolvers/root_query.js b/graph/resolvers/root_query.js index bdcdcef78..083526eff 100644 --- a/graph/resolvers/root_query.js +++ b/graph/resolvers/root_query.js @@ -71,6 +71,10 @@ const RootQuery = { return null; } + if (sort === 'COMMENTS') { + return Comments.getActivity({from, to, limit}); + } + return Comments.get({from, to, sort, limit}); }, diff --git a/graph/typeDefs.graphql b/graph/typeDefs.graphql index 6cb4cb046..2821952e4 100644 --- a/graph/typeDefs.graphql +++ b/graph/typeDefs.graphql @@ -476,6 +476,21 @@ enum USER_STATUS { APPROVED } +enum COMMENT_METRICS_SORT { + + # Represents a LikeAction. + LIKE + + # Represents a FlagAction. + FLAG + + # Represents a don't agree action. + DONTAGREE + + # Represents activity. + ACTIVITY +} + type RootQuery { # Site wide settings and defaults. @@ -507,7 +522,7 @@ type RootQuery { # Comment metrics related to user actions are saturated into the comments # returned. - commentMetrics(from: Date!, to: Date!, sort: ACTION_TYPE!, limit: Int = 10): [Comment!] + commentMetrics(from: Date!, to: Date!, sort: COMMENT_METRICS_SORT!, limit: Int = 10): [Comment!] } ################################################################################ @@ -646,14 +661,14 @@ type SetCommentStatusResponse implements Response { type AddCommentTagResponse implements Response { # An array of errors relating to the mutation that occured. comment: Comment - errors: [UserError] + errors: [UserError] } # Response to removeCommentTag mutation type RemoveCommentTagResponse implements Response { # An array of errors relating to the mutation that occured. comment: Comment - errors: [UserError] + errors: [UserError] } # All mutations for the application are defined on this object.