Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed to support json with new release of watson sdk #6

Merged
merged 8 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/bower_components/
/node_modules/
/npm-debug.log
.idea
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ The nodes both contain quing systems, and will auto retry if discovery responds
```javascript
msg.datatype = "JSON"
msg.payload = {
sampledata:"some data goes here"
};
content: {
sampledata:"some data goes here"
},
filename: "sample_json.json"
};
return msg;
```

Expand All @@ -26,9 +29,9 @@ return msg;
```javascript
msg.datatype = "BIN"
msg.payload = {
content = msg.payload.content //This should be a Buffer from an fs.readFileSync(),
fileName = "updated_doc_v2.pdf"
}
content: msg.payload.content, //This should be a Buffer from an fs.readFileSync(),
filename: "updated_doc_v2.pdf"
};
msg.document_id = "6022b729-f180-4772-88a7-f73333594ead"

return msg;
Expand All @@ -39,9 +42,11 @@ return msg;
```javascript
msg.datatype = "JSON"
msg.payload = {
sampledata:"some data goes here"
};
content: {
sampledata:"some data goes here"
}
};
msg.environment_id = "env_id";
msg.collection_id = "collection_id"
return msg;
```
```
96 changes: 96 additions & 0 deletions discovery-insert/delete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<script type="text/javascript">
RED.nodes.registerType('discovery-delete', {
category: 'function',
color: '#a6bbcf',
defaults: {
name: {
value: ""
},
endpoint: {
value: ""
},
environment: {
value: ""
},
collection: {
value: ""
},
delay: {
value: 1000,
validate: RED.validators.number()
},
max_Q_size: {
value: 10000,
validate: RED.validators.number()
},
},
credentials: {
username: {
type: "text"
},
password: {
type: "password"
},
apikey: {
type: "password"
}
},
inputs: 1,
outputs: 1,
icon: "file.png",
label: function () {
return this.name || "discovery-delete";
}
});
</script>

<script type="text/x-red" data-template-name="discovery-delete">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-username"><i class="icon-tag"></i> Username</label>
<input type="text" id="node-input-username">
</div>
<div class="form-row">
<label for="node-input-password"><i class="icon-tag"></i> Password</label>
<input type="password" id="node-input-password">
</div>
<div class="form-row">
<label for="node-input-apikey"><i class="icon-tag"></i> APIKEY</label>
<input type="password" id="node-input-apikey">
</div>
<div class="form-row">
<label for="node-input-endpoint"><i class="icon-tag"></i> Service Endpoint</label>
<input type="text" id="node-input-endpoint">
</div>
<div class="form-row">
<label for="node-input-environment"><i class="icon-tag"></i> Environment</label>
<input type="text" id="node-input-environment" placeholder="environment">
</div>
<div class="form-row">
<label for="node-input-collection"><i class="icon-tag"></i> Collection</label>
<input type="text" id="node-input-collection" placeholder="collection">
</div>
<div class="form-row">
<label for="node-input-delay"><i class="icon-tag"></i> Delay</label>
<input type="text" id="node-input-delay" placeholder="1000">
</div>
<div class="form-row">
<label for="node-input-max_Q_size"><i class="icon-tag"></i> Max Q</label>
<input type="text" id="node-input-max_Q_size" placeholder="10000">
</div>
</script>

<script type="text/x-red" data-help-name="discovery-delete">
<p>A simple node that deletes a document from watson discovery service.</p>
<code>msg.environment_id</code> will override environment_id
<code>msg.collection_id</code> will override collection_id
<code>msg.document_id</code> is <b>Required</b>

For use with Binary files. e.g pdf or docx set

<code>msg.payload.content</code> to a JSON or a Buffer containing the file contents
<code>msg.payload.filename</code> to the name of the file.
</script>
142 changes: 142 additions & 0 deletions discovery-insert/delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
var crypto = require('crypto');
var stream = require('stream');
var { IamAuthenticator, BasicAuthenticator } = require('ibm-watson/auth');
var DiscoveryV1 = require('ibm-watson/discovery/v1');

module.exports = function (RED) {
function DiscoveryDelete(config) {
RED.nodes.createNode(this, config);
var node = this;

var options = {
version: '2019-04-30',
headers: {
'X-Watson-Learning-Opt-Out': 'true'
}
};

if (config.endpoint) {
options.url = config.endpoint;
}

if (this.credentials.apikey) {
options.authenticator = new IamAuthenticator({apikey: this.credentials.apikey});
} else {
options.authenticator = new BasicAuthenticator({username: this.credentials.username, password: this.credentials.password});
}

var discovery = new DiscoveryV1(options);
var environment = config.environment;
var collection = config.collection;

var delete_queue = [];

//allow for override of delay
var delay = (config.delay !== 0) ? parseInt(config.delay) : 1000;
var max_Q_size = (config.max_Q_size !== 0) ? parseInt(config.max_Q_size) : 10000;

//update Q size once per second.
var status_update_period = 750;

//start Q
setInterval(processQueue, delay);
setInterval(updateStatus, status_update_period);


//START OFF FUNCTIONS
function deleteDocumentDiscovery(msg) {
return new Promise(function (resolve, reject) {

var env = (msg.hasOwnProperty('environment_id')) ? msg.environment_id : environment;
var col = (msg.hasOwnProperty('collection_id')) ? msg.collection_id : collection;
var doc = (msg.hasOwnProperty('document_id')) ? msg.document_id : null;

var document_obj = {
environmentId: env,
collectionId: col,
documentId: doc,
};

discovery.deleteDocument(document_obj).then((response) => {
resolve(response);
}).catch((err) => {
if (err.code === 429) {
resolve(429);
} else {
reject(err);
}
});
});
}

function processQueue() {
if (delete_queue.length !== 0) {
var msg = delete_queue.pop();
deleteDiscovery(msg);
}
}

function updateStatus() {
var size = delete_queue.length;
if (size !== 0) {
node.status({
fill: "red",
shape: "dot",
text: "Queue Size: " + size
});
} else {
node.status({
fill: "green",
shape: "dot",
text: "Queue empty"
});
}
}


function deleteDiscovery(msg) {
deleteDocumentDiscovery(msg).then(function (response) {
if (response !== 429) {
msg.payload = response;
msg.q_size = delete_queue.length;
node.send(msg);
} else if (response === 429) {
addToQueue(msg);
}
}).catch(function (err) {
if (("" + err).includes("ECONNREFUSED")) {
addToQueue(msg);
} else {
node.error("" + err);
}
});
}

function addToQueue(msg) {
if (delete_queue.length < max_Q_size) {
delete_queue.push(msg);
} else {
node.error("Queue Full, dropping Message");
}
}

node.on('input', function (msg) {
deleteDiscovery(msg);
});
}


RED.nodes.registerType("discovery-delete", DiscoveryDelete, {
credentials: {
username: {
type: "text"
},
password: {
type: "password"
},
apikey: {
type: "password"
}
}
});
};
38 changes: 24 additions & 14 deletions discovery-insert/insert.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
name: {
value: ""
},
endpoint: {
value: ""
},
environment: {
value: ""
},
Expand All @@ -27,6 +30,9 @@
},
password: {
type: "password"
},
apikey: {
type: "password"
}
},
inputs: 1,
Expand All @@ -39,14 +45,6 @@
</script>

<script type="text/x-red" data-template-name="discovery-insert">
<div class="form-row">
<label for="node-input-environment"><i class="icon-tag"></i> Environment</label>
<input type="text" id="node-input-environment" placeholder="environment">
</div>
<div class="form-row">
<label for="node-input-collection"><i class="icon-tag"></i> Collection</label>
<input type="text" id="node-input-collection" placeholder="collection">
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
Expand All @@ -59,6 +57,22 @@
<label for="node-input-password"><i class="icon-tag"></i> Password</label>
<input type="password" id="node-input-password">
</div>
<div class="form-row">
<label for="node-input-apikey"><i class="icon-tag"></i> APIKEY</label>
<input type="password" id="node-input-apikey">
</div>
<div class="form-row">
<label for="node-input-endpoint"><i class="icon-tag"></i> Service Endpoint</label>
<input type="text" id="node-input-endpoint">
</div>
<div class="form-row">
<label for="node-input-environment"><i class="icon-tag"></i> Environment</label>
<input type="text" id="node-input-environment" placeholder="environment">
</div>
<div class="form-row">
<label for="node-input-collection"><i class="icon-tag"></i> Collection</label>
<input type="text" id="node-input-collection" placeholder="collection">
</div>
<div class="form-row">
<label for="node-input-delay"><i class="icon-tag"></i> Delay</label>
<input type="text" id="node-input-delay" placeholder="1000">
Expand All @@ -74,12 +88,8 @@
<code>msg.environment_id</code> will override environment_id
<code>msg.collection_id</code> will override collection_id

<p>To switch between JSON & Files set </p>
<code>msg.datatype</code> to <code>"JSON"</code> or <code>"BIN"</code>

For use with Binnary files. e.g pdf. or dox set
For use with Binary files. e.g pdf or docx set

<code>msg.payload.content</code> to a Buffer containg the file contents
<code>msg.payload.content</code> to a JSON or a Buffer containing the file contents
<code>msg.payload.filename</code> to the name of the file.

</script>
Loading