How many API Request is considered by Parse.com? - parse-platform

I have a cloud code written
Parse.Cloud.define("getApartmentVendorProduct", function(request, response) {
var isApartmentCallComplete = false;
var isVendorCallComplete = false;
var isProductCallComplete = false;
var result = {};
var apartmentQuery = new Parse.Query("Apartment");
apartmentQuery.find({
success: function(results) {
isApartmentCallComplete = true;
results.apartments = results;
}
});
var vendorQuery = new Parse.Query("Vendor");
vendorQuery.find({
success: function(results) {
isVendorCallComplete = true;
results.vendors = results;
}
});
var productQuery = new Parse.Query("Product");
productQuery.find({
success: function(results) {
isProductCallComplete = true;
results.products = results;
}
});
setInterval(function () {
if (isApartmentCallComplete && isVendorCallComplete && isProductCallComplete) {
response.success(results);
}
}, 50);
});
PS: I'm well aware that setInterval wont work on Parse.. This code is just for understanding.
In this cloud function i'm making 3 Query operation.
From my Android application i'm calling this cloud code.
Here is my question.
How many API request is this considered?
1) 3 API Request made by cloud code and 1 API Request made by Android - Total 4
2) Just 1 API Request made by Android. - Total 1

The option is 1 it makes 4 requests.
I tried with a sample code to test Burst Limit
Parse.Cloud.define("testBurstLimit", function(request, response) {
var globalI = 0;
for(var i = 0; i < 500; i++) {
var productQuery = new Parse.Query("Product");
productQuery.find({
success: function(results) {
console.log("success " + i + " " + globalI);
globalI++;
if (globalI == 250) {
response.success("success");
}
},
error: function(error) {
isApartmentCallComplete = true;
if (isApartmentCallComplete && isVendorCallComplete && isProductCallComplete) {
console.log(error.message + " " + error.code);
}
}
});
}
});
One thing strange i noticed is that. Parse doesn't calculate requests/second, instead it calculates in Requests per/min. Check the response from Parse when i perform the BurstLimit cloud code again and again
{"code":155,"error":"This application performed 1814 requests over the last 28s, and exceeded its request limit. Please retry in 32s"}

Related

Parallel asynchronous Ajax calls from the client

I have 20 data packet in the client and I am pushing one by one to the server via Ajax post. Each call take approximately one minute to yield the response. Is there any way to make few of these requests run parallel.
I have used Jquery promise. However, still the request waiting for the prior one to get completed.
var dataPackets=[{"Data1"},{"Data2"},{"Data3"},{"Data4"},{"Data5"},
{"Data6"},{"Data7"},{"Data8"},{"Data9"},{"Data10"},
{"Data11"},{"Data12"},{"Data13"},{"Data14"},{"Data15"},{"Data16"},
{"Data17"},{"Data18"},{"Data19"},{"Data20"}];
$(dataPackets).each(function(indx, request) {
var req = JSON.stringify(request);
setTimeout({
$.Ajax({
url: "sample/sampleaction",
data: req,
success: function(data) {
UpdateSuccessResponse(data);
}
});
}, 500);
});
The when...done construct in jQuery runs ops in parallel..
$.when(request1(), request2(), request3(),...)
.done(function(data1, data2, data3) {});
Here's an example:
http://flummox-engineering.blogspot.com/2015/12/making-your-jquery-ajax-calls-parallel.html
$.when.apply($, functionArray) allows you to place an array of functions that can be run in parallel. This function array can be dynamically created. In fact, I'm doing this to export a web page to PDF based on items checked in a radio button list.
Here I create an empty array, var functionArray = []; then based on selected items I push a function on to the array f = createPDF(checkedItems[i].value)
$(document).ready(function () {
});
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
function exportPDFCollection() {
var f = null;
var x = 0;
var checkedItems = $("input:checked");
var count = checkedItems.length;
var reportList = $(checkedItems).map(
function () {
return $(this).next("label").text();
})
.get().join(",");
var functionArray = [];
var pdf = null;
for (var i = 0; i < count; i++) {
f = createPDF(checkedItems[i].value)
.done(function () {
pdf = checkedItems[x++].value;
alert('PDF => ' + pdf + ' created.');
})
.fail(function (jqxhr, errorText, errorThrown) {
alert('ajax call failed');
});
functionArray.push(f);
}
$.when.apply($, functionArray)
.done(function () {
$.get("http://yourserver/ExportPage.aspx",{reports: reportList})
.done(function () {
alert('PDF merge complete.');
})
.fail(function (jqxhr, errorText, errorThrown) {
alert('PDF merge failed. Please try again.');
});
return true;
});
}
function createPDF(webPage) {
return $.get(webPage);
}

