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

Improve status display and add --all params ( #334 ) #482

Open
wants to merge 2 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
5 changes: 4 additions & 1 deletion bin/clever.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ function run () {
description: 'Short name for the application',
complete: Application('listAvailableAliases'),
}),
all: cliparse.flag('all', {
description: 'For all apps',
}),
before: cliparse.option('before', {
metavar: 'before',
aliases: ['until'],
Expand Down Expand Up @@ -692,7 +695,7 @@ function run () {
const status = lazyRequirePromiseModule('../src/commands/status.js');
const statusCommand = cliparse.command('status', {
description: 'See the status of an application on Clever Cloud',
options: [opts.alias],
options: [opts.alias, opts.all],
}, status('status'));

// STOP COMMAND
Expand Down
83 changes: 75 additions & 8 deletions src/commands/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ const colors = require('colors/safe');
const AppConfig = require('../models/app_configuration.js');
const Logger = require('../logger.js');

const { get: getApplication, getAllInstances } = require('@clevercloud/client/cjs/api/v2/application.js');
const { get: getApplication, getAllInstances, getAll } = require('@clevercloud/client/cjs/api/v2/application.js');
const { sendToApi } = require('../models/send-to-api.js');
const table = require('text-table');

function displayGroupInfo (instances, commit) {
return `(${displayFlavors(instances)}, Commit: ${commit || 'N/A'})`;
Expand Down Expand Up @@ -64,15 +65,81 @@ function displayScalability (app) {
Dedicated build: ${app.separateBuild ? colors.bold(app.buildFlavor.name) : colors.red('disabled')}`;
}

async function status (params) {
const { alias } = params.options;
const { ownerId, appId } = await AppConfig.getAppDetails({ alias });
function appStatus (allInstances) {
return allInstances.map((instances) => {
const upInstances = _.filter(instances, ({ state }) => state === 'UP');
const isUp = !_.isEmpty(upInstances);
// const upCommit = _(upInstances).map('commit').head();

const deployingInstances = _.filter(instances, ({ state }) => state === 'DEPLOYING');
const isDeploying = !_.isEmpty(deployingInstances);
// const deployingCommit = _(deployingInstances).map('commit').head();
// table doesn't work with multilines
const statusMessage = isUp
? `${colors.bold.green('running')} ${displayFlavors(instances)}`
: isDeploying
? `${colors.bold.yellow('deploying')} ${displayFlavors(deployingInstances)}`
: colors.bold.red('stopped');

return statusMessage;
});

}

function allName (apps) {
return apps.map((app) => colors.cyan(app.name));
}

function allCommitId (apps) {
return apps.map((app) => colors.bold.gray(app.commitId ? app.commitId.substr(0, 12) : 'none'));
}

const instances = await getAllInstances({ id: ownerId, appId }).then(sendToApi);
const app = await getApplication({ id: ownerId, appId }).then(sendToApi);
function allAutoScalability (apps) {
return apps.map((app) => {
const { minFlavor, maxFlavor, minInstances, maxInstances } = app.instance;
return (minFlavor.name !== maxFlavor.name) || (minInstances !== maxInstances) ? colors.bold.green('enabled') : colors.bold.red('disabled');
});
}

Logger.println(computeStatus(instances, app));
Logger.println(displayScalability(app));
function allScalers (apps) {
return apps.map((app) => {
const { minInstances, maxInstances } = app.instance;
return colors.bold.gray(minInstances + (minInstances !== maxInstances ? ' to ' + maxInstances : ''));
});
}

function allSizes (apps) {
return apps.map((app) => {
const { minFlavor, maxFlavor } = app.instance;
return colors.bold.green(minFlavor.name + (minFlavor.name !== maxFlavor.name ? ' to ' + maxFlavor.name : ''));
});
}

function allDedicatedBuild (apps) {
return apps.map((app) => {
return app.separateBuild ? colors.bold(app.buildFlavor.name) : colors.bold.red('disabled');
});
}

async function status (params) {
const { alias, all } = params.options;
const apps = all ? await getAll({}).then(sendToApi) : [await getApplication(await AppConfig.getAppDetails({ alias })).then(sendToApi)];
const allInstances = await Promise.all(apps.map(async (app) => {
return await getAllInstances({ id: app.ownerId, appId: app.id }).then(sendToApi);
}));
const output = table([
['Name'] .concat(...allName(apps)),
['Status'] .concat(...appStatus(allInstances)),
['Commit'] .concat(...allCommitId(apps)),
['Auto scalability'].concat(...allAutoScalability(apps)),
['Scalers'] .concat(...allScalers(apps)),
['Sizes'] .concat(...allSizes(apps)),
['Dedicated build'] .concat(...allDedicatedBuild(apps)),
], {
hsep: ' ',
stringLength: (str) => colors.strip(str).length,
});
Logger.println(output);
}

module.exports = { status };