Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehran-S committed Jul 10, 2018
2 parents b6446fd + 6293edb commit 092c9fd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
35 changes: 23 additions & 12 deletions RTView-Cumulocity-Node/cumulocity_connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ var getMeasurements = function(tableName, res, query, result, callback) {
var series = getValue(fmap,'series','');
delete fmap['id']; delete fmap['name']; delete fmap['series']; // tell cacheproxy we've handled these filters

var tr = Number(getValue(query,'tr',10))*1000;
// define a time range for the query; default 300 secs, but for current cache, we'll do 'revert' to get last point
var tr = Number(getValue(query,'tr',300))*1000;
var dateTo = Number(getValue(query, 'te', (new Date()).getTime()));
var dateFrom = Number(getValue(query, 'tb', dateTo - tr));

Expand All @@ -315,16 +316,17 @@ var getMeasurements = function(tableName, res, query, result, callback) {
//console.log('... measurement interval ' + dateFrom + ' to '+ dateTo + " - tr " + tr);

// if series name provided as filter, include in query
// DEVNOTE: in either case, for current do NOT use pageSize, since we will be using 'revert' below
if (series) {
seriesPath = name + '.' + series;
//console.log('***** seriesPath = ' + seriesPath);
url += (tableName == 'current' ? '?pageSize=100' : ('/series?series='+seriesPath));
url += (tableName == 'current' ? '?pageSizeX=100' : ('/series?series='+seriesPath));

// DEVNOTE: the below fails:
// if no series name provided, TRY to make a query that works ... but it doesn't !!
} else {
//console.log('***** fragment = ' + name);
url += (tableName == 'current' ? '?pageSize=100' : ('/series?fragmentType='+name));
url += (tableName == 'current' ? '?pageSizeX=100' : ('/series?fragmentType='+name));
}

if (id != '' && id != '*') {
Expand All @@ -350,8 +352,15 @@ var getMeasurements = function(tableName, res, query, result, callback) {
if(dateFrom >= dateTo) {
console.log("Error: bad date/time range: "+url);
console.log('... ' + JSON.stringify(query)+'\n');
return;
return;
}

// for current table, query measurements in reverse order to get last value, independent of query interval
// note: 'revert' must be the last argument of the query for it to work correctly
if (tableName == "current") {
url += '&revert=true';
}

// create object to hold info related to this request
urlInfo = { res:res, result:result, tableName:tableName, query:query, url:url, dateFrom:dateFrom, dateTo:dateTo, id:id, name:name, series:series }

Expand All @@ -370,8 +379,9 @@ var getMeasurements = function(tableName, res, query, result, callback) {
return function(error, response, body) {
if (!error && response.statusCode == 200) {
var b = JSON.parse(body);
urlInfo.res.queryStatus = response.statusCode;
urlInfo.res.queryStatusText = "OK";
//urlInfo.res.queryStatus = response.statusCode;
urlInfo.res.queryStatus = 0;
urlInfo.res.queryStatusText = "OK";
console.log('... measurement query ' + url);
//console.log(' measurements: ' + JSON.stringify(b.measurements, 0, 2));
var rtvdata = [];
Expand All @@ -386,8 +396,8 @@ var getMeasurements = function(tableName, res, query, result, callback) {
console.log(' ... getMeasurements exec_time: ' + (Date.now() - urlInfo.dateTo) + ' ' +
urlInfo.name+'.'+urlInfo.tableName + ' ' + urlInfo.result.data.length + ' rows\n');// + JSON.stringify(urlInfo.result)+'\n');
} else {
urlInfo.res.queryStatus = response ? response.statusCode : 0;
urlInfo.res.queryStatusText = error;
urlInfo.res.queryStatus = response ? response.statusCode : 0;
urlInfo.res.queryStatusText = error;
console.log('ERROR: query for: ' + urlInfo.url + ' ' + error + ' resp code: ' + urlInfo.res.queryStatus );
}
//if (response == undefined) console.log('WARNING: no response from: ' + urlInfo.url);
Expand Down Expand Up @@ -489,8 +499,9 @@ var getCacheData = function(arrayName, url, var_meta, var_map, tableName, res, q
return function(error, response, body) {
if (!error && response.statusCode == 200) {
var b = JSON.parse(body);
urlInfo.res.queryStatus = response.statusCode;
urlInfo.res.queryStatusText = "OK";
//urlInfo.res.queryStatus = response.statusCode;
urlInfo.res.queryStatus = 0;
urlInfo.res.queryStatusText = "OK";
console.log('... ' + urlInfo.arrayName + ' query: ' + url);
//if(b.statistics) // print paging stats
//console.log('... b.statistics: ' + JSON.stringify(b.statistics, 0, 2));
Expand All @@ -514,8 +525,8 @@ var getCacheData = function(arrayName, url, var_meta, var_map, tableName, res, q
console.log(' ... getData exec_time: ' + (Date.now() - urlInfo.start) + ' ' +
urlInfo.arrayName+'.'+urlInfo.tableName + ' ' + urlInfo.result.data.length + ' rows\n');// + JSON.stringify(urlInfo.result)+'\n');
} else {
urlInfo.res.queryStatus = response ? response.statusCode : 0;
urlInfo.res.queryStatusText = error;
urlInfo.res.queryStatus = response ? response.statusCode : 0;
urlInfo.res.queryStatusText = error;
console.log('ERROR: query for: ' + urlInfo.url + ' ' + error + ' resp code: ' + urlInfo.res.queryStatus);
}
//if (response == undefined) console.log('WARNING: no response from: ' + urlInfo.url);
Expand Down
28 changes: 20 additions & 8 deletions RTView-Cumulocity-Node/rtview_cacheproxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var path = require('path');
// CONFIGURATION SETTINGS

// RTView Cache Proxy Server port
var port = process.env.RTVIEW_SERVER_PORT || 8081
var port = process.env.RTVIEW_SERVER_PORT || process.env.SERVER_PORT || 8081

var verbose = false

Expand Down Expand Up @@ -183,6 +183,10 @@ var processCacheQuery = function (cacheName, tableName, query, res, callback) {
// process normal cache query, with asynchronous execution
properties = cacheMap[cacheName]
metadata = metadataMap[cacheName]
if (!properties || !metadata) {
console.log('ERROR: cannot find cache definition: ' + cacheName);
return callback(res, null, query);
}
indexColArray = properties.indexColumnNames.split(';')
histColArray = properties.historyColumnNames.split(';')
if (histColArray === undefined || histColArray === null) histColArray = []
Expand Down Expand Up @@ -243,6 +247,7 @@ var processRTViewDs = function(cacheName, tableName, query, res) {
data.push( { Table: (cacheName + '.history'), Rows: 1000 } );
}
result = { metadata: metadata, metadataFull: metadata, data: data }
res.queryStatus = 0; res.queryStatusText = 'OK';
return result
}

Expand Down Expand Up @@ -288,27 +293,30 @@ var processRTViewDs = function(cacheName, tableName, query, res) {
});
}
result = { metadata: cdMetadata, metadataFull: cdMetadata, data: data }
res.queryStatus = 0; res.queryStatusText = 'OK';
return result
}

if (tableName == 'CacheObjectProperties') {
cdMetadata = [{name:'cacheName',type:'string'},{name:'maxNumberOfHistoryRows',type:'int'},{name:'timestampColumnName',type:'string'},{name:'historyColumnNames',type:'string'}]
data = [];
for (var cacheName in cacheMap) {
for (var cacheName in cacheMap) {
properties = cacheMap[cacheName];
//metadata = metadataMap[cacheName];
//indexColArray = properties.indexColumnNames;
histColArray = properties.historyColumnNames;
data.push( { cacheName:cacheName,
maxNumberOfHistoryRows:3000,
maxNumberOfHistoryRows:3000,
timestampColumnName:"time_stamp",
historyColumnNames:histColArray
});
}
result = { metadata: cdMetadata, metadataFull: cdMetadata, data: data }
res.queryStatus = 0; res.queryStatusText = 'OK';
return result
}


res.queryStatus = 0; res.queryStatusText = 'OK';
return null;
}

Expand Down Expand Up @@ -421,15 +429,14 @@ var formatAndSend = function (res, query, result) {
//console.log(' ----> row:; ' + r + ' = ' + result[r])
if (result[r] !== undefined) {
if (r > 0) fmtData += ','
fmtData += formatTable(result[r])
fmtData += formatTable(res, result[r])
}
}
fmtData += ']'
} else {
//console.log('... formatting single ... ')
fmtData += formatTable(result)
fmtData += formatTable(res, result)
}
fmtData += ',"queryStatus":' + res.queryStatus + ', "queryStatusText":"'+res.queryStatusText+'"\n}\n';
fmtData += ');} catch (ex) {if (window.console && console.log) console.log(ex);}';

try {
Expand All @@ -453,7 +460,7 @@ var formatAndSend = function (res, query, result) {
}

// Apply JSONP formatting to table
var formatTable = function (table) {
var formatTable = function (res, table) {
metadata = table.metadata; data = table.data; paging = table.paging;
//console.log(' +++++++++++ got result: ' + JSON.stringify(metadata))
//console.log(' ... formatting table of n rows: ' + (table.data ? table.data.length : -1))
Expand All @@ -466,7 +473,12 @@ var formatTable = function (table) {
if (paging !== undefined) {
fmtData += ',"paging":' + JSON.stringify(paging)
}
// capture the actual response code, instead of forcing OK, but only if not 200
//fmtData += ',"queryStatus":0, "queryStatusText":"OK"\n}\n';
if (res.queryStatus == 200) {
res.queryStatus = 0;
}
fmtData += ',"queryStatus":' + res.queryStatus + ', "queryStatusText":"'+res.queryStatusText+'"\n}\n';
return fmtData
}

Expand Down

0 comments on commit 092c9fd

Please sign in to comment.