package main import ( "github.com/aws/aws-sdk-go/aws" d "github.com/aws/aws-sdk-go/service/dynamodb" da "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" "github.com/aws/aws-sdk-go/service/dynamodb/expression" ) // GetDevice is to get a device from id func GetDevice(svc *d.DynamoDB, deviceID string) (Device, error) { var device Device input := &d.GetItemInput{ Key: map[string]*d.AttributeValue{ "device": { S: aws.String(deviceID), }, }, TableName: aws.String("iot-item"), } result, err := svc.GetItem(input) if err != nil { return device, err } err = da.UnmarshalMap(result.Item, &device) if err != nil { return device, err } return device, nil } // GetDevices is to get all devices we know func GetDevices(svc *d.DynamoDB) (*d.ScanOutput, error) { proj := expression.NamesList( expression.Name("device"), expression.Name("name")) expr, _ := expression.NewBuilder().WithProjection(proj).Build() // Build the query input parameters params := &d.ScanInput{ ExpressionAttributeNames: expr.Names(), ExpressionAttributeValues: expr.Values(), ProjectionExpression: expr.Projection(), TableName: aws.String("iot-item"), } return svc.Scan(params) } // GetHistory is a query history for any device func GetHistory(svc *d.DynamoDB, deviceID string, limit int64) (*d.QueryOutput, error) { keyCond := expression.Key("device").Equal(expression.Value(deviceID)) qProj := expression.NamesList( expression.Name("device"), expression.Name("timestamp"), expression.Name("info"), expression.Name("data")) qBuilder := expression.NewBuilder().WithKeyCondition(keyCond).WithProjection(qProj) qExpression, _ := qBuilder.Build() queryInput := &d.QueryInput{ KeyConditionExpression: qExpression.KeyCondition(), ProjectionExpression: qExpression.Projection(), ExpressionAttributeNames: qExpression.Names(), ExpressionAttributeValues: qExpression.Values(), ScanIndexForward: aws.Bool(false), TableName: aws.String("iot-logger"), Limit: aws.Int64(limit), } return svc.Query(queryInput) }