From 9e759c7667ceebd1fd4b975f78db3f1333866b9d Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Wed, 22 Feb 2017 13:55:48 -0700 Subject: [PATCH] Adjusted BE beheviour --- .../Dashboard/MostLikedCommentsWidget.js | 8 ++- graph/loaders/metrics.js | 42 +++++++----- test/graph/loaders/metrics.js | 66 +++++++++++++++++++ 3 files changed, 96 insertions(+), 20 deletions(-) create mode 100644 test/graph/loaders/metrics.js diff --git a/client/coral-admin/src/containers/Dashboard/MostLikedCommentsWidget.js b/client/coral-admin/src/containers/Dashboard/MostLikedCommentsWidget.js index 6e7d03185..39f01b41b 100644 --- a/client/coral-admin/src/containers/Dashboard/MostLikedCommentsWidget.js +++ b/client/coral-admin/src/containers/Dashboard/MostLikedCommentsWidget.js @@ -1,13 +1,15 @@ import React from 'react'; import {compose} from 'react-apollo'; import {mostLikedComments} from 'coral-admin/src/graphql/queries'; -import I18n from 'coral-framework/modules/i18n/i18n'; -import translations from 'coral-admin/src/translations'; + +// import I18n from 'coral-framework/modules/i18n/i18n'; +// import translations from 'coral-admin/src/translations'; + import ModerationQueue from 'coral-admin/src/containers/ModerationQueue/ModerationQueue'; import {Spinner} from 'coral-ui'; import styles from './Widget.css'; -const lang = new I18n(translations); +// const lang = new I18n(translations); const MostLikedCommentsWidget = props => { diff --git a/graph/loaders/metrics.js b/graph/loaders/metrics.js index 8591ecefe..1b0719d0f 100644 --- a/graph/loaders/metrics.js +++ b/graph/loaders/metrics.js @@ -17,6 +17,10 @@ const getAssetMetrics = ({loaders: {Metrics, Assets}}, {from, to, sort, limit}) .then((actionSummaries) => { commentMetrics = actionSummaries.reduce((acc, {item_id, action_type, count}) => { + if (action_type !== sort) { + return acc; + } + if (!(item_id in acc)) { acc[item_id] = []; } @@ -47,29 +51,24 @@ const getAssetMetrics = ({loaders: {Metrics, Assets}}, {from, to, sort, limit}) })); return {action_summaries, id: asset_id}; - }); + }) + + .filter((asset) => { + let contextActionSummary = asset.action_summaries.find((({action_type}) => action_type === sort)); + if (contextActionSummary === null) { + return false; + } + + return true; + }) // Sort these metrics by the predefined sort order. This will ensure that // if the action summary does not exist on the object, that it is less // prefered over the one that does have it. - assetMetrics.sort((a, b) => { + .sort((a, b) => { let aActionSummary = a.action_summaries.find((({action_type}) => action_type === sort)); let bActionSummary = b.action_summaries.find((({action_type}) => action_type === sort)); - // 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.actionCount - aActionSummary.actionCount; @@ -138,7 +137,16 @@ const getCommentMetrics = ({loaders: {Metrics, Comments}}, {from, to, sort, limi commentActionSummaries = _.groupBy(actionSummaries, 'item_id'); - let commentIDs = _.uniq(actionSummaries.map(({item_id}) => item_id)); + // Grab the comment id's for comment where they have at least one of the + // actions being sorted by. + let commentIDs = Object.keys(commentActionSummaries).filter((item_id) => { + let contextActionSummary = commentActionSummaries[item_id].find(({action_type}) => action_type === sort); + if (contextActionSummary == null) { + return false; + } + + return true; + }); // Only keep the top `limit`. commentIDs = commentIDs.slice(0, limit); diff --git a/test/graph/loaders/metrics.js b/test/graph/loaders/metrics.js new file mode 100644 index 000000000..303b40897 --- /dev/null +++ b/test/graph/loaders/metrics.js @@ -0,0 +1,66 @@ +const {expect} = require('chai'); +const {graphql} = require('graphql'); + +const schema = require('../../../graph/schema'); +const Context = require('../../../graph/context'); +const UserModel = require('../../../models/user'); +const SettingsService = require('../../../services/settings'); +const ActionModel = require('../../../models/action'); +const CommentModel = require('../../../models/comment'); + +describe('graph.loaders.Metrics', () => { + beforeEach(() => SettingsService.init()); + + describe('#Comments', () => { + const query = ` + query CommentMetrics($from: Date!, $to: Date!) { + liked: commentMetrics(from: $from, to: $to, sort: LIKE) { + id + } + flagged: commentMetrics(from: $from, to: $to, sort: FLAG) { + id + } + } + `; + + describe('different comment states', () => { + + beforeEach(() => CommentModel.create([ + {id: '1', body: 'a new comment!'} + ])); + + [ + {liked: 0, flagged: 0, actions: []}, + {liked: 1, flagged: 0, actions: [{action_type: 'LIKE', item_id: '1', item_type: 'COMMENTS'}]}, + {liked: 0, flagged: 1, actions: [{action_type: 'FLAG', item_id: '1', item_type: 'COMMENTS'}]}, + {liked: 1, flagged: 1, actions: [ + {action_type: 'FLAG', item_id: '1', item_type: 'COMMENTS'}, + {action_type: 'LIKE', item_id: '1', item_type: 'COMMENTS'} + ]} + ].forEach(({liked, flagged, actions}) => { + + describe(`with actions=${actions.length}`, () => { + + beforeEach(() => ActionModel.create(actions)); + + it(`returns the correct amount of metrics liked=${liked} flagged=${flagged}`, () => { + const context = new Context({user: new UserModel({roles: ['ADMIN']})}); + + return graphql(schema, query, {}, context, { + from: (new Date()).setMinutes((new Date()).getMinutes() - 5), + to: (new Date()).setMinutes((new Date()).getMinutes() + 5) + }) + .then(({data, errors}) => { + expect(errors).to.be.undefined; + expect(data.liked).to.have.length(liked); + expect(data.flagged).to.have.length(flagged); + }); + }); + + }); + + }); + + }); + }); +});