package main import ( "github.com/aws/aws-sdk-go/aws" d "github.com/aws/aws-sdk-go/service/dynamodb" "github.com/aws/aws-sdk-go/service/dynamodb/expression" ) // 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("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) }