Last active
May 17, 2022 21:15
-
-
Save fernandojunior/7064132b655905bebb97 to your computer and use it in GitHub Desktop.
Simple lib to execute a mdx query using saiku rest api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.min.js"></script> | |
<!-- https://raw.githubusercontent.com/fernandojunior/prototype_class.js/v1.0.0/prototype_class.min.js --> | |
<script type="text/javascript" src="prototype_class.min.js"></script> | |
<script type="text/javascript"> | |
// needs prototype_class.min.js (https://github.com/fernandojunior/prototype_class.js) and jquery | |
var SaikuUtils = Class.simple_extend({ | |
constructor: function (rootpath) { | |
this.rootpath = rootpath || '/pentaho/plugin/saiku/api/'; | |
}, | |
// creates a query | |
open: function (conf) { | |
var username = conf.username; | |
var queryname = conf.queryname; | |
var cube = conf.cube; | |
var success = conf.success; | |
var error = conf.error; | |
var uri = this.rootpath + username + "/query/" + queryname; | |
var parameters = { | |
catalog: cube.catalogName || cube.schemaName, | |
connection: cube.connectionName || cube.schemaName, | |
schema: cube.schemaName, | |
cube: cube.name, | |
}; | |
$.post(uri, parameters, function(data, status, jqXHR) { | |
success(data, status, jqXHR); | |
}).fail(function() { | |
console.log(arguments); | |
error(arguments); | |
}); | |
}, | |
// executes a mdx over a query | |
execute: function (conf) { | |
var username = conf.username; | |
var queryname = conf.queryname; | |
var cube = conf.cube; | |
var mdx = conf.mdx; | |
var mimetype = conf.mimetype || 'json'; | |
var success = conf.success; | |
var error = conf.error; | |
var self = this; | |
if (typeof cube != 'undefined') // create query | |
return self.open({ | |
username: username, | |
queryname: queryname, | |
cube: cube, | |
success: function (data) { | |
delete conf['cube']; | |
self.execute(conf); | |
}, | |
error: error | |
}); | |
var uri = this.rootpath + username + "/query/" + queryname + "/result"; | |
$.post(uri, { mdx: mdx}, function(data, status, jqXHR) { | |
if (mimetype != 'json') // export result set | |
return self.export({ | |
username: username, | |
queryname: queryname, | |
mimetype: mimetype, | |
success: success, | |
error: error | |
}); | |
success(data, status, jqXHR); | |
}).fail(function() { | |
console.log(arguments); | |
error(arguments); | |
}); | |
}, | |
export: function (conf) { | |
var username = conf.username; | |
var queryname = conf.queryname; | |
var mimetype = conf.mimetype; | |
var success = conf.success; | |
var error = conf.error; | |
var uri = this.rootpath + username + "/query/" + queryname + "/export/" + mimetype; | |
if (mimetype != 'csv' && mimetype != 'pdf' && mimetype != 'xls') | |
return console.log('MIME type not suported: ' + type); | |
$.get(uri, function(data, status, jqXHR) { | |
success(data, status, jqXHR); | |
}).fail( function() { | |
console.log(arguments); | |
error(arguments); | |
}); | |
}, | |
}); | |
var test = function () { | |
SaikuUtils.create('http://localhost:8080/pentaho/plugin/saiku/api/').execute({ | |
username: 'admin', | |
queryname: 'test123', | |
cube: { | |
schemaName: 'SteelWheels', | |
name: 'SteelWheelsSales', | |
}, | |
mdx: 'SELECT NON EMPTY {Hierarchize({[Measures].[Quantity]})} ON COLUMNS, NON EMPTY {Hierarchize({[Product].[Product].Members})} ON ROWS FROM [SteelWheelsSales]', | |
mimetype: 'csv', // default json | |
success: function (data) { | |
console.log('MDX was executed: '); | |
console.log(data); | |
}, | |
}); | |
}; | |
test(); | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment