Forked from fernandojunior/gist:7064132b655905bebb97
Last active
August 29, 2015 14:10
-
-
Save brenopolanski/a71983a90efd68602c46 to your computer and use it in GitHub Desktop.
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
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(); | |
// 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); | |
}); | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment