forked from log-just/dynamic-dynamodb-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.js
147 lines (141 loc) · 4.88 KB
/
tasks.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var env = { };
// initialize
exports.init = function(config) {
env.AWS = require('aws-sdk') // AWS object
env.AWS.config.update({region: config.region}); // update Region info
env.db = new env.AWS.DynamoDB(); // dynamoDB object
env.cloudwatch = new env.AWS.CloudWatch(); // cloudWatch object
env.timeframeMin = config.timeframeMin; // used capa check Period
// startTime,endTime for using cloudwatch API
env.startTime = new Date();
env.endTime = new Date();
env.startTime.setTime(env.endTime-(60000*env.timeframeMin));
env.decreaseDailyLimit = 4;
};
// get Table info
exports.getTask_tableDesc = function(tableName, callback) {
var params = { TableName: tableName };
env.db.describeTable(params, function(err, data) {
if (err) {
callback({
tableName : tableName,
code : 'error',
result : 'describeTable API failed',
detail : err.message
});
}
else {
callback(null,{
readCapa : data.Table.ProvisionedThroughput.ReadCapacityUnits,
writeCapa : data.Table.ProvisionedThroughput.WriteCapacityUnits,
status : data.Table.TableStatus,
remainDecreaseNum : env.decreaseDailyLimit-data.Table.ProvisionedThroughput.NumberOfDecreasesToday
});
}
});
};
// get Table's consumed ReadCapacity (avg)
exports.getTask_consumedReadCapa = function(tableName, callback) {
var params = {
EndTime: env.endTime, // required
MetricName: 'ConsumedReadCapacityUnits', // required
Namespace: 'AWS/DynamoDB', //required
Period: (env.timeframeMin*60), // required
StartTime: env.startTime, // required
Statistics: [ 'Sum' ],
Dimensions: [
{
Name: 'TableName', // required
Value: tableName // required
}],
Unit: 'Count'
};
env.cloudwatch.getMetricStatistics(params, function(err, data) {
if (err) {
callback({
tableName : tableName,
code : 'error',
result : 'getMetricStatistics(get ConsumedReadCapacityUnits) API failed',
detail : err.message
});
}
else {
callback(null,data.Datapoints.length === 0 ? 0 : data.Datapoints[0].Sum/env.timeframeMin/60);
}
});
};
// get Table's consumed WriteCapacity (avg)
exports.getTask_consumedWriteCapa = function(tableName, callback) {
var params = {
EndTime: env.endTime, // required
MetricName: 'ConsumedWriteCapacityUnits', // required
Namespace: 'AWS/DynamoDB', //required
Period: (env.timeframeMin*60), // required
StartTime: env.startTime, // required
Statistics: [ 'Sum' ],
Dimensions: [
{
Name: 'TableName', // required
Value: tableName // required
}],
Unit: 'Count'
};
env.cloudwatch.getMetricStatistics(params, function(err, data) {
if (err) {
callback({
tableName : tableName,
code : 'error',
result : 'getMetricStatistics(get ConsumedWriteCapacityUnits) API failed',
detail : err.message
});
}
else {
callback(null,data.Datapoints.length === 0 ? 0 : data.Datapoints[0].Sum/env.timeframeMin/60);
}
});
};
// calculate Capacity to update
exports.getTask_newCapa = function(capa,used,upperThsd,lowerThsd,increseAmt,decreseAmt,base) {
var rate = (used/capa)*100;
if ( rate > upperThsd )
{
return Math.round(capa+(capa*(increseAmt/100)));
}
else if ( rate < lowerThsd )
{
return Math.max(Math.round(capa-(capa*(decreseAmt/100))),base);
}
else
{
return capa;
}
};
// update Table with now Capacity
exports.setTask_updateTable = function(tableName,readCapa,readUsed,newReadCapa,writeCapa,writeUsed,newWriteCapa,callback) {
var params = {
TableName: tableName, // required
ProvisionedThroughput: {
ReadCapacityUnits: newReadCapa, /* required */
WriteCapacityUnits: newWriteCapa /* required */
}
};
env.db.updateTable(params, function(err, data) {
if (err) {
callback({
tableName : tableName,
code : 'error',
result : 'updateTable API failed',
detail : err.message
});
}
else {
callback({
tableName : tableName,
code : 'update',
result : 'table capacity updated',
detail : 'read - capacity:'+readCapa+', consumed:'+readUsed+' => changedCapa:'+newReadCapa
+' // write - capacity:'+writeCapa+', consumed:'+writeUsed+' => changedCapa:'+newWriteCapa
});
}
});
};