Skip to content

Commit ea499a8

Browse files
authored
Merge pull request #296 from chughts/tone
Tone & Discovery
2 parents 128985e + 9de4250 commit ea499a8

File tree

9 files changed

+515
-174
lines changed

9 files changed

+515
-174
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ Node-RED Watson Nodes for IBM Bluemix
77

88
<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>
99

10+
### New in version 0.5.7
11+
- Fix to Tone Analyzer to allow JSON as input
12+
- Enabled Conversation Tone method to Tone Analyzer Node
13+
- Discovery Node now supports: Create Environment, Create Configuration,
14+
Create Collection
15+
1016
### New in version 0.5.6
1117
- User Agent added to following nodes: Conversation, Conversation Workspace Manager,
1218
Discovery, Discovery Query Builder, Document Conversion, Language Translator,

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-node-watson",
3-
"version": "0.5.6",
3+
"version": "0.5.7",
44
"description": "A collection of Node-RED nodes for IBM Watson services",
55
"dependencies": {
66
"alchemy-api": "^1.3.0",
@@ -11,7 +11,7 @@
1111
"temp": "^0.8.3",
1212
"qs": "6.x",
1313
"image-type": "^2.0.2",
14-
"watson-developer-cloud": "^2.31.1",
14+
"watson-developer-cloud": "^2.31.2",
1515
"kuromoji": "^0.0.5",
1616
"is-docx": "^0.0.3"
1717
},

services/discovery/discovery-utils.js

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,24 @@ DiscoveryUtils.prototype = {
2525
} else if (msg.discoveryparams && msg.discoveryparams.configurationname) {
2626
params.name = msg.discoveryparams.configurationname;
2727
} else if (config.configurationname) {
28-
params.name = config.cofigurationname;
28+
params.name = config.configurationname;
29+
} else if (msg.discoveryparams && msg.discoveryparams.collection_name) {
30+
params.name = msg.discoveryparams.collection_name;
31+
} else if (config.collection_name) {
32+
params.name = config.collection_name;
33+
}
34+
return params;
35+
},
36+
37+
buildParamsForPayload: function(msg, config, params) {
38+
var isJSON = this.isJsonString(msg.payload) ||
39+
this.isJsonObject(msg.payload);
40+
41+
// Payload (text to be analysed) must be a string (content is either raw string or Buffer)
42+
if (typeof msg.payload === 'string' || isJSON) {
43+
params.file = this.isJsonObject(msg.payload) ?
44+
JSON.stringify(msg.payload) :
45+
msg.payload;
2946
}
3047
return params;
3148
},
@@ -47,18 +64,23 @@ DiscoveryUtils.prototype = {
4764
},
4865

4966
buildParams: function(msg, config) {
50-
var params = {};
67+
var params = {},
68+
me = this;
5169

52-
params = DiscoveryUtils.prototype.buildParamsForName(msg, config, params);
70+
params = this.buildParamsForName(msg, config, params);
5371

54-
['environment_id','collection_id','configuration_id','query'].forEach(function(f) {
55-
params = DiscoveryUtils.prototype.buildParamsFor(msg, config, params, f);
72+
['environment_id','collection_id','configuration_id',
73+
'collection_name',
74+
'query','description','size'].forEach(function(f) {
75+
params = me.buildParamsFor(msg, config, params, f);
5676
});
5777

5878
['count','filter','aggregation','return'].forEach(function(f) {
59-
params = DiscoveryUtils.prototype.buildParamsFromConfig(config, params, f);
79+
params = me.buildParamsFromConfig(config, params, f);
6080
});
6181

82+
params = this.buildParamsForPayload(msg, config, params);
83+
6284
return params;
6385
},
6486

@@ -97,6 +119,30 @@ DiscoveryUtils.prototype = {
97119
return response;
98120
},
99121

122+
paramJSONCheck: function (params) {
123+
var response = '';
124+
if (!params.file) {
125+
response = 'Missing JSON file on payload';
126+
}
127+
return response;
128+
},
129+
130+
paramNameCheck: function (params) {
131+
var response = '';
132+
if (!params.name) {
133+
response = 'Missing Name ';
134+
}
135+
return response;
136+
},
137+
138+
paramDescriptionCheck: function (params) {
139+
var response = '';
140+
if (!params.description) {
141+
response = 'Missing Description ';
142+
}
143+
return response;
144+
},
145+
100146
paramCollectionCheck: function (params) {
101147
var response = '';
102148
if (!params.collection_id) {
@@ -123,7 +169,7 @@ DiscoveryUtils.prototype = {
123169
}
124170

125171
if ('object' === typeof d[k]) {
126-
fields = DiscoveryUtils.prototype.buildFieldByStep(d[k], fields, t);
172+
fields = this.buildFieldByStep(d[k], fields, t);
127173
} else {
128174
switch (k) {
129175
case 'text':
@@ -150,22 +196,39 @@ DiscoveryUtils.prototype = {
150196
if ('results' === k &&
151197
'object' === typeof schemaData[k] &&
152198
'object' === typeof schemaData[k][0]) {
153-
fields = DiscoveryUtils.prototype.buildFieldByStep(schemaData[k][0], fields, '');
199+
fields = this.buildFieldByStep(schemaData[k][0], fields, '');
154200
}
155201
}
156202
if (fields.length) {
157-
fields = fields.filter(DiscoveryUtils.prototype.uniqueFilter);
203+
fields = fields.filter(this.uniqueFilter);
158204
}
159205
}
160206
return fields;
161207
},
162208

163-
reportError: function (node, msg, message) {
164-
var messageTxt = message.error ? message.error : message;
165-
node.status({fill:'red', shape:'dot', text: messageTxt});
166-
node.error(message, msg);
209+
// reportError: function (node, msg, message) {
210+
// var messageTxt = message.error ? message.error : message;
211+
// node.status({fill:'red', shape:'dot', text: messageTxt});
212+
// node.error(message, msg);
213+
// } ,
214+
215+
isJsonString: function(str) {
216+
try {
217+
JSON.parse(str);
218+
} catch (e) {
219+
return false;
220+
}
221+
return true;
222+
},
223+
224+
isJsonObject: function(str) {
225+
if (str instanceof Array || str instanceof Object) {
226+
return true;
227+
}
228+
return false;
167229
}
168230

231+
169232
};
170233

171234
var discoveryutils = new DiscoveryUtils();

services/discovery/v1.html

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@
3636
<div class="form-row">
3737
<label for="node-input-discovery-method"><i class="fa fa-book"></i> Method: </label>
3838
<select type="text" id="node-input-discovery-method" style="display: inline-block; width: 70%;">
39-
<option value="listEnvrionments">List existing envionments</option>
39+
<option value="createEnvrionment">Create new environment</option>
40+
<option value="listEnvrionments">List existing environments</option>
4041
<option value="getEnvironmentDetails">Retrieve environment details</option>
42+
<option value="createCollection">Create new collection</option>
4143
<option value="listCollections">List existing collections</option>
4244
<option value="getCollectionDetails">Retrieve collection details</option>
45+
<option value="createConfiguration">Create new configuration</option>
4346
<option value="listConfigurations">List existing configurations</option>
4447
<option value="getConfigurationDetails">Retrieve configuration details</option>
4548
<option value="query">Search in collection</option>
@@ -54,6 +57,10 @@
5457
<label for="node-input-environment_id"><i class="fa fa-tag"></i> Environment ID</label>
5558
<input type="text" id="node-input-environment_id" placeholder="">
5659
</div>
60+
<div class="form-row">
61+
<label for="node-input-collection_name"><i class="fa fa-tag"></i> Collection Name</label>
62+
<input type="text" id="node-input-collection_name" placeholder="">
63+
</div>
5764
<div class="form-row">
5865
<label for="node-input-collection_id"><i class="fa fa-tag"></i> Collection ID</label>
5966
<input type="text" id="node-input-collection_id" placeholder="">
@@ -86,16 +93,42 @@
8693
<label for="node-input-return"><i class="fa fa-tag"></i> List of sections to return</label>
8794
<input type="text" id="node-input-return" placeholder="">
8895
</div>
96+
<div class="form-row">
97+
<label for="node-input-description"><i class="fa fa-tag"></i> Description</label>
98+
<input type="text" id="node-input-description" placeholder="Name">
99+
</div>
100+
<div class="form-row">
101+
<label for="node-input-size">Envrionment Size</label>
102+
<input type="number" min="0" max="4"
103+
id="node-input-size" />
104+
</div>
89105

90106
</script>
91107

92108
<script type="text/x-red" data-help-name="watson-discovery-v1">
93109
<p>This is the Node for the V1 Watson Discovery service.</p>
94110
<p>The following methods are available:</p>
95111
<ul>
112+
<li>
113+
<p><b>Create New Environment</b><p>
114+
<p>For this method the node needs an Environment Name,
115+
Description and Environment Size as input.
116+
</p>
117+
<p>The name and description can be overridden by specifying
118+
in <code>msg.discoveryparams.environmentname</code>
119+
, <code>msg.discoveryparams.description</code>
120+
and <code>msg.discoveryparams.size</code>
121+
</p>
122+
<p>Node output : </p>
123+
<ul>
124+
<li><code>msg.environment</code> : Information about the
125+
newly created envrionment.</li>
126+
</ul>
127+
</li>
128+
<br/>
96129
<li>
97-
<p><b>List Existing Envrionments</b><p>
98-
<p>For this methong the node does not need any input. If the envrionment name field is left blank
130+
<p><b>List Existing Environments</b><p>
131+
<p>For this method the node does not need any input. If the envrionment name field is left blank
99132
it will list all envrionments created for the instance of the Discovery service.
100133
If the envrionment name is specified, then only the details for
101134
the Discovery envrionment with the specified name will be returned.
@@ -229,6 +262,7 @@
229262
disV1.hideAll = function() {
230263
$('#node-input-environmentname').parent().hide();
231264
$('#node-input-environment_id').parent().hide();
265+
$('#node-input-collection_name').parent().hide();
232266
$('#node-input-collection_id').parent().hide();
233267
$('#node-input-configurationname').parent().hide();
234268
$('#node-input-configuration_id').parent().hide();
@@ -237,6 +271,8 @@
237271
$('#node-input-filter').parent().hide();
238272
$('#node-input-aggregation').parent().hide();
239273
$('#node-input-return').parent().hide();
274+
$('#node-input-description').parent().hide();
275+
$('#node-input-size').parent().hide();
240276
};
241277

242278
disV1.showSelectedFields = function(fields) {
@@ -249,9 +285,21 @@
249285
disV1.hideAll();
250286
fields = [];
251287
switch (method) {
288+
case 'createEnvrionment':
289+
fields.push('#node-input-environmentname'
290+
+ ', #node-input-size'
291+
+ ', #node-input-description');
292+
break;
252293
case 'listEnvrionments':
253294
fields.push('#node-input-environmentname');
254295
break;
296+
case 'createCollection':
297+
fields.push('#node-input-environment_id'
298+
+ ', #node-input-configuration_id'
299+
+ ', #node-input-collection_name'
300+
+ ', #node-input-description');
301+
break;
302+
case 'createConfiguration':
255303
case 'getEnvironmentDetails':
256304
case 'listCollections':
257305
fields.push('#node-input-environment_id');
@@ -317,11 +365,14 @@
317365
collection_id: {value: ""},
318366
configurationname: {value: ""},
319367
configuration_id: {value: ""},
368+
collection_name: {value: ""},
320369
count: {value: "1"},
321370
query: {value: ""},
322371
filter: {value: ""},
323372
aggregation: {value: ""},
324373
return: {value: ""},
374+
description: {value: ""},
375+
size: {value: 0},
325376
"discovery-method": {value:"listEnvrionments"}
326377
},
327378
credentials: {

0 commit comments

Comments
 (0)