From da49e829841ae103fd512719e6cdf10196068353 Mon Sep 17 00:00:00 2001 From: Claudio Date: Mon, 15 Nov 2021 12:19:54 +0100 Subject: [PATCH] feat: revisited logic for AD lookup by channel, extended mongodb index --- backend/routes/ads.js | 33 ++++++++++++++++---------------- backend/scripts/build-indexes.js | 1 + 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/backend/routes/ads.js b/backend/routes/ads.js index cfb66d735..42ccd61fd 100644 --- a/backend/routes/ads.js +++ b/backend/routes/ads.js @@ -42,35 +42,36 @@ async function perChannel(req) { ]} }; + /* the logic is otherway around compared to the function + * below, as we initially pick from metadata and then lookup to + * ad. this impact the filtering/map function below */ const r = await mongo3.aggregate(mongoc, - nconf.get('schema').ads, [ + nconf.get('schema').metadata, [ { $sort: { savingTime: -1} }, { $match: filter }, { $limit: 400 }, { $lookup: { - from: 'metadata', foreignField: 'id', - localField: 'metadataId', as: 'metadata' } + from: 'ads', foreignField: 'metadataId', + localField: 'id', as: 'ad' } } ]); - debug("look ads by Channel (%s) found %d matches, hardcoded 400 max", - channelId, r.length); + debug("looking for metadata by Channel (%j) found %d matches, hardcoded 400 max", + filter, r.length); await mongoc.close(); - const x = _.compact(_.map(r, function(adret) { - const rv = _.pick(adret, - ['href', 'selectorName', 'sponsoredName', 'sponsoredSite', 'savingTime'] - ); - if(adret.metadata && - adret.metadata.length && - adret.metadata[0].type === 'video') { - rv.authorName = adret.metadata[0].authorName; - rv.authorSource = adret.metadata[0].authorSource; - rv.videoTitle = adret.metadata[0].title; + const x = _.compact(_.map(r, function(metaret) { + if(metaret.ad && metaret.ad.length) { + metaret.sponsoredName = metaret.ad[0].sponsoredName; + metaret.sponsoredSite = metaret.ad[0].sponsoredSite; + metaret.selectorName = metaret.ad[0].selectorName; } else return null; - return rv; + + return _.pick(metaret, ['href', 'selectorName', + 'sponsoredName', 'sponsoredSite', 'authorName', + 'authorSource', 'title', 'savingTime']); })); debug("ads by Channel, selected results %d", x.length); diff --git a/backend/scripts/build-indexes.js b/backend/scripts/build-indexes.js index 9d505c288..e084ba652 100644 --- a/backend/scripts/build-indexes.js +++ b/backend/scripts/build-indexes.js @@ -4,6 +4,7 @@ ret = db.metadata.createIndex({ type: 1 }); checkret('metadata type', ret); ret = db.metadata.createIndex({ "related.videoId": 1 }); checkret('metadata related.videoId', ret); ret = db.metadata.createIndex({ "selected.videoId": 1 }); checkret('metadata selected.videoId', ret); ret = db.metadata.createIndex({ authorName: 1 }); checkret('metadata authorName', ret); +ret = db.metadata.createIndex({ authorSource: 1 }); checkret('metadata authorSource', ret); ret = db.metadata.createIndex({ savingTime: -1 }); checkret('metadata savingTime', ret); ret = db.metadata.createIndex({ "experiment.experimentId": 1 }); checkret('metadata experiment.experimentId', ret);