Skip to content

Commit ca24d6d

Browse files
committed
recorder files, commands and functions
1 parent 7782921 commit ca24d6d

11 files changed

+189
-233
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:8.10
1+
FROM node:12.10
22

33
ARG NPM_TOKEN={$NPM_TOKEN}
44

README.md

+2-10
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,11 @@ In order to fully take advantage of the potential, you should Tag your resources
2929

3030
## Instructions
3131

32-
This function could be set to be used as standalone, but the way we use it is by setting a [AWS Cloudwatch](https://aws.amazon.com/cloudwatch/), and a [AWS Lambda](https://aws.amazon.com/lambda/) function
33-
34-
Since this project uses `node_modules`, we have 2 ways of passing the code to [AWS Lambda](https://aws.amazon.com/lambda/)
35-
36-
- Uploading to [AWS S3](https://aws.amazon.com/s3/)
37-
- Creating a ZIP file and uploading to Lambda (We usually use this, but could be changed)
32+
// TO FOLLOW
3833

3934
## Installation
4035

41-
- Create a ZIP file
42-
- Upload to [AWS Lambda](https://aws.amazon.com/lambda/)
43-
- Set the [AWS Cloudwatch](https://aws.amazon.com/cloudwatch/) to call the function on the desired period of time
44-
- You are all set
36+
// TO FOLLOW
4537

4638
## Neccesary envars
4739

common.js

-65
This file was deleted.

daily_report.package.json

-18
This file was deleted.

deploy.sh

-35
This file was deleted.

monthly_report.package.json

-18
This file was deleted.

package-lock.json

+23-21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
"description": "",
55
"scripts": {
66
"test": "echo \"Error: no test specified\" && exit 1",
7-
"standard": "node_modules/.bin/standard \"**.js\" --parser babel-eslint"
7+
"standard": "node_modules/.bin/standard \"**.js\" --parser babel-eslint",
8+
"standard:fix": "node_modules/.bin/standard \"**.js\" --parser babel-eslint --fix",
9+
"report:daily": "node src/daily_report.js",
10+
"report:monthly": "node src/daily_report.js"
811
},
912
"author": "",
1013
"license": "ISC",
1114
"dependencies": {
1215
"async": "3.1.0",
1316
"aws-sdk": "2.518.0",
14-
"csv-parse": "4.4.5",
17+
"csv-parse": "^4.11.1",
1518
"request": "2.87.0",
1619
"unzipper": "0.10.0"
1720
},

src/common.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
const request = require('request')
2+
const SLACK_URL = process.env.SLACK_URL
3+
const AWS = require('aws-sdk')
4+
const s3 = new AWS.S3()
5+
const unzipper = require('unzipper')
6+
const fs = require('fs')
7+
8+
9+
/**
10+
* Send a notification to Slack
11+
* @param {{
12+
* message: string
13+
* }}
14+
*/
15+
const slackNotify = (message) => {
16+
console.log('Sending to Slack...')
17+
18+
// curate string to avoid errors
19+
message = message.replace('&', '')
20+
21+
let options = {
22+
url: SLACK_URL,
23+
method: 'POST',
24+
form: message
25+
}
26+
27+
// Start the request
28+
request(options, function (error, response, body) {
29+
if (error) return callback(error)
30+
31+
if (response.statusCode !== 200) {
32+
let payload = 'statusCode: ' + response.statusCode + ' statusMessage: ' + response.statusMessage + ' body: ' + body
33+
callback(payload)
34+
}
35+
36+
return('Message sent to Slack...' + body)
37+
})
38+
}
39+
40+
/**
41+
* Download and Extract the cost report
42+
* @param {{
43+
* BUCKET: string
44+
* KEY: string
45+
* FILE: string
46+
* parser: string
47+
* }}
48+
*/
49+
const downloadAndExctract = (BUCKET, KEY, FILE, parser) => {
50+
// Download File, Unzip, Extract to /tmp and Read it
51+
s3.getObject({ Bucket: BUCKET, Key: KEY }).createReadStream()
52+
.on('error', function (err) {
53+
console.log('ERROR: ', err)
54+
})
55+
.on('end', function () {
56+
fs.createReadStream('/tmp/' + FILE).pipe(parser)
57+
})
58+
.pipe(unzipper.Extract({ path: '/tmp' }))
59+
}
60+
61+
/**
62+
* Get Column Positions
63+
* We use this function to know in which column is the data that we need
64+
* For example: In which column number is the Product Name...
65+
* @param {{
66+
* BUCKET: string
67+
* KEY: string
68+
* FILE: string
69+
* parser: string
70+
* }}
71+
*/
72+
const getColumnPositions = (line) => {
73+
let columnHeaders = {}
74+
75+
columnHeaders.productNameColumnNumber = line.indexOf('ProductName')
76+
columnHeaders.instanceNameColumnNumber = line.indexOf('user:Name')
77+
columnHeaders.blendedCostColumnNumber = line.indexOf('BlendedCost')
78+
columnHeaders.usageStartDateColumnNumber = line.indexOf('UsageStartDate')
79+
80+
return columnHeaders
81+
}
82+
83+
module.exports = {
84+
slackNotify,
85+
downloadAndExctract,
86+
getColumnPositions
87+
}

0 commit comments

Comments
 (0)