Skip to content

Commit

Permalink
Merge pull request #296 from chughts/tone
Browse files Browse the repository at this point in the history
Tone & Discovery
  • Loading branch information
chughts authored May 21, 2017
2 parents 128985e + 9de4250 commit ea499a8
Show file tree
Hide file tree
Showing 9 changed files with 515 additions and 174 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ Node-RED Watson Nodes for IBM Bluemix

<a href="https://cla-assistant.io/watson-developer-cloud/node-red-node-watson"><img src="https://cla-assistant.io/readme/badge/watson-developer-cloud/node-red-node-watson" alt="CLA assistant" /></a>

### New in version 0.5.7
- Fix to Tone Analyzer to allow JSON as input
- Enabled Conversation Tone method to Tone Analyzer Node
- Discovery Node now supports: Create Environment, Create Configuration,
Create Collection

### New in version 0.5.6
- User Agent added to following nodes: Conversation, Conversation Workspace Manager,
Discovery, Discovery Query Builder, Document Conversion, Language Translator,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-node-watson",
"version": "0.5.6",
"version": "0.5.7",
"description": "A collection of Node-RED nodes for IBM Watson services",
"dependencies": {
"alchemy-api": "^1.3.0",
Expand All @@ -11,7 +11,7 @@
"temp": "^0.8.3",
"qs": "6.x",
"image-type": "^2.0.2",
"watson-developer-cloud": "^2.31.1",
"watson-developer-cloud": "^2.31.2",
"kuromoji": "^0.0.5",
"is-docx": "^0.0.3"
},
Expand Down
89 changes: 76 additions & 13 deletions services/discovery/discovery-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,24 @@ DiscoveryUtils.prototype = {
} else if (msg.discoveryparams && msg.discoveryparams.configurationname) {
params.name = msg.discoveryparams.configurationname;
} else if (config.configurationname) {
params.name = config.cofigurationname;
params.name = config.configurationname;
} else if (msg.discoveryparams && msg.discoveryparams.collection_name) {
params.name = msg.discoveryparams.collection_name;
} else if (config.collection_name) {
params.name = config.collection_name;
}
return params;
},

buildParamsForPayload: function(msg, config, params) {
var isJSON = this.isJsonString(msg.payload) ||
this.isJsonObject(msg.payload);

// Payload (text to be analysed) must be a string (content is either raw string or Buffer)
if (typeof msg.payload === 'string' || isJSON) {
params.file = this.isJsonObject(msg.payload) ?
JSON.stringify(msg.payload) :
msg.payload;
}
return params;
},
Expand All @@ -47,18 +64,23 @@ DiscoveryUtils.prototype = {
},

