Example #1
0
Writer.prototype.configure = function(callback)
{
    var self = this;

    var adapterOptions = {
        transcode: this.transcode.convert,
        encoding: this.options.encoding.to
    };

    var adapters = [
        (new (require('./writer/id3tag'))(adapterOptions)),
        (new (require('./writer/eyeD3'))(adapterOptions))
    ];

    async.map(adapters, function(adapter, callback) {

        adapter.configure(function(err) {

            if (err) {
                return callback && callback(err);
            }

            adapter.checkDependencies(adapter.methods, function(err, map) {

                if (err) {
                    return callback && callback(err);
                }

                callback && callback(null, {
                    name: adapter.name,
                    methods: map,
                    adapter: adapter
                });
            });
        });

    }, function(err, results) {

        if (err) {
            return callback && callback(err);
        }

        self.adapters = {};

        results.forEach(function(item) {

            var available = false;

            Object.keys(item.methods).forEach(function(name) {

                var method = item.methods[name];

                if (true === method) {
                    item.adapter.availableMethods.push(name);
                    available = true;
                }
            });

            if (true === available) {
                self.adapters[item.name] = item.adapter;
            }
        });

        // Check if we got any available adapters
        if (0 === Object.keys(self.adapters).length) {
            return callback && callback(
                new Error('No available write adapters found')
            );
        }

        callback && callback();
    });
};
 after(function(done) {
     var names = schemas.map(function(s) { return s.name; });
     async.map(names, deleteSchema, done);
 });
Example #3
0
	query.exec(function(err, results) {

		var sendCSV = function(data) {

			var columns = data.length ? Object.keys(data[0]) : [];
			res.attachment(req.list.path + '-' + moment().format('YYYYMMDD-HHMMSS') + '.csv');
			res.setHeader('Content-Type', 'application/octet-stream');

			csv().from(data).to.options({
				header: true,
				columns: columns,
				delimiter: keystone.get('csv field delimiter') || ','
			}).to.string(function(data) {
				res.end("\ufeff" + data, 'utf-8');
			});
		};

		if (!results.length) {
			// fast bail on no results
			return sendCSV([]);
		}
		var data;

		if (results[0].toCSV) {

			/**
			 * Custom toCSV Method present
			 *
			 * Detect dependencies and call it. If the last dependency is `callback`, call it asynchronously.
			 *
			 * Support dependencies are:
			 *   - req (current express request object)
			 *   - user (currently authenticated user)
			 *   - row (default row data, as generated without custom toCSV())
			 *   - callback (invokes async mode, must be provided last)
			 */

			var deps = _.map(results[0].toCSV.toString().match(FN_ARGS)[1].split(','), function(i) { return i.trim(); });

			var includeRowData = (deps.indexOf('row') > -1);

			var map = {
				req: req,
				user: req.user
			};

			var applyDeps = function(fn, _this, _map) {
				var args = _.map(deps, function(key) {
					return _map[key];
				});
				return fn.apply(_this, args);
			};

			if (_.last(deps) === 'callback') {
				// Allow async toCSV by detecting the last argument is callback
				return async.map(results, function(i, callback) {
					var _map = _.clone(map);
					_map.callback = callback;
					if (includeRowData) {
						_map.row = getRowData(i);
					}
					applyDeps(i.toCSV, i, _map);
				}, function(err, results) {
					if (err) {
						console.log('Error generating CSV for list ' + req.list.key);
						console.log(err);
						return res.send(keystone.wrapHTMLError('Error generating CSV', 'Please check the log for more details, or contact support.'));
					}
					sendCSV(results);
				});
			} else {
				// Without a callback, toCSV must return the value
				data = [];
				if (includeRowData) {
					// if row data is required, add it to the map for each iteration
				_.each(results, function(i) {
						var _map = _.clone(map);
						_map.row = getRowData(i);
						data.push(applyDeps(i.toCSV, i, _map));
					});
				} else {
					// fast path: use the same map for each iteration
					_.each(results, function(i) {
					data.push(applyDeps(i.toCSV, i, map));
				});
				}
				return sendCSV(data);
			}

		} else {

			/**
			 * Generic conversion to CSV
			 *
			 * Loops through each of the fields in the List and uses each field's `format` method
			 * to generate the data
			 */

			data = [];
			_.each(results, function(i) {
				data.push(getRowData(i));
			});
			return sendCSV(data);
		}

	});
Example #4
0
  fs.readdir("/dev/serial/by-id", function (err, files) {
    if (err) {
      // if this directory is not found this could just be because it's not plugged in
      if (err.errno === 34) {
        return callback(null, []);
      }

      // TODO: emit error event
      if (callback) {
        callback(err);
      }
      return;
    }

    var dirName = "/dev/serial/by-id";
    async.map(files, function (file, callback) {
      var fileName = path.join(dirName, file);
      fs.readlink(fileName, function (err, link) {
        if (err) {
          // TODO: emit error event
          if (callback) {
            callback(err);
          }
          return;
        }

        link = path.resolve(dirName, link);
        callback(null, {
          comName: link,
          manufacturer: undefined,
          pnpId: file
        });
      });
    // Suspect code per ticket: #104 removed for deeper inspection.
    // fs.readdir("/dev/serial/by-path", function(err_path, paths) {
    //   if (err_path) {
    //     if (err.errno === 34) return callback(null, []);
    //     return console.log(err);
    //   }

    //   var dirName, items;
    //   //check if multiple devices of the same id are connected
    //   if (files.length !== paths.length) {
    //     dirName = "/dev/serial/by-path";
    //     items = paths;
    //   } else {
    //     dirName = "/dev/serial/by-id";
    //     items = files;
    //   }

    //   async.map(items, function (file, callback) {
    //     var fileName = path.join(dirName, file);
    //     fs.readlink(fileName, function (err, link) {
    //       if (err) {
    //         return callback(err);
    //       }
    //       link = path.resolve(dirName, link);
    //       callback(null, {
    //         comName: link,
    //         manufacturer: undefined,
    //         pnpId: file
    //       });
    //     });
    //   }, callback);
    }, callback);
  });
Example #5
0
let http = require('http');
let async = require('async');
 
let urls = [process.argv[2], process.argv[3]];

