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

Commit

Permalink
feat: revisited logic for AD lookup by channel, extended mongodb index
Browse files Browse the repository at this point in the history
  • Loading branch information
vecna committed Nov 15, 2021
1 parent 3606b1f commit da49e82
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
33 changes: 17 additions & 16 deletions backend/routes/ads.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions backend/scripts/build-indexes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit da49e82

Please sign in to comment.