Search code examples
javascriptpentaho-data-integration

Pentaho Data Integration: extract language distribution code


I'm using PDI API to get the language distribution for code in my projects.

Using a request like

var url = "http://<my_sonar_instance>/api/measures/component?additionalFields=metrics&component=<sonar_key>&metricKeys=duplicated_lines_density%2Cblocker_violations%2Ccomment_lines_density%2Ccritical_violations%2Cnew_maintainability_rating%2Cnew_duplicated_lines_density%2Cnew_reliability_rating%2Cnew_security_rating%2Cncloc%2Cncloc_language_distribution";

and parsing the JSON result by

$.component.measures[?(@.metric=="ncloc_language_distribution")].value

to assign the value at a variable named

sonar_ncloc_language_distribution

I obtain for example

java=10514;js=237;jsp=3995;web=5;xml=42

Now I'd like to assign those values at some variables in a PDI transformation using a Modified JavaScript value using this Javascript code

var map = new Map();
sonar_ncloc_language_distribution.split(";").forEach(x => map[x.split("=")[0]] = x.split("=")[1]);

var sonar_ncloc_java = map.java;
var sonar_ncloc_js = map.js;
var sonar_ncloc_jsp = map.jsp;
var sonar_ncloc_web = map.web;
var sonar_ncloc_xml = map.xml;

but it doesn't work and the error is

Unable compile javascript: 
syntax error (script#2)

(NOTE: if I try to execute the javascript code "alone" in a HTML file all works fine ...)

<!DOCTYPE html>
<html>
<body>

<script>
var sonar_ncloc_language_distribution = "java=10514;js=237;jsp=3995;web=5;xml=42";

var map = new Map();

sonar_ncloc_language_distribution.split(";").forEach(x => map[x.split("=")[0]] = x.split("=")[1]);
console.log(map);

var sonar_ncloc_java = map.java;
var sonar_ncloc_js = map.js;
var sonar_ncloc_jsp = map.jsp;
var sonar_ncloc_web = map.web;
var sonar_ncloc_xml = map.xml;

console.log(map.java);
console.log(map.js);
console.log(map.jsp);
console.log(map.web);
console.log(map.xml);

</script>

</body>
</html> 

Any suggestion will be appreciated and thank you in advance


Solution

  • This is result of lacking support for ES6 in Rhino engine.

    In browser (or node) you using different javascript engine that have implemented Map object documented here

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

    But you not using this as Map should be used - I mean set and get methods. Instead you using map like Array.

    So to fix your problem you can simply replace

    var map = new Map();
    

    by

    var map = [];
    

    Sources:

    PDI uses the Rhino engine, from Mozilla. Rhino is an open source implementation of the core JavaScript language; it doesn’t contain objects or methods related to manipulation of web pages.

    https://intellipaat.com/blog/tutorial/pentaho-tutorial/transforming-your-data-with-javascript-code-and-the-javascript-step/

    Support of Rhino for Map and ES6

    https://mozilla.github.io/rhino/compat/engines.html

    enter image description here