buildParams: function(msg, config) {
var params = {};
var params = {},
me = this;

params = DiscoveryUtils.prototype.buildParamsForName(msg, config, params);
params = this.buildParamsForName(msg, config, params);

['environment_id','collection_id','configuration_id','query'].forEach(function(f) {
params = DiscoveryUtils.prototype.buildParamsFor(msg, config, params, f);
['environment_id','collection_id','configuration_id',
'collection_name',
'query','description','size'].forEach(function(f) {
params = me.buildParamsFor(msg, config, params, f);
});

['count','filter','aggregation','return'].forEach(function(f) {
params = DiscoveryUtils.prototype.buildParamsFromConfig(config, params, f);
params = me.buildParamsFromConfig(config, params, f);
});

params = this.buildParamsForPayload(msg, config, params);

return params;
},

Expand Down Expand Up @@ -97,6 +119,30 @@ DiscoveryUtils.prototype = {
return response;
},

paramJSONCheck: function (params) {
var response = '';
if (!params.file) {
response = 'Missing JSON file on payload';
}
return response;
},

paramNameCheck: function (params) {
var response = '';
if (!params.name) {
response = 'Missing Name ';
}
return response;
},

paramDescriptionCheck: function (params) {
var response = '';
if (!params.description) {
response = 'Missing Description ';
}
return response;
},

paramCollectionCheck: function (params) {
var response = '';
if (!params.collection_id) {
Expand All @@ -123,7 +169,7 @@ DiscoveryUtils.prototype = {
}

if ('object' === typeof d[k]) {
fields = DiscoveryUtils.prototype.buildFieldByStep(d[k], fields, t);
fields = this.buildFieldByStep(d[k], fields, t);
} else {
switch (k) {
case 'text':
Expand All @@ -150,22 +196,39 @@ DiscoveryUtils.prototype = {
if ('results' === k &&
'object' === typeof schemaData[k] &&
'object' === typeof schemaData[k][0]) {
fields = DiscoveryUtils.prototype.buildFieldByStep(schemaData[k][0], fields, '');
fields = this.buildFieldByStep(schemaData[k][0], fields, '');
}
}
if (fields.length) {
fields = fields.filter(DiscoveryUtils.prototype.uniqueFilter);
fields = fields.filter(this.uniqueFilter);
}
}
return fields;
},

reportError: function (node, msg, message) {
var messageTxt = message.error ? message.error : message;
node.status({fill:'red', shape:'dot', text: messageTxt});
node.error(message, msg);
// reportError: function (node, msg, message) {
// var messageTxt = message.error ? message.error : message;
// node.status({fill:'red', shape:'dot', text: messageTxt});
// node.error(message, msg);
// } ,

isJsonString: function(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
},

isJsonObject: function(str) {
if (str instanceof Array || str instanceof Object) {
return true;
}
return false;
}


};

var discoveryutils = new DiscoveryUtils();
Expand Down
57 changes: 54 additions & 3 deletions services/discovery/v1.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@
<div class="form-row">
<label for="node-input-discovery-method"><i class="fa fa-book"></i> Method: </label>
<select type="text" id="node-input-discovery-method" style="display: inline-block; width: 70%;">
<option value="listEnvrionments">List existing envionments</option>
<option value="createEnvrionment">Create new environment</option>
<option value="listEnvrionments">List existing environments</option>
<option value="getEnvironmentDetails">Retrieve environment details</option>
<option value="createCollection">Create new collection</option>
<option value="listCollections">List existing collections</option>
<option value="getCollectionDetails">Retrieve collection details</option>
<option value="createConfiguration">Create new configuration</option>
<option value="listConfigurations">List existing configurations</option>
<option value="getConfigurationDetails">Retrieve configuration details</option>
<option value="query">Search in collection</option>
Expand All @@ -54,6 +57,10 @@
<label for="node-input-environment_id"><i class="fa fa-tag"></i> Environment ID</label>
<input type="text" id="node-input-environment_id" placeholder="">
</div>
<div class="form-row">
<label for="node-input-collection_name"><i class="fa fa-tag"></i> Collection Name</label>
<input type="text" id="node-input-collection_name" placeholder="">
</div>
<div class="form-row">
<label for="node-input-collection_id"><i class="fa fa-tag"></i> Collection ID</label>
<input type="text" id="node-input-collection_id" placeholder="">
Expand Down Expand Up @@ -86,16 +93,42 @@
<label for="node-input-return"><i class="fa fa-tag"></i> List of sections to return</label>
<input type="text" id="node-input-return" placeholder="">
</div>
<div class="form-row">
<label for="node-input-description"><i class="fa fa-tag"></i> Description</label>
<input type="text" id="node-input-description" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-size">Envrionment Size</label>
<input type="number" min="0" max="4"
id="node-input-size" />
</div>

</script>

<script type="text/x-red" data-help-name="watson-discovery-v1">
<p>This is the Node for the V1 Watson Discovery service.</p>
<p>The following methods are available:</p>
<ul>
<li>
<p><b>Create New Environment</b><p>
<p>For this method the node needs an Environment Name,
Description and Environment Size as input.
</p>
<p>The name and description can be overridden by specifying
in <code>msg.discoveryparams.environmentname</code>
, <code>msg.discoveryparams.description</code>
and <code>msg.discoveryparams.size</code>
</p>
<p>Node output : </p>
<ul>
<li><code>msg.environment</code> : Information about the
newly created envrionment.</li>
</ul>
</li>
<br/>
<li>
<p><b>List Existing Envrionments</b><p>
<p>For this methong the node does not need any input. If the envrionment name field is left blank
<p><b>List Existing Environments</b><p>
<p>For this method the node does not need any input. If the envrionment name field is left blank
it will list all envrionments created for the instance of the Discovery service.
If the envrionment name is specified, then only the details for
the Discovery envrionment with the specified name will be returned.
Expand Down Expand Up @@ -229,6 +262,7 @@
disV1.hideAll = function() {
$('#node-input-environmentname').parent().hide();
$('#node-input-environment_id').parent().hide();
$('#node-input-collection_name').parent().hide();
$('#node-input-collection_id').parent().hide();
$('#node-input-configurationname').parent().hide();
$('#node-input-configuration_id').parent().hide();
Expand All @@ -237,6 +271,8 @@
$('#node-input-filter').parent().hide();
$('#node-input-aggregation').parent().hide();
$('#node-input-return').parent().hide();
$('#node-input-description').parent().hide();
$('#node-input-size').parent().hide();
};

disV1.showSelectedFields = function(fields) {
Expand All @@ -249,9 +285,21 @@
disV1.hideAll();
fields = [];
switch (method) {
case 'createEnvrionment':
fields.push('#node-input-environmentname'
+ ', #node-input-size'
+ ', #node-input-description');
break;
case 'listEnvrionments':
fields.push('#node-input-environmentname');
break;
case 'createCollection':
fields.push('#node-input-environment_id'
+ ', #node-input-configuration_id'
+ ', #node-input-collection_name'
+ ', #node-input-description');
break;
case 'createConfiguration':
case 'getEnvironmentDetails':
case 'listCollections':
fields.push('#node-input-environment_id');
Expand Down Expand Up @@ -317,11 +365,14 @@
collection_id: {value: ""},
configurationname: {value: ""},
configuration_id: {value: ""},
collection_name: {value: ""},
count: {value: "1"},
query: {value: ""},
filter: {value: ""},
aggregation: {value: ""},
return: {value: ""},
description: {value: ""},
size: {value: 0},
"discovery-method": {value:"listEnvrionments"}
},
credentials: {
Expand Down
Loading

0 comments on commit ea499a8

Please sign in to comment.