105 lines
2.1 KiB
JavaScript
105 lines
2.1 KiB
JavaScript
SOC.Query = function (options) {
|
|
|
|
if (options && options.path) {
|
|
this.path = options.path;
|
|
} else {
|
|
this.path = null;
|
|
}
|
|
|
|
if (options && options.queryStart) {
|
|
this.queryStart = options.queryStart;
|
|
} else {
|
|
this.queryStart = 'now-7d';
|
|
}
|
|
|
|
if (options && options.queryEnd) {
|
|
this.queryEnd = options.queryEnd;
|
|
} else {
|
|
this.queryEnd = 'now+4h';
|
|
}
|
|
|
|
if (options && options.query) {
|
|
this.query = options.query;
|
|
} else {
|
|
this.query = null;
|
|
}
|
|
|
|
if (options && options.onData) {
|
|
this.onData = options.onData;
|
|
} else {
|
|
this.onData = function() {
|
|
throw new Error(
|
|
"You should specify a onData function in the constructor to your query, ex. SOC.Query.SomeQuery({onData : myCallbackFunction})"
|
|
)
|
|
};
|
|
}
|
|
|
|
this.run();
|
|
|
|
};
|
|
|
|
SOC.Query.prototype = Object.create(SOC.prototype);
|
|
SOC.Query.prototype.constructor = SOC;
|
|
|
|
SOC.Query.prototype.run = function() {
|
|
|
|
if (this.path === null) {
|
|
throw new Error('query path must be set');
|
|
}
|
|
|
|
if (this.query === null) {
|
|
throw new Error('query must be set');
|
|
}
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open(
|
|
'POST',
|
|
this.apiUrl + this.path,
|
|
true
|
|
);
|
|
|
|
xhr.onload = function(event){
|
|
console.log('loaded');
|
|
this.onData(JSON.parse(event.target.responseText));
|
|
}.bind(this);
|
|
|
|
xhr.onerror = function(event) {
|
|
throw new Error('An error occured during query execution');
|
|
};
|
|
|
|
xhr.setRequestHeader(
|
|
'Content-Type',
|
|
'application/json'
|
|
);
|
|
|
|
var queryText = JSON.stringify(this.query);
|
|
|
|
queryText = queryText.replace('%QUERY_START', this.queryStart);
|
|
queryText = queryText.replace('%QUERY_END', this.queryEnd);
|
|
|
|
xhr.send(
|
|
queryText
|
|
);
|
|
};
|
|
|
|
SOC.Query.prototype.drawTable = function(divId, data) {
|
|
|
|
return function() {
|
|
|
|
var dataTable = new google.visualization.DataTable();
|
|
|
|
data.columns.map(
|
|
function(column) {
|
|
dataTable.addColumn(column.type, column.name);
|
|
}
|
|
);
|
|
|
|
dataTable.addRows(data.rows);
|
|
|
|
var table = new google.visualization.Table(document.getElementById(divId));
|
|
|
|
table.draw(dataTable, {showRowNumber: true, width: '100%', height: '100%'});
|
|
}
|
|
};
|