Skip to content

Commit

Permalink
Merge pull request #3 from asumaran/issue_2
Browse files Browse the repository at this point in the history
Reindexand valores
  • Loading branch information
asumaran committed Feb 1, 2016
2 parents 70b26d6 + 54435c6 commit c6395ae
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 24 deletions.
Binary file added .DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ gulp.task('index', function() {
indexer.perform();
});

gulp.task('reindex', function () {
indexer.reindex();
});

gulp.task('run', ['import', 'process_template', 'index']);

gulp.task('default', ['import'], function() {
Expand Down
68 changes: 46 additions & 22 deletions lib/gimporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ var
dotenv.config();

var connection = mysql.createConnection({
host : process.env.DB_HOST || '127.0.0.1',
user : process.env.DB_USER,
password : process.env.DB_PASSWORD,
database : process.env.DB_DATABASE,
port : process.env.DB_PORT || 3306,
dateStrings : true
host: process.env.DB_HOST || '127.0.0.1',
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
port: process.env.DB_PORT || 3306,
dateStrings: true
});

connection.connect();
Expand Down Expand Up @@ -48,21 +48,56 @@ function unwrap(childrens) {
return escapedChildrens;
}

gimporter.updateRecord = function (data,tableName) {
var deferred = Q.defer();
data.indexado = 'NO';
data.is_new = 'NO';

connection.query('UPDATE ?? SET ? WHERE cms_id = ?', [tableName, data, data.cms_id], function (err, rows, fields) {
if (err) {
throw err;
}
deferred.resolve();
});

return deferred.promise;
};

gimporter.addRecord = function (data,tableName) {
var deferred = Q.defer();
connection.query('INSERT INTO ?? SET ?', [tableName, data], function (err, rows, fields) {
if (err) {
throw err;
}
deferred.resolve();
});
return deferred.promise;
};

gimporter.insertFile = function(data, tableName) {
var deferred = Q.defer();
var self = this;
var baseData = {
created_at: new Date(),
updated_at: new Date()
};

var data2 = extend(data, baseData);
connection.query('INSERT INTO ' + tableName + ' SET ?', data2, function (err, rows, fields) {
var q = connection.query('SELECT * FROM ?? WHERE cms_id = ? LIMIT 1', [tableName, data.cms_id], function (err, rows) {
if (err) {
throw err;
}
deferred.resolve();
});
var data2 = extend(data, baseData);
if(rows[0]){
self.updateRecord(data2,tableName).then(function () {
deferred.resolve();
});

}else{
self.addRecord(data2,tableName).then(function () {
deferred.resolve();
});

}
});
return deferred.promise;
};

Expand Down Expand Up @@ -112,8 +147,6 @@ gimporter.getLegislacionData = function($file, file) {
data = {
titulo_norma: titleEl.text() || '',
titulo_norma_html: titleEl.html() || '',
//Add en laravel
//field longtext
content_html: contentHtml,
content: contentText,
number: $view.attr('number'),
Expand Down Expand Up @@ -164,23 +197,14 @@ gimporter.getJurisprudenciaData = function($file, file) {
data = {
titulo_norma: titleEl.text() || '',
titulo_norma_html: titleEl.html() || '',
//Add en laravel
//field longtext
content_html: contentHtml,
content: contentText,
//Add Laravel
// size 25
numero_norma: $view.attr('numero'),
//Add Laravel
// size 20
anio: $view.attr('anio') || '',
tipo_norma_jurisp: $view.attr('tipo_norma_jurisp'),
emisor_jurisp: $view.attr('emisor_jurisp') || '',
cms_collection_name: $view.attr('cms_collection_name'),
//Add en Laravel
//cms_collection_id: $view.attr('cms_collection_id'),
cms_config_id: $view.attr('cms_config_id'),
//Add en Laravel cms_config 20
cms_config: $view.attr('cms_config'),
cms_id: $view.attr('cms_id'),
cms_parent_name: $view.attr('cms_parent_name'),
Expand Down
77 changes: 75 additions & 2 deletions lib/indexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ indexer.indexRows = function (rows, docType) {
if (err) {
throw err;
}
console.log('Finalizó indexación');
deferred.resolve();
});

Expand All @@ -102,6 +101,21 @@ function temp(from, total, tableName) {
return deferred.promise;
}

function temp_reindex(from, total, tableName){
var deferred = Q.defer();
indexer.indexRowsLeft( tableName)
.then(function() {
if (from + INDEX_INTERVAL >= total) {
deferred.resolve();
} else {
var newFrom = from + INDEX_INTERVAL;
temp_reindex(newFrom, total, tableName);
}
});
return deferred.promise;
}


indexer.indexTable = function (tableName) {
var self = this,
docType = DOC_TYPES[tableName];
Expand All @@ -120,12 +134,30 @@ indexer.indexTable = function (tableName) {
});
};

indexer.getRowsLeft = function(tableName){
var self = this,
docType = DOC_TYPES[tableName];

connection.query('SELECT count(*) as total FROM ?? WHERE indexado=?', [tableName,'NO'], function(err, results) {
if (err) {
throw err;
}

var from = 0,
total = results[0].total;

temp_reindex(from, total, tableName)
.then(function() {
});

});
};


indexer.indexRowsRange = function(from, tableName) {
var self = this,
deferred = Q.defer();


var q = connection.query('SELECT ?? FROM ?? LIMIT ?,?', [INDEX_FIELDS, tableName, from, 100], function (err, rows) {
if (err) {
throw err;
Expand All @@ -140,6 +172,40 @@ indexer.indexRowsRange = function(from, tableName) {
return deferred.promise;
}

indexer.indexRowsLeft = function( tableName) {
var self = this,
deferred = Q.defer();

var q = connection.query('SELECT ?? FROM ?? WHERE indexado = ? LIMIT ?,?', [INDEX_FIELDS, tableName, 'NO', 0, 100], function (err, rows) {
if (err) {
throw err;
}
var ids = rows.map(function(a) {return a.id;});

self.indexRows(rows, DOC_TYPES[tableName])
.then(function () {
self.updateRecord(ids,tableName).then(function(){
deferred.resolve();
});
});
});

return deferred.promise;
}

indexer.updateRecord = function (ids,tableName) {
var deferred = Q.defer();
var data = {indexado: 'SI'};
var q = connection.query('UPDATE ?? SET ? WHERE id IN (?)', [tableName, data, ids], function (err, rows, fields) {
if (err) {
throw err;
}
deferred.resolve();
});
return deferred.promise;
};


indexer.perform = function() {
var self = this;

Expand All @@ -148,4 +214,11 @@ indexer.perform = function() {
});
};

indexer.reindex = function () {
var self = this;
DOCUMENT_TABLES.forEach(function (tableName){
self.getRowsLeft(tableName);
});
}

module.exports = indexer;

0 comments on commit c6395ae

Please sign in to comment.