Skip to content

Commit

Permalink
Remove unnecessary db call
Browse files Browse the repository at this point in the history
  • Loading branch information
synzen committed Dec 10, 2024
1 parent ab405d7 commit 6ca32a2
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions services/user-feeds/src/articles/articles.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,19 @@ export class ArticlesService {
newArticles,
storedComparisons
);

if (debug) {
logger.datadog(
`Debug feed ${id}: ${articlesPastBlocks.length} articles past blocking comparisons`,
{
articles: articlesPastBlocks.map((a) => ({
id: a.flattened.id,
title: a.flattened.title,
})),
}
);
}

const articlesPassedComparisons = await this.checkPassingComparisons(
{
id,
Expand All @@ -454,6 +467,18 @@ export class ArticlesService {
storedComparisons
);

if (debug) {
logger.datadog(
`Debug feed ${id}: ${articlesPassedComparisons.length} articles past passing comparisons`,
{
articles: articlesPassedComparisons.map((a) => ({
id: a.flattened.id,
title: a.flattened.title,
})),
}
);
}

// any new comparisons stored must re-store all articles
if (newArticles.length > 0) {
await this.storeArticles(id, newArticles, {
Expand Down Expand Up @@ -840,29 +865,14 @@ export class ArticlesService {
return [];
}

const storedComparisonResults = await this.areComparisonsStored(
id,
passingComparisons
);

const relevantComparisons = storedComparisonResults
.filter((r) => r.isStored)
.map((r) => r.field);

if (relevantComparisons.length === 0) {
/**
* Just store the comparison values, otherwise all articles would get delivered since none
* of the comparison values have been seen before.
*/
return [];
}
// All passing comparisons are confirmed to be stored

const articlesToSend = await Promise.all(
seenArticles.map(async (article) => {
const shouldPass = await this.articleFieldsSeenBefore(
id,
article,
relevantComparisons
currentlyStoredComparisons
);

return shouldPass ? null : article;
Expand Down

0 comments on commit 6ca32a2

Please sign in to comment.