Skip to content
This repository has been archived by the owner on May 10, 2023. It is now read-only.

Commit

Permalink
chore: do not count rejected sentences as 'in review'
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKohler committed May 29, 2020
1 parent 64627bb commit 663f351
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 28 deletions.
81 changes: 55 additions & 26 deletions server/lib/sentences.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,7 @@ async function getSentencesForReview({ locale, user }) {
async function getRejectedSentences({ user }) {
debug('GETTING_REJECTED_SENTENCES');

const query = `
SELECT
Sentences.id,
Sentences.sentence,
Sentences.localeId,
SUM(Votes.approval) as number_of_approving_votes,
COUNT(Votes.approval) as number_of_votes
FROM Sentences
LEFT JOIN Votes ON (Votes.sentenceId=Sentences.id)
WHERE Sentences.user = "${user}"
GROUP BY Sentences.id
HAVING
(
number_of_votes = 3 AND
number_of_approving_votes < 2
) OR (
number_of_votes = 2 AND
number_of_approving_votes = 0
)
ORDER BY Sentences.createdAt DESC;
`;

const query = getRejectedSentencesQuery({ user });
const sentences = await sequelize.query(query, { type: QueryTypes.SELECT });
const sentencesPerLocale = sentences.reduce((perLocale, sentenceInfo) => {
perLocale[sentenceInfo.localeId] = perLocale[sentenceInfo.localeId] || [];
Expand All @@ -83,8 +62,8 @@ async function getStats(locales) {

const totalStats = {};
for (const locale of locales) {
const validatedQueryResult = await getValidatedSentencesCountForLocale(locale);
const validated = validatedQueryResult[0]['COUNT(*)'];
const validated = await getValidatedSentencesCountForLocale(locale);
const rejected = await getRejectedSentencesCountForLocale(locale);

const options = {
where: {
Expand All @@ -95,6 +74,7 @@ async function getStats(locales) {
totalStats[locale] = {
added: sentenceTotalCountByLocale,
validated,
rejected,
};
}

Expand All @@ -105,14 +85,16 @@ async function getStats(locales) {
};
}

function getValidatedSentencesCountForLocale(locale) {
async function getValidatedSentencesCountForLocale(locale) {
const validatedSentencesQuery = getValidatedSentencesQuery({ locale });
const query = `
SELECT COUNT(*) FROM
(${validatedSentencesQuery}) as approved_sentences;
`;

return sequelize.query(query, { type: QueryTypes.SELECT });
const queryResult = await sequelize.query(query, { type: QueryTypes.SELECT });
const validatedCount = queryResult[0] && queryResult[0]['COUNT(*)'];
return validatedCount;
}

async function getUnreviewedByYouCountForLocales(locales, user) {
Expand All @@ -132,6 +114,18 @@ async function getUnreviewedByYouCountForLocales(locales, user) {
return stats;
}

async function getRejectedSentencesCountForLocale(locale) {
const rejectedQuery = getRejectedSentencesQuery({ locale });
const query = `
SELECT COUNT(*) FROM
(${rejectedQuery}) as rejected_sentences;
`;

const queryResult = await sequelize.query(query, { type: QueryTypes.SELECT });
const rejectedCount = queryResult[0] && queryResult[0]['COUNT(*)'];
return rejectedCount;
}

function calculateStats(stats, sentenceInfo) {
const localeId = sentenceInfo.localeId;
stats[localeId] = stats[localeId] || {
Expand Down Expand Up @@ -259,3 +253,38 @@ function getValidatedSentencesQuery({ locale }) {
HAVING
number_of_approving_votes >= 2`;
}

function getRejectedSentencesQuery({ user, locale }) {
let whereClause = '';

if (user) {
whereClause = `WHERE Sentences.user = '${user}'`;
}

if (locale) {
whereClause = whereClause ?
`${whereClause} and Sentences.localeId = '${locale}'` :
`WHERE Sentences.localeId = '${locale}'`;
}

return `
SELECT
Sentences.id,
Sentences.sentence,
Sentences.localeId,
SUM(Votes.approval) as number_of_approving_votes,
COUNT(Votes.approval) as number_of_votes
FROM Sentences
LEFT JOIN Votes ON (Votes.sentenceId=Sentences.id)
${whereClause}
GROUP BY Sentences.id
HAVING
(
number_of_votes = 3 AND
number_of_approving_votes < 2
) OR (
number_of_votes = 2 AND
number_of_approving_votes = 0
)
ORDER BY Sentences.createdAt DESC`;
}
6 changes: 5 additions & 1 deletion server/tests/lib/sentences.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,10 @@ test.serial('addedSentences: should return duplicate counter', async (t) => {
test.serial('getStats: should fetch all stats correctly', async (t) => {
const locales = ['en', 'de'];
sequelize.query.onCall(0).resolves([{ 'COUNT(*)': 3 }]);
sequelize.query.onCall(1).resolves([{ 'COUNT(*)': 2 }]);
Sentence.count.onCall(0).resolves(10);
sequelize.query.onCall(1).resolves([{ 'COUNT(*)': 4 }]);
sequelize.query.onCall(2).resolves([{ 'COUNT(*)': 4 }]);
sequelize.query.onCall(3).resolves([{ 'COUNT(*)': 1 }]);
Sentence.count.onCall(1).resolves(15);
Sentence.count.onCall(2).resolves(1000);
Sentence.count.onCall(3).resolves(5);
Expand All @@ -162,10 +164,12 @@ test.serial('getStats: should fetch all stats correctly', async (t) => {
en: {
added: 10,
validated: 3,
rejected: 2,
},
de: {
added: 15,
validated: 4,
rejected: 1,
},
total: 1000,
languages: 5,
Expand Down
4 changes: 3 additions & 1 deletion web/src/components/language-info.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const LanguageInfo = (props) => {
const {
total,
validated,
rejected,
unreviewedByYou,
language,
languageName,
Expand All @@ -18,7 +19,7 @@ const LanguageInfo = (props) => {
<ul>
<li>{total} total sentences.</li>
<li>
{total - validated} sentences in review.&nbsp;
{total - validated - rejected} sentences in review.&nbsp;
</li>
<li>
{unreviewedByYou} sentences left for you to review.&nbsp;
Expand All @@ -30,6 +31,7 @@ const LanguageInfo = (props) => {
)}
</li>
<li>{validated} validated sentences.</li>
<li>{rejected} rejected sentences.</li>
</ul>
</section>
);
Expand Down
1 change: 1 addition & 0 deletions web/src/components/pages/home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const LanguageStats = ({ languages, allLanguages, languageStats, userUnreviewedS
nativeLanguageName={lang.nativeName}
total={languageStats[lang.id].added}
validated={languageStats[lang.id].validated}
rejected={languageStats[lang.id].rejected}
unreviewedByYou={userUnreviewedStats[lang.id]}
/>
)).filter(Boolean);
Expand Down

0 comments on commit 663f351

Please sign in to comment.