Skip to main content
1-Visitor
August 2, 2019
Solved

Stream AddStreamEntry overwrites the data

  • August 2, 2019
  • 1 reply
  • 4488 views

hello, i am trying to cache some data, so: i have a service that has its output as an InfoTable and retrieves a couple of rows and 5 columns of data. i would like to cache this service as it takes some time to run, but the below code only caches the last row form the server even thought the loop is ok and reads all the rows. (that s why i aded the ii object, for debugging purposes) in monitoring i could not find anything. i also wanted to try the AddStreamEntries service but i got stuck with its implementation

 

can you please help?

 

var values;

var params = {
	infoTableName : "InfoTable",
	dataShapeName : "GabiDS"
};

// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(AMTable_ModuleDDS_CRA_PG)
var table = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

 var tags = new Array();
 var timestamp = new Date();
		
	// result: INFOTABLE dataShape: AMTable_ModuleDDS_CRA_PG
	var actions = Things["GabiThing"].GetData();

 if (actions) {
 // loop through infotable
 if(Things["GabiStream"]){
 var params = {
 endDate: timestamp /* DATETIME */,
 immediate: true /* BOOLEAN */,
 startDate: undefined /* DATETIME */
 };
 // no return
 Things["GabiStream"].PurgeStreamEntries(params);
 }

 var tableLength = actions.rows.length;

 for (var x = 0; x < tableLength; x++) {
 var row = actions[x];
 values = Things["GabiStream"].CreateValues();

 try {
 values.x = row.x; //INTEGER
 } catch (err) {
 logger.error('x is null');
 }
 try {
 values.y = row.y; //INTEGER
 } catch (err) {
 logger.error('y is null');
 }
 try {
 values.z = row.z; //INTEGER
 } catch (err) {
 logger.error('z is null');
 }
 try {
 values.t = row.t; //INTEGER
 } catch (err) {
 logger.error('t is null');
 }

 var ii = new Object();
 ii.x = row.x;
 ii.y = row.y;
 ii.z = row.z;
 ii.t = row.t;
 
 table.AddRow(ii);
 
 var location = new Object();
 location.latitude = 0;
 location.longitude = 0;
 location.elevation = 0;
 location.units ="WGS84";
 
 
 var params = {
 tags : tags,
 timestamp : timestamp,
 source : me.name,
 values : values,
 location : location
 }; 
 Things["GabiStream"].AddStreamEntry(params);
 }
 }

var result = table;
Best answer by jwasney

I ran across this the other day.  I was only getting 1/20th of my data in the stream due to the timestamp.  To fix this I set timestamp = new Date(Date.now() + (i * 10));  That ensured there were no duplicate timestamps.

1 reply

1-Visitor
August 2, 2019

Does the 'table' for which you are add a new row ('ii', in your example) get created properly?

 

I'm confused because I see you creating this 'values' variable, and then setting the values in your loop (ex: values.t = row.t), but then after you set them, I don't see you doing anything further with the 'values' variable. 

 

Am I missing something?

gabitudor1-VisitorAuthor
1-Visitor
August 2, 2019
That table is only created for the debugging purposes, to see if in for loop takes all the data from GetData service. But "values" is basically the row that is added in the stream, if you look closer
gabitudor1-VisitorAuthor
1-Visitor
August 6, 2019

Thanks guys, indeed that solved the problem. It is pretty strange, because before i also tried using the pause snippet, but that did not work. Thank you so much to both of you!