From 08856f6eb357d9cb39e355c3cf5e13ddfac1cb22 Mon Sep 17 00:00:00 2001 From: Alfredo Sumaran Date: Tue, 2 Feb 2016 16:02:11 -0500 Subject: [PATCH] Implement ESLint and fixes coding style #6 --- .DS_Store | Bin 6148 -> 0 bytes .eslintrc | 8 ++ .gitignore | 1 + gulpfile.js | 48 +++++++---- lib/elastic.js | 71 +++++++-------- lib/gimporter.js | 170 +++++++++++++++++++----------------- lib/indexer.js | 220 +++++++++++++++++++++++++---------------------- package.json | 5 ++ 8 files changed, 293 insertions(+), 230 deletions(-) delete mode 100644 .DS_Store create mode 100644 .eslintrc diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 0761d8143b38eb0750e48c8f17eff39270870b35..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKPm9w)6o1oJH|@$2S%n2JIVu#{cHM=&W$P+<5Q>pq#MPQ?Vmlf$p-Hz&Ddg@Eeo; z(if~mEh~sjjqw;b#1O$%c(PD!8jJ!)f&Wbb`R=Yl7Y6v=rf~ZG&2GiwIEY1n6>@vH ziGF#a3^RZ-GB^ZtRbS_-&OrhKLfjKN@JwZLrU>RnXLlBODfn;PhfzRz+s}K`{H_r3uiOs~cr@(T7p{vW_jwZaPf?E!c@m0YOAL}Q zJM#2+hdj)C9s6RtADtkz?I+Oz&v?LNp1-J~S=ElcF&bTK?$n)TV{2S@M!Q?hy0g>R z9gppgmUZdM_TKG#>G0-;YdRk?0 z2^F9}*;-w~wV5Ru1&jiJr-0la9CV2;VWClN9T?OV0I`D2$}pE73zFjyx`c&B>_KCi zim0iKt{6A>jn!DwbiHx#C3$MGFi4$RePqEWyoFsndWKicyAzwzh$ z|18No83l|2OQis-?0fqyToRqF3yYIyt%LpuU7D;bG%5-PI*wH$kKzq [cms_config="titulo"]'); var contentHtml = ''; @@ -124,14 +136,13 @@ gimporter.getLegislacionData = function($file, file) { var data = {}; var $view = $file('view'); - $file('version').not(function(i, el){ + $file('version').not(function (i, el) { return cmsConfigExcludes.indexOf(el.attribs.cms_config) > -1; }).each(function (k, v) { + var $el = $file(v); + var childrens = []; - var $el = $file(v), - childrens = []; - - $el.contents().each(function(i, el) { + $el.contents().each(function (i, el) { childrens.push(el); }); @@ -165,7 +176,7 @@ gimporter.getLegislacionData = function($file, file) { return data; }; -gimporter.getJurisprudenciaData = function($file, file) { +gimporter.getJurisprudenciaData = function ($file, file) { var cmsConfigExcludes = ['datos_generales']; var titleEl = $file('view > [cms_config="Sumilla"]'); var contentHtml = ''; @@ -175,13 +186,13 @@ gimporter.getJurisprudenciaData = function($file, file) { var data = {}; var $view = $file('view'); - $file('version').not(function(i, el){ + $file('version').not(function (i, el) { return cmsConfigExcludes.indexOf(el.attribs.cms_config) > -1; }).each(function (k, v) { - var $el = $file(v), - childrens = []; + var $el = $file(v); + var childrens = []; - $el.contents().each(function(i, el) { + $el.contents().each(function (i, el) { childrens.push(el); }); @@ -214,25 +225,24 @@ gimporter.getJurisprudenciaData = function($file, file) { return data; }; -gimporter.getTableName = function($file, file) { +gimporter.getTableName = function ($file) { var fileType = $file('view').attr('cms_collection_id'); if (fileType === LEGISLACION_CODE) { return 'legislations'; - } - else if (fileType === JURISPRUDENCIA_CODE) { + } else if (fileType === JURISPRUDENCIA_CODE) { return 'jurisprudencias'; } - throw 'Archivo con cms_collection_id desconocido'; + throw new Error('Archivo con cms_collection_id desconocido'); }; -gimporter.processFile = function($, file) { +gimporter.processFile = function ($, file) { var deferred = Q.defer(); var data = this.getDataFromFile($, file); + var tableName = this.getTableName($, file); - var tableName = this.getTableName($,file); - this.insertFile(data, tableName).then(function(){ + this.insertFile(data, tableName).then(function () { deferred.resolve(); }); diff --git a/lib/indexer.js b/lib/indexer.js index b0e8bac..385b192 100644 --- a/lib/indexer.js +++ b/lib/indexer.js @@ -1,55 +1,52 @@ -var - elasticsearch = require('elasticsearch'), - mysql = require('mysql'), - Q = require('q'); - - LEGISLACION_TABLE = 'legislations', - JURISPRUDENCIA_TABLE = 'jurisprudencias', - - INDEX_DOCUMENT = 'documento', - - INDEX_INTERVAL = 100, - - FIELD_TITULO_NORMA = 'titulo_norma', - FIELD_CONTENT = 'content', - FIELD_ID = 'id', - - INDEX_FIELDS = [FIELD_ID, FIELD_TITULO_NORMA, FIELD_CONTENT], - DOCUMENT_TABLES = [LEGISLACION_TABLE, JURISPRUDENCIA_TABLE], - - indexer = {}, - DOC_TYPES = {}; - - DOC_TYPES[LEGISLACION_TABLE] = 'legislacion'; - DOC_TYPES[JURISPRUDENCIA_TABLE] = 'jurisprudencias'; - -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 +var elasticsearch = require('elasticsearch'); +var mysql = require('mysql'); +var Q = require('q'); + +var indexer = {}; +var DOC_TYPES = {}; +var connection = {}; +var client = {}; + +var LEGISLACION_TABLE = 'legislations'; +var JURISPRUDENCIA_TABLE = 'jurisprudencias'; +var INDEX_DOCUMENT = 'documento'; +var INDEX_INTERVAL = 100; +var FIELD_TITULO_NORMA = 'titulo_norma'; +var FIELD_CONTENT = 'content'; +var FIELD_ID = 'id'; +var INDEX_FIELDS = [FIELD_ID, FIELD_TITULO_NORMA, FIELD_CONTENT]; +var DOCUMENT_TABLES = [LEGISLACION_TABLE, JURISPRUDENCIA_TABLE]; + +DOC_TYPES[LEGISLACION_TABLE] = 'legislacion'; +DOC_TYPES[JURISPRUDENCIA_TABLE] = 'jurisprudencias'; + +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 }); connection.connect(); -var client = new elasticsearch.Client({ +client = new elasticsearch.Client({ host: 'localhost:9200' }); indexer.indexRows = function (rows, docType) { - var deferred = Q.defer(), - data = []; - - + var deferred = Q.defer(); + var data = []; - rows.map(function(item) { - var content = item.content.trim(), - title = item.titulo_norma.trim(), - id = item.id; + rows.map(function (item) { + var content = item.content.trim(); + var title = item.titulo_norma.trim(); + var id = item.id; + var docData = {}; + var docAction = {}; - var docData = { + docData = { title: title, content: content, suggest: { @@ -62,11 +59,11 @@ indexer.indexRows = function (rows, docType) { } }; - var docAction = { + docAction = { index: { _index: INDEX_DOCUMENT, _type: docType, - _id: id, + _id: id } }; @@ -76,7 +73,7 @@ indexer.indexRows = function (rows, docType) { client.bulk({ body: data - }, function(err, response) { + }, function (err) { if (err) { throw err; } @@ -89,76 +86,81 @@ indexer.indexRows = function (rows, docType) { function temp(from, total, tableName) { var deferred = Q.defer(); + indexer.indexRowsRange(from, tableName) - .then(function() { + .then(function () { + var newFrom; if (from + INDEX_INTERVAL >= total) { deferred.resolve(); } else { - var newFrom = from + INDEX_INTERVAL; + newFrom = from + INDEX_INTERVAL; temp(newFrom, total, tableName); } }); + return deferred.promise; } -function temp_reindex(from, total, tableName){ +function tempReindex(from, total, tableName) { var deferred = Q.defer(); - indexer.indexRowsLeft( tableName) - .then(function() { + + indexer.indexRowsLeft(tableName) + .then(function () { + var newFrom; if (from + INDEX_INTERVAL >= total) { deferred.resolve(); } else { - var newFrom = from + INDEX_INTERVAL; - temp_reindex(newFrom, total, tableName); + newFrom = from + INDEX_INTERVAL; + tempReindex(newFrom, total, tableName); } }); + return deferred.promise; } indexer.indexTable = function (tableName) { - var self = this, - docType = DOC_TYPES[tableName]; + connection.query('SELECT count(*) as total FROM ??', [tableName], function (err, results) { + var from; + var total; - connection.query('SELECT count(*) as total FROM ??', [tableName], function(err, results) { if (err) { throw err; } - var from = 0, - total = results[0].total; + from = 0; + total = results[0].total; - temp(from, total, tableName) - .then(function() { - }); + temp(from, total, tableName); }); }; -indexer.getRowsLeft = function(tableName){ - var self = this, - docType = DOC_TYPES[tableName]; +indexer.getRowsLeft = function (tableName) { + var queryParams = [tableName, 'NO']; - connection.query('SELECT count(*) as total FROM ?? WHERE indexado=?', [tableName,'NO'], function(err, results) { - if (err) { - throw err; - } + connection.query('SELECT count(*) as total FROM ?? WHERE indexado=?', queryParams, + function (err, results) { + var from; + var total; - var from = 0, - total = results[0].total; + if (err) { + throw err; + } - temp_reindex(from, total, tableName) - .then(function() { - }); + from = 0; + total = results[0].total; - }); + tempReindex(from, total, tableName); + }); }; -indexer.indexRowsRange = function(from, tableName) { - var self = this, - deferred = Q.defer(); +indexer.indexRowsRange = function (from, tableName) { + var self = this; + var deferred = Q.defer(); + var queryParams = [INDEX_FIELDS, tableName, from, 100]; - var q = connection.query('SELECT ?? FROM ?? LIMIT ?,?', [INDEX_FIELDS, tableName, from, 100], function (err, rows) { + connection.query('SELECT ?? FROM ?? LIMIT ?,?', queryParams, function (err, rows) { if (err) { throw err; } @@ -170,43 +172,59 @@ indexer.indexRowsRange = function(from, tableName) { }); return deferred.promise; -} +}; -indexer.indexRowsLeft = function( tableName) { - var self = this, - deferred = Q.defer(); +indexer.indexRowsLeft = function (tableName) { + var self = this; + var deferred = Q.defer(); + var indexed = 'NO'; + var queryParams = ['a']; - 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;}); + queryParams = [INDEX_FIELDS, tableName, indexed, 0, 100]; - self.indexRows(rows, DOC_TYPES[tableName]) - .then(function () { - self.updateRecord(ids,tableName).then(function(){ - deferred.resolve(); - }); + connection.query('SELECT ?? FROM ?? WHERE indexado = ? LIMIT ?,?', queryParams, + function (err, rows) { + var ids; + + if (err) { + throw err; + } + + 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) { +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) { + var data = { + indexado: 'SI' + }; + var queryParams = [tableName, data, ids]; + + connection.query('UPDATE ?? SET ? WHERE id IN (?)', queryParams, + function (err) { if (err) { throw err; } deferred.resolve(); }); - return deferred.promise; + + return deferred.promise; }; -indexer.perform = function() { +indexer.perform = function () { var self = this; DOCUMENT_TABLES.forEach(function (tableName) { @@ -216,9 +234,9 @@ indexer.perform = function() { indexer.reindex = function () { var self = this; - DOCUMENT_TABLES.forEach(function (tableName){ + DOCUMENT_TABLES.forEach(function (tableName) { self.getRowsLeft(tableName); }); -} +}; -module.exports = indexer; \ No newline at end of file +module.exports = indexer; diff --git a/package.json b/package.json index 7a3f06b..8804c4f 100644 --- a/package.json +++ b/package.json @@ -16,5 +16,10 @@ "gulp-cheerio": "^0.6.2", "mysql": "^2.10.2", "q": "^1.4.1" + }, + "devDependencies": { + "eslint": "^1.10.3", + "eslint-config-airbnb": "^4.0.0", + "gulp-eslint": "^1.1.1" } }