Skip to content

Instantly share code, notes, and snippets.

@aakashlpin
Created August 24, 2016 12:44
Show Gist options
  • Save aakashlpin/3df846ea39dc6b7472fb3638dae0c60a to your computer and use it in GitHub Desktop.
Save aakashlpin/3df846ea39dc6b7472fb3638dae0c60a to your computer and use it in GitHub Desktop.
diff --git a/app/index.html b/app/index.html
index 9d28c87..ae6664f 100644
--- a/app/index.html
+++ b/app/index.html
@@ -199,7 +199,7 @@
</div>
</div>
-<!-- <div class="bg-light">
+ <div class="bg-light">
<div class="section-padded section-sidewise-padded">
<h1 class="text-center mb-30">Everything people are tracking</h1>
<h4 class="text-light">Narrow down products by sellers:</h4>
@@ -232,7 +232,7 @@
<img src="images/loader.gif" style="width: 32px;" alt="Loading..">
</div>
</div>
- -->
+
<div class="bg-light">
<footer class="footer">
<ul class="list-unstyled list-inline">
diff --git a/server/app/controllers/jobs.js b/server/app/controllers/jobs.js
index bdcebfc..2eb8f7d 100644
--- a/server/app/controllers/jobs.js
+++ b/server/app/controllers/jobs.js
@@ -256,7 +256,7 @@ function createSearchForFullContacts () {
function init() {
//for request from home page, pre-process all products and keep the data in memory
- // createCronTabForAllProducts();
+ createCronTabForAllProducts();
if (!config.isCronActive) {
logger.log('info', '=========== Cron Jobs are disabled =============');
diff --git a/server/app/lib/background.js b/server/app/lib/background.js
index 23051ee..f5bff3a 100644
--- a/server/app/lib/background.js
+++ b/server/app/lib/background.js
@@ -17,14 +17,6 @@ var currentDeal;
var processedData = [];
var totalPages = 0;
-function getLeastPriceFromHistory (history) {
- if (!history.length) {
- return null;
- }
-
- return _.sortBy(history, 'price')[0];
-}
-
function refreshDeal (callback) {
callback = callback || function() {};
@@ -85,50 +77,61 @@ module.exports = {
processAllProducts: function () {
logger.log('info', 'processing all products for home page', {at: moment().format('MMMM Do YYYY, h:mm:ss a')});
var allTracks = [];
- var hotCache = {};
- async.each(_.keys(config.sellers), function (seller, asyncEachCb) {
+ async.each(_.keys(config.sellers), function (seller, sellerAsyncCb) {
var sellerModel = sellerUtils.getSellerJobModelInstance(seller);
-
- sellerModel
- .find({}, {isActive: 0, productPriceHistory: 0})
- .lean()
- .exec(function(err, sellerJobs) {
- async.each(sellerJobs, function (sellerJob, sellerJobAsyncCb) {
- sellerModel
- .findOne({email: sellerJob.email, productURL: sellerJob.productURL}, {email: 0, alertFromPrice: 0, alertToPrice: 0, source: 0})
- .lean()
- .exec(function (err, sjph) {
- var dataInCache = hotCache[sjph.productURL] || false;
- var leastPriceForJob;
- if (dataInCache) {
- dataInCache.eyes += 1;
- //compare with the productPriceHistory of this simiar track
- //if lesser price is found, update the entry
- leastPriceForJob = getLeastPriceFromHistory(sjph.productPriceHistory);
- if (leastPriceForJob && (leastPriceForJob.price < dataInCache.ltp)) {
- dataInCache.ltp = leastPriceForJob.price;
- dataInCache._id = leastPriceForJob._id ? leastPriceForJob._id.toHexString(): dataInCache._id;
+ var sellerModelProductPriceHistory = sellerUtils.getSellerProductPriceHistoryModelInstance(seller);
+
+ sellerModelProductPriceHistory
+ .aggregate(
+ [
+ {
+ $group:
+ {
+ _id: '$jobId',
+ min: { $min: '$price' }
+ }
+ }
+ ], function (err, aggregatedResults) {
+ console.log(aggregatedResults);
+ async.each(aggregatedResults, function (aggregatedResult, aggregatedResultAsynCb) {
+ sellerModel
+ .findOne(
+ {
+ _id: aggregatedResult._id
+ },
+ {
+ productURL : 1,
+ productName : 1,
+ productImage : 1,
+ currentPrice : 1
+ }
+ )
+ .lean()
+ .exec(function (err, storedJob) {
+ if (err) {
+ return aggregatedResultAsynCb(err);
}
+ _.extend(storedJob, {
+ ltp : aggregatedResult.min,
+ seller : seller
+ });
- } else {
- sjph.eyes = 1;
- sjph.seller = seller;
- leastPriceForJob = getLeastPriceFromHistory(sjph.productPriceHistory);
- sjph.ltp = leastPriceForJob ? ( leastPriceForJob.price < sjph.currentPrice ? leastPriceForJob.price : sjph.currentPrice ) : sjph.currentPrice;
- delete sjph.productPriceHistory;
- hotCache[sjph.productURL] = sjph;
+ allTracks.push(storedJob);
+
+ aggregatedResultAsynCb();
+
+ });
+
+ }, function (err) {
+ if (err) {
+ return sellerAsyncCb(err);
}
- sellerJobAsyncCb();
+ sellerAsyncCb();
});
- }, function () {
- allTracks.push(_.values(hotCache));
- hotCache = {};
- asyncEachCb(err);
- });
- });
-
+ }
+ );
}, function () {
// processedData = _.sortBy(_.flatten(allTracks, true), 'eyes').reverse();
processedData = _.shuffle(_.flatten(allTracks, true));
@@ -137,7 +140,6 @@ module.exports = {
//make them available for GC
allTracks = null;
- hotCache = null;
if (processedData.length < initialBatchSize) {
totalPages = 1;
diff --git a/server/app/lib/jobs.js b/server/app/lib/jobs.js
index 9625321..add6f06 100644
--- a/server/app/lib/jobs.js
+++ b/server/app/lib/jobs.js
@@ -164,31 +164,19 @@ function handleJobComplete (job) {
var scrapedPrice = jobResult.productPrice;
- //modify the DB's currentPrice field and productPriceHistory array
+ //modify the DB's currentPrice field
sellerUtils
.getSellerJobModelInstance(jobData.seller)
- .findById(jobData._id, function(err, storedJob) {
- if (err || !storedJob || (storedJob && !storedJob.productPriceHistory)) {
+ .findById(jobData._id, {productPriceHistory: 0}, function(err, storedJob) {
+ if (err || !storedJob) {
logger.log('error', 'error getting seller job id #%d for seller %s when job complete', jobData._id, jobData.seller);
removeJob(job);
} else {
// update params
- var updateWith = {};
-
- /*TODO
- unwind all the array entries and create a new document out of each object
- store it a new collection called <seller_code>product_price_history
- */
- storedJob.productPriceHistory.push({
- date: new Date(),
- price: scrapedPrice
- });
-
- _.extend(updateWith, {
- currentPrice: scrapedPrice,
- productPriceHistory: storedJob.productPriceHistory
- });
+ var updateWith = {
+ currentPrice: scrapedPrice
+ };
if (sendAlert(jobData, jobResult)) {
//if going to send an aler, update relevant params
@@ -206,6 +194,24 @@ function handleJobComplete (job) {
}
removeJob(job);
});
+
+ sellerUtils
+ .getSellerProductPriceHistoryModelInstance(jobData.seller)
+ .collection
+ .insert({
+ jobId: jobData._id,
+ price: scrapedPrice,
+ email: storedJob.email,
+ productURL: storedJob.productURL,
+ date: new Date()
+ }, function (err, pphDoc) {
+ if (err) {
+ logger.log('error', 'error inserting new product price history doc in db', {error: err});
+ }
+ if (pphDoc) {
+ logger.log('info', 'inserted new product price history doc in db', pphDoc);
+ }
+ });
}
});
}
diff --git a/server/app/models/sellerJob.js b/server/app/models/sellerJob.js
index d2fbaa6..47561ad 100644
--- a/server/app/models/sellerJob.js
+++ b/server/app/models/sellerJob.js
@@ -30,38 +30,6 @@ SellerJobSchema.statics.removeJob = function(query, callback) {
this.find(query).remove(callback);
};
-// SellerJobSchema.statics.updateNewPrice = function(query, updateWith, callback) {
-// var newPrice = updateWith.price;
-// this.findOne(query, function(err, doc) {
-// if (!err && doc) {
-// var updateParams = {
-// productPriceHistory: doc.productPriceHistory
-// };
-// var updateOptions = {};
-
-// updateParams.productPriceHistory.push({
-// date: new Date(),
-// price: newPrice
-// });
-
-// updateParams.currentPrice = newPrice;
-
-// if (updateWith.alertFromPrice) {
-// updateParams.alertFromPrice = updateWith.alertFromPrice;
-// }
-
-// if (updateWith.alertToPrice) {
-// updateParams.alertToPrice = updateWith.alertToPrice;
-// }
-
-// this.update(query, updateParams, updateOptions, callback);
-
-// } else {
-// callback(err, null);
-// }
-// }.bind(this));
-// };
-
SellerJobSchema.statics.getOneGeneric = function(query, callback) {
this.findOne(query).lean().exec(callback);
};
@@ -77,38 +45,6 @@ SellerJobSchema.statics.addJob = function(jobData, callback) {
(new this(data)).save(callback);
};
-// SellerJobSchema.statics.markActive = function(done) {
-// this.update({}, {isActive: true}, {multi: true}, function(err, doc) {
-// if (err) {
-// console.log('error while marking active at db level', err);
-// done(err);
-// return;
-// }
-// done(null, doc);
-// });
-// };
-
-// SellerJobSchema.statics.normalizeURL = function(callback) {
-// this.find({}, {productURL: 1}, function(err, records) {
-// if (err) {
-// console.log('error', 'error getting records', err);
-// return callback('error');
-// }
-// records.forEach(function(record) {
-// var currentURL = record.productURL;
-// var normalizedURL = sellerUtils.getDeepLinkURL('flipkart', currentURL);
-// this.update({_id: record._id}, {productURL: normalizedURL}, {}, function(err, updatedDoc) {
-// if (err) {
-// console.log('error', 'error updating record', err);
-// } else {
-// console.log('info', 'productURL changed to ', normalizedURL);
-// }
-// });
-// }.bind(this));
-// callback(null);
-// }.bind(this));
-// };
-
var sellers = _.keys(config.sellers);
_.each(sellers, function(seller) {
var modelName = seller + '_job';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment