You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.5 KiB

7 years ago
package main
import (
"github.com/aws/aws-sdk-go/aws"
d "github.com/aws/aws-sdk-go/service/dynamodb"
7 years ago
"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) {
7 years ago
proj := expression.NamesList(
expression.Name("device"),
expression.Name("name"))
expr, _ := expression.NewBuilder().WithProjection(proj).Build()
// Build the query input parameters
params := &d.ScanInput{
7 years ago
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) {
7 years ago
keyCond := expression.Key("device").Equal(expression.Value(deviceID))
qProj := expression.NamesList(
expression.Name("device"),
expression.Name("timestamp"),
expression.Name("data"))
7 years ago
qBuilder := expression.NewBuilder().WithKeyCondition(keyCond).WithProjection(qProj)
qExpression, _ := qBuilder.Build()
queryInput := &d.QueryInput{
7 years ago
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)
}