Skip to content
This repository has been archived by the owner on Nov 3, 2020. It is now read-only.

Commit

Permalink
feat(list): add cost estimate and totalhours to droplet
Browse files Browse the repository at this point in the history
  • Loading branch information
Satya Rohith committed Sep 6, 2018
1 parent 8cb96fb commit 6f872d3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
39 changes: 28 additions & 11 deletions actions/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
'use strict';
const { callMatchingMethod, DoAPI, spinner } = require('../util');
const {
callMatchingMethod,
DoAPI,
spinner,
calculateCostAndHours
} = require('../util');
const List = require('../inquirer/list');
const chalk = require('chalk');
const Action = require('./init');
Expand Down Expand Up @@ -48,18 +53,30 @@ module.exports.droplets = async () => {
}`
);
list.body.droplets.map(droplet => {
const calData = calculateCostAndHours(
droplet.created_at,
droplet.size.price_hourly
);

const ipAdrs =
droplet.networks.v4.length > 0
? droplet.networks.v4[0].ip_address
: droplet.networks.v6[0].ip_address;
console.log('----------------------');
console.log(chalk.bold(' Name:'), chalk.blue(droplet.name));
console.log(chalk.bold(' Id:'), droplet.id);
console.log(chalk.bold('Memory:'), droplet.memory);
console.log(chalk.bold(' Image:'), droplet.image.slug);
console.log(chalk.bold('Status:'), droplet.status);
console.log(chalk.bold('Region:'), droplet.region.name);
console.log(chalk.bold(' Name:'), chalk.blue(droplet.name));
console.log(chalk.bold(' Id:'), droplet.id);
console.log(chalk.bold(' Memory:'), droplet.memory);
console.log(chalk.bold(' Image:'), droplet.image.slug);
console.log(chalk.bold(' Status:'), droplet.status);
console.log(chalk.bold(' Region:'), droplet.region.name);
console.log(chalk.bold(' Ip:'), ipAdrs);
console.log(chalk.bold('Total Hours:'), calData.totalHours);
console.log(
chalk.bold(' Ip:'),
droplet.networks.v4.length > 0
? chalk.green(droplet.networks.v4[0].ip_address)
: chalk.green(droplet.networks.v6[0].ip_address)
chalk.bold(' CE:'),
calData.totalCost.toLocaleString('en-US', {
style: 'currency',
currency: 'USD'
})
);
});
}
Expand Down
7 changes: 7 additions & 0 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,10 @@ module.exports.callMatchingMethod = (object, method) => {
console.error(`Couldn't find the method/property ${method} in ${object} `);
}
};

module.exports.calculateCostAndHours = (createdAt, hourlyPrice) => {
const createdDate = new Date(createdAt);
const totalHours = Math.ceil(Math.abs(Date.now() - createdDate) / 36e5);
const totalCost = totalHours * hourlyPrice;
return { totalCost, totalHours };
};

0 comments on commit 6f872d3

Please sign in to comment.