Added commentMetrics edge.

This commit is contained in:
Wyatt Johnson
2017-02-21 14:30:52 -07:00
parent 30fbcd30a9
commit 2360f280d2
4 changed files with 85 additions and 8 deletions
+63 -2
View File
@@ -5,7 +5,10 @@ const {objectCacheKeyFn} = require('./util');
const CommentModel = require('../../models/comment');
const ActionModel = require('../../models/action');
const getMetrics = ({loaders: {Metrics, Assets}}, {from, to, sort, limit}) => {
/**
* Returns a list of assets with action metadata included on the models.
*/
const getAssetMetrics = ({loaders: {Metrics, Assets}}, {from, to, sort, limit}) => {
let commentMetrics = {};
let assetMetrics = [];
@@ -99,6 +102,59 @@ const getMetrics = ({loaders: {Metrics, Assets}}, {from, to, sort, limit}) => {
});
};
/**
* Returns a list of comments that are retrieved based on most activity within
* the indicated time range.
*/
const getCommentMetrics = ({loaders: {Metrics, Assets}}, {from, to, sort, limit}) => {
let commentActionSummaries = {};
return Metrics.getRecentActions.load({from, to})
.then((actionSummaries) => {
actionSummaries.sort((a, b) => {
let aActionSummary = a.action_type === sort ? a : null;
let bActionSummary = b.action_type === sort ? b : null;
// If either a or b don't have this action type, then one of them will
// automatically win.
if (aActionSummary == null || bActionSummary == null) {
if (bActionSummary != null) {
return 1;
}
if (aActionSummary != null) {
return -1;
}
return 0;
}
// Both of them had an actionCount, hence we can determine that we could
// compare the actual values directly.
return bActionSummary.count - aActionSummary.count;
});
commentActionSummaries = _.groupBy(actionSummaries, 'item_id');
let commentIDs = _.uniq(actionSummaries.map(({item_id}) => item_id));
// Only keep the top `limit`.
commentIDs = commentIDs.slice(0, limit);
// Find those comments.
return Metrics.getSpecificComments.loadMany(commentIDs);
})
.then((comments) => comments.map((comment) => {
// Add in the action summaries genrerated.
comment.action_summaries = commentActionSummaries[comment.id];
return comment;
}));
};
const getRecentActions = (context, {from, to}) => {
return ActionModel.aggregate([
@@ -150,6 +206,11 @@ module.exports = (context) => ({
batch: false,
cacheKeyFn: objectCacheKeyFn('from', 'to')
}),
get: ({from, to, sort, limit}) => getMetrics(context, {from, to, sort, limit})
Assets: {
get: ({from, to, sort, limit}) => getAssetMetrics(context, {from, to, sort, limit})
},
Comments: {
get: ({from, to, sort, limit}) => getCommentMetrics(context, {from, to, sort, limit})
}
}
});
+5 -1
View File
@@ -25,7 +25,11 @@ const Comment = {
return null;
},
action_summaries({id}, _, {loaders: {Actions}}) {
action_summaries({id, action_summaries}, _, {loaders: {Actions}}) {
if (action_summaries) {
return action_summaries;
}
return Actions.getSummariesByItemID.load(id);
},
asset({asset_id}, _, {loaders: {Assets}}) {
+10 -2
View File
@@ -56,12 +56,20 @@ const RootQuery = {
return Comments.getCountByQuery({statuses, asset_id, parent_id});
},
metrics(_, {from, to, sort, limit = 10}, {user, loaders: {Metrics}}) {
assetMetrics(_, {from, to, sort, limit = 10}, {user, loaders: {Metrics: {Assets}}}) {
if (user == null || !user.hasRoles('ADMIN')) {
return null;
}
return Metrics.get({from, to, sort, limit});
return Assets.get({from, to, sort, limit});
},
commentMetrics(_, {from, to, sort, limit = 10}, {user, loaders: {Metrics: {Comments}}}) {
if (user == null || !user.hasRoles('ADMIN')) {
return null;
}
return Comments.get({from, to, sort, limit});
},
// This returns the current user, ensure that if we aren't logged in, we
+7 -3
View File
@@ -456,9 +456,13 @@ type RootQuery {
# role.
me: User
# Metrics related to user actions are saturated into the assets returned. The
# sort will affect if it will allow
metrics(from: Date!, to: Date!, sort: ACTION_TYPE!, limit: Int = 10): [Asset]
# Asset metrics related to user actions are saturated into the assets
# returned.
assetMetrics(from: Date!, to: Date!, sort: ACTION_TYPE!, limit: Int = 10): [Asset]
# Comment metrics related to user actions are saturated into the comments
# returned.
commentMetrics(from: Date!, to: Date!, sort: ACTION_TYPE!, limit: Int = 10): [Comment]
}
################################################################################