-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (118 loc) · 3.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var async = require('async');
var AWS = require('aws-sdk');
var csvParse = require('csv-parse');
var util = require('util');
function readConfig(s3, bucket, cb) {
var params = {
Bucket: bucket,
Key: "lambda-billing-config.json"
};
s3.getObject(params, function(err, data) {
var results = JSON.parse(data.Body.toString());
cb(results);
});
}
var s3 = new AWS.S3();
var db = new AWS.DynamoDB();
exports.handler = function(event, context) {
//console.log("Reading options from event:\n", util.inspect(event, {depth: 5}));
var bucket = event.Records[0].s3.bucket.name;
var key = event.Records[0].s3.object.key;
async.waterfall([
function download(next) {
console.log("getting " + bucket + "/" + key);
if (!key.match(/aws-billing-csv/)) {
console.log("skipping file");
context.succeed();
}
s3.getObject({ Bucket: bucket, Key: key }, next);
},
function parse(response, next) {
console.log("parsing file");
csvParse(response.Body, {columns: true}, function(err, data) {
var categoryFees = [];
data.forEach(function(row) {
var category = row.ProductCode;
var fees = row.TotalCost;
if (row.ItemDescription && row.ItemDescription.match(/^Total statement amount for period/)) {
category = "Total";
}
if (!category) {
return;
}
if (!categoryFees[category]) {
categoryFees[category] = 0.0;
}
categoryFees[category] += parseFloat(fees);
});
if (err) {
next(err);
} else {
readConfig(s3, bucket, function(data) {
categoryFees.config = data;
next(null, categoryFees);
});
}
});
},
function writeToDynamo(data, next) {
// console.log("parsed data:", util.inspect(data));
var config = data.config;
var currentFees = data.Total;
var percentOfExpected = currentFees / config.expectedFees * 100;
var alertLevel = Math.floor(percentOfExpected / 25);
var timestamp = new Date().toISOString();
// console.log("Total Fees: " + currentFees);
// console.log("POE: " + percentOfExpected);
// console.log("Alert: " + alertLevel);
// console.log("Timestamp: " + timestamp);
// build map for categorized fees
var categorized = {};
Object.keys(data).forEach(function(key) {
if (key === "config") {
return;
}
categorized[key] = { N: String(data[key]) };
});
// setup item params
var params = {
Item: {
Account: {
S: config.awsAccountId
},
Timestamp: {
S: timestamp
},
TotalFees: {
N: String(currentFees)
},
AlertLevel: {
N: String(alertLevel)
},
ExpectedFees: {
N: String(config.expectedFees)
},
FeesByCategory: {
M: categorized
}
},
TableName: config.tableName,
};
// console.log(util.inspect(params, {depth: 5}));
db.putItem(params, function(err, data) {
if (err) {
next(err);
} else {
next(null, data);
}
});
}
], function(err) {
if (err) {
console.error("Err:", err);
context.fail(err);
} else {
context.succeed();
}
});
};