Skip to content

Commit

Permalink
Update delivery queries
Browse files Browse the repository at this point in the history
  • Loading branch information
zarathustra323 committed Jan 17, 2019
1 parent 77aa695 commit 56a6265
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
25 changes: 12 additions & 13 deletions services/delivery/src/deliver/get-ad.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const moment = require('moment');
const { mongo } = require('../connections');
const { log, randomElement } = require('../utils');

const getSchedules = (adUnitId, date) => {
const getSchedules = (adunitId, date) => {
const pipeline = [
{ $match: { adUnitId, start: { $gte: date }, end: { $lte: date } } },
{ $match: { adunitId, start: { $lte: date }, end: { $gte: date } } },
{ $project: { _id: 1, priority: 1 } },
{ $group: { _id: '$priority', ids: { $push: '$_id' } } },
{ $sort: { _id: -1 } },
Expand All @@ -23,23 +22,23 @@ const getSchedules = (adUnitId, date) => {
const updateCorrelator = async (correlator, adUnit, schedule, ad) => {
const { src, url } = ad;
const adId = ad._id;
const adUnitId = adUnit._id;
mongo.db().collection('correlators').updateOne({ correlator }, { $set: { src, url, adId } }, { upsert: true });
const adunitId = adUnit._id;
mongo.db().collection('correlators').updateOne({ value: correlator }, { $set: { src, url, adId } }, { upsert: true });

const { deploymentId, publisherId } = adUnit;
const { lineItemId } = schedule;
const { lineitemId } = schedule;
const lineItemOpts = {
projection: {
orderId: 1,
advertiserId: 1,
},
};
const lineItem = await mongo.db().collection('lineitems').findOne({ _id: lineItemId }, lineItemOpts);
const lineItem = await mongo.db().collection('lineitems').findOne({ _id: lineitemId }, lineItemOpts);
const { orderId, advertiserId } = lineItem;
const eventSet = {
adUnitId,
adunitId,
adId,
lineItemId,
lineitemId,
orderId,
advertiserId,
deploymentId,
Expand All @@ -52,15 +51,15 @@ const updateCorrelator = async (correlator, adUnit, schedule, ad) => {
/**
* Retrieves an ad for the request
*/
module.exports = async (correlator, adUnit, strdate) => {
const date = moment(strdate).toDate(); // @todo moment
const correlated = await mongo.db().collection('correlators').findOne({ correlator });
module.exports = async (correlator, adUnit, date) => {
const correlated = await mongo.db().collection('correlators').findOne({ value: correlator });
if (correlated) return correlated;

const schedules = await getSchedules(adUnit._id, date).toArray();
log({ schedules });
if (!schedules.length) return null;

const scheduleId = randomElement(schedules);
const scheduleId = randomElement(schedules[0].ids);
const schedule = await mongo.db().collection('schedules').findOne({ _id: scheduleId });
if (!schedule) return null;

Expand Down
13 changes: 9 additions & 4 deletions services/delivery/src/deliver/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
const moment = require('moment');
const recordEvent = require('./record-event');
const getAd = require('./get-ad');
const { log } = require('../utils');

module.exports = async (adUnit, type, query) => {
// @todo throw 400 error if date, rand, or email are missing.
const adUnitId = adUnit._id;
const { width, height } = adUnit;
const { date, email, rand } = query;
const { date: dateStr, email, rand } = query;

const date = moment(dateStr).toDate();

if (query.placeholder) {
return {
src: `https://placehold.it/${width}x${height}`,
Expand All @@ -14,15 +19,15 @@ module.exports = async (adUnit, type, query) => {
}

const correlate = {
adUnitId,
date,
adUnitId: `${adUnitId}`,
date: date.valueOf(),
email,
rand,
};
const correlator = Buffer.from(JSON.stringify(correlate)).toString('base64');

// write the request event
recordEvent(correlator);
if (type === 'view') recordEvent(correlator);
// write the click/view event
recordEvent(correlator, type);

Expand Down
2 changes: 1 addition & 1 deletion services/delivery/src/routes/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = (app) => {
if (!adunit) return res.status(404).send('Invalid Ad Unit');

const winner = await deliver(adunit, 'view', query);
if (!winner) return res.status(204).send();
if (!winner) return res.status(200).send();

return res.redirect(302, winner.src);
}));
Expand Down
4 changes: 2 additions & 2 deletions services/delivery/src/utils/get-ad-unit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { ObjectId } = require('mongodb');
const { mongo } = require('../connections');

const fields = { width: 1, height: 1 };
const projection = { width: 1, height: 1 };
const criteria = id => ({ _id: new ObjectId(id), deleted: false });

module.exports = async id => mongo.db().collection('adunits').findOne(criteria(id), fields);
module.exports = async id => mongo.db().collection('adunits').findOne(criteria(id), { projection });

0 comments on commit 56a6265

Please sign in to comment.