1
- import AWS from 'aws-sdk' ;
1
+ import AWS from 'aws-sdk-promise ' ;
2
2
import Global from './global' ;
3
3
import CloudWatch from './CloudWatch' ;
4
4
const {
@@ -13,36 +13,49 @@ export default class DynamoDB {
13
13
}
14
14
15
15
async listTablesAsync ( ) {
16
+ logger . debug ( 'DynamoDB.listTablesAsync' ) ;
16
17
let sw = stats . timer ( 'DynamoDB.listTablesAsync' ) . start ( )
17
18
try {
18
- return await this . _db . listTables ( ) . promise ( ) ;
19
- }
20
- finally {
19
+ let res = await this . _db . listTables ( ) . promise ( ) ;
20
+ return res . data ;
21
+ } catch ( ex ) {
22
+ logger . warn ( 'DynamoDB.listTablesAsync failed' ) ;
23
+ throw ex ;
24
+ } finally {
21
25
sw . end ( ) ;
22
26
}
23
27
}
24
28
25
29
async describeTableAsync ( params ) {
30
+ logger . debug ( 'DynamoDB.describeTableAsync' ) ;
26
31
let sw = stats . timer ( 'DynamoDB.describeTableAsync' ) . start ( ) ;
27
32
try {
28
- return await this . _db . describeTable ( params ) . promise ( ) ;
29
- }
30
- finally {
33
+ let res = await this . _db . describeTable ( params ) . promise ( ) ;
34
+ return res . data ;
35
+ } catch ( ex ) {
36
+ logger . warn ( 'DynamoDB.describeTableAsync failed' , JSON . stringify ( { params} ) ) ;
37
+ throw ex ;
38
+ } finally {
31
39
sw . end ( ) ;
32
40
}
33
41
}
34
42
35
43
async updateTableAsync ( params ) {
44
+ logger . debug ( 'DynamoDB.updateTableAsync' ) ;
36
45
let sw = stats . timer ( 'DynamoDB.updateTableAsync' ) . start ( ) ;
37
46
try {
38
- return this . _db . updateTable ( params ) . promise ( ) ;
39
- }
40
- finally {
47
+ let res = this . _db . updateTable ( params ) . promise ( ) ;
48
+ return res . data ;
49
+ } catch ( ex ) {
50
+ logger . warn ( 'DynamoDB.updateTableAsync failed' , JSON . stringify ( { params} ) ) ;
51
+ throw ex ;
52
+ } finally {
41
53
sw . end ( ) ;
42
54
}
43
55
}
44
56
45
57
async describeTableConsumedCapacityAsync ( params , periodMinutes ) {
58
+ logger . debug ( 'DynamoDB.describeTableConsumedCapacityAsync' ) ;
46
59
let sw = stats . timer ( 'DynamoDB.describeTableConsumedCapacityAsync' ) . start ( ) ;
47
60
try {
48
61
// Make all the requests concurrently
@@ -79,38 +92,52 @@ export default class DynamoDB {
79
92
GlobalSecondaryIndexes : gsis
80
93
}
81
94
} ;
82
- }
83
- finally {
95
+ } catch ( ex ) {
96
+ logger . warn ( 'DynamoDB.describeTableConsumedCapacityAsync failed' , JSON . stringify ( { params, periodMinutes} ) ) ;
97
+ throw ex ;
98
+ } finally {
84
99
sw . end ( ) ;
85
100
}
86
101
}
87
102
88
103
getTotalTableProvisionedThroughput ( params ) {
89
- let ReadCapacityUnits = params . Table . ProvisionedThroughput . ReadCapacityUnits ;
90
- let WriteCapacityUnits = params . Table . ProvisionedThroughput . WriteCapacityUnits ;
104
+ logger . debug ( 'DynamoDB.getTotalTableProvisionedThroughput' ) ;
105
+ try {
106
+ let ReadCapacityUnits = params . Table . ProvisionedThroughput . ReadCapacityUnits ;
107
+ let WriteCapacityUnits = params . Table . ProvisionedThroughput . WriteCapacityUnits ;
91
108
92
- if ( params . Table . GlobalSecondaryIndexes ) {
93
- ReadCapacityUnits += params . Table . GlobalSecondaryIndexes . reduce ( ( prev , curr , i ) => prev + curr . ProvisionedThroughput . ReadCapacityUnits , 0 ) ;
94
- WriteCapacityUnits += params . Table . GlobalSecondaryIndexes . reduce ( ( prev , curr , i ) => prev + curr . ProvisionedThroughput . WriteCapacityUnits , 0 ) ;
95
- }
109
+ if ( params . Table . GlobalSecondaryIndexes ) {
110
+ ReadCapacityUnits += params . Table . GlobalSecondaryIndexes . reduce ( ( prev , curr , i ) => prev + curr . ProvisionedThroughput . ReadCapacityUnits , 0 ) ;
111
+ WriteCapacityUnits += params . Table . GlobalSecondaryIndexes . reduce ( ( prev , curr , i ) => prev + curr . ProvisionedThroughput . WriteCapacityUnits , 0 ) ;
112
+ }
96
113
97
- return {
98
- ReadCapacityUnits,
99
- WriteCapacityUnits
100
- } ;
114
+ return {
115
+ ReadCapacityUnits,
116
+ WriteCapacityUnits
117
+ } ;
118
+ } catch ( ex ) {
119
+ logger . warn ( 'DynamoDB.getTotalTableProvisionedThroughput failed' , JSON . stringify ( { params} ) ) ;
120
+ throw ex ;
121
+ }
101
122
}
102
123
103
124
getMonthlyEstimatedTableCost ( provisionedThroughput ) {
104
- const averageHoursPerMonth = 720 ;
105
- const readCostPerHour = 0.0065 ;
106
- const readCostUnits = 50 ;
107
- const writeCostPerHour = 0.0065 ;
108
- const writeCostUnits = 10 ;
109
-
110
- let readCost = provisionedThroughput . ReadCapacityUnits / readCostUnits * readCostPerHour * averageHoursPerMonth ;
111
- let writeCost = provisionedThroughput . WriteCapacityUnits / writeCostUnits * writeCostPerHour * averageHoursPerMonth ;
112
-
113
- return readCost + writeCost ;
125
+ logger . debug ( 'DynamoDB.getMonthlyEstimatedTableCost' ) ;
126
+ try {
127
+ const averageHoursPerMonth = 720 ;
128
+ const readCostPerHour = 0.0065 ;
129
+ const readCostUnits = 50 ;
130
+ const writeCostPerHour = 0.0065 ;
131
+ const writeCostUnits = 10 ;
132
+
133
+ let readCost = provisionedThroughput . ReadCapacityUnits / readCostUnits * readCostPerHour * averageHoursPerMonth ;
134
+ let writeCost = provisionedThroughput . WriteCapacityUnits / writeCostUnits * writeCostPerHour * averageHoursPerMonth ;
135
+
136
+ return readCost + writeCost ;
137
+ } catch ( ex ) {
138
+ logger . warn ( 'DynamoDB.getMonthlyEstimatedTableCost failed' , JSON . stringify ( { provisionedThroughput} ) ) ;
139
+ throw ex ;
140
+ }
114
141
}
115
142
116
143
getArrayOrDefault ( value ) {
@@ -122,28 +149,34 @@ export default class DynamoDB {
122
149
}
123
150
124
151
async getConsumedCapacityAsync ( isRead , tableName , globalSecondaryIndexName , periodMinutes ) {
125
- let EndTime = new Date ( ) ;
126
- let StartTime = new Date ( ) ;
127
- StartTime . setTime ( EndTime - ( 60000 * periodMinutes ) ) ;
128
- let MetricName = isRead ? 'ConsumedReadCapacityUnits' : 'ConsumedWriteCapacityUnits' ;
129
- let Dimensions = this . getDimensions ( tableName , globalSecondaryIndexName ) ;
130
- let params = {
131
- Namespace : 'AWS/DynamoDB' ,
132
- MetricName,
133
- Dimensions,
134
- StartTime,
135
- EndTime,
136
- Period : ( periodMinutes * 60 ) ,
137
- Statistics : [ 'Average' ] ,
138
- Unit : 'Count'
139
- } ;
140
-
141
- let data = await this . _cw . getMetricStatisticsAsync ( params ) ;
142
- return {
143
- tableName,
144
- globalSecondaryIndexName,
145
- data
146
- } ;
152
+ logger . debug ( 'DynamoDB.getConsumedCapacityAsync' ) ;
153
+ try {
154
+ let EndTime = new Date ( ) ;
155
+ let StartTime = new Date ( ) ;
156
+ StartTime . setTime ( EndTime - ( 60000 * periodMinutes ) ) ;
157
+ let MetricName = isRead ? 'ConsumedReadCapacityUnits' : 'ConsumedWriteCapacityUnits' ;
158
+ let Dimensions = this . getDimensions ( tableName , globalSecondaryIndexName ) ;
159
+ let params = {
160
+ Namespace : 'AWS/DynamoDB' ,
161
+ MetricName,
162
+ Dimensions,
163
+ StartTime,
164
+ EndTime,
165
+ Period : ( periodMinutes * 60 ) ,
166
+ Statistics : [ 'Average' ] ,
167
+ Unit : 'Count'
168
+ } ;
169
+
170
+ let data = await this . _cw . getMetricStatisticsAsync ( params ) ;
171
+ return {
172
+ tableName,
173
+ globalSecondaryIndexName,
174
+ data
175
+ } ;
176
+ } catch ( ex ) {
177
+ logger . warn ( 'DynamoDB.getConsumedCapacityAsync failed' , JSON . stringify ( { isRead, tableName, globalSecondaryIndexName, periodMinutes} ) ) ;
178
+ throw ex ;
179
+ }
147
180
}
148
181
149
182
getDimensions ( tableName , globalSecondaryIndexName ) {
0 commit comments