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

View usage #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
69 changes: 68 additions & 1 deletion src/lib/ic.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Containers {
*/
constructor() {
this.endpoint = (env.endpoint || '').replace('/api', '/containers-api');
this.usageEndpoint = (env.endpoint || '').replace('/api', '/alchemyopsui');
}

/**
Expand Down Expand Up @@ -196,8 +197,74 @@ class Containers {
});
return promise;
}
}

/**
* CPU Usage of a container
*
* @param {String} containerGuid [Container guid]
* @param {string} spaceGuid [The space where the containers exist]
* @return {JSON} [information about the container]
*/
cpuUsage(containerGuid, spaceGuid) {
// GET
let duration = '.cpu-*.cpu-idle,-100)))&format=json&from=-60min&to=-0min/';
let alcEndPoint = this.usageEndpoint + '/graphiteData/target=absolute(sumSeries(offset(' + spaceGuid + '.0000.' + containerGuid + duration + spaceGuid + '/metric-cpu';
let options = {
method: 'GET',
url: alcEndPoint,
headers: {
Authorization: cf.token.access_token
}
};
// console.log('CPU URL GET ' + alcEndPoint);
let promise = new Promise((resolve, reject) => {
request(options, function(error, response, body) {
if (error) {
reject(body);
}
else {
resolve(body);
}
});
});
return promise;
}


/**
* Memory Usage of a container
*
* @param {String} containerGuid [Container guid]
* @param {string} spaceGuid [The space where the containers exist]
* @return {JSON} [information about the container]
*/
memoryUsage(containerGuid, spaceGuid) {
// GET
let duration = '.memory.memory-used,0.000001)))&format=json&from=-60min&to=-0min/';
let alcEndPoint = this.usageEndpoint + '/graphiteData/target=absolute(sumSeries(scale(' + spaceGuid + '.0000.' + containerGuid + duration + spaceGuid + '/metric-memory';
let options = {
method: 'GET',
url: alcEndPoint,
headers: {
Authorization: cf.token.access_token
}
};
// console.log('MEM URL GET ' + alcEndPoint);
let promise = new Promise((resolve, reject) => {
// console.log('mem' + Date());
request(options, function(error, response, body) {
// console.log('mem2' + Date());
if (error) {
reject(body);
}
else {
resolve(body);
}
});
});
return promise;
}
}

/**
* This public class manages the operations related with ContainerGroups in Bluemix
Expand Down
110 changes: 70 additions & 40 deletions src/scripts/container.list.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
// jamesjong
//
/*
* 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.
*/
* 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 path = require('path');
Expand Down Expand Up @@ -67,56 +67,86 @@ module.exports = (robot) => {
let resultJson = null;
const spaceGuid = cf.activeSpace(robot, res).guid;
const spaceName = cf.activeSpace(robot, res).name;

// Get containers info.
robot.logger.info(`${TAG}: Asynch call using containers library to get containers for space guid ${spaceGuid}.`);
ic.containers.getContainers(spaceGuid).then((result) => {
let containerNames = [];

resultJson = JSON.parse(result);
// Set the container cache.
ic.setCache(spaceGuid, resultJson);
// Iterate the containers and create a suitable response.
const attachments = resultJson.map((container) => {
let arrayPro = [];
resultJson.map((container) => {
containerNames.push(container.Name);

const ports = container.Ports.reduce((list, port) => {
if (list) {
list += '\n';
}
list += 'Private: ' + port.PrivatePort + ' Public: ' + port.PublicPort;
return list;
}, '');

const attachment = {
title: container.Name,
color: palette[container.Status.toLowerCase()] || palette.normal
};
attachment.fields = [
{title: 'status', value: container.Status.toLowerCase(), short: true},
{title: 'memory', value: container.Memory, short: true},
{title: 'Network', value: container.NetworkSettings.IPAddress, short: true},
{title: 'ports', value: ports},
{title: 'group', value: (container.Group && container.Group.Name) ? container.Group.Name : 'Not applicable'}
];
return attachment;
arrayPro.push(containerAttach(container, spaceGuid));
});

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

// Emit as an attachment
robot.emit('ibmcloud.formatter', {
response: res,
attachments
Promise.all(arrayPro).then((attachments) => {
// Emit as an attachment
robot.emit('ibmcloud.formatter', {
response: res,
attachments
});
activity.emitBotActivity(robot, res, { activity_id: 'activity.container.list', space_name: spaceName, space_guid: spaceGuid});
});
activity.emitBotActivity(robot, res, { activity_id: 'activity.container.list', space_name: spaceName, space_guid: spaceGuid});
nlcconfig.updateGlobalParameterValues('IBMcloudContainerManagment_containername', containerNames);
}).catch((reason) => {
robot.logger.error(`${TAG}: reason=${reason}`);
if (reason && reason.dumpstack) {
robot.logger.error(reason.dumpstack);
}
robot.emit('ibmcloud.formatter', { response: res, message: i18n.__('containers.not.found')});
});
};
}
function containerAttach(container, spaceGuid){
const ports = container.Ports.reduce((list, port) => {
if (list) {
list += '\n';
}
list += 'Private: ' + port.PrivatePort + ' Public: ' + port.PublicPort;
return list;
}, '');
let cache = ic.getCache(spaceGuid);
const attachment = {
title: container.Name,
color: palette[container.Status.toLowerCase()] || palette.normal
};
attachment.fields = [
{title: 'status', value: container.Status.toLowerCase(), short: true},
{title: 'memory', value: container.Memory + 'M', short: true},
{title: 'Network', value: container.NetworkSettings.IPAddress, short: true},
{title: 'ports', value: ports},
{title: 'group', value: (container.Group && container.Group.Name) ? container.Group.Name : 'Not applicable'}
];
let p = new Promise((resolve, reject) => {
ic.containers.memoryUsage(cache[container.Name], spaceGuid).then((result) => {
let resultJson = JSON.parse(result);
let data = resultJson[0].datapoints;
let sum = 0;
for (let i = 0; i < data.length; i++) {
if (parseFloat(data[i], 10)) {
sum = sum + parseFloat(data[i], 10);
i++;
}
}
let memUsage = Number(sum / 60).toFixed(2);
attachment.fields.push({title: 'memory usage', value: memUsage + 'M', short: true});
return attachment;
}).then((attachment) => {
ic.containers.cpuUsage(cache[container.Name], spaceGuid).then((result) => {
let resultJson = JSON.parse(result);
let data = resultJson[0].datapoints;
let sum = 0;
for (let i = 0; i < data.length; i++) {
if (parseFloat(data[i], 10)) {
sum = sum + parseFloat(data[i], 10);
i++;
}
}
let cpuUsage = Number(sum / 60).toFixed(2);
attachment.fields.push({title: 'cpu usage(%)', value: cpuUsage, short: true});
resolve(attachment);
});
});
});
return p;
}
};
127 changes: 83 additions & 44 deletions src/scripts/container.status.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
// jamesjong
//
/*
* 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.
*/
* 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 path = require('path');
Expand Down Expand Up @@ -86,59 +86,98 @@ module.exports = (robot) => {
ic.setCache(spaceGuid, resultJson);

// Iterate the containers and return info for match on name.
const attachments = resultJson.map((container) => {
let arrayPro = [];
resultJson.map((container) => {
if (container.Name.toLowerCase() === name.toLowerCase()){
let message = i18n.__('container.status.in.progress', name, spaceName);
robot.emit('ibmcloud.formatter', { response: res, message: message});
const ports = container.Ports.reduce((list, port) => {
if (list) {
list += '\n';
}
list += 'IP: ' + port.IP + ' , Port private: ' + port.PrivatePort + ' public: ' + port.PublicPort;
return list;
}, '');
arrayPro.push(getContainerInfo(container, spaceGuid));
}
});

const attachment = {
title: container.Name,
color: palette[container.Status.toLowerCase()] || palette.normal
};
attachment.fields = [
{title: 'status', value: container.Status.toLowerCase(), short: true},
{title: 'memory', value: container.Memory, short: true},
{title: 'IP address', value: container.NetworkSettings.IPAddress, short: true},
{title: 'ports', value: ports}
];
return attachment;
Promise.all(arrayPro).then(attachments => {
console.log('return in');
if (attachments.length > 0) {
// Emit as an attachment
robot.emit('ibmcloud.formatter', {
response: res,
attachments
});

activity.emitBotActivity(robot, res, {
activity_id: 'activity.container.status',
space_name: spaceName,
space_guid: spaceGuid
});
}
else {
let message = i18n.__('container.name.not.found', name);
robot.emit('ibmcloud.formatter', { response: res, message: message});
}
}).filter(function(attachment){
if (attachment != null){

if (attachments != null){
return true;
}
return false;
});

if (attachments.length > 0) {
// Emit as an attachment
robot.emit('ibmcloud.formatter', {
response: res,
attachments
});

activity.emitBotActivity(robot, res, {
activity_id: 'activity.container.status',
space_name: spaceName,
space_guid: spaceGuid
});
}
else {
let message = i18n.__('container.name.not.found', name);
robot.emit('ibmcloud.formatter', { response: res, message: message});
}
}).catch((reason) => {
robot.logger.error(`${TAG}: reason=${reason}`);
robot.logger.error(reason.dumpstack);
let message = i18n.__('container.name.not.found', name);
robot.emit('ibmcloud.formatter', { response: res, message: message});
});
};
function getContainerInfo(container, spaceGuid){
const ports = container.Ports.reduce((list, port) => {
if (list) {
list += '\n';
}
list += 'IP: ' + port.IP + ' , Port private: ' + port.PrivatePort + ' public: ' + port.PublicPort;
return list;
}, '');

const attachment = {
title: container.Name,
color: palette[container.Status.toLowerCase()] || palette.normal
};
attachment.fields = [
{title: 'status', value: container.Status.toLowerCase(), short: true},
{title: 'memory', value: container.Memory + 'M', short: true},
{title: 'IP address', value: container.NetworkSettings.IPAddress, short: true},
{title: 'ports', value: ports}
];
let cache = ic.getCache(spaceGuid);
let p = new Promise((resolve, reject) => {
ic.containers.memoryUsage(cache[container.Name], spaceGuid).then((result) => {
let resultJson = JSON.parse(result);
let data = resultJson[0].datapoints;
let sum = 0;
for (let i = 0; i < data.length; i++) {
if (parseFloat(data[i], 10)) {
sum = sum + parseFloat(data[i], 10);
i++;
}
}
let memUsage = Number(sum / 60).toFixed(2);
attachment.fields.push({title: 'memory usage', value: memUsage + 'M', short: true});
return attachment;
}).then((attachment) => {
ic.containers.cpuUsage(cache[container.Name], spaceGuid).then((result) => {
let resultJson = JSON.parse(result);
let data = resultJson[0].datapoints;
let sum = 0;
for (let i = 0; i < data.length; i++) {
if (parseFloat(data[i], 10)) {
sum = sum + parseFloat(data[i], 10);
i++;
}
}
let cpuUsage = Number(sum / 60).toFixed(2);
attachment.fields.push({title: 'cpu usage(%)', value: cpuUsage, short: true});
resolve(attachment);
});
});
});
return p;
};
};
Loading