async.map(urls, (url, done) => {
    let body = '';  
    http.get(url, (res) => {
      res.on('data', (chunk) => {
        body += chunk.toString();
      });
      res.on('end', () => {
        return done(null, body);
      });
    });
  }, 
  (err, res) => {
    if (err) return console.log(err);
    console.log(res);
  }
);
Example #6
0
    deleter.deleteBatch(_.pluck(batch, 'id'), indexes, function (err) {
      var dbInstructions = [];
      batch.forEach(function (doc) {
        var docIndexEntries = [];
        if (err) log.warn(err);
        if (!_.isPlainObject(doc))
          return callbacky(new Error('Malformed document'), {});
        doc = removeInvalidFields(doc);
        batchOptions = _.defaults(batchOptions, options);
        if (batchOptions.fieldsToStore == 'all')
          batchOptions.fieldsToStore = Object.keys(doc);
        log.info('indexing ' + doc.id);
        docIndexEntries.push({
          type: 'put',
          key: 'DOCUMENT○' + doc.id + '○',
          value:  _.pick(doc, batchOptions.fieldsToStore)
        });
        var freqsForComposite = []; //put document frequencies in here
        _.forEach(doc, function (field, fieldName) {
          var fieldOptions = _.defaults(_.find(batchOptions.fieldOptions, 'fieldName', fieldName) || {}, batchOptions.defaultFieldOptions);
          if (fieldName == 'id') fieldOptions.stopwords = '';   // because you cant run stopwords on id field
          else fieldOptions.stopwords = batchOptions.stopwords;
          if (_.isArray(field)) field = field.join(' '); // make filter fields searchable
          var v = tv.getVector(field + '', {
            separator: batchOptions.separator,
            stopwords: fieldOptions.stopwords,
            nGramLength: fieldOptions.nGramLength
          });
          v.push(['*', 1]); //can do wildcard searh on this field

          var freq = tf.getTermFrequency(v, {
            scheme: 'doubleLogNormalization0.5',
            weight: fieldOptions.weight
          });
          if (fieldOptions.searchable)
            freqsForComposite.push(freq);
          var deleteKeys = [];
          if (fieldOptions.fieldedSearch) {
            freq.forEach(function (item) {
              batchOptions.filters.forEach(function (filter) {
                _.forEach(doc[filter], function (filterKey) {
                  docIndexEntries.push({
                    type: 'put',
                    key: 'TF○' + fieldName + '○' + item[0] + '○' + filter + '○' + filterKey,
                    value: [doc.id]
                  });
                  docIndexEntries.push({
                    type: 'put',
                    key: 'RI○' + fieldName + '○' + item[0] + '○' + filter + '○' + filterKey,
                    value: [[item[1], doc.id]]
                  });
                });
              });
              docIndexEntries.push({
                type: 'put',
                key: 'TF○' + fieldName + '○' + item[0] + '○○',
                value: [doc.id]
              });
              docIndexEntries.push({
                type: 'put',
                key: 'RI○' + fieldName + '○' + item[0] + '○○',
                value: [[item[1], doc.id]]
              });
            });
          };
        });
        //generate * field
        _(freqsForComposite)
          .flatten()
          .sort()
          .reduce(function (prev, item) {
            if (!prev[0]) prev.push(item);
            else if (item[0] == _.last(prev)[0]) {
              _.last(prev)[1] = _.last(prev)[1] + item[1];
            }
            else
              prev.push(item);
            return prev;
          }, [])
          .forEach(function (item) {
            batchOptions.filters.forEach(function (filter) {
              _.forEach(doc[filter], function (filterKey) {
                docIndexEntries.push({
                  type: 'put',
                  key: 'TF○*○' + item[0] + '○' + filter + '○' + filterKey,
                  value: [doc.id]
                });
                docIndexEntries.push({
                  type: 'put',
                  key: 'RI○*○' + item[0] + '○' + filter + '○' + filterKey,
                  value: [[item[1], doc.id]]
                });
              });
            });
            docIndexEntries.push({
              type: 'put',
              key: 'TF○*○' + item[0] + '○○',
              value: [doc.id]
            });
            docIndexEntries.push({
              type: 'put',
              key: 'RI○*○' + item[0] + '○○',
              value: [[item[1], doc.id]]
            });
          });
        docIndexEntries.push({
          type: 'put',
          key: 'DELETE-DOCUMENT○' + doc.id,
          value: _.pluck(docIndexEntries, 'key')
        });
        dbInstructions.push(docIndexEntries);
      });
      dbInstructions.push({
        type: 'put',
        key: 'DOCUMENT-COUNT',
        value: batch.length
      });
      dbInstructions = _(dbInstructions)
        .flatten()
        .sortBy('key')
        .reduce(function (prev, item) {
          if (item.key.substring(0, 6) == 'DELETE')
            prev.push(item);
          else if (item.key.substring(0, 8) == 'DOCUMENT')
            prev.push(item);
          else if (item.key.substring(0, 2) == 'RI') {
            if (item.key == _.last(prev).key)
              _.last(prev).value.push(item.value[0]);
            else
              prev.push(item);
          }
          else if (item.key.substring(0, 2) == 'TF') {
            if (item.key == _.last(prev).key)
              _.last(prev).value = _.last(prev).value.concat(item.value);
            else
              prev.push(item);
          }
          return prev;
        }, []);
      async.map(
        dbInstructions,
        function (item, callback) {
          indexes.get(item.key, function (err, val) {
            if (item.key.substring(0, 2) == 'TF') {
              if (val)
                item.value = item.value.concat(val);
              item.value = item.value.sort();
            }
            else if (item.key.substring(0, 2) == 'RI') {
              if (val)
                item.value = item.value.concat(val);
              item.value = item.value.sort(function (a, b) {
                return b[0] - a[0];
              });
            }
            else if (item.key == 'DOCUMENT-COUNT') {
              if (val)
                item.value = +val + +(item.value);
            }
            return callback(null, item);
          });
        },
        function (err, mergeDbInstructions) {
          //          console.log(JSON.stringify(mergeDbInstructions, null, 2))
          indexes.batch(mergeDbInstructions, function (err) {
            if (err) log.warn('Ooops!', err);
            else log.info('batch indexed!');
            return callbacky(null);
          });
        });
    });
Example #7
0
/**
 * 对集合中的每一个元素,执行某个异步操作,得到结果。所有的结果将汇总到最终的callback里。与each的区别是,each只关心操作不管最后的值,而map关心的最后产生的值。
 *
 * 提供了两种方式:
 * 1. 并行执行。同时对集合中所有元素进行操作,结果汇总到最终callback里。如果出错,则立刻返回错误以及已经执行完的任务的结果,未执行完的占个空位
 * 2. 顺序执行。对集合中的元素一个一个执行操作,结果汇总到最终callback里。如果出错,则立刻返回错误以及已经执行完的结果,未执行的被忽略。
 */
var async = require('async');

var arr = [{name:'Jack', delay:200}, {name:'Mike', delay: 100}, {name:'Freewind', delay:300}, {name:'Test', delay: 50}];

/**
 * 并行执行
 * 所有操作均正确执行,未出错。所有结果按元素顺序汇总给最终的callback。
 */
async.map(arr, function(item, callback) {
    console.log('enter: ' + item.name);
    setTimeout(function() {
        console.log('handle: ' + item.name);
        callback(null, item.name + '!!!');
    }, item.delay);
}, function(err,results) {
    console.log('err: ', err);
    console.log('results: ', results);
});
Example #8
0
				teasers: function (next) {
					async.map(roomIds, function (roomId, next) {
						Messaging.getTeaser(uid, roomId, next);
					}, next);
				},
Example #9
0
 it('should remove objects created for test', done => {
   async.map([_100kbObjectName, _100kbObjectBufferName, _100kbObjectStringName, _11mbObjectName], (objectName, cb) => client.removeObject(bucketName, objectName, cb), done)
 })
