Skip to content
This repository was archived by the owner on Dec 1, 2022. It is now read-only.

Commit 357123d

Browse files
committed
Removed winston logging. Changed dotenv file name to support windows OS.
1 parent d8d6ce3 commit 357123d

13 files changed

+309
-121
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ connection with, the use of this code.
7373
4. Select upload a zip file and select 'dist.zip' which you created earlier
7474
5. Set the handler to 'index.handler'
7575
6. Set the Role to 'DynamoDBLambdaAutoscale'
76-
7. Set the Memory to the highest value to give the best performance
77-
8. Set the Timeout to 5 seconds (higher possibly depending on the amount of tables you have)
76+
7. Set the Memory to the lowest value initially but test different values at a later date to see how it affects performance
77+
8. Set the Timeout to approximately 5 seconds (higher or lower depending on the amount of tables you have and the selected memory setting)
7878
9. Once the function is created, attach a 'scheduled event' event source and make it run every minute
7979

8080
## Configuration

gulpfile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ gulp.task('clean', function(cb) {
1717

1818
gulp.task("webpack", function () {
1919
return gulp.src('src/Index.js')
20-
.pipe(webpack( require('./webpack-prod.config.js') ))
20+
.pipe(webpack( require('./webpack-dev.config.js') ))
2121
.pipe(gulp.dest('dist/'));
2222
});
2323

@@ -40,7 +40,7 @@ gulp.task('npm', function() {
4040
gulp.task('env', function() {
4141
return gulp
4242
.src('./config.env.production')
43-
.pipe(rename('.env'))
43+
.pipe(rename('config.env'))
4444
.pipe(gulp.dest('./dist'));
4545
});
4646

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"debug": "node-debug ./scripts/Start.js"
2424
},
2525
"dependencies": {
26-
"winston": "^2.2.0"
2726
},
2827
"devDependencies": {
2928
"aws-sdk-promise": "0.0.2",
@@ -47,6 +46,8 @@
4746
"webpack-stream": "^3.2.0",
4847
"stats-webpack-plugin": "^0.3.1",
4948
"eslint": "^2.9.0",
50-
"eslint-plugin-babel": "^3.2.0"
49+
"eslint-plugin-babel": "^3.2.0",
50+
"invariant": "^2.2.1",
51+
"warning": "^2.1.0"
5152
}
5253
}

scripts/start.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,23 @@ try {
1010
console.log(JSON.stringify(data));
1111
}
1212
} catch (e) {
13+
console.log(e.stack);
1314
console.error(e);
1415
}
1516
},
1617
fail: function(e) {
18+
console.log(e.stack);
1719
console.error(e);
1820
}
1921
};
2022

21-
var event = {};
23+
var event = {
24+
json: { padding: 2 }
25+
};
26+
2227
lambda.handler(event, context);
2328

2429
} catch (e) {
30+
console.log(e.stack);
2531
console.error(e);
2632
}

src/CloudWatch.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
import AWS from 'aws-sdk-promise';
2-
import Global from './Global';
3-
const {
2+
import {
3+
json,
44
stats,
5-
logger
6-
} = Global;
5+
warning,
6+
invariant } from '../src/Global';
77

88
export default class CloudWatch {
99
constructor(cloudWatchOptions) {
10+
invariant(typeof cloudWatchOptions !== 'undefined',
11+
'Parameter \'cloudWatchOptions\' is not set');
1012
this._cw = new AWS.CloudWatch(cloudWatchOptions);
1113
}
1214

1315
async getMetricStatisticsAsync(params) {
14-
logger.debug('CloudWatch.getMetricStatisticsAsync');
1516
let sw = stats.timer('CloudWatch.getMetricStatisticsAsync').start();
1617
try {
18+
invariant(typeof params !== 'undefined',
19+
'Parameter \'params\' is not set');
1720
let res = await this._cw.getMetricStatistics(params).promise();
1821
return res.data;
1922
} catch (ex) {
20-
logger.warn(
21-
'CloudWatch.getMetricStatisticsAsync failed',
22-
JSON.stringify({params}));
23+
warning(JSON.stringify({
24+
class: 'CloudWatch',
25+
function: 'getMetricStatisticsAsync',
26+
params
27+
}, null, json.padding));
2328
throw ex;
2429
} finally {
2530
sw.end();

src/Config.js

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import ConfigurableProvisioner from './ConfigurableProvisioner';
22
import RateLimitedDecrement from './RateLimitedDecrement';
33
import Throughput from './Throughput';
4+
import { invariant } from '../src/Global';
45

56
const provisioner = new ConfigurableProvisioner({
67
readCapacity: {
78
increment: {
8-
isAdjustmentRequired:
9-
(data, calcFunc) => // eslint-disable-line no-unused-vars
10-
Throughput.getReadCapacityUtilisationPercent(data) > 90,
9+
isAdjustmentRequired: (data, calcFunc) => {
10+
invariant(typeof data !== 'undefined',
11+
'Parameter \'data\' is not set');
12+
invariant(typeof calcFunc !== 'undefined',
13+
'Parameter \'calcFunc\' is not set');
14+
15+
return Throughput.getReadCapacityUtilisationPercent(data) > 90;
16+
},
1117
calculateValue: data => {
18+
invariant(typeof data !== 'undefined',
19+
'Parameter \'data\' is not set');
20+
1221
// adjustmentPercent or adjustmentUnits is used, which ever is bigger
1322
const adjustmentPercent = 100;
1423
const adjustmentUnits = 3;
@@ -21,6 +30,11 @@ const provisioner = new ConfigurableProvisioner({
2130
},
2231
decrement: {
2332
isAdjustmentRequired: (data, calcFunc) => {
33+
invariant(typeof data !== 'undefined',
34+
'Parameter \'data\' is not set');
35+
invariant(typeof calcFunc !== 'undefined',
36+
'Parameter \'calcFunc\' is not set');
37+
2438
// minimum possible downward adjustment, there is no point
2539
// wasting 1 of 4 daily decrements on a small value
2640
const minAdjustment = 3;
@@ -32,16 +46,28 @@ const provisioner = new ConfigurableProvisioner({
3246
minGracePeriodAfterLastIncrementMinutes,
3347
minGracePeriodAfterLastDecrementMinutes);
3448
},
35-
calculateValue: data =>
36-
Math.max(data.ConsumedThroughput.ReadCapacityUnits, 1),
49+
calculateValue: data => {
50+
invariant(typeof data !== 'undefined',
51+
'Parameter \'data\' is not set');
52+
53+
return Math.max(data.ConsumedThroughput.ReadCapacityUnits, 1);
54+
},
3755
}
3856
},
3957
writeCapacity: {
4058
increment: {
41-
isAdjustmentRequired:
42-
(data, calcFunc) => // eslint-disable-line no-unused-vars
43-
Throughput.getWriteCapacityUtilisationPercent(data) > 90,
59+
isAdjustmentRequired: (data, calcFunc) => {
60+
invariant(typeof data !== 'undefined',
61+
'Parameter \'data\' is not set');
62+
invariant(typeof calcFunc !== 'undefined',
63+
'Parameter \'calcFunc\' is not set');
64+
65+
return Throughput.getWriteCapacityUtilisationPercent(data) > 90;
66+
},
4467
calculateValue: data => {
68+
invariant(typeof data !== 'undefined',
69+
'Parameter \'data\' is not set');
70+
4571
// adjustmentPercent or adjustmentUnits is used, which ever is bigger
4672
const adjustmentPercent = 100;
4773
const adjustmentUnits = 3;
@@ -54,6 +80,11 @@ const provisioner = new ConfigurableProvisioner({
5480
},
5581
decrement: {
5682
isAdjustmentRequired: (data, calcFunc) => {
83+
invariant(typeof data !== 'undefined',
84+
'Parameter \'data\' is not set');
85+
invariant(typeof calcFunc !== 'undefined',
86+
'Parameter \'calcFunc\' is not set');
87+
5788
// minimum possible downward adjustment, there is no point
5889
// wasting 1 of 4 daily decrements on a small value
5990
const minAdjustment = 3;
@@ -65,8 +96,12 @@ const provisioner = new ConfigurableProvisioner({
6596
minGracePeriodAfterLastIncrementMinutes,
6697
minGracePeriodAfterLastDecrementMinutes);
6798
},
68-
calculateValue: data =>
69-
Math.max(data.ConsumedThroughput.WriteCapacityUnits, 1),
99+
calculateValue: data => {
100+
invariant(typeof data !== 'undefined',
101+
'Parameter \'data\' is not set');
102+
103+
return Math.max(data.ConsumedThroughput.WriteCapacityUnits, 1);
104+
},
70105
}
71106
}
72107
});
@@ -89,6 +124,12 @@ export default {
89124
}
90125
}
91126
},
92-
getTableUpdate: (description, consumedCapacityDescription) =>
93-
provisioner.getTableUpdate(description, consumedCapacityDescription)
127+
getTableUpdate: (description, consumedCapacityDescription) => {
128+
invariant(typeof description !== 'undefined',
129+
'Parameter \'description\' is not set');
130+
invariant(typeof consumedCapacityDescription !== 'undefined',
131+
'Parameter \'consumedCapacityDescription\' is not set');
132+
133+
return provisioner.getTableUpdate(description, consumedCapacityDescription);
134+
}
94135
};

src/ConfigurableProvisioner.js

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
import Global from './Global';
2-
const {
3-
logger
4-
} = Global;
1+
import {
2+
json,
3+
warning,
4+
invariant } from '../src/Global';
55

66
export default class ConfigurableProvisioner {
77

88
constructor(config) {
9+
invariant(typeof config !== 'undefined',
10+
'Parameter \'config\' is not set');
11+
912
this.config = config;
1013
}
1114

1215
getTableUpdate(tableDescription, tableConsumedCapacityDescription) {
1316
try {
14-
logger.debug('ConfigurableProvisioner.getTableUpdate');
17+
invariant(typeof tableDescription !== 'undefined',
18+
'Parameter \'tableDescription\' is not set');
19+
invariant(typeof tableConsumedCapacityDescription !== 'undefined',
20+
'Parameter \'tableConsumedCapacityDescription\' is not set');
1521

1622
let tableData = {
1723
TableName: tableDescription.Table.TableName,
@@ -49,18 +55,25 @@ export default class ConfigurableProvisioner {
4955

5056
return result;
5157
} catch (e) {
52-
logger.warn(
53-
'ConfigurableProvisioner.getTableUpdate failed',
54-
JSON.stringify({tableDescription, tableConsumedCapacityDescription}));
55-
56-
logger.error(e);
58+
warning(JSON.stringify({
59+
class: 'ConfigurableProvisioner',
60+
function: 'getTableUpdate',
61+
tableDescription,
62+
tableConsumedCapacityDescription
63+
}, null, json.padding));
64+
throw e;
5765
}
5866
}
5967

6068
getGlobalSecondaryIndexUpdate(
6169
tableDescription, tableConsumedCapacityDescription, gsi) {
6270
try {
63-
logger.debug('ConfigurableProvisioner.getGlobalSecondaryIndexUpdate');
71+
invariant(typeof tableDescription !== 'undefined',
72+
'Parameter \'tableDescription\' is not set');
73+
invariant(typeof tableConsumedCapacityDescription !== 'undefined',
74+
'Parameter \'tableConsumedCapacityDescription\' is not set');
75+
invariant(typeof gsi !== 'undefined',
76+
'Parameter \'gsi\' is not set');
6477

6578
let gsicc = tableConsumedCapacityDescription
6679
.Table.GlobalSecondaryIndexes.find(i => i.IndexName === gsi.IndexName);
@@ -83,18 +96,21 @@ export default class ConfigurableProvisioner {
8396
}
8497
};
8598
} catch (e) {
86-
logger.warn(
87-
'ConfigurableProvisioner.getGlobalSecondaryIndexUpdate failed',
88-
JSON.stringify(
89-
{tableDescription, tableConsumedCapacityDescription, gsi}));
90-
99+
warning(JSON.stringify({
100+
class: 'ConfigurableProvisioner',
101+
function: 'getGlobalSecondaryIndexUpdate',
102+
tableDescription,
103+
tableConsumedCapacityDescription,
104+
gsi
105+
}, null, json.padding));
91106
throw e;
92107
}
93108
}
94109

95110
getUpdatedProvisionedThroughput(params) {
96111
try {
97-
logger.debug('ConfigurableProvisioner.getUpdatedProvisionedThroughput');
112+
invariant(typeof params !== 'undefined',
113+
'Parameter \'params\' is not set');
98114

99115
let newProvisionedThroughput = {
100116
ReadCapacityUnits: params.ProvisionedThroughput.ReadCapacityUnits,
@@ -138,9 +154,10 @@ export default class ConfigurableProvisioner {
138154

139155
return newProvisionedThroughput;
140156
} catch (e) {
141-
logger.warn(
142-
'ConfigurableProvisioner.getUpdatedProvisionedThroughput failed',
143-
JSON.stringify({params}));
157+
warning(JSON.stringify({
158+
class: 'ConfigurableProvisioner',
159+
function: 'getUpdatedProvisionedThroughput', params
160+
}, null, json.padding));
144161
throw e;
145162
}
146163
}

0 commit comments

Comments
 (0)