Error Message- Failed with: success/error was not called

Parse.Cloud.job("JobSchedule", function(request, response) {
var Group = Parse.Object.extend("Group");
var query = new Parse.Query(Group);
query.equalTo("JobScheduled", true);
query.find({
success: function(results) {
for (var i = 0; i < results.length; ++i) {
var created = new Date(results[i].createdAt);
var current = new Date();
var timeDiff = Math.abs(current.getTime() - created.getTime());
var horsDiff = timeDiff / (60 * 60 * 1000);
if (horsDiff >= parseInt(results[i].get("JobHours"))) {
results[i].set("JobScheduled", false);
results[i].set("GroupType", "Private");
results[i].set("JobHours", 0);
results[i].save();
var GroupMembers = Parse.Object.extend("GroupMembers");
var query1 = new Parse.Query(GroupMembers);
query1.equalTo("GroupId", results[i].id);
query1.find({
success: function(grpresults) {
for (var j = 0; j < grpresults.length; ++j) {
grpresults[j].set("GroupType", "Private");
grpresults[j].save();
}
},
error: function(error) {
response.error(error);
}
})
}
}
},
error: function(error) {
response.error(error);
}
});
});
Script is running properly(it updates the values as defined). But in parse log it shown as Failed with: success/error was not called. Please suggest me over this issue
You simply never call response.success() or response.error(), and you should always call at least one of those.
You should really be storing all of the promises returned from your queries and saves and waiting until all of them are complete and then calling success or error based on the result from those promises.

Parse.com: Update of user table is not working

I have defined a cloud function to set custom column in User table. I have 6 results returned for my query but update of field only happens for 3 rows, I could not understand what's wrong with the code below,
Parse.Cloud.define("updateSaturdayAbsentWeeks", function(request, response) {
// User master key so that we can update all users
Parse.Cloud.useMasterKey();
// Tables to Query
var query = new Parse.Query(Parse.User);
// We want only players who are absent on Saturdays
query.equalTo("isSaturdayAbsent", true);
query.find({
success: function(results) {
console.error('updateSaturdayAbsentWeeks, Saturday absent players count ' + results.length);
for (var i = 0; i < results.length; i++) {
var absentUser = results[i];
var absentWeeks = absentUser.get('saturdayAbsentWeeks') - 1;
absentUser.set("saturdayAbsentWeeks", absentWeeks);
absentUser.save();
console.error('updateSaturdayAbsentWeeks, absentWeeks for user name = ' + absentUser.get('username') + ', is = ' + absentWeeks)
}
// All done
response.success("updateSaturdayAbsentWeeks finished successfully");
},
error: function() {
console.error("runSaturdayExpense, lookUp Failed - Each player enjoyed the game on Saturday ");
response.error("updateSaturdayAbsentWeeks failed");
}
});
});
After the query I can see it returned 6 records but in the for loop it only updates 3 records only and does not do anything to other three records and no errors returned.
Any pointers to understand why all 6 records are not getting updated would be greatly appreciated.
Thanks
This has to do with the fact that the save() function is asynchronous. The proper way to do this is using saveAll().
query.find({
success: function(results) {
var saveThese = [];
for (var i = 0; i < results.length; i++) {
var absentUser = results[i];
var absentWeeks = absentUser.get('saturdayAbsentWeeks') - 1;
absentUser.set("saturdayAbsentWeeks", absentWeeks);
saveThese.push(absentUser);
}
Parse.Object.saveAll(saveThese, {
success: function (list) {
response.success("updateSaturdayAbsentWeeks finished successfully");
},
error: function (error) {
response.error("did not save all the items");
}
});
},
error: function() {
response.error("updateSaturdayAbsentWeeks failed");
}
});

How to know if all uploads completed within a loop?

I have a code that works well for sending multiple files via ajax with FormData and XMLHttpRequest;
for (var i=0, j=this.files.length; i<j; i++) {
file = this.files[i];
var formdata = new FormData();
formdata.append("images[]", file);
var xhr = new XMLHttpRequest(),
upload = xhr.upload,
id = Math.floor((Math.random() * 100000));
upload.addEventListener("loadstart", function(e){
showUploadedItem(file, this.id);
});
upload.id = id;
upload.onprogress = function(e) {
var done = e.position || e.loaded, total = e.totalSize || e.total;
)
};
upload.onload = function(e) {
if (this.status == 200) {
console.log('');
}
};
xhr.onreadystatechange = function(e) {
if ( 4 == this.readyState ) {
console.log('');
}
};
xhr.open('post', '<?php echo Yii::app()->createUrl('url') ?>', true);
xhr.send(formdata);
}
I am sending each file as a new XMLHttpRequest object inside the loop, so I do not know when I'm getting all requests ending.
Can anyone help?
Take a look at the documentation for XMLHttpRequest.
There are a couple of options that I can think of. You could use the "loadend" callback for each of them and increment a variable outside of the loop and check for the total amount of requests that were sent in each one. Once the count reaches the total number of requests, you could perform any logic or call a function that would want to call.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest?redirectlocale=en-US&redirectslug=DOM%2FXMLHttpRequest%2FUsing_XMLHttpRequest
Otherwise, setting the async parameter to false would work as well, but then you take a performance hit waiting for each one to finish before starting the others.
Based on your answer, my solution;
var x = 0;
var lenght = this.files.length;
for (var i=0, j=lenght; i<j; i++) {
// code here
var xhr = new XMLHttpRequest(),
// code here
xhr.onreadystatechange = function(e) {
if ( 4 == this.readyState && this.status == 200 ) {
x++;
if(x == lenght) {
window.setTimeout( function(){
alert('finish');
}, 1000 );
}
}
};
// code here
}
Though it is a trivial function, it works.

Adding a .ajaxForm function to standard .ajax call

So I'm trying to find a method of getting a progress bar working on my .ajax call but not having much luck. I know that the ajaxForm plugin has the following code in it that allows for the uploadProgress option but the way my code works I'm not able to use that plugin. Is there anyway of adding the following code somehow so that it attaches to the standard .ajax call? Long shot I know!
// XMLHttpRequest Level 2 file uploads (big hat tip to francois2metz)
function fileUploadXhr(a) {
var formdata = new FormData();
for (var i=0; i < a.length; i++) {
formdata.append(a[i].name, a[i].value);
}
if (options.extraData) {
var serializedData = deepSerialize(options.extraData);
for (i=0; i < serializedData.length; i++)
if (serializedData[i])
formdata.append(serializedData[i][0], serializedData[i][1]);
}
options.data = null;
var s = $.extend(true, {}, $.ajaxSettings, options, {
contentType: false,
processData: false,
cache: false,
type: method || 'POST'
});
if (options.uploadProgress) {
// workaround because jqXHR does not expose upload property
s.xhr = function() {
var xhr = jQuery.ajaxSettings.xhr();
if (xhr.upload) {
xhr.upload.addEventListener('progress', function(event) {
var percent = 0;
var position = event.loaded || event.position; /*event.position is deprecated*/
var total = event.total;
if (event.lengthComputable) {
percent = Math.ceil(position / total * 100);
}
options.uploadProgress(event, position, total, percent);
}, false);
}
return xhr;
};
}
s.data = null;
var beforeSend = s.beforeSend;
s.beforeSend = function(xhr, o) {
o.data = formdata;
if(beforeSend)
beforeSend.call(this, xhr, o);
};
return $.ajax(s);
}
I do not guarantee on that, but try this:
xhr.upload.onprogress = function(event) {
var percent = 0;
var position = event.loaded || event.position;
var total = event.total;
if (event.lengthComputable) {
percent = Math.ceil(position / total * 100);
}
console.log("Progress: "+percent+"%"); //debug to see if the problem is there
options.uploadProgress(event, position, total, percent);
};
From posted code, I cannot even guess if the problem is in onprogress event handling or that options.uploadProgress, whatever it is.
I posted this as answer only because it wouldn't fit in comment.

Resources