Started adding query

This commit is contained in:
Riccardo
2022-07-16 11:31:09 +02:00
parent 54b2dda79a
commit a922a905c0
5 changed files with 46 additions and 7 deletions

View File

@@ -15,13 +15,14 @@ import (
type Table struct { type Table struct {
DynamoDbClient *dynamodb.Client DynamoDbClient *dynamodb.Client
TableName string TableName string
IndexName string
} }
type News struct { type News struct {
Id int `json:"id"` Id int `json:"id"`
Title string `json:"title"` Title string `json:"title"`
Text string `json:"text"` Text string `json:"text"`
Timestamp int `json:"time"` CreatedAt int `json:"time"`
Author string `json:"by"` Author string `json:"by"`
Url string `json:"url"` Url string `json:"url"`
Score int `json:"score"` Score int `json:"score"`
@@ -45,15 +46,15 @@ func CreateTable(basics Table) (*types.TableDescription, error) {
AttributeName: aws.String("Id"), AttributeName: aws.String("Id"),
KeyType: types.KeyTypeHash, KeyType: types.KeyTypeHash,
}, { }, {
AttributeName: aws.String("Title"), AttributeName: aws.String("CreatedAt"),
KeyType: types.KeyTypeRange, KeyType: types.KeyTypeRange,
}}, }},
AttributeDefinitions: []types.AttributeDefinition{{ AttributeDefinitions: []types.AttributeDefinition{{
AttributeName: aws.String("Id"), AttributeName: aws.String("Id"),
AttributeType: types.ScalarAttributeTypeN, AttributeType: types.ScalarAttributeTypeN,
}, { }, {
AttributeName: aws.String("Title"), AttributeName: aws.String("CreatedAt"),
AttributeType: types.ScalarAttributeTypeS, AttributeType: types.ScalarAttributeTypeN,
}}, }},
TableName: aws.String(basics.TableName), TableName: aws.String(basics.TableName),
ProvisionedThroughput: &types.ProvisionedThroughput{ ProvisionedThroughput: &types.ProvisionedThroughput{
@@ -115,3 +116,30 @@ func AddNewsBatch(basics Table, news []News, batchSize int) (int, error) {
return written, err return written, err
} }
func ReadTodayNews(basics Table, timeStart int, timeEnd int) ([]News, error) {
var news []News
params, err := attributevalue.MarshalList([]interface{}{timeStart, timeEnd})
if err != nil {
panic(err)
}
response, err := basics.DynamoDbClient.ExecuteStatement(context.TODO(), &dynamodb.ExecuteStatementInput{
Statement: aws.String(
fmt.Sprintf("SELECT * FROM \"%v\" WHERE CreatedAt>? AND CreatedAt<? ",
basics.TableName)),
Parameters: params,
})
if err != nil {
log.Printf("Couldn't get news from %v to %v: %v\n", timeStart, timeEnd, err)
} else {
err = attributevalue.UnmarshalListOfMaps(response.Items, &news)
if err != nil {
log.Printf("Couldn't unmarshal response: %v\n", err)
}
}
fmt.Println("MARSH", len(response.Items), news[0])
return news, err
}

1
go.mod
View File

@@ -6,6 +6,7 @@ require (
github.com/aws/aws-sdk-go-v2 v1.16.7 github.com/aws/aws-sdk-go-v2 v1.16.7
github.com/aws/aws-sdk-go-v2/config v1.15.14 github.com/aws/aws-sdk-go-v2/config v1.15.14
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.9.6 github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.9.6
github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression v1.4.12
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.15.9 github.com/aws/aws-sdk-go-v2/service/dynamodb v1.15.9
github.com/spf13/viper v1.12.0 github.com/spf13/viper v1.12.0
) )

2
go.sum
View File

@@ -46,6 +46,8 @@ github.com/aws/aws-sdk-go-v2/credentials v1.12.9 h1:DloAJr0/jbvm0iVRFDFh8GlWxrOd
github.com/aws/aws-sdk-go-v2/credentials v1.12.9/go.mod h1:2Vavxl1qqQXJ8MUcQZTsIEW8cwenFCWYXtLRPba3L/o= github.com/aws/aws-sdk-go-v2/credentials v1.12.9/go.mod h1:2Vavxl1qqQXJ8MUcQZTsIEW8cwenFCWYXtLRPba3L/o=
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.9.6 h1:vlEfSyZ2pZjOZe7zsPIAFem17w2HeeFULk7TPVWoDR4= github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.9.6 h1:vlEfSyZ2pZjOZe7zsPIAFem17w2HeeFULk7TPVWoDR4=
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.9.6/go.mod h1:+/KXTIzLmrjdlQVgiE14/jhy9GyDZnmMGQoykod99Lw= github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.9.6/go.mod h1:+/KXTIzLmrjdlQVgiE14/jhy9GyDZnmMGQoykod99Lw=
github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression v1.4.12 h1:sH0SffGPiNpvYCCfEF0dN0K9OC72KXBjW4HmiFvMVf0=
github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression v1.4.12/go.mod h1:0vvQ0FQRjyNB8EIkRdwT9tduJbkUdh00SnmuKnZRYLA=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.8 h1:VfBdn2AxwMbFyJN/lF/xuT3SakomJ86PZu3rCxb5K0s= github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.8 h1:VfBdn2AxwMbFyJN/lF/xuT3SakomJ86PZu3rCxb5K0s=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.8/go.mod h1:oL1Q3KuCq1D4NykQnIvtRiBGLUXhcpY5pl6QZB2XEPU= github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.8/go.mod h1:oL1Q3KuCq1D4NykQnIvtRiBGLUXhcpY5pl6QZB2XEPU=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.14 h1:2C0pYHcUBmdzPj+EKNC4qj97oK6yjrUhc1KoSodglvk= github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.14 h1:2C0pYHcUBmdzPj+EKNC4qj97oK6yjrUhc1KoSodglvk=

13
main.go
View File

@@ -8,6 +8,7 @@ import (
"hackernewsletter/hackernews" "hackernewsletter/hackernews"
"hackernewsletter/params" "hackernewsletter/params"
"log" "log"
"time"
"github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb" "github.com/aws/aws-sdk-go-v2/service/dynamodb"
@@ -38,12 +39,12 @@ func main() {
TableName: table, TableName: table,
} }
newTable := db.Table{client, *table} newsTable := db.Table{client, *table, conf.TableIndex}
resp, err := db.GetTableInfo(context.TODO(), client, input) resp, err := db.GetTableInfo(context.TODO(), client, input)
if err != nil { if err != nil {
println(("Table not found. Creating it...")) println(("Table not found. Creating it..."))
_, new_err := db.CreateTable(newTable) _, new_err := db.CreateTable(newsTable)
if new_err != nil { if new_err != nil {
panic("Failed creating table " + *table) panic("Failed creating table " + *table)
} }
@@ -62,5 +63,11 @@ func main() {
newsBatch = append(newsBatch, myNews) newsBatch = append(newsBatch, myNews)
} }
db.AddNewsBatch(newTable, newsBatch, conf.BatchSize) db.AddNewsBatch(newsTable, newsBatch, conf.BatchSize)
timeEnd := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.UTC)
timeStart := timeEnd.Add(-time.Hour * 24)
todayNews, _ := db.ReadTodayNews(newsTable, int(timeStart.Unix()), int(timeEnd.Unix()))
println("NEWS", todayNews)
} }

View File

@@ -4,6 +4,7 @@ import "github.com/spf13/viper"
type Config struct { type Config struct {
TableName string `mapstructure:"TABLE_NAME"` TableName string `mapstructure:"TABLE_NAME"`
TableIndex string `mapstructure:"GLOBAL_SECONDARY_INDEX"`
AwsRegion string `mapstructure:"AWS_REGION"` AwsRegion string `mapstructure:"AWS_REGION"`
TopNews string `mapstructure:"TOP_NEWS"` TopNews string `mapstructure:"TOP_NEWS"`
SingleNews string `mapstructure:"SINGLE_NEWS"` SingleNews string `mapstructure:"SINGLE_NEWS"`