Skip to content

Commit

Permalink
add more Read and Write info to ConsumedCapacity
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Jan 14, 2020
1 parent ebd6c15 commit e57b28d
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 3 deletions.
56 changes: 53 additions & 3 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,35 @@ type ConsumedCapacity struct {
// Total is the total number of capacity units consumed during this operation.
Total float64
// Read is the total number of read capacity units consumed during this operation.
// This seems to be only set for transactions.
Read float64
// Write is the total number of write capacity units consumed during this operation.
// This seems to be only set for transactions.
Write float64
// GSI is a map of Global Secondary Index names to consumed capacity units.
// GSI is a map of Global Secondary Index names to total consumed capacity units.
GSI map[string]float64
// GSI is a map of Local Secondary Index names to consumed capacity units.
// GSIRead is a map of Global Secondary Index names to consumed read capacity units.
// This seems to be only set for transactions.
GSIRead map[string]float64
// GSIWrite is a map of Global Secondary Index names to consumed write capacity units.
// This seems to be only set for transactions.
GSIWrite map[string]float64
// LSI is a map of Local Secondary Index names to total consumed capacity units.
LSI map[string]float64
// Table is the amount of throughput consumed by the table.
// LSIRead is a map of Local Secondary Index names to consumed read capacity units.
// This seems to be only set for transactions.
LSIRead map[string]float64
// LSIWrite is a map of Local Secondary Index names to consumed write capacity units.
// This seems to be only set for transactions.
LSIWrite map[string]float64
// Table is the amount of total throughput consumed by the table.
Table float64
// TableRead is the amount of read throughput consumed by the table.
// This seems to be only set for transactions.
TableRead float64
// TableWrite is the amount of write throughput consumed by the table.
// This seems to be only set for transactions.
TableWrite float64
// TableName is the name of the table affected by this operation.
TableName string
}
Expand All @@ -109,6 +129,18 @@ func addConsumedCapacity(cc *ConsumedCapacity, raw *dynamodb.ConsumedCapacity) {
}
for name, consumed := range raw.GlobalSecondaryIndexes {
cc.GSI[name] = cc.GSI[name] + *consumed.CapacityUnits
if consumed.ReadCapacityUnits != nil {
if cc.GSIRead == nil {
cc.GSIRead = make(map[string]float64, len(raw.GlobalSecondaryIndexes))
}
cc.GSIRead[name] = cc.GSIRead[name] + *consumed.ReadCapacityUnits
}
if consumed.WriteCapacityUnits != nil {
if cc.GSIWrite == nil {
cc.GSIWrite = make(map[string]float64, len(raw.GlobalSecondaryIndexes))
}
cc.GSIWrite[name] = cc.GSIWrite[name] + *consumed.WriteCapacityUnits
}
}
}
if len(raw.LocalSecondaryIndexes) > 0 {
Expand All @@ -117,10 +149,28 @@ func addConsumedCapacity(cc *ConsumedCapacity, raw *dynamodb.ConsumedCapacity) {
}
for name, consumed := range raw.LocalSecondaryIndexes {
cc.LSI[name] = cc.LSI[name] + *consumed.CapacityUnits
if consumed.ReadCapacityUnits != nil {
if cc.LSIRead == nil {
cc.LSIRead = make(map[string]float64, len(raw.LocalSecondaryIndexes))
}
cc.LSIRead[name] = cc.LSIRead[name] + *consumed.ReadCapacityUnits
}
if consumed.WriteCapacityUnits != nil {
if cc.LSIWrite == nil {
cc.LSIWrite = make(map[string]float64, len(raw.LocalSecondaryIndexes))
}
cc.LSIWrite[name] = cc.LSIWrite[name] + *consumed.WriteCapacityUnits
}
}
}
if raw.Table != nil {
cc.Table += *raw.Table.CapacityUnits
if raw.Table.ReadCapacityUnits != nil {
cc.TableRead += *raw.Table.ReadCapacityUnits
}
if raw.Table.WriteCapacityUnits != nil {
cc.TableWrite += *raw.Table.WriteCapacityUnits
}
}
if raw.TableName != nil {
cc.TableName = *raw.TableName
Expand Down
59 changes: 59 additions & 0 deletions table_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package dynamo

import (
"reflect"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
)

func TestAddConsumedCapacity(t *testing.T) {
raw := &dynamodb.ConsumedCapacity{
TableName: aws.String("TestTable"),
Table: &dynamodb.Capacity{
CapacityUnits: aws.Float64(9),
ReadCapacityUnits: aws.Float64(4),
WriteCapacityUnits: aws.Float64(5),
},
GlobalSecondaryIndexes: map[string]*dynamodb.Capacity{
"TestGSI": &dynamodb.Capacity{
CapacityUnits: aws.Float64(3),
ReadCapacityUnits: aws.Float64(1),
WriteCapacityUnits: aws.Float64(2),
},
},
LocalSecondaryIndexes: map[string]*dynamodb.Capacity{
"TestLSI": &dynamodb.Capacity{
CapacityUnits: aws.Float64(30),
ReadCapacityUnits: aws.Float64(10),
WriteCapacityUnits: aws.Float64(20),
},
},
CapacityUnits: aws.Float64(42),
ReadCapacityUnits: aws.Float64(15),
WriteCapacityUnits: aws.Float64(27),
}
expected := ConsumedCapacity{
TableName: *raw.TableName,
Table: *raw.Table.CapacityUnits,
TableRead: *raw.Table.ReadCapacityUnits,
TableWrite: *raw.Table.WriteCapacityUnits,
GSI: map[string]float64{"TestGSI": *raw.GlobalSecondaryIndexes["TestGSI"].CapacityUnits},
GSIRead: map[string]float64{"TestGSI": *raw.GlobalSecondaryIndexes["TestGSI"].ReadCapacityUnits},
GSIWrite: map[string]float64{"TestGSI": *raw.GlobalSecondaryIndexes["TestGSI"].WriteCapacityUnits},
LSI: map[string]float64{"TestLSI": *raw.LocalSecondaryIndexes["TestLSI"].CapacityUnits},
LSIRead: map[string]float64{"TestLSI": *raw.LocalSecondaryIndexes["TestLSI"].ReadCapacityUnits},
LSIWrite: map[string]float64{"TestLSI": *raw.LocalSecondaryIndexes["TestLSI"].WriteCapacityUnits},
Total: *raw.CapacityUnits,
Read: *raw.ReadCapacityUnits,
Write: *raw.WriteCapacityUnits,
}

var cc ConsumedCapacity
addConsumedCapacity(&cc, raw)

if !reflect.DeepEqual(cc, expected) {
t.Error("bad ConsumedCapacity:", cc, "≠", expected)
}
}

0 comments on commit e57b28d

Please sign in to comment.