I have a flot graph that polls data via ajax from an XML document. I use mode: time for x axis. It works fine, except intermittently, some values from the xml Document gets dropped. Here is the code that fetches xml via ajax.
function graphUpdate() {
$.ajax( {
url: "EnergyDM.xml",
type:"GET",
dataType: "XML",
success: onxmlReceived,
error: function(xhr, textStatus, errorThrown)
{
alert(textStatus + ' ' + errorThrown);
}
});
// setTimeout(graphUpdate,10000);
}
function onxmlReceived(data) {
var xmlData = null;
//for Non IE Browsers
if(window.DOMParser) {
//xmlData = data;
parser = new DOMParser();
xmlData = parser.parseFromString(data, "text/xml");
}
// For IE
else {
xmlData = new ActiveXObject("Microsoft.XMLDOM");
xmlData.async = false;
xmlData.loadXML(data);
}
$(xmlData).find('energyDM').each(function(){
Rec_Count = parseInt($(this).find('edNum').text());
d1=[];
for(var i = 0; i < Rec_Count; i++) {
if(i < 10) {
d1.push([(parseInt($(this).find('et000'+i).text(),10))*1000,
parseFloat($(this).find('ed000'+i).text())]);
} else if(i < 100) {
d1.push([(parseInt($(this).find('et00'+i).text(),10))*1000,
parseFloat($(this).find('ed00'+i).text())]);
} else {
d1.push([(parseInt($(this).find('et0'+i).text(),10))*1000,
parseFloat($(this).find('ed0'+i).text())]);
}
}
plotWithOptions();
});
}
The XML file looks like this:
<energyDM>
<edNum>305</edNum>
<TMF>UTC </TMF>
<pm339> 1.271</pm339><!-- pm339 max energy demand value -->
<pm340>1325033716</pm340><!-- pm340 max energy demand occured time -->
<ed0000> 0.282</ed0000>
<et0000>1325545093</et0000>
<ed0001> 0.283</ed0001>
<et0001>1325544792</et0001>
<ed0002> 0.284</ed0002>
<et0002>1325544492</et0002>
...
<ed0305> 0.284</ed0305>
<et0305>1325544492</et0305>
</energyDM>
When the returned XML misses nodes, if I use Chrome to view the values, it shows something like:
<ed0023> 0.283</ed0023>
<et0023>1325538176</et0023>
<ed0024> 0.281</ed0024>
<et0024>1325537875</et0024>
<ed0035> 0.281</ed0035>
<et0035>1325534564</et0035>
<ed0036> 0.280</ed0036>
<et0036>1325534263</et0036>
And this does not always happen. If I keep refreshing the page, it may drop few nodes, then the whole thing comes up, etc. And it is not always the same nodes getting dropped either.
I’d appreciate any hints.
As suggested by @MArk, The problem was with the XML generation. The processor from microchip which works out the XML file uses a stack which caused some bugs when multiple hits were made to the server simultaneously. When this happens, the subsequent interrupts were handled without completely taking care of the current action. This was found, and fixed and now the xml is good.