Skip to content
This repository was archived by the owner on Oct 5, 2023. It is now read-only.

Commit 8bfc336

Browse files
author
Ilya Volodarsky
committed
new api
1 parent 74ce754 commit 8bfc336

File tree

5 files changed

+51
-95
lines changed

5 files changed

+51
-95
lines changed

History.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11

2+
1.0.0 - January 7, 2015
3+
-------------------------
4+
- change the billing API per product
5+
26
0.0.1 - February 28, 2014
37
-------------------------
48
:sparkles:

Readme.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,22 @@ billing(function (err, costs) {
2929
});
3030
```
3131

32-
The `costs` variable estimates your 30 day rolling billing costs for ec2 and non-ec2 costs:
32+
The `costs` variable shows the costs for the current billing period by product.
3333

3434
```js
35-
{
36-
ec2: 13393,
37-
nonEc2: 2493,
38-
total: 15886
39-
}
35+
{ total: 4839.25,
36+
start: Thu Jan 01 2015 00:00:00 GMT+0000 (UTC),
37+
end: Thu Jan 08 2015 02:34:50 GMT+0000 (UTC),
38+
products:
39+
{ 'data transfer': 432.12,
40+
'elastic mapreduce': 864.43,
41+
'cloudfront': 124.42,
42+
'support (business)': 120.12,
43+
'elasticache': 124.12,
44+
'simple storage service': 172.46,
45+
'redshift': 423.77,
46+
'elastic compute cloud': 123.32,
47+
'route 53': 454.73 } }
4048
```
4149

4250
## License

lib/index.js renamed to index.js

Lines changed: 26 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11

2-
var Batch = require('batch');
32
var bind = require('bind');
43
var csv = require('csv');
54
var debug = require('debug')('aws-billing');
65
var Ec2 = require('awssum-amazon-ec2').Ec2;
76
var knox = require('knox');
8-
var once = require('once');
9-
var prices = require('./ec2-prices');
7+
var Dates = require('date-math');
108

119
/**
1210
* Expose `AWSBilling`.
@@ -47,52 +45,28 @@ function AWSBilling (accountId, key, secret, bucket, region) {
4745
*/
4846

4947
AWSBilling.prototype.get = function (callback) {
50-
new Batch(this.getEc2, this.getNonEc2).end(function (err, results) {
48+
this.products(function (err, products) {
5149
if (err) return callback(err);
52-
callback(null, {
53-
ec2: results[0],
54-
nonEc2: results[1],
55-
total: results[0] + results[1]
50+
var total = 0.0;
51+
Object.keys(products).forEach(function (product) {
52+
total += products[product];
5653
});
57-
});
58-
};
59-
60-
/**
61-
* Get the current cost of EC2 instances.
62-
*
63-
* @param {Function} callback
64-
*/
65-
66-
AWSBilling.prototype.getEc2 = function (callback) {
67-
callback = once(callback); // hack: get rid of a horrible AWS sdk bug
68-
var self = this;
69-
debug('describing ec2 instances ..');
70-
this.ec2.DescribeInstances(function(err, res) {
71-
if (err) return callback(err);
72-
var reservations = res.Body.DescribeInstancesResponse.reservationSet.item; // wow, wtf?
73-
var instances = [];
74-
reservations.forEach(function (r) {
75-
var item = r.instancesSet.item;
76-
if (Array.isArray(item)) instances.push.apply(instances, item);
77-
else instances.push(item);
54+
callback(null, {
55+
total: total,
56+
start: Dates.month.floor(new Date()),
57+
end: new Date(),
58+
products: products
7859
});
79-
debug('described ec2 instances');
80-
var cost = instances
81-
.filter(function (i) { return i.instanceState.name === 'running'; })
82-
.map(function (i) { return prices[i.instanceType] * 24 * 30; })
83-
.reduce(function (memo, cost) { return memo + parseFloat(cost); }, 0);
84-
debug('monthly ec2 cost: $%d', cost);
85-
callback(null, cost);
8660
});
8761
};
8862

8963
/**
90-
* Get the cost of non-EC2 AWS stuff.
64+
* Get the cost of AWS products
9165
*
9266
* @param {Function} callback
9367
*/
9468

95-
AWSBilling.prototype.getNonEc2 = function (callback) {
69+
AWSBilling.prototype.products = function (callback) {
9670
var accountId = this.accountId.replace(/-/g, '');
9771
var now = new Date();
9872
var file = accountId + '-aws-billing-csv-' +
@@ -104,19 +78,21 @@ AWSBilling.prototype.getNonEc2 = function (callback) {
10478
csv()
10579
.from.stream(stream)
10680
.to.array(function (data) {
107-
var productCol = data[0].indexOf('ProductCode');
81+
var products = {};
82+
var productCol = data[0].indexOf('ProductCode') + 1;
10883
var costCol = data[0].indexOf('TotalCost');
109-
var cost = data
110-
.filter(function (row) {
111-
return row[productCol] &&
112-
row[productCol] !== 'AmazonEC2' &&
113-
!isNaN(row[costCol]);
114-
})
115-
.reduce(function (memo, row) { return memo + parseFloat(row[costCol]); }, 0);
116-
var monthFraction = new Date().getDate() / 30;
117-
var rolling30DayCost = cost / monthFraction;
118-
debug('rolling 30 days non-ec2 cost: %d', rolling30DayCost);
119-
callback(err, rolling30DayCost);
84+
data.forEach(function (row) {
85+
var product = row[productCol].toLowerCase()
86+
.replace(/amazon /, '')
87+
.replace(/aws /, '');
88+
var cost = parseFloat(row[costCol]);
89+
if (product && cost > 0) {
90+
if (!products[product]) products[product] = 0;
91+
products[product] += cost;
92+
}
93+
});
94+
debug('parsed AWS product costs');
95+
callback(err, products);
12096
});
12197
});
12298
};

lib/ec2-prices.json

Lines changed: 0 additions & 30 deletions
This file was deleted.

package.json

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"name": "aws-billing",
3-
"version": "0.0.2",
4-
"main": "./lib",
3+
"version": "1.0.0",
54
"repository": "git://github.com/segmentio/aws-billing.git",
65
"license": "MIT",
76
"description": "AWS billing API for node",
@@ -13,15 +12,14 @@
1312
"API"
1413
],
1514
"dependencies": {
16-
"bind": "git://github.com/ianstormtaylor/bind",
17-
"debug": "~0.7.4",
18-
"csv": "~0.3.7",
19-
"knox": "~0.8.9",
20-
"batch": "~0.5.0",
21-
"once": "~1.3.0",
2215
"awssum": "~1.2.0",
2316
"awssum-amazon": "~1.3.0",
24-
"awssum-amazon-ec2": "~1.4.0"
17+
"awssum-amazon-ec2": "~1.4.0",
18+
"bind": "git://github.com/ianstormtaylor/bind",
19+
"csv": "~0.3.7",
20+
"date-math": "0.0.1",
21+
"debug": "~0.7.4",
22+
"knox": "~0.8.9"
2523
},
2624
"devDependencies": {
2725
"mocha": "~1.17.1"

0 commit comments

Comments
 (0)