Skip to content

Commit

Permalink
Add entity functions for fuzzy match entity extraction. Closes #10.
Browse files Browse the repository at this point in the history
  • Loading branch information
JonHoughton committed Aug 25, 2016
1 parent ad7e375 commit 5356230
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.16",
"description": "hubot for IBM Cloud Container interactions",
"main": "index.js",
"respository": {
"repository": {
"url": "https://github.com/ibm-cloud-solutions/hubot-ibmcloud-container-management",
"type": "git"
},
Expand Down
70 changes: 70 additions & 0 deletions src/lib/container.entities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Licensed Materials - Property of IBM
* (C) Copyright IBM Corp. 2016. All Rights Reserved.
* US Government Users Restricted Rights - Use, duplication or
* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*/
'use strict';

const cf = require('hubot-cf-convenience');
const ic = require('./ic');
const nlcconfig = require('hubot-ibmcloud-cognitive-lib').nlcconfig;

const NAMESPACE = 'IBMcloudContainerManagment';
const PARAM_CONTAINERNAME = 'containername';
const PARAM_CONTAINERGROUPNAME = 'containergroupname';

var functionsRegistered = false;


function buildGlobalName(parameterName) {
return NAMESPACE + '_' + parameterName;
}
function buildGlobalFuncName(parameterName) {
return NAMESPACE + '_func' + parameterName;
}

function registerEntityFunctions() {
if (!functionsRegistered) {
nlcconfig.setGlobalEntityFunction(buildGlobalFuncName(PARAM_CONTAINERNAME), getContainerNames);
nlcconfig.setGlobalEntityFunction(buildGlobalFuncName(PARAM_CONTAINERGROUPNAME), getContainerGroupNames);
functionsRegistered = true;
}
}

function getContainerNames(robot, res, parameterName, parameters) {
return new Promise(function(resolve, reject) {
const spaceGuid = cf.activeSpace(robot, res).guid;
ic.containers.getContainers(spaceGuid).then((result) => {
let resultJson = JSON.parse(result);
var containerNames = resultJson.map(function(container){
return container.Name;
});
nlcconfig.updateGlobalParameterValues(buildGlobalName(PARAM_CONTAINERNAME), containerNames);
resolve(containerNames);
}).catch(function(err) {
reject(err);
});
});
}

function getContainerGroupNames(robot, res, parameterName, parameters) {
return new Promise(function(resolve, reject) {
const spaceGuid = cf.activeSpace(robot, res).guid;
const containerGroups = new ic.ContainerGroups();
containerGroups.getContainerGroups(spaceGuid).then((result) => {
let resultJson = JSON.parse(result);
var containerGroupNames = resultJson.map(function(containergroup){
return containergroup.Name;
});
nlcconfig.updateGlobalParameterValues(buildGlobalName(PARAM_CONTAINERGROUPNAME), containerGroupNames);
resolve(containerGroupNames);
}).catch(function(err) {
reject(err);
});
});
}

module.exports.registerEntityFunctions = registerEntityFunctions;
module.exports.getContainerNames = getContainerNames;
module.exports.getContainerGroupNames = getContainerGroupNames;
28 changes: 21 additions & 7 deletions src/nlc/NLC.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
],
"parameters": [{
"name": "containername",
"title": "container name",
"type": "entity",
"prompt": "OK. What is the name of the container for which you want the logs?"
"prompt": "OK. What is the name of the container for which you want the logs?",
"entityfunction": "funccontainername"
}]
}, {
"class": "bluemix.container.remove",
Expand All @@ -49,8 +51,10 @@
],
"parameters": [{
"name": "containername",
"title": "container name",
"type": "entity",
"prompt": "OK. What is the name of the container you want to remove?"
"prompt": "OK. What is the name of the container you want to remove?",
"entityfunction": "funccontainername"
}]
}, {
"class": "bluemix.container.start",
Expand All @@ -65,8 +69,10 @@
],
"parameters": [{
"name": "containername",
"title": "container name",
"type": "entity",
"prompt": "OK. What is the name of the container you want to start?"
"prompt": "OK. What is the name of the container you want to start?",
"entityfunction": "funccontainername"
}]
}, {
"class": "bluemix.container.status",
Expand All @@ -79,8 +85,10 @@
],
"parameters": [{
"name": "containername",
"title": "container name",
"type": "entity",
"prompt": "OK. What is the name of the container for which you want the status?"
"prompt": "OK. What is the name of the container for which you want the status?",
"entityfunction": "funccontainername"
}]
}, {
"class": "bluemix.container.stop",
Expand All @@ -96,8 +104,10 @@
],
"parameters": [{
"name": "containername",
"title": "container name",
"type": "entity",
"prompt": "OK. What is the name of the container you want to stop?"
"prompt": "OK. What is the name of the container you want to stop?",
"entityfunction": "funccontainername"
}]
}, {
"class": "bluemix.containergroup.help",
Expand Down Expand Up @@ -131,8 +141,10 @@
],
"parameters": [{
"name": "containergroupname",
"title": "container group name",
"type": "entity",
"prompt": "OK. What is the name of the containergroup you want to remove?"
"prompt": "OK. What is the name of the containergroup you want to remove?",
"entityfunction": "funccontainergroupname"
}]
}, {
"class": "bluemix.containergroup.scale",
Expand All @@ -149,8 +161,10 @@
],
"parameters": [{
"name": "containergroupname",
"title": "container group name",
"type": "entity",
"prompt": "OK. What is the name of the containergroup you want to scale?"
"prompt": "OK. What is the name of the containergroup you want to scale?",
"entityfunction": "funccontainergroupname"
}, {
"name": "instances",
"type": "number",
Expand Down
4 changes: 4 additions & 0 deletions src/scripts/container.logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const cf = require('hubot-cf-convenience');
const activity = require('hubot-ibmcloud-activity-emitter');
const ic = require('../lib/ic');
const palette = require('hubot-ibmcloud-utils').palette;
const entities = require('../lib/container.entities');
// --------------------------------------------------------------
// i18n (internationalization)
// It will read from a peer messages.json file. Later, these
Expand All @@ -48,6 +49,9 @@ const LOGS_ID = 'bluemix.container.logs';

module.exports = (robot) => {

// Register entity handling functions
entities.registerEntityFunctions();

// Natural Language match
robot.on(LOGS_ID, (res, parameters) => {
robot.logger.debug(`${TAG}: ${LOGS_ID} Natural Language match. res.message.text=${res.message.text}.`);
Expand Down
4 changes: 4 additions & 0 deletions src/scripts/container.remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const cf = require('hubot-cf-convenience');
const ic = require('../lib/ic');
const utils = require('hubot-ibmcloud-utils').utils;
const activity = require('hubot-ibmcloud-activity-emitter');
const entities = require('../lib/container.entities');
// --------------------------------------------------------------
// i18n (internationalization)
// It will read from a peer messages.json file. Later, these
Expand Down Expand Up @@ -81,6 +82,9 @@ function removeDialog(switchBoard, res, robot, name, containerGuid, spaceGuid, s
// Slack entry point.
module.exports = (robot) => {

// Register entity handling functions
entities.registerEntityFunctions();

var switchBoard = new Conversation(robot);

// Natural Language match
Expand Down
4 changes: 4 additions & 0 deletions src/scripts/container.start.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var TAG = path.basename(__filename);
const cf = require('hubot-cf-convenience');
const ic = require('../lib/ic');
const activity = require('hubot-ibmcloud-activity-emitter');
const entities = require('../lib/container.entities');
// --------------------------------------------------------------
// i18n (internationalization)
// It will read from a peer messages.json file. Later, these
Expand All @@ -47,6 +48,9 @@ const START_ID = 'bluemix.container.start';

module.exports = (robot) => {

// Register entity handling functions
entities.registerEntityFunctions();

// Natural Language match
robot.on(START_ID, (res, parameters) => {
robot.logger.debug(`${TAG}: ${START_ID} Natural Language match. res.message.text=${res.message.text}.`);
Expand Down
4 changes: 4 additions & 0 deletions src/scripts/container.status.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const cf = require('hubot-cf-convenience');
const ic = require('../lib/ic');
const palette = require('hubot-ibmcloud-utils').palette;
const activity = require('hubot-ibmcloud-activity-emitter');
const entities = require('../lib/container.entities');
// --------------------------------------------------------------
// i18n (internationalization)
// It will read from a peer messages.json file. Later, these
Expand All @@ -49,6 +50,9 @@ const STATUS_ID = 'bluemix.container.status';
// Slack entry point.
module.exports = (robot) => {

// Register entity handling functions
entities.registerEntityFunctions();

// Natural Language match
robot.on(STATUS_ID, (res, parameters) => {
robot.logger.debug(`${TAG}: ${STATUS_ID} Natural Language match. res.message.text=${res.message.text}.`);
Expand Down
4 changes: 4 additions & 0 deletions src/scripts/container.stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const cf = require('hubot-cf-convenience');
const ic = require('../lib/ic');
const activity = require('hubot-ibmcloud-activity-emitter');
const utils = require('hubot-ibmcloud-utils').utils;
const entities = require('../lib/container.entities');
// --------------------------------------------------------------
// i18n (internationalization)
// It will read from a peer messages.json file. Later, these
Expand All @@ -49,6 +50,9 @@ const STOP_ID = 'bluemix.container.stop';

module.exports = (robot) => {

// Register entity handling functions
entities.registerEntityFunctions();

var switchBoard = new Conversation(robot);

// Natural Language match
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/containergroup.list.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ module.exports = (robot) => {
});

// update names for NLC
nlcconfig.updateGlobalParameterValues('IBMcloudContainerManagment_containername', containergroupNames);
nlcconfig.updateGlobalParameterValues('IBMcloudContainerManagment_containergroupname', containergroupNames);

// Emit as an attachment
robot.emit('ibmcloud.formatter', {
Expand Down
4 changes: 4 additions & 0 deletions src/scripts/containergroup.remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const ic = require('../lib/ic');
const utils = require('hubot-ibmcloud-utils').utils;
const containerGroups = new ic.ContainerGroups();
const activity = require('hubot-ibmcloud-activity-emitter');
const entities = require('../lib/container.entities');
// --------------------------------------------------------------
// i18n (internationalization)
// It will read from a peer messages.json file. Later, these
Expand Down Expand Up @@ -81,6 +82,9 @@ function removeDialog(switchBoard, res, robot, name, spaceGuid, spaceName) {
// Slack entry point.
module.exports = (robot) => {

// Register entity handling functions
entities.registerEntityFunctions();

var switchBoard = new Conversation(robot);

// Natural Language match
Expand Down
4 changes: 4 additions & 0 deletions src/scripts/containergroup.scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var TAG = path.basename(__filename);
const cf = require('hubot-cf-convenience');
const activity = require('hubot-ibmcloud-activity-emitter');
const ic = require('../lib/ic');
const entities = require('../lib/container.entities');
// --------------------------------------------------------------
// i18n (internationalization)
// It will read from a peer messages.json file. Later, these
Expand Down Expand Up @@ -55,6 +56,9 @@ const SCALE_ID = 'bluemix.containergroup.scale';
// Slack entry point.
module.exports = (robot) => {

// Register entity handling functions
entities.registerEntityFunctions();

// Natural Language match
robot.on(SCALE_ID, (res, parameters) => {
robot.logger.debug(`${TAG}: ${SCALE_ID} Natural Language match. res.message.text=${res.message.text}.`);
Expand Down
25 changes: 25 additions & 0 deletions test/bluemix.containers.cognitive.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,29 @@ describe('Interacting with Bluemix containers via natural language', function()
});
});

context('verify entity functions', function() {

it('should retrieve set of container names', function(done) {
const entities = require('../src/lib/container.entities');
var res = { message: {text: '', user: {id: 'mimiron'}}, response: room };
entities.getContainerNames(room.robot, res, 'containername', {}).then(function(containerNames) {
expect(containerNames.length).to.eql(3);
done();
}).catch(function(error) {
done(error);
});
});

it('should retrieve set of container group names', function(done) {
const entities = require('../src/lib/container.entities');
var res = { message: {text: '', user: {id: 'mimiron'}}, response: room };
entities.getContainerGroupNames(room.robot, res, 'containergroupname', {}).then(function(containerGroupNames) {
expect(containerGroupNames.length).to.eql(1);
done();
}).catch(function(error) {
done(error);
});
});
});

});

0 comments on commit 5356230

Please sign in to comment.