Example #10
0
var makeUnit = function(mysqlc, unit, playerId, cb) {

  var i = -1;

  async.map(unit, function(item, callback) {
    var positionId    = item.position; // current position in the unit
    var playerCardId  = BASEID + positionId + ((missionId - 1) * UNIT_LENGTH);
    i++;

    console.log("playerCardId : " + playerCardId);
    console.log("positionId : " + positionId);

    var weaponId    = 0;
    var defenderId  = 0;
    var shoeId      = 0;
    var jewelryId   = 0;
    var stdskill1Id = 0;
    var stdskill2Id = 0;

    var card  = item.card;
    var unitMeridian = card[0].unit_meridian; // Jing Mai

    async.auto({
      um: function(callback) {
        insertMeridian(mysqlc, unitMeridian, playerCardId, callback);
      },
      card: function(callback) {
        // insert player_card
        playerCardDao.add(mysqlc, playerCardId, playerId, card[0].id, 0, card[0].level, 100, 
          function(err, res) {
            if (err) {
              callback(err, null);
            }
            playerCardDao.arm(mysqlc, playerCardId, callback);
          }
        );
      },
      weapon: function(callback) {
        var weapon = item.weapon;
        if (weapon[0].id > 0) {
          var playerEquipId = BASEID + ((missionId - 1) * EQUIP_LENGTH * UNIT_LENGTH) + i * EQUIP_LENGTH + TYPE_ATK;
          weaponId = playerEquipId;
          //console.log("weapon playerEquipId : " + playerEquipId);
          playerEquipDao.add(
            mysqlc, 
            playerEquipId, playerId, 
            weapon[0].id, weapon[0].level,
            function(err, res) {
              if (err) {
                callback(err, null);
              }
              playerEquipDao.arm(mysqlc, playerEquipId, callback);
            }
          );
        }
        else {
          callback(null, null);
        }
      },
      defender: function(callback) {
        var defender = item.defender;
        if (defender[0].id > 0) {
          var playerEquipId = BASEID + ((missionId - 1) * EQUIP_LENGTH * UNIT_LENGTH) + i * EQUIP_LENGTH + TYPE_DEF;
          defenderId = playerEquipId;
          //console.log("defender playerEquipId : " + playerEquipId);

          playerEquipDao.add(
            mysqlc, 
            playerEquipId, playerId, 
            defender[0].id, defender[0].level,
            function(err, res) {
              if (err) {
                callback(err, null);
              }
              playerEquipDao.arm(mysqlc, playerEquipId, callback);
            }
          );
        }
        else {
          callback(null, null);
        }
      },
      shoe: function(callback) {
        var shoe = item.shoe;
        if (shoe[0].id > 0) {
          var playerEquipId = BASEID + ((missionId - 1) * EQUIP_LENGTH * UNIT_LENGTH) + i * EQUIP_LENGTH + TYPE_AGI;
          shoeId = playerEquipId;
          //console.log("shoe playerEquipId : " + playerEquipId);
          playerEquipDao.add(
            mysqlc, 
            playerEquipId, playerId, 
            shoe[0].id, shoe[0].level,
            function(err, res) {
              if (err) {
                callback(err, null);
              }
              playerEquipDao.arm(mysqlc, playerEquipId, callback);
            }
          );
        }
        else {
          callback(null, null);
        }
      },
      jewelry: function(callback) {
        var jewelry = item.jewelry;
        if (jewelry[0].id > 0) {
          var playerEquipId = BASEID + ((missionId - 1) * EQUIP_LENGTH * UNIT_LENGTH) + i * EQUIP_LENGTH + TYPE_HP;
          jewelryId = playerEquipId;
          //console.log("jewelry playerEquipId : " + playerEquipId);
          playerEquipDao.add(
            mysqlc, 
            playerEquipId, playerId, 
            jewelry[0].id, jewelry[0].level,
            function(err, res) {
              if (err) {
                callback(err, null);
              }
              playerEquipDao.arm(mysqlc, playerEquipId, callback);
            }
          );
        }
        else {
          callback(null, null);
        }
      },
      stdskill1: function(callback) {
        var stdskill1 = item.stdskill1;
        if (stdskill1[0].id > 0) {
          var playerSkillId = BASEID + ((missionId - 1) * SKILL_LENGTH * UNIT_LENGTH) + i * SKILL_LENGTH + 1;
          stdskill1Id = playerSkillId;
          //console.log("skill1 Id : " + playerSkillId + ", id:" + stdskill1[0].id + ", level:" + stdskill1[0].level);
          playerSkillDao.add(
            mysqlc, 
            playerSkillId, playerId, 
            stdskill1[0].id, 0, stdskill1[0].level,
            function(err, res) {
              if (err) {
                callback(err, null);
              }
              playerSkillDao.arm(mysqlc, playerSkillId, callback);
            }
          ); // 0: exp = 0
        }
        else {
          callback(null, null);
        }
      },
      stdskill2: function(callback) {
        var stdskill2 = item.stdskill2;
        if (stdskill2[0].id > 0) {
          var playerSkillId = BASEID + ((missionId - 1) * SKILL_LENGTH * UNIT_LENGTH) + i * SKILL_LENGTH + 2;
          stdskill2Id = playerSkillId;
          //console.log("skill2 Id : " + playerSkillId);
          playerSkillDao.add(
            mysqlc, 
            playerSkillId, playerId, 
            stdskill2[0].id, 0, stdskill2[0].level,
            function(err, res) {
              if (err) {
                callback(err, null);
              }
              playerSkillDao.arm(mysqlc, playerSkillId, callback);
            }
          ); // 0: exp = 0
        }
        else {
          callback(null, null);
        }
      }
    }, function(err, res) {
      if (err) {
        console.log('NPCgenerator failed');
        console.log(err);
        callback(err, null);
      }
      else {
        playerUnitDao.add(mysqlc, playerId, positionId, playerCardId, function(err, res10) {
          if (err) {
            callback(err, null);
          }
          // 1~6 is the equip/skill position of the here sceren
          async.series([
            function(callback1) {
              if (weaponId) {
                playerUnitDao.arm(mysqlc, playerId, positionId, 3, weaponId, callback1);
              }
              else {
                callback1(null, null);
              }
            },
            function(callback1) {
              if (defenderId) {
                playerUnitDao.arm(mysqlc, playerId, positionId, 4, defenderId, callback1);
              }
              else {
                callback1(null, null);
              }
            },
            function(callback1) {
              if (shoeId) {
                playerUnitDao.arm(mysqlc, playerId, positionId, 5, shoeId, callback1);
              }
              else {
                callback1(null, null);
              }
            },
            function(callback1) {
              if (jewelryId) {
                playerUnitDao.arm(mysqlc, playerId, positionId, 6, jewelryId, callback1);
              }
              else {
                callback1(null, null);
              }
            },
            function(callback1) {
              if (stdskill1Id) {
                playerUnitDao.arm(mysqlc, playerId, positionId, 1, stdskill1Id, callback1);
              }
              else {
                callback1(null, null);
              }
            },
            function(callback1) {
              if (stdskill2Id) {
                playerUnitDao.arm(mysqlc, playerId, positionId, 2, stdskill2Id, callback1);
              }
              else {
                callback1(null, null);
              }
            }
          ],
          function(err, res11) {
            if (err) {
              callback(err, null);
            }
            callback(null, res);
          });
        });
        //console.log("makeUnit done");
        //console.log(res);
        //callback(null, res);
      }
    });
  }, function(err, res) {
    if (err) {
      console.log(err);
      cb(err, null);
    }
    else {
      cb(null, res);
    }
  });
};
Example #11
0
				value: this.props.many ? cachedValues : cachedValues[0],
			});
			return;
		}
		this.setState({
			loading: true,
			value: null,
		});
		async.map(values, (value, done) => {
			xhr({
				url: Keystone.adminPath + '/api/' + this.props.refList.path + '/' + value + '?basic',
				responseType: 'json',
			}, (err, resp, data) => {
				if (err || !data) return done(err);
				this.cacheItem(data);
				done(err, data);
			});
		}, (err, expanded) => {
			if (!this.isMounted()) return;
			this.setState({
				loading: false,
				value: this.props.many ? expanded : expanded[0],
			});
		});
	},

	// NOTE: this seems like the wrong way to add options to the Select
	loadOptionsCallback: {},
	loadOptions (input, callback) {
		// NOTE: this seems like the wrong way to add options to the Select
		this.loadOptionsCallback = callback;
		const filters = this.buildFilters();
var async = require('async');

// gets a user from the db
// and calls back with (err, user) signature
var getUser = function(name, cb) {
  // simulate async
  setTimeout(function(){
    cb(null, {name: name});
  }, 100);
};


var people = ["Mary", "Todd", "Mike"];

// Map the people array to getUser
// Using async.map
// Log the new array when you are done
async.map(people, getUser, function(err, users) {
  console.log(users);
});
Example #13
0
File: base.js Project: 4rg0n/greppy
BaseAuthHandler.prototype.process = function(username, credentials, req, callback)
{
    if (!this.options) {
        throw new Error('No options specified');
        return;
    }

    if (!this.options.adapter) {
        throw new Error('No adapter(s) were specified as option');
        return;
    }

    var self = this;

    if (!util.isArray(this.options.adapter)) {
        this.options.adapter = [this.options.adapter];
    }

    async.map(this.options.adapter, function(adapter, callback) {

        if ('function' !== typeof adapter.authentication) {
            return callback && callback(
                new Error('Specified adapter does not have a valid authentication method')
            );
        }

        adapter.authentication(username, credentials, function(err, isAuthenticated, entity) {

            if (err) {
                return callback && callback(err);
            }

            if (entity && req.greppy) {

                if (!req.greppy.auth) {
                    req.greppy.auth = {};
                }

                req.greppy.auth.entity = entity;
            }

            callback && callback(null, isAuthenticated);
        });

    }, function(err, results) {

        if (err) {

            if ('function' === typeof self.options.error) {
                self.options.error();
            }

            return callback && callback(err);
        }

        var success = false;

        results.forEach(function(result) {

            if (true === result) {
                success = true;
            }
        });

        logger.debug(
            'Authentification ' + (success ? 'succeeded' : 'failed')
            + ' for ' + (username ? username.green.bold : '[none]')
        );

        if (false === success && 'function' === typeof self.options.error) {
            self.options.error(username);
        }

        if (true === success && 'function' === typeof self.options.success) {
            self.options.success(username);
        }

        self.post(success, function() {
            return callback && callback(null, success);
        });
    });
};
Example #14
0
function deleteOldFiles(cb) {
  if (DELETE_IN_PROGRESS) {
    console.log("Already deleting images.");
    if (typeof cb === "function") {
      cb(null);
      return;
    }
  }

  var filenames = fs
    .readdirSync(localPath(""))
    .filter(function(filename) {
      return new RegExp(/.*\.png/).test(filename);
    });

  console.log(filenames.length, "files in local-file-cache");

  // There's room for more files; carry on
  if (filenames.length < MAX_LOCAL_FILES) {
    if (typeof cb === "function") {
      cb(null);
      return;
    }
  }

  DELETE_IN_PROGRESS = true;

  var numFilesToDelete = filenames.length - MAX_LOCAL_FILES + 1;
  console.log("deleting", numFilesToDelete, "files");

  var fullPaths = filenames.map(function(filename) {
    return localPath(filename);
  });

  async.map(fullPaths, fs.stat, function(err, results) {
    if (err) console.log(err);
    
    // insert filenames into fs.stat results
    results.forEach(function(info, idx) {
      info.path = fullPaths[idx];
    });

    // delete oldest files first
    results.sort(function(fileA, fileB) {
      return fileA.mtime.getTime() - fileB.mtime.getTime();
    });

    var pathsToDelete = results
      .slice(0, numFilesToDelete)
      .map(function(file) { return file.path });

    async.map(pathsToDelete, fs.unlink, function(err) {
      DELETE_IN_PROGRESS = false;
      if (err) console.log(err);
      if (typeof cb === "function") {
        cb(null);
        return;
      }
    });
  });
}
Example #15
0
		mget : function(keys, cb) {
			async.map(keys,this.get,cb);
		}
	/**
	 * Read all files pertaining to a bundle from disk and return a promise
	 * resolving to that bunch of files.
	 *
	 * The params are expected to have been escaped.
	 *
	 * @param sessionPath Full path to the session directory
	 * @param bundleName Name of the bundle, without _bndl suffix.
	 * @param wsConnect The connection that requested the bundle.
	 * @returns A promise resolving to a a "bundle bunch of files".
	 */
	 function readBundleFromDisk(sessionPath, bundleName, wsConnect) {
	 	var deferred = Q.defer();

	 	var bundlePath = path.join(sessionPath, bundleName + '_bndl');

	 	var bundle = {};
	 	bundle.ssffFiles = [];

	 	var allFilePaths = [];

		// add media file path
		var mediaFilePath = path.join(bundlePath, bundleName + '.' + wsConnect.dbConfig.mediafileExtension);
		allFilePaths.push(mediaFilePath);

		// add annotation file path
		var annotFilePath = path.join(bundlePath, bundleName + '_annot.json');
		allFilePaths.push(annotFilePath);

		// add ssff file paths
		var ssffFilePaths = [];
		wsConnect.allTrackDefsNeededByEMUwebApp.forEach(function (td) {
			var ssffFilePath = path.join(bundlePath, bundleName + '.' + td.fileExtension);
			allFilePaths.push(ssffFilePath);
			ssffFilePaths.push(ssffFilePath);
		});

		// read in files using async.map
		async.map(allFilePaths, fs.readFile, function (err, results) {
			if (err) {
				log.error('reading bundle components:', err,
					'; clientID:', wsConnect.connectionID,
					'; clientIP:', wsConnect._socket.remoteAddress);

				deferred.reject();
			} else {
				var fileIdx;

				// set media file
				fileIdx = allFilePaths.indexOf(mediaFilePath);
				bundle.mediaFile = {
					encoding: 'BASE64',
					data: results[fileIdx].toString('base64')
				};

				// set annotation file
				fileIdx = allFilePaths.indexOf(annotFilePath);
				bundle.annotation = JSON.parse(results[fileIdx].toString('utf8'));

				// set ssffTracks
				ssffFilePaths.forEach(function (sfp) {
					fileIdx = allFilePaths.indexOf(sfp);
					// extract file ext
					var fileExt = path.extname(sfp).split('.').pop();
					bundle.ssffFiles.push({
						fileExtension: fileExt,
						encoding: 'BASE64',
						data: results[fileIdx].toString('base64')
					});
				});

				log.info('Finished reading bundle components. Now returning them.',
					'; clientID:', wsConnect.connectionID,
					'; clientIP:', wsConnect._socket.remoteAddress);

				deferred.resolve(bundle);
			}
		});

return deferred.promise;
}
Example #17
0
        }, function (err, results){

            if (err) {
                throw err; // TODO: replace with boom err
            }

            var shortlist = results.shortlist.rows.filter(function(module){

                return NameMatchPrefix.exec(module.id) || NameMatchSuffix.exec(module.id);
            }).map(function(module){

                return module.id;
            });

            var hapiKeyword = results.hapiKeyword.rows.map(function(module){

                return module.key[1];
            });
            var hapijsKeyword = results.hapijsKeyword.rows.map(function(module){

                return module.key[1];
            });
            var hapidotjsKeyword = results.hapidotjsKeyword.rows.map(function(module){

                return module.key[1];
            });

            var plugins = _.uniq(shortlist.concat(hapiKeyword).concat(hapijsKeyword).concat(hapidotjsKeyword));

            function getPluginData (name, callback) {

                NPMPackages.getByName(name, callback);
            }

            Async.map(plugins, getPluginData, function (err, results){

                if (err) {
                    throw err;
                }

                results = results.filter(function(result){

                    if (result.time.hasOwnProperty('unpublished')) {
                        if (new Date(result.time.unpublished.time) > new Date(result.time.modified)) {
                            return false;
                        }
                    }
                    return true;
                });

                DB.plugin.batchCreate(results, function (err, success) {

                    if (err) {
                        throw err;
                    }
                    
                    DB.download.remove({}, function(){
                        Async.map(results, internals.updateDownloadCounts(NPMDownloads), function(err, output){
                            if (err) {
                                throw err;
                            }
                            
                            return reply("<pre>" + JSON.stringify(results, null, 2) + "</pre>");
                        });
                    });
                });
            });
        });
Example #18
0
TestImages.prototype.testBaselines = function (files, cb) {
  this.emitTestSize(ld.size(files));
  async.map(files, this.testBaseline, cb);
};
Example #19
0
  exports.build = function (options, callback) {
    var buildSpecs = {},
      databases = [],
      extension,
      getRegisteredExtensions = function (database, callback) {
        var credsClone = JSON.parse(JSON.stringify(creds));
        credsClone.database = database;
        inspectDatabaseExtensions(credsClone, function (err, paths) {
          callback(null, {
            extensions: paths,
            database: database,
            keepSql: options.keepSql,
            populateData: options.populateData,
            wipeViews: options.wipeViews,
            clientOnly: options.clientOnly,
            databaseOnly: options.databaseOnly
          });
        });
      },
      config;

    if (options.config) {
      config = require(path.resolve(process.cwd(), options.config));
    } else {
      config = require(path.resolve(__dirname, "../../node-datasource/config.js"));
    }
    creds = config.databaseServer;
    creds.encryptionKeyFile = config.datasource.encryptionKeyFile;
    creds.host = creds.hostname; // adapt our lingo to node-postgres lingo
    creds.username = creds.user; // adapt our lingo to orm installer lingo

    if (options.database) {
      // the user has specified a particular database
      databases.push(options.database);
    } else {
      // build all the databases in node-datasource/config.js
      databases = config.datasource.databases;
    }

    if (options.clientOnly && options.databaseOnly) {
      // This request doesn't make any sense.
      callback("Make up your mind.");

    } else if (options.backup && options.source) {
      callback("You can build from backup or from source but not both.");

    } else if (options.backup && options.extension) {
      callback("When you're building from a backup you get whatever extensions the backup merits.");

    } else if (options.initialize &&
        (options.backup || options.source) &&
        options.database &&
        (!options.extension || options.extension === 'foundation-database')) {
      // Initialize the database. This is serious business, and we only do it if
      // the user does all the arguments correctly. It must be on one database only,
      // with no extensions, with the initialize flag, and with a backup file.

      buildSpecs.database = options.database;
      if (options.backup) {
        buildSpecs.backup = path.resolve(process.cwd(), options.backup);
        buildSpecs.extensions = false;
        // we'll determine the extensions by looking at the db after restore
      }
      if (options.source) {
        buildSpecs.source = path.resolve(process.cwd(), options.source);
        // if we initialize with the foundation, that means we want
        // an unmobilized build
        buildSpecs.extensions = options.extension ?
          [options.extension] :
          defaultExtensions;
      }
      buildSpecs.initialize = true;
      buildSpecs.keepSql = options.keepSql;
      buildSpecs.populateData = options.populateData;
      buildSpecs.wipeViews = options.wipeViews;
      buildSpecs.clientOnly = options.clientOnly;
      buildSpecs.databaseOnly = options.databaseOnly;
      buildAll([buildSpecs], creds, callback);

    } else if (options.initialize || options.backup || options.source) {
      // The user has not been sufficiently serious.
      callback("If you want to initialize the database, you must specifify " +
        " a database, and use no extensions, and use both the init and either the backup or source flags");

    } else if (options.extension) {
      // the user has specified an extension to build or unregister
      // extensions are assumed to be specified relative to the cwd
      buildSpecs = _.map(databases, function (database) {
        var extension = path.resolve(process.cwd(), options.extension);
        return {
          database: database,
          frozen: options.frozen,
          keepSql: options.keepSql,
          populateData: options.populateData,
          wipeViews: options.wipeViews,
          clientOnly: options.clientOnly,
          databaseOnly: options.databaseOnly,
          extensions: [extension]
        };
      });

      if (options.unregister) {
        unregister(buildSpecs, creds, callback);
      } else {
        // synchronous build
        buildAll(buildSpecs, creds, callback);
      }
    } else {
      // build all registered extensions for the database
      async.map(databases, getRegisteredExtensions, function (err, results) {
        // asynchronous...
        buildAll(results, creds, callback);
      });
    }
  };
Example #20
0
Occasion.mapRsvps = function(array, cb){
  async.map(array, attachEvent, function(err, array){
    cb(null, array);
  });
};
Example #21
0
    return function(req, res) {
      var DEFAULT_PREFIX = "p",
          DEFAULT_LAYOUT = "index",
          LIMIT = 12,
          layouts = {
            index: {
              template: "make-flip.html",
              tags: ['webmaker:recommended'],
              process: function(makes) {
                if( makes[2] ) {
                  makes[2].size = "large";
                }
                if( makes[3] ) {
                  makes[3].size = "large";
                }
                return makes;
              }
            },
            teach: {
              template: "make-teach.html",
              tags: ['webmaker:recommended','guide']
            }
          };

      options = options || {};

      // prefix: Set the app-tag prefix for our intial layout settings
      var prefix = options.prefix || (req.query.prefix || DEFAULT_PREFIX).toString(),
          stickyPrefix = "webmaker:" + (prefix + "-");

      // layout: Choose a layout - should it look like the home or teach page?
      // Sets the processing function and template piece
      var layoutName = options.layout || (req.query.layout || DEFAULT_LAYOUT).toString(),
          layout = layouts[layoutName] || layouts[DEFAULT_LAYOUT];
      layout.name = layoutName;

      // page: This is for rendering the view.
      var page = options.page || layoutName;

      function getMakes(options, callback) {
        make
          .find(options)
          .process( function( err, data, totalHits ) {
            callback(err, data);
          });
      }

      var stickyOptions = {
        tagPrefix: stickyPrefix,
        limit: LIMIT,
        sortByField: ["createdAt", "desc"]
      };

      var normalOptions = {
        tagPrefix: [stickyPrefix, true], // true = NOT search
        tags: { tags: layout.tags },
        limit: LIMIT,
        sortByField: ["createdAt", "desc"]
      };

      async.map([stickyOptions, normalOptions], getMakes, function(err, data) {
        var sticky = [],
            warnings = [],
            normal,
            all = [];

        if (err) {
          return res.send(err);
        }

        if (data[0].length) {
          sticky = make.sortByPriority(stickyPrefix, data[0]);
        }

        // Send warning messages to editor about missing stickies
        for(i=0; i<LIMIT; i++) {
          if(!sticky[i]) {
            warnings.push("No sticky set for " + stickyPrefix + (i+1));
          }
        }

        normal = data[1];
        all = sticky.concat(normal);

        // Is there a special processing function for this layout?
        if (layout.process) {
          all = layout.process(all);
        }

        res.render( page + ".html", {
          makes: all,
          warnings: warnings,
          page: page,
          prefix: prefix,
          layout: layout.name,
          template: layout.template,
          isAdmin: req.isAdmin || false
        });
      });
    };
Example #22
0
function _getMeta(ret, next) {
  var parts = REGEXP.exec(ret.title);
      
  if (parts === null) {
    debug('Torrent did not match regexp', parts, ret.title);
    next(null, null);
    return;
  }

  ret.title = parts[1];
  ret.season = parseInt(parts[3], 10);
  ret.episode = parseInt(parts[4], 10);
  ret.episodeTo = parts[5] && parseInt(parts[5].substring(1), 10);
  ret.isEpisodeRange = !!ret.episodeTo;
  ret.episodeSpecial = parts[6] || '';
  ret.fullSeason = parts[7] || null;
  ret.isFullSeason = !!parts[8];
  ret.quality = parts[9] || '';
  ret.extra = parts[10] || '';
  
  // - Torrent search query
  ret.torrentSearch = _buildSearchQuery(ret);
  
  // - handing episodes range
  if (ret.isEpisodeRange) {
    async.map(Number.range(ret.episode, ret.episodeTo).every(), function (episodeNumber, innerNext) {
      var current = Object.clone(ret);
      current.episode = episodeNumber;
      current.torrentSearch = _buildSearchQuery(current);
      
      _searchAdd(current.torrentSearch.escapeURL(true), function (_, first_result) {
        if (!first_result) {
          debug('Cannot get info_hash from web service (isEpisodeRange===true):', current.torrentSearch, first_result);
          innerNext(null, null);
          return;
        }
        
        current.infoHash = first_result[1];
        current.size = first_result[3];
        current.seeds = first_result[4];
        current.peers = first_result[5];
        current.magnet = 'magnet:?xt=urn:btih:' + current.infoHash + '&' + TRACKERS;
        innerNext(null, current);
      });
      
    }, function (_, episodes) {
      next(null, episodes.compact());
    });
    
  } else {
    
    debug('Req', TORRENTZ_URL.assign({ q: ret.torrentSearch.escapeURL(true) }));
    debug('Parsed meta', ret);

    _searchAdd(ret.torrentSearch.escapeURL(true), function (_, first_result) {
      if (!first_result) {
        debug('Cannot get info_hash from web service (isEpisodeRange===false):', ret.torrentSearch, first_result);
        next(null, null);
        return;
      }
      
      ret.infoHash = first_result[1];
      ret.size = first_result[3];
      ret.seeds = first_result[4];
      ret.peers = first_result[5];
      ret.magnet = 'magnet:?xt=urn:btih:' + ret.infoHash + '&' + TRACKERS;
      next(null, ret);
    });
  }
}
Example #23
0
		"web#/github": function(req, qs, body, res) {
			res.end();

			var channels = [];
			if (qs.channels)
				channels = qs.channels.split(",");

			var event = req.headers["X-Github-Event".toLowerCase()];
			var payload = JSON.parse(body);

			switch (event) {
				case "ping":
					var prefix = payload.repository.full_name;
					var bits = [];
					bits.push([, payload.sender.login + " pinged", 0]);
					bits.push(["events", payload.hook.events.join(", ")]);
					bot.plugins.gitio.shorten(payload.repository.html_url, function(shorturl) {
						bits.push([, shorturl, 2]);
						var str = bot.plugins.bits.format(prefix, bits);

						channels.forEach(function(channel) {
							bot.say(channel, str);
						});
					});
					break;

				case "push":
					var branch = payload.ref.replace("refs/heads/", "");

					if (branch == payload.repository.default_branch) {
						// payload.commits.reverse();
						var commits = payload.commits.filter(function(commit) {
							return commit.distinct;
						});

						if (self.pushCollapse !== null && commits.length >= self.pushCollapse) {
							bot.plugins.gitio.shorten(payload.compare, function(shorturl) {
								var prefix = payload.repository.full_name;
								var bits = [];
								bits.push([payload.pusher.name + " pushed", commits[0].message.replace(/^([^\r\n]+)[\s\S]*/, "$1")]); // keep only first line
								bits.push([, "and " + (commits.length - 1) + " other commits", 0]);
								bits.push([, shorturl, 2]);
								var str = bot.plugins.bits.format(prefix, bits);

								channels.forEach(function(channel) {
									bot.say(channel, str);
								});
							});
						}
						else {
							async.map(commits, function(commit, callback) {
								bot.plugins.gitio.shorten(commit.url, function(shorturl) {
									commit.shorturl = shorturl;
									callback(null, commit);
								});
							}, function(err, commits) {
								commits.forEach(function(commit) {
									var prefix = payload.repository.full_name;
									var bits = [];
									bits.push([commit.author.username + " committed", commit.message.replace(/^([^\r\n]+)[\s\S]*/, "$1")]); // keep only first line
									bits.push([, commit.shorturl, 2]);
									var str = bot.plugins.bits.format(prefix, bits);

									channels.forEach(function(channel) {
										bot.say(channel, str);
									});
								});
							});
						}
					}
					break;

				case "issues":
					switch (payload.action) {
						case "opened":
						case "closed":
						case "reopened":
							var prefix = payload.repository.full_name;
							var bits = [];
							bits.push([payload.sender.login + " " + payload.action + " issue #" + payload.issue.number, payload.issue.title]);
							bot.plugins.gitio.shorten(payload.issue.html_url, function(shorturl) {
								bits.push([, shorturl, 2]);
								var str = bot.plugins.bits.format(prefix, bits);

								channels.forEach(function(channel) {
									bot.say(channel, str);
								});
							});
							break;

						default:
							break;
					}
					break;

				case "pull_request":
					switch (payload.action) {
						case "opened":
						case "closed":
						case "reopened":
						case "synchronize":
							var prefix = payload.repository.full_name;
							var bits = [];

							var action = payload.action;
							if (action == "synchronize")
								action = "updated";
							else if (action == "closed" && payload.pull_request.merged)
								action = "merged";

							bits.push([payload.sender.login + " " + action + " pull request #" + payload.pull_request.number, payload.pull_request.title]);
							bot.plugins.gitio.shorten(payload.pull_request.html_url, function(shorturl) {
								bits.push([, shorturl, 2]);
								var str = bot.plugins.bits.format(prefix, bits);

								channels.forEach(function(channel) {
									bot.say(channel, str);
								});
							});
							break;

						default:
							break;
					}
					break;

				case "fork":
					var prefix = payload.repository.full_name;
					var bits = [];
					bits.push([payload.sender.login + " forked", payload.forkee.full_name]);
					bot.plugins.gitio.shorten(payload.forkee.html_url, function(shorturl) {
						bits.push([, shorturl, 2]);
						var str = bot.plugins.bits.format(prefix, bits);

						channels.forEach(function(channel) {
							bot.say(channel, str);
						});
					});
					break;

				default:
					bot.out.debug("github", [qs, event, payload]);
			}
		}
Example #24
0
(function() {
  'use strict';

  /* Globals */
  var AWS   = require('aws-sdk');
  var async = require('async');
  var _     = require('underscore');

  /* Cache */
  var amiCache   = [];

  /* Config */
  var regions = ['us-west-2', 'us-east-1', 'eu-west-1', 'ap-southeast-2'];

  /* Functions */
  async.map(regions, function(region, done) {
    var ec2 = new AWS.EC2({region: region});
    async.waterfall([
      // Get EC2 Info
      function(callback) {
        ec2.describeInstances(callback);
      },
      // Parse Instances
      function(data, callback) {
        // console.log(data);
        callback(null, _.flatten(data.Reservations.map(function(reservation) {
          return reservation.Instances;
        })));
      },
      // Populate Users for AMI
      function(instances, callback) {
        // console.log(instances);
        async.eachSeries(instances, function(instance, callback) {
          if (amiCache[instance.ImageId]) {
            return callback();
          }

          var ami = ec2.describeImages({
            ImageIds: [
              instance.ImageId,
            ],
          }, function(err, data) {
            if (err) { return callback(err); }
            async.each(data.Images, function(image, callback) {
              var user = null;
              var name = _.values(
                           _.pick(image, 'Name', 'Description'))
                         .join(' ').toLowerCase();

              // Guess User
              if (name.includes('ubuntu')) {
                user = '******';
              } else if (name.includes('amazon linux')) {
                user = '******';
              } else {
                user = '******';
              }

              amiCache[image.ImageId] = user;
              callback();
            }, callback);
          });
        }, function(err) {
          callback(err, instances);
        });
      },
      // Build Hosts List
      function(instances, callback) {
        // console.log(instances);
        callback(null, instances.map(function(instance) {
          var tags = _.object(instance.Tags.map(function(tag) {
            return [tag.Key, tag.Value];
          }));

          var config = {
            Host: tags.Name || instance.InstanceId,
            _HostName: instance.PrivateIpAddress,
            _User: amiCache[instance.ImageId],
            _IdentityFile: '~/.ssh/' + instance.KeyName + '.pem',
            _Env: tags.Env || 'EC2',
          };

          if (tags.Service != 'bastion') {
            // config._ProxyCommand = 'ssh -q ' + tags.Bastion + ' nc %h 22';
          } else {
            // config._Env = 'Bastion';
            config._HostName = instance.PublicIpAddress;
            // config._DynamicForward = '127.0.0.1:1080';
          }

          return config;
        }));
      },
      // Filter List
      function(config, callback) {
        callback(null, config.filter(function(host) {
          return host._HostName && host._User;
        }));
      }],

      // Return results
      done
    );
  }, function(err, results) {
    if (err) { console.error(err); }
    var config = [].concat.apply([], results);
    config = _.sortBy(config, 'Host');
    var hosts = _.groupBy(config, '_Env');
    console.log('### Generated SSH Config from AWS ###');
    console.log('## Hosts: ' + JSON.stringify(_.countBy(config, '_Env')));
    console.log();
    if (hosts.Bastion && hosts.Bastion.length > 0) {
      console.log('## Bastion Hosts ##\n');
      hosts.Bastion.forEach(function(host) {
        console.log('Host ' + host.Host);
        console.log('  StrictHostKeyChecking no');
        console.log('  HostName ' + host._HostName);
        console.log('  User ' + host._User);
        console.log('  DynamicForward ' + host._DynamicForward);
        console.log('  IdentityFile ' + host._IdentityFile);
        console.log();
      });
      delete(hosts.Bastion);
    }
    _.keys(hosts).sort().forEach(function(env) {
      console.log('## ' + env + ' ##\n');
      hosts[env].forEach(function(host) {
        console.log('Host ' + host.Host);
        console.log('  StrictHostKeyChecking no');
        console.log('  HostName ' + host._HostName);
        console.log('  User ' + host._User);
        // console.log('  ProxyCommand ' + host._ProxyCommand);
        console.log('  IdentityFile ' + host._IdentityFile);
        console.log();
      });
    });
  });
}());
Example #25
0
    OPCUAClientBase.prototype._on_connection_reestablished.call(self,function(err){

        //
        // a new secure channel has be created, we need to reactivate the session,
        // and reestablish the subscription and restart the publish engine.
        //
        //
        // see OPC UA part 4 ( version 1.03 ) figure 34 page 106
        // 6.5 Reestablishing subscription....
        //
        //
        //
        //                      +---------------------+
        //                      | CreateSecureChannel |
        //                      | CreateSession       |
        //                      | ActivateSession     |
        //                      +---------------------+
        //                                |
        //                                |
        //                                v
        //                      +---------------------+
        //                      | CreateSubscription  |<-------------------------------------------------------------+
        //                      +---------------------+                                                              |
        //                                |                                                                         (1)
        //                                |
        //                                v
        //                      +---------------------+
        //     (2)------------->| StartPublishEngine  |
        //                      +---------------------+
        //                                |
        //                                V
        //                      +---------------------+
        //             +------->| Monitor Connection  |
        //             |        +---------------------+
        //             |                    |
        //             |                    v
        //             |          Good    /   \
        //             +-----------------/ SR? \______Broken_____+
        //                               \     /                 |
        //                                \   /                  |
        //                                                       |
        //                                                       v
        //                                                 +---------------------+
        //                                                 |                     |
        //                                                 | CreateSecureChannel |<-----+
        //                                                 |                     |      |
        //                                                 +---------------------+      |
        //                                                         |                    |
        //                                                         v                    |
        //                                                       /   \                  |
        //                                                      / SR? \______Bad________+
        //                                                      \     /
        //                                                       \   /
        //                                                         |
        //                                                         |Good
        //                                                         v
        //                                                 +---------------------+
        //                                                 |                     |
        //                                                 | ActivateSession     |
        //                                                 |                     |
        //                                                 +---------------------+
        //                                                         |
        //                                                         v                    +-------------------+       +----------------------+
        //                                                       /   \                  | CreateSession     |       |                      |
        //                                                      / SR? \______Bad_______>| ActivateSession   |-----> | TransferSubscription |
        //                                                      \     /                 |                   |       |                      |       (1)
        //                                                       \   /                  +-------------------+       +----------------------+        ^
        //                                                         | Good                                                      |                    |
        //                                                         v   (for each subscription)                                   |                    |
        //                                                 +--------------------+                                            /   \                  |
        //                                                 |                    |                                     OK    / OK? \______Bad________+
        //                                                 | RePublish          |<----------------------------------------- \     /
        //                                             +-->|                    |                                            \   /
        //                                             |   +--------------------+
        //                                             |           |
        //                                             |           v
        //                                             | GOOD    /   \
        //                                             +------  / SR? \______Bad SubscriptionInvalidId______>(1)
        // (2)                                                  \     /
        //  ^                                                    \   /
        //  |                                                      |
        //  |                                                      |
        //  |                             BadMessageNotAvailable   |
        //  +------------------------------------------------------+
        //



        debugLog(" Starting Session reactivation".red.bgWhite);
        // repair session
        var sessions = self._sessions;
        async.map(sessions, function (session, next) {

            debugLog("OPCUAClient#_on_connection_reestablished TRYING TO REACTIVATE SESSION");
            self._activateSession(session,function(err){
                //
                // Note: current limitation :
                //  - The reconnection doesn't work if connection break is cause by a server that crashes and restarts yet.
                //
                // todo :
                //   if failed => recreate a new Channel and transfer the subscription
                //   else
                //      call Republish
                //
                debugLog("ActivateSession : ",err ? err.message : "");
                console.log("err = ",err);
                if (!err)  {
                    return self._ask_for_subscription_republish(session,next);
                }
                next(err);
            });

        },function(err,results) {
            var err = err;
            return callback(err);
        });

    });
 function getHotWalletBalances (c, options, callback) {
   var viewOptions = options.subview ? options.subview : options.view;
   var balances    = viewOptions.group_level ? {} : 0;
         
   async.map(c.hotwallets, function(account, asyncCallback) {  
     var view = JSON.parse(JSON.stringify(viewOptions));
     var key  = c.currency + "." + account; 
     var balance = 0, intitial;
     
     view.startkey[0] = key;
     view.endkey[0]   = key;
   
     var initialOpts = {
        startkey   : view.startkey,
        endkey     : [key],
        reduce     : false,
        limit      : 1,
        descending : true,
        stale      : 'ok' 
     }
     
     db.view('currencyBalances', 'v1', view, function(err, resp) {
       if (err) {
         return asyncCallback(err);
       }
       
       if (!view.group_level) {
         if (resp.rows.length) balances += resp.rows[0].value;
         return asyncCallback(null); 
       }
         
       //get initial balance 
       db.view('currencyBalances', 'v1', initialOpts, function(err, initial) {
         if (err) {
           return asyncCallback(err);
         }
       
         if (initial.rows.length && initial.rows[0].value[0] === c.issuer) {
           balance = initial.rows[0].value[1];
         }
       
         var time = moment.utc(view.startkey.slice(1)).format();
         balances[time] = balances[time] ? balances[time] + balance : balance;
         
         resp.rows.forEach(function(row) {
           time = moment.utc(row.key.slice(1)).add(options.increment, 1).format();
           balance += row.value;
           if (balance > 0) {
             balances[time] = balances[time] ? balances[time] + balance : balance;
           }
         });
         
         asyncCallback(null);
       });
     });              
   
   }, function(err, results) {
     
     return callback(err, balances); 
   });    
 }
Example #27
0
  function process(parent, obj, item) {
    if ((item === "ta" || item === "hh" || item === "hh1" || item === "hh2") && (typeof obj[item] === "string" || Array.isArray(obj[item]))) {
      if (obj[item].indexOf("*") !== -1) {
        delete parent.term;
        outstanding++;
        var query;
        if (item === "ta") {
          query = {bool: {must: {wildcard: {_id: obj[item]}},
                          must_not: {wildcard: {_id: "http:header:*"}}
                         }
                  };
        } else {
          query = {wildcard: {_id: "http:header:" + obj[item].toLowerCase()}};
        }
        clients[node].search('tags', 'tag', {size:500, fields:["id", "n"], query: query}, function(err, result) {
          result = JSON.parse(result);
          var terms = [];
          result.hits.hits.forEach(function (hit) {
            terms.push(hit.fields.n);
          });
          parent.terms = {};
          parent.terms[item] = terms;
          outstanding--;
          if (finished && outstanding === 0) {
            doneCb(err, body);
          }
        });
      } else if (Array.isArray(obj[item])) {
        outstanding++;

        async.map(obj[item], function(str, cb) {
          var tag = (item !== "ta"?"http:header:" + str.toLowerCase():str);
          tagNameToId(node, tag, function (id) {
            if (id === null) {
              cb(null, -1);
            } else {
              cb(null, id);
            }
          });
        },
        function (err, results) {
          outstanding--;
          obj[item] = results;
          if (finished && outstanding === 0) {
            doneCb(err, body);
          }
        });
      } else {
        outstanding++;
        var tag = (item !== "ta"?"http:header:" + obj[item].toLowerCase():obj[item]);

        tagNameToId(node, tag, function (id) {
          outstanding--;
          if (id === null) {
            err = "Tag '" + tag + "' not found";
          } else {
            obj[item] = id;
          }
          if (finished && outstanding === 0) {
            doneCb(err, body);
          }
        });
      }
    /*} else if (item === "fileand" && typeof obj[item] === "string") {
      var name = obj.fileand;
      delete obj.fileand;
      outstanding++;
      Db.fileNameToFile(name, function (file) {
        outstanding--;
        if (file === null) {
          err = "File '" + name + "' not found";
        } else {
          obj.bool = {must: [{term: {no: file.node}}, {term: {fs: file.num}}]};
        }
        if (finished && outstanding === 0) {
          doneCb(err, body);
        }
      });*/
    } else if (typeof obj[item] === "object") {
      convert(obj, obj[item]);
    }
  }
function issuerCapitalization(params, callback) {

  var error, 
    maxLimit   = 500,
    currencies = [];
  
  //validate incoming currencies
  if (Array.isArray(params.currencies)) {
    
    params.currencies.forEach(function(c){

      if (c.issuer) {
        c.name       = tools.getGatewayName(c.issuer);
        c.hotwallets = tools.getHotWalletsForGateway(c.name);
        currencies.push(c);

      } else {
        error = 'issuer is required: '+JSON.stringify(c);
        return;
      } 
    });
    
    if (error) return callback(error);
    
  } else return callback('please specify at least one issuer-currency pair');
  
  if (currencies.length>15) return callback("Cannot retrieve more than 20 currencies");
  if (!params.startTime) params.startTime = "Jan 1 2013 12:00+0:00";
  
  //Parse start and end times
  var time = tools.parseTimeRange(params.startTime, params.endTime, params.descending);
  
  if (time.error)   return callback(time.error);
    
  var startTime = time.start;
  var endTime   = time.end;

  //Parse timeIncrement and timeMultiple
  var results     = tools.parseTimeIncrement(params.timeIncrement);
  var group       = results.group;
  var group_level = results.group_level;

  if (results.group !== false) {
    intervalCount = tools.countIntervals(time.start, time.end, results.name);
    if (intervalCount>maxLimit) {
      return callback("Please specify a smaller time range or larger interval");
    }
  }
    
  //currencies = [currencies.pop()]; //for testing
  
  //get capitalization data for each currency
  async.map(currencies, function(c, asyncCallbackPair){

    var options  = {};
    if (results.name && results.name != "all") {
      options.increment   = results.name;
      options.alignedTime = tools.getAlignedTime(startTime, results.name);
    }
    
    // Setup CouchDB view options
    options.view = {
      startkey      : [c.currency+"."+c.issuer].concat(startTime.toArray().slice(0,6)),
      endkey        : [c.currency+"."+c.issuer].concat(endTime.toArray().slice(0,6)),
      inclusive_end : false,
      reduce        : true,
    };
    
    if (group) viewOpts.group = group;
    if (group_level) {
      // +3 to account for 1-based indexing in CouchDB and 
      // startkey having the [c.issuer, c.currency] first
      options.view.group_level = group_level + 2; 
    }

    options.view.stale = "ok"; //dont wait for updates
 
    //get cached results first.  if there are any,
    //the view will be adjusted so that couch is queried for everything else
    if (CACHE) getCached(options, function(error) {
      
      if (error) return callback (error);
      return fromCouch(options, c, asyncCallbackPair)
    });
    
    //cache not activated    
    else fromCouch(options, c, asyncCallbackPair);
           

  }, function(error, results){
    if (error) return callback(error);
    
    return callback(null, results); //final results from API call
  });
  
  
  /*
   * retrieve data from couch according to specified options.
   * 
   */  
  function fromCouch (options, c, callback) {

    var viewOptions = options.subview ? options.subview : options.view;
    
    //if the start and end times are the same, there is no need to query couchDB
    if (viewOptions.startkey.toString()===viewOptions.endkey.toString()) {
      c.results = options.cached || [];
      return callback(null, c);
    }
    
    //Query CouchDB for changes in trustline balances    
    db.view('currencyBalances', 'v1', viewOptions, function(error, trustlineRes){
      
      if (error) return callback("CouchDB - " + error);
      
      if (!options.view.group_level) {
        c.amount = 0 - (trustlineRes.rows.length ? trustlineRes.rows[0].value : 0);
        
        if (c.hotwallets) {
          getHotWalletBalances(c, options, function(err, balances) {
            c.amount -= balances;
            callback (null, c);  
          });
        } else {            
          callback (null, c);
        }
        
        return;
      }
      
      //if there are cached results, we can get the start capitalization from there
      //NOTE: disrgarding this for now, might be contributing to bad results
      if (0 && options.cached && options.cached.length) {
        if (c.hotwallets) {
          getHotWalletBalances(c, options, function(err, balances) {
            if (err) {
              return callback(err);
            }
            
            startCapitalization = options.cached[options.cached.length-1][1];
            
            //a bit of a hack necessary so that the hot wallet is not subtracted twice
            var time = moment.utc(options.subview.startkey.slice(1)).format();
            if (balances[time]) {
              startCapitalization += balances[time];
            }
            
            c.results = prepareRows(options, startCapitalization, trustlineRes.rows, balances);
            return callback(null, c);
          });
        } else {
          startCapitalization = options.cached[options.cached.length-1][1];
          c.results = prepareRows(options, startCapitalization, trustlineRes.rows);
          return callback(null, c);
        }
        
      //otherwise, we need to get the reduced result from the begining of
      //time to the start of our range.  
      } else {
        var initialValueViewOpts = {
          startkey      : [c.currency+"."+c.issuer],
          endkey        : viewOptions.startkey,
          inclusive_end : false,
          group         : false,
          stale         : "ok"
        };
  
        //query couchDB for the start capitalization
        db.view('currencyBalances', 'v1', initialValueViewOpts, function(error, initValRes) {

          if (error) return callback("CouchDB - " + error);
          
          var startCapitalization = 0;
          if (initValRes && initValRes.rows && initValRes.rows.length > 0) {
            startCapitalization = 0 - initValRes.rows[0].value;
          }
          
          if (!viewOptions.group_level) {
            if (trustlineRes.rows.length) {
              c.amount = startCapitalization - trustlineRes.rows[0].value; //add inverted value
            }
            
            return callback(null, c);  
          }
          
          //if we have hot wallets, we need to factor in their balances 
          if (c.hotwallets) {
            getHotWalletBalances(c, options, function(err, balances) {
              if (err) {
                return callback(err);
              }
              c.results = prepareRows(options, startCapitalization, trustlineRes.rows, balances);
              return callback(null, c);
            });
          } else {
            c.results = prepareRows(options, startCapitalization, trustlineRes.rows);
            return callback(null, c);
          }
        });
      }
    });    
  }
  
  
  /*
   * this function take the start capitalization, range results, cached results
   * and options to finalize a result.
   */
  function prepareRows (options, startCapitalization, rows, balances) {

    var viewOptions = options.subview ? options.subview : options.view;
    
    // Format and add startCapitalization data to each row
    // We will add a row for each increment that is missing
    // with the same amount as the previous increment, because
    // a missing row indicates no balance changes.
    if (rows) {       
      var lastPeriodClose = startCapitalization;
      var time      = moment.utc(options.alignedTime);
      var firstTime = moment.utc(viewOptions.startkey.slice(1));
      var temp      = {};
      rows.forEach(function(row){
        temp[moment.utc(row.key.slice(1)).unix()] = row.value;
      });
      
      rows = [];

      rows.push([firstTime.format(), startCapitalization]);
      if (temp[time.unix()]) lastPeriodClose += (0-temp[time.unix()]);     
       
      time.add(options.increment, 1); //skip the first
      
      //since we are getting the cumulative balance changes for each 
      //interval, the value at each time is actually the final value
      //for the next time listed.  If there is not a result for the
      //time period, the value is unchanged.
      while (1) {
        if (lastPeriodClose < 0) lastPeriodClose = 0;
       
        //if we are past the end time, this is the value for the end time,
        //not the full interval past it.  Also we are finished.
        if (endTime.diff(time)<=0) {
          rows.push([endTime.format(), lastPeriodClose]);
          break;
        }
                   
        //set the value for the current time as the previous cumulative total
        rows.push([time.format(), lastPeriodClose]);
        if (temp[time.unix()]) lastPeriodClose += (0-temp[time.unix()]); 
        time.add(options.increment, 1); //forward 1 increment           
      }
      
      if (balances) {
        var last = 0, final;
        rows.forEach(function(row, index) {
          if (balances[row[0]]) {
            last = balances[row[0]];
          }
          
          if (rows[index][1] > last) {
            rows[index][1] -= last;
          } else {
            rows[index][1] = 0;
          }
        });
      
        if (balances[time.format()]) {
          rows[rows.length-1][1] = lastPeriodClose - balances[time.format()];
        }
      }
              
      if (CACHE) {     
      
        cacheResults(options, rows); //cache new results
        
        //get rid of the first row if it is a duplicate of
        //the first cached row, then combine the two
        if (options.cached && options.cached.length) {
          if (rows.length && rows[0][0]==options.cached[options.cached.length-1][0]) rows.shift();
          rows = options.cached.concat(rows);
        }
      }
    
    } else {
      winston.info("No results for currency:", util.inspect(c));
      rows = [];
    }
   
    return rows;
  }
   
   
  /*
   * check the cache for cached results that fit
   * the time range. If any are found, adjust the
   * view options accordingly.
   * 
   */ 
  function getCached (options, callback) {
    
    //we dont cache completely reduced results
    if (options.view.reduce===false || !options.view.group_level) {
      return callback (null);  
    } 
    
    var keyBase  = parseKey(options.view);
    var time     = moment.utc(options.alignedTime);
    var end      = tools.getAlignedTime(endTime, options.increment);
    var cached   = [], keys = [];
    
    //skip the first unless it happens to be properly aligned
    if (time.diff(startTime)) time.add(options.increment, 1);
    
    //set up key list      
    while(end.diff(time)>=0) {
      keys.push(keyBase+":"+time.unix());
      time.add(options.increment, 1);
    } 
    
    //get cached points for the range
    redis.mget(keys, function(error, res){
      if (error)       return callback(error);
      if (!res.length) return callback();
      var last;
      
      for (var i=0; i<res.length; i++) {
        if (!res[i]) break; //missing data from this point
        row  = JSON.parse(res[i]);
        last = row[0];

        cached.push(row); //add to the list of cached results;
      }
      
      if (!last) return callback();   //no cached rows 
      last = moment.utc(last);
        
      //adjust range of query to exclude cached results        
      var key     = options.view.startkey.slice(0,1);
      var subview = JSON.parse(JSON.stringify(options.view)); //shallow copy
      
      subview.startkey    = key.concat(last.toArray().slice(0,6));
      options.subview     = subview;
      options.cached      = cached;
      options.alignedTime = last;
      options.startTime   = last;
      callback();
    });
  }
  
  /*
   * create the key used for the cache from the options
   */
  
  function parseKey(options) {    
    return "CB:"+options.startkey[0]+":"+options.group_level; 
  }
  
  /*
   * Save results into the cache for future use
   */
  
  function cacheResults(options, rows) {

    var keyBase = parseKey(options.view);
    var time    = moment.utc(options.alignedTime);
    var max     = moment.utc().subtract(45, 'minutes');
    var points  = [];
    
    //use the lesser of current time or endTime    
    if (max.diff(endTime)>0) max = moment.utc(endTime);
    
    if (options.increment=="all") return; //ignore these
    if (options.view.reduce===false || !options.view.group_level) return; //ignore these too
    
    rows.forEach(function(row){
      var time    = moment.utc(row[0]);
      var aligned = tools.getAlignedTime(time, options.increment);
      var key     = keyBase+":"+time.unix();
            
      //exclude the ones that aren't aligned
      //this should be the first and last unless the
      //client aligned them properly beforehand
      if (time.diff(aligned)) return; 
      if (time.diff(max) >= 0) return;
      
      points.push(key);
      points.push(JSON.stringify(row));
    });
    
    if (points.length) {
      redis.mset(points, function(error, res){
        if (error) return callback("Redis - " + error);
        if (DEBUG) winston.info(points.length/2 + " points cached");
      });
    }  
  }
  
/**
 * getHotWalletBalances
 * @param {Object} c
 * @param {Object} options
 * @param {Object} callback
 */

  function getHotWalletBalances (c, options, callback) {
    var viewOptions = options.subview ? options.subview : options.view;
    var balances    = viewOptions.group_level ? {} : 0;
          
    async.map(c.hotwallets, function(account, asyncCallback) {  
      var view = JSON.parse(JSON.stringify(viewOptions));
      var key  = c.currency + "." + account; 
      var balance = 0, intitial;
      
      view.startkey[0] = key;
      view.endkey[0]   = key;
    
      var initialOpts = {
         startkey   : view.startkey,
         endkey     : [key],
         reduce     : false,
         limit      : 1,
         descending : true,
         stale      : 'ok' 
      }
      
      db.view('currencyBalances', 'v1', view, function(err, resp) {
        if (err) {
          return asyncCallback(err);
        }
        
        if (!view.group_level) {
          if (resp.rows.length) balances += resp.rows[0].value;
          return asyncCallback(null); 
        }
          
        //get initial balance 
        db.view('currencyBalances', 'v1', initialOpts, function(err, initial) {
          if (err) {
            return asyncCallback(err);
          }
        
          if (initial.rows.length && initial.rows[0].value[0] === c.issuer) {
            balance = initial.rows[0].value[1];
          }
        
          var time = moment.utc(view.startkey.slice(1)).format();
          balances[time] = balances[time] ? balances[time] + balance : balance;
          
          resp.rows.forEach(function(row) {
            time = moment.utc(row.key.slice(1)).add(options.increment, 1).format();
            balance += row.value;
            if (balance > 0) {
              balances[time] = balances[time] ? balances[time] + balance : balance;
            }
          });
          
          asyncCallback(null);
        });
      });              
    
    }, function(err, results) {
      
      return callback(err, balances); 
    });    
  }
}
Example #29
0
 function collectImageSizes (cb) {
   // Collect the images sizes
   async.map([actualPath, expectedPath], ImageDiff.getImageSize, cb);
 },
Example #30
0
 var queueThumbnails = function(src_paths, dst, callback){
   async.map(src_paths, t, function(result){
     callback(true);
   })
 };