Skip to content

Instantly share code, notes, and snippets.

@stabenfeldt
Last active August 27, 2015 09:55
Show Gist options
  • Save stabenfeldt/e8bbe39dd9b6c9278548 to your computer and use it in GitHub Desktop.
Save stabenfeldt/e8bbe39dd9b6c9278548 to your computer and use it in GitHub Desktop.
'use strict';
/*eslint-disable dot-notation */
/*eslint-disable handle-callback-err */
var async = require('async');
var config = require('../../config/config');
var request = require('superagent');
var mongoose = require('mongoose');
var mongoUrl = config.get('mongoUrl');
var Schema = mongoose.Schema;
var Faker = require('faker');
var elasticsearchUrl = config.get('elasticsearchUrl');
var ESUrl = 'http://'+elasticsearchUrl+'/harvester-test/entries';
mongoose.connect(mongoUrl);
var entrySchema = new Schema({
createdAt: Date,
description: String,
guid: String,
host: String,
image: String,
origin: String,
posted: Date,
source: String,
tags: Array,
title: String,
updateTime: Date,
url: String
});
var Entry = mongoose.model('Entry', entrySchema);
function fetchFromEs(mongo_id, next) {
console.log('fetchFromEs VISES ALDRI');
var url = ESUrl + '/_search?q=_id:'+mongo_id;
request
.get(url)
.end(function (err, res) {
next( res.text );
});
}
function waitForEsToIndex(mongo_id, next) {
console.log('waitForEsToIndex');
process.stdout.write(`Waiting for ${ mongo_id } to be indexed by Elasticsearch\r VISES`);
setTimeout(function () {
fetchFromEs(mongo_id, function(status) {
if ( status.match( '"total":0' ) ) {
console.log(`Status: ${ status} VISES ALDRI`);
waitForEsToIndex(mongo_id, next);
} else {
console.log(`OK: ${ status} VISES ALDRI`);
next();
}
});
}, 400);
}
function saveToMongo(entry_object, next) {
console.log('saveToMongo');
var entry = new Entry(entry_object);
entry.save(function (err) {
if (err) {
console.log('failed to save');
next(new Error("Could not save to MongoDB" + err.message));
} else {
next(null, entry['_id'] );
}
});
}
function saveEntry(entry_object, next) {
console.log('saveEntry');
/*eslint-disable no-shadow */ //=> (next)
async.waterfall([
function(next) { saveToMongo(entry_object, next); },
function(mongo_id, next) { waitForEsToIndex(mongo_id, next); }
], function (err) {
next(err, entry_object);
});
/*eslint-enable no-shadow */
}
/****************************************************************************************
*
* Creates an entry and saves it.
* You can override default values if necessary.
*
* Fabricate.entry({ title: 'Party mode!', next: next })
*
****************************************************************************************
*/
var today = (new Date()).toISOString();
var Fabricate = {
entry: function({
createdAt = today,
description = Faker.lorem.sentence(),
guid = Math.random().toString(36).substring(2),
host = Math.random().toString(36).substring(2),
image = Faker.image.imageUrl(),
next = () => { console.log("Fabricate requires next method"); },
origin = 'db.no',
posted = today,
tags = [ 'nyheter', 'regionale' ],
title = 'default title',
updateTime = today,
url = Math.random().toString(36).substring(2) +"."+ Faker.internet.domainName()
}) {
var entry_object = {
createdAt: createdAt,
description: description,
guid: guid,
host: host,
image: image,
origin: origin,
posted: posted,
tags: tags,
title: title,
updateTime: updateTime,
url: url
};
saveEntry(entry_object, next);
}
};
module.exports = Fabricate;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment