Skip to content

Instantly share code, notes, and snippets.

@tremaineeto
Created December 19, 2019 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tremaineeto/7b4e7f4a55ba58009f036f8be3c65bb3 to your computer and use it in GitHub Desktop.
Save tremaineeto/7b4e7f4a55ba58009f036f8be3c65bb3 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"os"
"strings"
"time"
)
const oldLayout = "1/2/2006"
const newLayout = "2006-01-02"
// TODO - Update this item to reflect table fields
type indexedItem struct {
UUID string `json:"uuid"`
Date string `json:"date"`
Metric string `json:"metric"`
Proxy string `json:"proxy"`
Value float64 `json:"value"`
WeekNumber string `json:"week_number"`
Weekday string `json:"weekday"`
}
func main() {
// TODO - Update config here
dbAccess := ""
dbSecret := ""
dbSession, _ := session.NewSession(&aws.Config{
Region: aws.String("us-west-2"),
Credentials: credentials.NewStaticCredentials(dbAccess, dbSecret, ""),
})
dbClient := dynamodb.New(dbSession)
weekdays := []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
for _, weekday := range weekdays {
var queryInput = &dynamodb.QueryInput{
TableName: aws.String(""), // TODO - Enter table name
IndexName: aws.String("weekday-date-index"), // TODO - make sure this index exists
KeyConditions: map[string]*dynamodb.Condition{
"weekday": {
ComparisonOperator: aws.String("EQ"),
AttributeValueList: []*dynamodb.AttributeValue{
{
S: aws.String(weekday),
},
},
},
},
}
var resp, err = dbClient.Query(queryInput)
if err != nil {
fmt.Println("Got error querying data:")
fmt.Println(err.Error())
return
}
// for each item that is returned in the response
for _, i := range resp.Items {
item := indexedItem{}
err = dynamodbattribute.UnmarshalMap(i, &item)
if err != nil {
fmt.Println("Got error unmarshalling:")
fmt.Println(err.Error())
os.Exit(1)
}
// check if date is formatted incorrectly
if strings.Contains(item.Date, "/") {
// TODO - remove debug statement
fmt.Println("Incorrectly formatted item found:", item)
// convert incorrectly formatted string to time.Time
incorrectDate, err := time.Parse(oldLayout, item.Date)
if err != nil {
fmt.Println("Error parsing date on item:")
fmt.Print(item)
os.Exit(1);
}
// reformat time to be the correct format
correctDate := incorrectDate.Format(newLayout)
// TODO - remove debug statement
fmt.Println("Newly formatted item date:", correctDate)
// create an update to send to the database
input := &dynamodb.UpdateItemInput{
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":d": {
S: aws.String(correctDate),
},
},
TableName: aws.String("TABLE-NAME-HERE"),
Key: map[string]*dynamodb.AttributeValue{
"uuid": {
S: aws.String(item.UUID),
},
},
ExpressionAttributeNames: map[string]*string {
"#date": aws.String("date"),
},
ReturnValues: aws.String("UPDATED_NEW"),
UpdateExpression: aws.String("set #date=:d"),
}
// update the item in the database
_, err = dbClient.UpdateItem(input)
if err != nil {
fmt.Println(err.Error())
return
}
// TODO - remove debug statement
fmt.Println("Successfully updated item in the database!", item)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment