0

我在节点模块中有一个函数,它是一组三个嵌套回调,它工作得很好,给了我需要的线性,因为每个嵌套回调都依赖于前一个回调的数据。问题是第二个函数的回调需要冒泡并递归调用其父函数。它与外部 API 进行通信。这是实际代码,变量重命名以混淆我的超级顶级 sekrit 业务逻辑:

exports.account_usage = function (req, res) {

  var domainID = req.body.domains,
    startDate = req.body.date_start,
    endDate = req.body.date_end,
    accountItems = {},
    usage = {},
    domainStats = {},
    page = 0;
  //req.cs is a module that communicates with an external API to gather usage data
  req.cs.exec("listAccountItems", {
    "listall": "true",
      "domainid": domainID
  },

  function (error, result) {
    accountItems = result.item;
    console.log("listAccountItems callback");

    //Get Usage Records

    req.cs.exec("listUsageRecords", {
      "startdate": startDate,
        "enddate": endDate,
        "domainid": domainID,
        "page": page,
        "pagesize": 2000 //try not to DDOS the server. only fetch 2000 records at a time
    }, function (error, result) {
      usage = req._.extend(usage, result.usagerecord); //this is underscore
      console.log("Usage Records: " + usage.length);
      page++;
      //right here, if result.length === 2000, I need to call
      // listUsageRecords until result.length < 2000

      //got list of  Usage,
      //now process usage here

      //filter usage item 1
      var bytesrecords1 = req._.filter(usage, function (usage) {
        return (usage.usagetype === 4);
      });
      //sum
      var bytes1 = req._.reduce(bytesrecords1, function (memo, record) {
        return memo + parseInt(record.rawusage, 10);
      }, 0);
      console.log("Bytes1: " + bytes1);
      domainStats.bytes1 = (((bytes1 / 1000) / 1000) / 1000).toFixed(4);

      //filter usage item 2
      var bytesrecords2 = req._.filter(usage, function (usage) {
        return (usage.usagetype === 5);
      });
      //sum
      var bytes2 = req._.reduce(bytesrecords2, function (memo, record) {
        return memo + parseInt(record.rawusage, 10);
      }, 0);
      console.log("Bytes2: " + bytes2);
      domainStats.bytes2 = (((bytes2 / 1000) / 1000) / 1000).toFixed(4);

      req._.each(accountItems, function (account) {
        //get runnning hours
        var recs = req._.filter(usage, function (usage) {
          return (usage.accountid === account.id && usage.usagetype === 1);
        });
        account.usage = req._.reduce(recs, function (memo, record) {
          return memo + parseInt(record.rawusage, 10);
        }, 0);

        //filter all the recs for each usage type, 1-14
        console.log("Account Usage: " + account.usage);
        console.log("Account ID: " + account.name);
      });


      console.log("ready to render");
      res.render('usage', {
        "title": "Usage Report",
        "domain": domainStats
      });


    });
  });


};

实际代码也在这个小提琴中:[http://jsfiddle.net/3wTQA/1/][1] 我一直使用谷歌,直到我的手指流血,我不知道如何阻止内部回调继续的递归。API 需要来自外部 API 的分页,以防止在需要获取大型数据集的情况下远程系统上的 DDOS。

编辑:这是实际代码,经过注释和清理,并附有一些我试图提取的数据示例:http: //jsfiddle.net/3wTQA/1/

4

2 回答 2

1

好的,我得到它的工作。我使用 async.whilst 函数留在回调中,直到获取所有数据。我是这样做的:

exports.account_usage = function (req, res) {

  var domainID = req.body.domains,
    startDate = req.body.date_start,
    endDate = req.body.date_end,
    accountItems = {},
    usage = {},
    domainStats = {},
    page = 0;
  //req.cs is a module that communicates with an external API to gather usage data
  req.cs.exec("listAccountItems", {
    "listall": "true",
      "domainid": domainID
  },

  function (error, result) {
    accountItems = result.item;
    console.log("listAccountItems callback");

    //Get Usage Records
async.whilst(
function(){return count === pagesize},
function (callback){
    req.cs.exec("listUsageRecords", {
      "startdate": startDate,
        "enddate": endDate,
        "domainid": domainID,
        "page": page,
        "pagesize": 2000 //try not to DDOS the server. only fetch 2000 records at a time
    }, function (error, result) {
       usage.usagerecord = usage.usagerecord.concat(result.usagerecord);
       count = result.usagerecord.length;

      console.log("Usage Records: " + usage.length);
      page++;
      callback();
      //now process usage here
},
function (err) {
      //filter usage item 1
      var bytesrecords1 = req._.filter(usage, function (usage) {
        return (usage.usagetype === 4);
      });
      //sum
      var bytes1 = req._.reduce(bytesrecords1, function (memo, record) {
        return memo + parseInt(record.rawusage, 10);
      }, 0);
      console.log("Bytes1: " + bytes1);
      domainStats.bytes1 = (((bytes1 / 1000) / 1000) / 1000).toFixed(4);

      //filter usage item 2
      var bytesrecords2 = req._.filter(usage, function (usage) {
        return (usage.usagetype === 5);
      });
      //sum
      var bytes2 = req._.reduce(bytesrecords2, function (memo, record) {
        return memo + parseInt(record.rawusage, 10);
      }, 0);
      console.log("Bytes2: " + bytes2);
      domainStats.bytes2 = (((bytes2 / 1000) / 1000) / 1000).toFixed(4);

      req._.each(accountItems, function (account) {
        //get runnning hours
        var recs = req._.filter(usage, function (usage) {
          return (usage.accountid === account.id && usage.usagetype === 1);
        });
        account.usage = req._.reduce(recs, function (memo, record) {
          return memo + parseInt(record.rawusage, 10);
        }, 0);

        //filter all the recs for each usage type, 1-14
        console.log("Account Usage: " + account.usage);
        console.log("Account ID: " + account.name);
      });


      console.log("ready to render");
      res.render('usage', {
        "title": "Usage Report",
        "domain": domainStats
      });


    });
  });
}),

};
于 2013-01-13T16:10:05.027 回答
0

我认为您可以pages在包含biglistofresults.

biglistofresults = {};
function pages(id, page) {
    page = page || 0;
    module.exec("anotherAPIcall", {"id": id, "page": page }, 
        function (error, result) {
            if (result.length === 2000) { //there is probably more data
                biglistofresults = _.extend(biglistofresults, result);
                pages(id, page + 1);
            }
    );
}
module.exec("externalAPIcall", {"listall": "true", "domainid": domainID}, 
    function (error, result) {
        _.map(result, pages);
});
于 2013-01-12T02:27:59.233 回答