diff --git a/lib/kubevela.js b/lib/kubevela.js index cba738b..9406649 100644 --- a/lib/kubevela.js +++ b/lib/kubevela.js @@ -3,98 +3,112 @@ const mathutils = require("./math"); const _ = require("lodash"); const transliterate = require('transliteration').transliterate; +// Normalize line endings in string fields to prevent AMQP decoding issues +const normalizeLineEndings = (str) => { + if (typeof str === 'string') { + return str.replace(/\r\n/g, '\n').replace(/\r/g, '\n'); + } + return str; +}; + module.exports = { - json: (doc) =>{ - let object = _.clone(doc) - object['title'] = transliterate(object['title']) - object['variables'] = _.map(doc['variables'], (v)=>{ - - return { - 'key': slugify(transliterate(v['name']).replaceAll("\s+"," ").replaceAll('/','_'),'_'), - 'path': '/'+v['name'], - 'type': 'float', - 'meaning': v['name'].split('/').pop(), - 'value' :{ - "lower_bound": v['lowerValue'], - "higher_bound": v['higherValue'], + json: (doc) => { + let object = _.clone(doc) + object['title'] = transliterate(object['title']) + + // Normalize line endings in content field + if (object['content']) { + object['content'] = normalizeLineEndings(object['content']); } - } - }) - - const applicationDeploymentPriceUsed = _.find(doc["utilityFunctions"],(f)=>{ - return _.find(f['functionExpressionVariables'],(v) => { - return v['valueVariable'] === 'application_deployment_price' - }) - }) - - if(applicationDeploymentPriceUsed){ - - object['variables'].push({ - 'key': 'application_deployment_price', - 'path': '', - 'type': 'float', - 'meaning': 'price', - 'value' :{ - "lower_bound": 0, - "higher_bound": 0, + object['variables'] = _.map(doc['variables'], (v) => { + + + return { + 'key': slugify(transliterate(v['name']).replaceAll("\s+", " ").replaceAll('/', '_'), '_'), + 'path': '/' + v['name'], + 'type': 'float', + 'meaning': v['name'].split('/').pop(), + 'value': { + "lower_bound": v['lowerValue'], + "higher_bound": v['higherValue'], + } + } + }) + + const applicationDeploymentPriceUsed = _.find(doc["utilityFunctions"], (f) => { + return _.find(f['functionExpressionVariables'], (v) => { + return v['valueVariable'] === 'application_deployment_price' + }) + }) + + if (applicationDeploymentPriceUsed) { + + object['variables'].push({ + 'key': 'application_deployment_price', + 'path': '', + 'type': 'float', + 'meaning': 'price', + 'value': { + "lower_bound": 0, + "higher_bound": 0, + } + }) } - }) - } - object['sloViolations'] = JSON.parse(doc['sloViolations']) - object['metrics'] = _.map(doc['metrics'], (v)=>{ + object['sloViolations'] = JSON.parse(doc['sloViolations']) + object['metrics'] = _.map(doc['metrics'], (v) => { + + if (v['type'] === 'composite') { + v['arguments'] = mathutils.extractVariableNames( + mathutils.extractFromEquation(v['formula'])) + } + + return v + }) + + object["utilityFunctions"] = _.map(doc['utilityFunctions'], (v) => { + const f = { + "name": v['functionName'], + "type": v['functionType'], + "expression": { + "formula": v["functionExpression"], + "variables": _.map(v["functionExpressionVariables"], (k) => { + return { + "name": k['nameVariable'], + "value": slugify(k['valueVariable'].replaceAll('/', '_'), '_') + } + }) + } + } + + if (v['functionType'] === 'constraint') { + f['operator'] = v['functionConstraintOperator'] + } + + if (v['functionType'] === 'maximize' || v['functionType'] === 'minimize') { + f['selected'] = v['selected']; + } - if(v['type'] === 'composite'){ - v['arguments'] = mathutils.extractVariableNames( - mathutils.extractFromEquation(v['formula'])) - } + return f - return v - }) + }) - object["utilityFunctions"] = _.map(doc['utilityFunctions'], (v)=>{ - const f = { - "name": v['functionName'], - "type": v['functionType'], - "expression":{ - "formula":v["functionExpression"], - "variables": _.map(v["functionExpressionVariables"], (k)=>{ + object["resources"] = _.map(doc['resources'], (v) => { return { - "name":k['nameVariable'], - "value": slugify(k['valueVariable'].replaceAll('/','_'),'_') + "title": v['title'], + "uuid": v['uuid'], + "platform": v['platform'], + "enabled": v['enabled'], + "regions": v['_regions'], + "valid_instance_types": v['_valid_instance_types'] || [], } - }) - } - } - - if(v['functionType'] === 'constraint'){ - f['operator'] = v['functionConstraintOperator'] - } - - if(v['functionType'] === 'maximize' || v['functionType'] === 'minimize'){ - f['selected'] = v['selected']; - } - - return f - - }) - - object["resources"] = _.map(doc['resources'], (v)=>{ - return { - "title": v['title'], - "uuid": v['uuid'], - "platform": v['platform'], - "enabled": v['enabled'], - "regions": v['_regions'], - "valid_instance_types": v['_valid_instance_types'] || [], - } - }) - - var protected_variables = ["_id","type",,"metaType","organization","_edit","_publish"] - _.each(protected_variables, (p)=>{ - delete object[p] - }) - - return object - } + }) + + var protected_variables = ["_id", "type", , "metaType", "organization", "_edit", "_publish"] + _.each(protected_variables, (p) => { + delete object[p] + }) + + return object + } } \ No newline at end of file diff --git a/modules/exn/index.js b/modules/exn/index.js index 3edfedb..e12d50e 100644 --- a/modules/exn/index.js +++ b/modules/exn/index.js @@ -169,6 +169,7 @@ module.exports = { return } else { const connection = container.connect(connection_options); + console.log("Connected to ",connection_options.host, ":", connection_options.port ); }