-
Notifications
You must be signed in to change notification settings - Fork 0
/
bvc.js
756 lines (635 loc) · 27.8 KB
/
bvc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
exports.BvClient = function (options) {
/**
* Module dependencies.
*/
var async = require('async'),
fs = require('fs'),
util = require('util'),
_ = require('underscore'),
program = require('commander'),
logger = require('logger'),
mysql = require('db-mysql'),
mongo = require('mongoskin');
var self = this;
var MODULE_CONFIG = {
mysqldb : function (cluster) {
var type = 'a';
var servers = {
prod: {
host: 'localhost',
port: 3306,
user: "test",
pwd: "test",
db: 'local'
},
bi: {
host: 'localhost',
port: 5029,
user: "test",
pwd: "test",
db: "local_bi"
},
nightly: {
host: 'localhost',
port: 3306,
user: "test",
pwd: "test",
db: 'local'
}};
if (cluster.indexOf(':') > 0) {
var clusterServer = cluster.split(":");
cluster = clusterServer[0];
type = clusterServer[1];
}
var server = servers[type];
// specific server for c7
if (cluster == "none" && type == "b") {
server.host = "localhost-new";
server.user = "test2";
server.pwd = "test2";
}
server.host = self.getCommand(server.host, {CLUSTER : cluster});
server.cluster = cluster;
return server;
},
mongodb : function (aliasName) {
var servers = {
local: {
host: 'localhost',
port: 0,
db: 'test'
}
};
if (aliasName.indexOf(':') > 0) {
var serverEntry = aliasName.split(":");
return {
host: serverEntry[0],
port: 27017,
db: serverEntry[1]
};
}
return servers[aliasName || 'local'];
}
};
var CONFIG = {
mysqlProductPageLinksLimit: 10,
emailsActivetime: '2012-10-10',
mysqlClientRecordsLimit: 0,
mongoSelectCondition: {},
mongoFiledsToSelect: false, // default fields: _id:0,name:1,bundle:1
mongoRecordsSkip: 0,
mongoRecordsLimit: 0,
mongoClientListType: "array",
showLogMessages : true,
tempPath: "/tmp/clients",
};
var mySqlCommands = {};
// keep connected db objects
// to avoid making multiple connections
var clusterConnections = {};
var mongoDbConnections = {};
this.setup = function (config) {
CONFIG = _.extend(CONFIG, config);
return self;
};
this.getCollectionData = function (uri, collectionName, callback) {
_getMongoDBLink(MODULE_CONFIG.mongodb(uri), function(error, client) {
var collection = new mongo.Collection(client, collectionName);
// setup selectors
var _condition = (CONFIG.mongoSelectCondition || {});
var _filedsToSelect = (CONFIG.mongoFiledsToSelect || {_id:0, name:1});
_innerLogger("Mongo fields to select: %s", JSON.stringify(_filedsToSelect));
_innerLogger("Mongo condition: %s", JSON.stringify(_condition));
_innerLogger("Mongo skip records: %s", CONFIG.mongoRecordsSkip);
_innerLogger("Mongo data limit: %s", CONFIG.mongoRecordsLimit);
_innerLogger("Mongo data return type: %s", CONFIG.mongoClientListType);
collection
.find(_condition, _filedsToSelect)
.skip((CONFIG.mongoRecordsSkip)? CONFIG.mongoRecordsSkip : 0)
.limit(((CONFIG.mongoRecordsLimit)? CONFIG.mongoRecordsLimit : 0))
.toArray(callback);
});
};
this.getClientCount = function (callback) {
if (CONFIG.clientRecordsLimit) {
callback(CONFIG.clientRecordsLimit);
return;
}
_getMongoDBLink(MODULE_CONFIG.mongodb('qa'), function(error, client) {
if (error) throw error;
var collectionClients = new mongo.Collection(client, "clients");
collectionClients.count(function(err, count){
if (typeof(callback) === 'function')
callback(count);
else
self.log("Client count: " + count);
});
});
};
this.getClientObjects = function (name, callback) {
_getMongoDBLink(MODULE_CONFIG.mongodb('qa'), function(error, client) {
if (error) throw error;
// collections
var collectionClients = new mongo.Collection(client, "clients");
var collectionClientBundles = new mongo.Collection(client, "bundles");
// setup selectors
var _clientSelector = {};
if (!!name && name !== "*")
_clientSelector = {name:name};
if (name.split(",").length > 1) {
var _names = name.split(",");
var _nameSelector = [];
for (var i = 0; i < _names.length; i++)
_nameSelector.push({name:_names[i]});
_clientSelector = {$or:_nameSelector};
}
var _filedsToSelect = (CONFIG.mongoFiledsToSelect || {_id:0, name:1,bundle:1});
_innerLogger("Mongo fields to select: %s", JSON.stringify(_filedsToSelect));
_innerLogger("Mongo client selector: %s", JSON.stringify(_clientSelector));
_innerLogger("Mongo skip records: %s", CONFIG.mongoRecordsSkip);
_innerLogger("Mongo data limit: %s", CONFIG.mongoRecordsLimit);
_innerLogger("Mongo data return type: %s", CONFIG.mongoClientListType);
// get clients data
collectionClients
.find(_clientSelector, _filedsToSelect)
.skip((CONFIG.mongoRecordsSkip)? CONFIG.mongoRecordsSkip : 0)
.limit(((CONFIG.mongoRecordsLimit)? CONFIG.mongoRecordsLimit : 0))
.toArray(function(errrClient, clientRecords) {
if (errrClient) throw errrClient;
//console.dir(clientRecords);
if (clientRecords.length == 0)
callback("Can't find client record", null);
// get client name and bundle name map
var clientNameToBundleNameMap = {};
clientRecords.forEach(function(clientRecord){
clientNameToBundleNameMap[clientRecord.name] = clientRecord.bundle;
});
// get client bundle data
var clientList = false;
if (CONFIG.mongoClientListType == "object")
clientList = {};
else
clientList = [];
// get all bundels
collectionClientBundles.
find({}, {_id:0}).
toArray(function(errBundle, bundleRecords) {
// now we have all bundles
if (errBundle) throw errBundle;
var bundleNameToEntryMap = {};
bundleRecords.forEach(function(bundleRecord){
bundleNameToEntryMap[bundleRecord.name] = bundleRecord;
});
//_innerLogger("client loop");
// append received bundle data for each client
clientRecords.forEach(function(currentClientRecord) {
var conditionFnResult = null;
if (CONFIG.conditionFn)
conditionFnResult = CONFIG.conditionFn(currentClientRecord);
//console.dir(currentClientRecord.feedConfiguration._exportConfigurations);
if (conditionFnResult === false) {
self.log("%s - removed according to condition", currentClientRecord.name);
return;
}
//console.dir(conditionFnResult);
var clientInfo = {
clientName: currentClientRecord.name,
bundle: currentClientRecord.bundle,
custom: conditionFnResult,
clusters: []
};
if (bundleNameToEntryMap.hasOwnProperty(clientInfo.bundle) &&
bundleNameToEntryMap[clientInfo.bundle].clusters) {
clientInfo.clusters = bundleNameToEntryMap[clientInfo.bundle].clusters;
//_innerLogger("Client: %s; cluster: %s", clientInfo.clientName, clientInfo.clusters.join(','));
}
else
_innerLogger("Can't find bundle '%s' info for client %s", clientInfo.bundle, clientInfo.clientName);
//_innerLogger("Current bundle : %s", currentClientRecord.bundle);
if (_.isArray(clientList))
clientList.push(clientInfo);
else
clientList[currentClientRecord.name] = clientInfo;
});
//console.dir(bundleNameToEntryMap);
//console.dir(bundleRecords);
//console.dir(clientList);
callback(null, clientList);
}); // bundle collection: toArray
}); // client collection: toArray
}); // mongodb link
};
this.runQueryWithClientObj = function (clientObj, query, queryOptionsFn, finalCallback, onDataReceivedCallback) {
// exit function when clientObj is empty
if (_.isEmpty(clientObj)) {
_innerLogger("runQueryWithClientObj: Client list is empty");
finalCallback(null, false);
return;
}
// group clients by clasters
// and get uniq cluster names
var clusterGroups = self.groupClientsByClusters(clientObj);
function _inner_executeNextClientQuery (activeCluster, clusterEntry, clusterData, clusterCallback) {
// leave cluster process when it has all data
if (clusterEntry.length == 0) {
clusterCallback(null, clusterData);
return;
}
var clientObj = clusterEntry.shift();
_innerLogger("Running client: %s", clientObj.clientName);
self.executeMySqlQuery(
MODULE_CONFIG.mysqldb(activeCluster),
query,
function (mysqllink) {
var standartQueryValues = {
CLIENTNAME: mysqllink.escape(clientObj.clientName),
LIMIT: CONFIG.mysqlProductPageLinksLimit
};
var customQueryValues = {};
if (queryOptionsFn)
customQueryValues = queryOptionsFn(mysqllink, clientObj);
return _.extend(standartQueryValues, customQueryValues);
},
function(error, rows, cols) {
if (error) throw error;
_innerLogger("Cluster: %s, Items left: %s, Data received: %s; Rows count: %s",
activeCluster,
clusterEntry.length,
clientObj.clientName,
rows.length);
var recordObj = {
rows: rows,
cols: cols
};
if (onDataReceivedCallback) {
var callbackResult = onDataReceivedCallback(null, recordObj, clientObj);
if (_.isObject(callbackResult))
recordObj = callbackResult;
}
//console.dir(recordObj);
clusterData.push(recordObj);
_inner_executeNextClientQuery(activeCluster, clusterEntry, clusterData, clusterCallback);
}
);
} // end of inner fn
// make connection to involved clusters
self.getClusterConnections(clusterGroups.clusterNames, function () {
var clusterTasks = {};
// #2. get product page links and check them
for (var clusterID in clusterGroups.clusterDataMap) {
// #3. get products client links per cluster
(function(activeCluster, clusterEntry) {
// #4. add cluster task
clusterTasks[activeCluster] = function(clusterCallback) {
// get client count per cluster
//var activeTasksCount = clusterEntry.length;
//var clusterData = [];
_innerLogger("Cluster: %s, total clients: %s", activeCluster, clusterEntry.length);
_inner_executeNextClientQuery(activeCluster, clusterEntry, [], clusterCallback);
// run all clients of particular cluster
//clusterEntry.forEach(function(clientObj){
//});
}; // #4 add cluster task
})(clusterID, clusterGroups.clusterDataMap[clusterID]); // lock cluster
} // #2
// run parallel tasks for all clusters
async.parallel(clusterTasks, finalCallback);
}); // cluster connections
};
this.getClientsAndRunQuery = function (name, query, queryOptionsFn, finalCallback, onDataReceivedCallback) {
_innerLogger("Client list: %s", (name == "*")? "all" : name);
// #1. get bundles
self.getClientObjects(name, function(error, clientObjects){
//console.dir(clientObjects);
if (error) throw error;
self.runQueryWithClientObj(clientObjects, query, queryOptionsFn, finalCallback, onDataReceivedCallback);
}); // get client objects
};
// query wrappers
// ***************************
this.getActiveProductCount = function (name, finalCallback, perClientCallback) {
var commonProductCounter = 0;
this.getClientsAndRunQuery(name,
mySqlCommands.getProductCount,
false,
function(){finalCallback(null, commonProductCounter);},
// on data received callback
function (e, dataObj, clientObj) {
if (e) throw e;
if (dataObj.rows && dataObj.rows.length == 1)
commonProductCounter += dataObj.rows[0].Count;
if (perClientCallback)
perClientCallback(null, dataObj.rows, clientObj);
return dataObj.rows;
}
);
};
this.getProductPageLinksByClientNames = function (name, finalCallback, perClientCallback) {
this.getClientsAndRunQuery(name,
mySqlCommands.getActivePDPURLs,
false,
finalCallback,
perClientCallback
);
};
this.getSmartSeoClientWithPDPLinks = function (name, finalCallback, perClientCallback) {
CONFIG.mongoFiledsToSelect = {_id:0,name:1,bundle:1,"feedConfiguration._exportConfigurations":1};
CONFIG.mongoClientListType = "array"
CONFIG.conditionFn = function (singleClientObj) {
//console.dir(singleClientObj);
// do not include client with empty export feeds
if (!!!singleClientObj.feedConfiguration ||
!!!singleClientObj.feedConfiguration._exportConfigurations ||
singleClientObj.feedConfiguration._exportConfigurations.length == 0)
return false;
var smartSeoIsAdvanced = null;
singleClientObj.feedConfiguration._exportConfigurations.forEach(function(config){
// we only want feeds that are scheduled to generated
if (config._schedule._days.length &&
config._exportName &&
config._exportName.indexOf('smartseo.zip') > 0) {
//self.log("Client: %s, Feed Name: %s;", singleClientObj.name, config._exportName);
// check smart seo is advanced or basic
if (config._generatorConfiguration._maximumPageNumber > 1)
smartSeoIsAdvanced = true;
else
smartSeoIsAdvanced = false;
} else
return false; // skip this client
});
// do not include client with non-smartSeo feeds
if (smartSeoIsAdvanced === null)
return false;
return {seoAdvanced: smartSeoIsAdvanced};
};
this.getClientsAndRunQuery(name,
mySqlCommands.getActivePDPURLs,
false,
finalCallback,
// on data received callback
function (e, dataObj, clientObj) {
if (e) throw e;
//console.dir(dataObj);
var rowList = [];
if (dataObj.rows) {
for (var id in dataObj.rows)
rowList.push(dataObj.rows[id].Value);
rowList = _.uniq(rowList);
}
if (perClientCallback)
perClientCallback(null, rowList, clientObj);
return rowList;
}
);
};
this.getRecentUsedEmailTemplatesByClientNames = function (name, finalCallback, perClientCallback) {
console.log(CONFIG.emailsActivetime);
this.getClientsAndRunQuery(name,
mySqlCommands.getRecentClientEmails,
function (mysqllink,clientObj){
return {ACTIVETIME: CONFIG.emailsActivetime};
},
finalCallback,
// on data received callback
function (e, dataObj, clientObj) {
if (e) throw e;
var emailsRowList = {clientName: clientObj.clientName, EmailTemplates: []};
//If a client has emails
if (!!dataObj.rows[0]) {
for (var id in dataObj.rows)
emailsRowList.EmailTemplates.push({
TemplateName: dataObj.rows[id].TemplateName,
Count: dataObj.rows[id].Count,
ServiceProvider: dataObj.rows[id].ProviderType
});
}
if (perClientCallback)
perClientCallback(null, emailsRowList, clientObj);
return emailsRowList;
}
);
};
// helpers
// ***************************
this.groupClientsByClusters = function (clientObj) {
// #1. group clients by clusters
var clientsPerCluster = {};
var involvedClusterNames = [];
for (var clientID in clientObj) {
if (!!!clientObj[clientID].clusters || clientObj[clientID].clusters.length == 0)
continue;
var cluster = clientObj[clientID].clusters[0];
involvedClusterNames.push(cluster);
if (!!!clientsPerCluster[cluster])
clientsPerCluster[cluster] = [];
clientsPerCluster[cluster].push(clientObj[clientID]);
}
return {
clusterNames: _.uniq(involvedClusterNames),
clusterDataMap: clientsPerCluster
};
};
this.getCommand = function (cmd, args) {
for(var argID in args)
cmd = cmd.replace(new RegExp('\\$\\$' + argID + '\\$\\$', 'ig'), args[argID]);
//_innerLogger(cmd);
return cmd.replace(/ +(?= )/g,'').trim();
};
// data import/export
// ***************************
this.getSavePath = function (fname) {
// add default extension
if (fname.indexOf(".") < 0)
fname += ".json";
return CONFIG.tempPath + "/" + fname;
};
this.importDataToJsonFile = function (data, fname, doNotSaveIfEmpty, callback) {
var fpath = self.getSavePath(fname);
if (doNotSaveIfEmpty && !!!data) {
if (callback)
callback("Empty data", data, fpath);
return
}
if (callback)
fs.writeFile(fpath, JSON.stringify(data), callback);
else
fs.writeFileSync(fpath, JSON.stringify(data));
};
// config structure
// host
// db
// collection
// mode
// options
// port
this.importDataToMongoDb = function(config, data, callback) {
self.log("Import to mongodb");
self.log(" -- host: " + config.host);
self.log(" -- database: " + config.db);
var activeTasks = [];
_getMongoDBLink(config, function(error, client) {
if (error) throw error;
var collection = new mongo.Collection(client, config.collection);
for (var key in data)
(function(activeKey) {
activeTasks.push(1);
//console.dir(data[activeKey]);
switch (config.mode) {
case "update": {
self.log("performing update: " + activeKey);
collection.update(
{name: activeKey},
{$set: data[activeKey]},
config.options,
function(e) {
dbActionCallback(e, activeKey);
});
break;
}
case "insert": {
self.log("performing insert: " + activeKey);
collection.insert(data[activeKey], function(e) {
dbActionCallback(e,activeKey);
});
break;
}
default: {
callback("Unknown action: " + config.mode);
break;
}
}
})(key);
function dbActionCallback(error, activeKey) {
activeTasks.pop();
if (error) {
console.warn(err.message);
} else
self.log('[' + activeKey + ']: successfully updated');
if (activeTasks.length == 0) {
self.log("mongodb: complete");
callback(null, data);
}
}
});
};
this.importFileDataToMongoDb = function(config, fname, onDataEmpty, finalCallback) {
// get json object
self.getJsonFileObject(fname, function(jsonData){
if (_.isEmpty(jsonData))
jsonData = onDataEmpty();
// export ot mongo
self.importDataToMongoDb(config, jsonData, finalCallback);
});
}
this.getJsonFileObject = function (fname, callback) {
var result = self.getJsonFile(fname, (callback?function(error, data){
if (error) throw error;
callback(JSON.parse(data));
}:null));
if (!callback)
return result;
}
this.getJsonFile = function (fname, callback) {
// get filepath
var fpath = self.getSavePath(fname);
if (!fs.existsSync(fpath)) {
if (callback)
callback(false, false);
return false;
}
if (callback)
fs.readFile(fpath, 'utf8', callback);
else
return fs.readFileSync(fpath, 'utf8');
};
// database and connections
// ***************************
this.getClusterConnections = function (clusters, callback) {
var nonConnectedClusterCount = clusters.length;
for (var cx in clusters) {
_getMySQLLink(MODULE_CONFIG.mysqldb(clusters[cx]), function(){
nonConnectedClusterCount--;
if (nonConnectedClusterCount <= 0)
callback();
});
}
};
this.executeMySqlQuery = function (mySqlConfig, query, queryOptionsFn, onDataReceivedCallback) {
// mysqlConnection
_getMySQLLink(mySqlConfig, function(error, mysqllink) {
// throw error message
if (error) throw error;
// get mysql query
var q = self.getCommand(query, queryOptionsFn(mysqllink));
//self.log(q);
// perform query
mysqllink.query(q).execute(onDataReceivedCallback);
}); // mysqlConnection
};
this.getMySQLConnection = function (config, callback) {
if (_.isString(config))
config = MODULE_CONFIG.mysqldb(config);
_getMySQLLink(config, callback);
};
this.getMongoDBConnection = function (config, callback) {
if (_.isString(config))
config = MODULE_CONFIG.mongodb(config);
_getMongoDBLink(config, callback);
};
function _getMySQLLink (config, callback) {
// check for existed connection
if (clusterConnections.hasOwnProperty(config.host)) {
//_innerLogger('--- using existed mysql connection: ' + config.host);
callback(null, clusterConnections[config.host], config.cluster);
return;
}
//create new connection
_innerLogger('--- new mysql connection to ' + config.host + ":" + config.port);
new mysql.Database({
hostname: config.host,
user: config.user,
password: config.pwd,
database: config.db,
port: config.port
}).connect(function(error) {
if (error) {
return _innerLogger("CONNECTION ERROR: " + error);
}
_innerLogger("mysql " + config.host + ": connection ok");
clusterConnections[config.host] = this;
callback(error, this, config.cluster);
});
};
function _getMongoDBLink (config, callback) {
// return existed connection
var connectionID = config.host.toString(24);
if (mongoDbConnections.hasOwnProperty(connectionID)) {
_innerLogger("--- using existed mongodb connection: " + config.host);
return callback(null, mongoDbConnections[connectionID]);
}
// make new connection
_innerLogger("--- new mongodb connection to: " + config.host);
new mongo.Db(
config.db,
new mongo.Server(config.host, config.port, {}),
{safe : true}
).open(function(error, client) {
if (error)
throw error;
_innerLogger("mongodb " + config.host + ": connection ok");
mongoDbConnections[connectionID] = client;
callback(error, client);
});
};
// loggers
// ***************************
function _innerLogger () {
if (CONFIG.showLogMessages)
logger.log(util.format.apply(null, arguments));
};
this.log = function () {
logger.log(util.format.apply(null, arguments));
};
if (options)
return self.setup(options);
}