Skip to content

Instantly share code, notes, and snippets.

@icodejs
Created April 23, 2012 08:36
Show Gist options
  • Save icodejs/2469598 to your computer and use it in GitHub Desktop.
Save icodejs/2469598 to your computer and use it in GitHub Desktop.
Run DD Analytics ajax reports in batches
/*
* March 2012 - TSJ
* -----------------------------------------------------------------
* Loads charts in batches using Batchelor due to the maximum number
* of http request in most browsers being limited to 6.
*
*/
var reportsPerBatch = 6;
function Batchelor(objectArr, noOfBatches, func) {
var ajaxCallCount = 0,
numberOfBatches = noOfBatches,
batch = [],
fn = func;
(function () {
var i, j = 0,
lastInBatch, lastInArray, oneMoreLeft, childBatch = [],
len = objectArr.length;
for (i = 0; i < len; i += 1) {
lastInBatch = (j % numberOfBatches) === (numberOfBatches - 1);
oneMoreLeft = (len - (batch.length * numberOfBatches) === 1);
lastInArray = (len === i + 1);
if (lastInBatch) {
childBatch.push(objectArr[i])
batch.push(childBatch.slice(0));
childBatch = [];
j = 0;
} else if ((lastInArray && childBatch.length > 0) || (lastInArray && oneMoreLeft)) { // get left overs
childBatch.push(objectArr[i])
batch.push(childBatch.slice(0));
return;
} else {
childBatch.push(objectArr[i])
j += 1;
}
}
}());
return {
exec: function () {
var batchNo = 0;
fn(batch[batchNo], (batchNo + 1), batch.length);
$("html").bind("ajaxComplete", function () {
ajaxCallCount += 1;
if ((ajaxCallCount) === numberOfBatches) {
ajaxCallCount = 0;
batchNo += 1;
if ((batchNo + 1) <= batch.length) {
fn(batch[batchNo], (batchNo + 1), batch.length);
} else {
return;
}
}
});
}
};
} // end Batchelor
function runBatch(clientReportList, step, stepOf) {
var i, len = clientReportList.length;
$('#debug').fadeOut(700, function () {
$(this).html('<span class="note">Loading reports batch: ' + step + ' of ' + stepOf + '</span>').fadeIn(700);
});
for (i = 0; i < len; i += 1) {
render(clientReportList[i].clientAccountId, clientReportList[i].clientReportId, clientReportList[i].reportDetailsReportId, clientReportList[i].editable);
}
}
function render(ca_id, cr_id, rdr_id, editable) {
var err_msg = 'Error: An error occurred while trying to retrieve your report data. Please email the page URL to technical support.',
url = 'js/previewAjaxFunctions.aspx',
html = '';
$.ajax({
url: url,
dataType: 'html',
beforeSend: function () {
$('#content').append('<div id="' + rdr_id + '" class="reportContainer"><img src="/img/ajax-loader1.gif" /></div>');
},
data: {
'ajaxcall': 'true',
'id': ca_id,
'clientReportId': String(cr_id),
'reportDetailsReportId': rdr_id
},
async: true,
success: function (data) {
if (data) {
html += '<div class="editNoteContainer">';
html += (editable ? '<p class="buttonContainer"><a title="Edit" class="editNote edit" editMode="edit" parentReportDetailId="' + rdr_id + '" href="#">Add/Edit notes</a></p>' : '');
html += '</div>';
$('#content').attr({
clientAccountId: ca_id,
clientReportId: cr_id
}).find('#' + rdr_id).replaceWith(
$(data).attr('reportDetailId', rdr_id).append(html));
} else {
$('#content').find('.reportContainer').html('<span class="error">' + err_msg + '</span>');
}
}
});
}
function renderChartTable(clientReportList) {
var batch = new Batchelor(clientReportList, reportsPerBatch, runBatch);
batch.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment