|
| 1 | +/** |
| 2 | + * Copyright 2016 IBM Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + **/ |
| 16 | +const pkg = require('../../package.json'), |
| 17 | + DiscoveryV1 = require('ibm-watson/discovery/v1'), |
| 18 | + { IamAuthenticator } = require('ibm-watson/auth'); |
| 19 | + |
| 20 | + |
| 21 | +function DiscoveryUtils() {} |
| 22 | +DiscoveryUtils.prototype = { |
| 23 | + |
| 24 | + buildService: function(username, password, apikey, endpoint) { |
| 25 | + let authSettings = {}; |
| 26 | + let serviceSettings = { |
| 27 | + version: '2019-04-30', |
| 28 | + headers: { |
| 29 | + 'User-Agent': pkg.name + '-' + pkg.version |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + if (apikey) { |
| 34 | + authSettings.apikey = apikey; |
| 35 | + } else { |
| 36 | + authSettings.username = username; |
| 37 | + authSettings.password = password; |
| 38 | + } |
| 39 | + serviceSettings.authenticator = new IamAuthenticator(authSettings); |
| 40 | + |
| 41 | + if (endpoint) { |
| 42 | + serviceSettings.url = endpoint; |
| 43 | + } |
| 44 | + |
| 45 | + return new DiscoveryV1(serviceSettings); |
| 46 | + |
| 47 | + }, |
| 48 | + |
| 49 | + buildParamsForName: function(msg, config, params) { |
| 50 | + if (msg.discoveryparams && msg.discoveryparams.environmentname) { |
| 51 | + params.name = msg.discoveryparams.environmentname; |
| 52 | + } else if (config.environmentname) { |
| 53 | + params.name = config.environmentname; |
| 54 | + } else if (msg.discoveryparams && msg.discoveryparams.configurationname) { |
| 55 | + params.name = msg.discoveryparams.configurationname; |
| 56 | + } else if (config.configurationname) { |
| 57 | + params.name = config.configurationname; |
| 58 | + } else if (msg.discoveryparams && msg.discoveryparams.collection_name) { |
| 59 | + params.name = msg.discoveryparams.collection_name; |
| 60 | + } else if (config.collection_name) { |
| 61 | + params.name = config.collection_name; |
| 62 | + } |
| 63 | + return params; |
| 64 | + }, |
| 65 | + |
| 66 | + buildParamsForQuery: function(msg, config, params) { |
| 67 | + var sourceField = 'query', |
| 68 | + targetField = 'query'; |
| 69 | + |
| 70 | + if (config.nlp_query || (msg.discoveryparams && msg.discoveryparams.nlp_query)) { |
| 71 | + targetField = 'naturalLanguageQuery'; |
| 72 | + } |
| 73 | + if (msg.discoveryparams && msg.discoveryparams[sourceField]) { |
| 74 | + params[targetField] = msg.discoveryparams[sourceField]; |
| 75 | + } else if (config[sourceField]) { |
| 76 | + params[targetField] = config[sourceField]; |
| 77 | + } |
| 78 | + return params; |
| 79 | + }, |
| 80 | + |
| 81 | + buildParamsForPayload: function(msg, config, params) { |
| 82 | + var isJSON = this.isJsonString(msg.payload) || |
| 83 | + this.isJsonObject(msg.payload); |
| 84 | + |
| 85 | + // Payload (text to be analysed) must be a string (content is either raw string or Buffer) |
| 86 | + if (typeof msg.payload === 'string' || isJSON) { |
| 87 | + params.file = this.isJsonObject(msg.payload) ? |
| 88 | + JSON.stringify(msg.payload) : |
| 89 | + msg.payload; |
| 90 | + params.fileContentType = 'application/json'; |
| 91 | + } |
| 92 | + return params; |
| 93 | + }, |
| 94 | + |
| 95 | + buildParamsFor: function(msg, config, params, field) { |
| 96 | + if (msg.discoveryparams && msg.discoveryparams[field]) { |
| 97 | + params[field] = msg.discoveryparams[field]; |
| 98 | + } else if (config[field]) { |
| 99 | + params[field] = config[field]; |
| 100 | + } |
| 101 | + return params; |
| 102 | + }, |
| 103 | + |
| 104 | + buildParamsForPassages: function (me, msg, config, params) { |
| 105 | + let passagesFound = false; |
| 106 | + |
| 107 | + // Allow the passages parameters to be passed in three ways |
| 108 | + // 1. As the API was expecting (watson-developer-cloud) |
| 109 | + ['passages.fields', 'passages.count', 'passages.characters' |
| 110 | + ].forEach(function(f) { |
| 111 | + params = me.buildParamsFor(msg, config, params, f); |
| 112 | + passagesFound = true; |
| 113 | + }); |
| 114 | + |
| 115 | + // 2. As anyone misreading the documentation might do it. |
| 116 | + // (again watson-developer-cloud) |
| 117 | + if (msg.discoveryparams && msg.discoveryparams.passages) { |
| 118 | + passagesFound = true; |
| 119 | + ['fields', 'count', 'characters' |
| 120 | + ].forEach(function(f) { |
| 121 | + if (msg.discoveryparams.passages[f]) { |
| 122 | + params['passages.' + f] = msg.discoveryparams.passages[f]; |
| 123 | + } |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + // 3. As the ibm-watson SDK expects |
| 128 | + ['passagesFields', 'passagesCount', 'passagesCharacters' |
| 129 | + ].forEach(function(f) { |
| 130 | + params = me.buildParamsFor(msg, config, params, f); |
| 131 | + passagesFound = true; |
| 132 | + }); |
| 133 | + |
| 134 | + if (passagesFound) { |
| 135 | + // Perform autocorrect for watson-developer-cloud to ibm-watson |
| 136 | + // differences |
| 137 | + ['passages.fields', 'passages.count', 'passages.characters' |
| 138 | + ].forEach(function(f) { |
| 139 | + if (params[f]) { |
| 140 | + let parts = f.split("."); |
| 141 | + let secondPart = parts[1] |
| 142 | + let reformedField = parts[0] + secondPart[0].toUpperCase() + secondPart.slice(1); |
| 143 | + params[reformedField] = params[f]; |
| 144 | + } |
| 145 | + }); |
| 146 | + params.passages = true; |
| 147 | + } |
| 148 | + |
| 149 | + return params; |
| 150 | + }, |
| 151 | + |
| 152 | + buildParamsFromConfig: function(config, params, field) { |
| 153 | + if (config[field]) { |
| 154 | + params[field] = config[field]; |
| 155 | + } |
| 156 | + return params; |
| 157 | + }, |
| 158 | + |
| 159 | + // The field to create a new language collection is language, but |
| 160 | + // the SDK creates a language_code field which it defaults to 'en-us' |
| 161 | + languageCodeFix: function(params) { |
| 162 | + if (params.language_code) { |
| 163 | + params.language = params.language_code; |
| 164 | + } |
| 165 | + return params; |
| 166 | + }, |
| 167 | + |
| 168 | + buildParams: function(msg, config) { |
| 169 | + var params = {}, |
| 170 | + me = this; |
| 171 | + |
| 172 | + params = me.buildParamsForName(msg, config, params); |
| 173 | + params = me.buildParamsForQuery(msg, config, params); |
| 174 | + |
| 175 | + ['environmentId', 'collectionId', 'configurationId', |
| 176 | + 'collection_name', 'language_code', |
| 177 | + 'passages', 'description', 'size', 'filename', |
| 178 | + 'highlight' |
| 179 | + ].forEach(function(f) { |
| 180 | + params = me.buildParamsFor(msg, config, params, f); |
| 181 | + }); |
| 182 | + |
| 183 | + params = me.buildParamsForPassages(me, msg, config, params); |
| 184 | + |
| 185 | + ['count', 'filter', 'aggregation', 'return'].forEach(function(f) { |
| 186 | + params = me.buildParamsFromConfig(config, params, f); |
| 187 | + }); |
| 188 | + |
| 189 | + params = me.languageCodeFix(params); |
| 190 | + |
| 191 | + params = me.buildParamsForPayload(msg, config, params); |
| 192 | + |
| 193 | + return params; |
| 194 | + }, |
| 195 | + |
| 196 | + buildMsgOverrides: function(msg, config) { |
| 197 | + var params = {}; |
| 198 | + if (config.environment) { |
| 199 | + params.environmentId = config.environment; |
| 200 | + } |
| 201 | + if (config.collection) { |
| 202 | + params.collectionId = config.collection; |
| 203 | + } |
| 204 | + if (config.passages) { |
| 205 | + params.passages = config.passages; |
| 206 | + } |
| 207 | + if (config.collection) { |
| 208 | + params.filename = config.filename; |
| 209 | + } |
| 210 | + |
| 211 | + params = this.buildMsgQueryOverrides(msg, config, params); |
| 212 | + |
| 213 | + return params; |
| 214 | + }, |
| 215 | + |
| 216 | + buildMsgQueryOverrides: function(msg, config, params) { |
| 217 | + if (config.nlp_query) { |
| 218 | + params.query = config.querynlp; |
| 219 | + params.nlp_query = config.nlp_query; |
| 220 | + } else { |
| 221 | + params = this.buildStructuredQuery(msg, config, params); |
| 222 | + } |
| 223 | + return params; |
| 224 | + }, |
| 225 | + |
| 226 | + buildStructuredQuery: function(msg, config, params) { |
| 227 | + if (config.query1 && config.queryvalue1) { |
| 228 | + params.query = config.query1 + ':"' + config.queryvalue1 + '"'; |
| 229 | + } |
| 230 | + if (config.query2 && config.queryvalue2) { |
| 231 | + if (params.query) { |
| 232 | + params.query += ','; |
| 233 | + } |
| 234 | + params.query += config.query2 + ':"' + config.queryvalue2 + '"'; |
| 235 | + } |
| 236 | + if (config.query3 && config.queryvalue3) { |
| 237 | + if (params.query) { |
| 238 | + params.query += ','; |
| 239 | + } |
| 240 | + params.query += config.query3 + ':"' + config.queryvalue3 + '"'; |
| 241 | + } |
| 242 | + return params; |
| 243 | + }, |
| 244 | + |
| 245 | + |
| 246 | + paramEnvCheck: function(params) { |
| 247 | + var response = ''; |
| 248 | + if (!params.environmentId) { |
| 249 | + response = 'Missing Environment ID '; |
| 250 | + } |
| 251 | + return response; |
| 252 | + }, |
| 253 | + |
| 254 | + paramJSONCheck: function(params) { |
| 255 | + var response = ''; |
| 256 | + if (!params.file) { |
| 257 | + response = 'Missing JSON file on payload'; |
| 258 | + } |
| 259 | + return response; |
| 260 | + }, |
| 261 | + |
| 262 | + paramDocumentCheck: function(params) { |
| 263 | + var response = ''; |
| 264 | + if (!params.file) { |
| 265 | + response = 'Missing document file on payload'; |
| 266 | + } |
| 267 | + return response; |
| 268 | + }, |
| 269 | + |
| 270 | + paramNameCheck: function(params) { |
| 271 | + var response = ''; |
| 272 | + if (!params.name) { |
| 273 | + response = 'Missing Name '; |
| 274 | + } |
| 275 | + return response; |
| 276 | + }, |
| 277 | + |
| 278 | + paramDescriptionCheck: function(params) { |
| 279 | + var response = ''; |
| 280 | + if (!params.description) { |
| 281 | + response = 'Missing Description '; |
| 282 | + } |
| 283 | + return response; |
| 284 | + }, |
| 285 | + |
| 286 | + paramCollectionCheck: function(params) { |
| 287 | + var response = ''; |
| 288 | + if (!params.collectionId) { |
| 289 | + response = 'Missing Collection ID '; |
| 290 | + } |
| 291 | + return response; |
| 292 | + }, |
| 293 | + |
| 294 | + paramConfigurationCheck: function(params) { |
| 295 | + var response = ''; |
| 296 | + if (!params.configurationId) { |
| 297 | + response = 'Missing Configuration ID '; |
| 298 | + } |
| 299 | + return response; |
| 300 | + }, |
| 301 | + |
| 302 | + // Looking for Text, Type and label |
| 303 | + buildFieldByStep: function(d, fields, txt) { |
| 304 | + for (var k in d) { |
| 305 | + var t = txt; |
| 306 | + if (isNaN(k)) { |
| 307 | + t += txt ? '.' : ''; |
| 308 | + t += k; |
| 309 | + } |
| 310 | + |
| 311 | + if ('object' === typeof d[k]) { |
| 312 | + fields = this.buildFieldByStep(d[k], fields, t); |
| 313 | + } else { |
| 314 | + switch (k) { |
| 315 | + case 'text': |
| 316 | + case 'type': |
| 317 | + case 'label': |
| 318 | + fields.push(t); |
| 319 | + break; |
| 320 | + } |
| 321 | + } |
| 322 | + } |
| 323 | + return fields; |
| 324 | + }, |
| 325 | + |
| 326 | + // sorting functions |
| 327 | + uniqueFilter: function(value, index, self) { |
| 328 | + return self.indexOf(value) === index; |
| 329 | + }, |
| 330 | + |
| 331 | + // Looking for Text, Type and label |
| 332 | + buildFieldList: function(schemaData) { |
| 333 | + var fields = []; |
| 334 | + |
| 335 | + if (schemaData && |
| 336 | + 'object' === typeof schemaData && |
| 337 | + schemaData.result && |
| 338 | + 'object' === typeof schemaData.result && |
| 339 | + schemaData.result['fields'] && |
| 340 | + Array.isArray(schemaData.result['fields'])) { |
| 341 | + schemaData.result['fields'].forEach((f) => { |
| 342 | + if (f['field'] && f['type'] && 'nested' !== f['type']) { |
| 343 | + fields.push(f['field']); |
| 344 | + } |
| 345 | + }); |
| 346 | + } |
| 347 | + |
| 348 | + if (fields.length) { |
| 349 | + fields = fields.filter(this.uniqueFilter); |
| 350 | + fields.sort(); |
| 351 | + } |
| 352 | + |
| 353 | + return fields; |
| 354 | + }, |
| 355 | + |
| 356 | + // reportError: function (node, msg, message) { |
| 357 | + // var messageTxt = message.error ? message.error : message; |
| 358 | + // node.status({fill:'red', shape:'dot', text: messageTxt}); |
| 359 | + // node.error(message, msg); |
| 360 | + // } , |
| 361 | + |
| 362 | + isJsonString: function(str) { |
| 363 | + try { |
| 364 | + JSON.parse(str); |
| 365 | + } catch (e) { |
| 366 | + return false; |
| 367 | + } |
| 368 | + return true; |
| 369 | + }, |
| 370 | + |
| 371 | + isJsonObject: function(str) { |
| 372 | + if (str instanceof Array || str instanceof Object || |
| 373 | + 'object' === typeof str || Array.isArray(str)) { |
| 374 | + return true; |
| 375 | + } |
| 376 | + return false; |
| 377 | + } |
| 378 | + |
| 379 | + |
| 380 | +}; |
| 381 | + |
| 382 | +var discoveryutils = new DiscoveryUtils(); |
| 383 | + |
| 384 | +module.exports = discoveryutils; |
0 commit comments