This repository has been archived by the owner on Jul 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
etl.go
196 lines (153 loc) · 4.53 KB
/
etl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package database
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/fischersean/phish-food/internal/reddit"
"github.com/fischersean/phish-food/internal/report"
"fmt"
//"strings"
"bytes"
"encoding/json"
"io/ioutil"
"time"
)
const (
GetLatestRedditMaxLookback = 10
)
func NewRedditPostArchiveRecord(p reddit.Post, sub string, t time.Time) (r RedditPostArchiveRecord) {
r.Post = p
r.Permalink = p.Permalink
r.Hour = t.Hour()
r.Key = fmt.Sprintf("%s_%s/%d/%s.json",
sub,
t.Format(DateFormat),
r.Hour,
r.Permalink[:len(r.Permalink)-1],
)
return r
}
func (c *Connection) PutRedditPostArchiveRecord(record RedditPostArchiveRecord) (err error) {
buf, err := json.Marshal(record)
if err != nil {
return err
}
input := &s3.PutObjectInput{
Bucket: aws.String(c.redditPostArchiveBucket),
Key: aws.String(record.Key),
Body: bytes.NewReader(buf),
}
_, err = c.s3Service.PutObject(input)
return err
}
func (c *Connection) ListRedditPostArchiveRecord(input RedditPostArchiveListInput) (keys []string, err error) {
startAfterPrefix := fmt.Sprintf("%s_%s/%d/r/%s/comments",
input.Subreddit,
input.Date.Format(DateFormat),
input.Date.Hour(),
input.Subreddit,
)
listInput := &s3.ListObjectsV2Input{
Bucket: aws.String(c.redditPostArchiveBucket),
Prefix: aws.String(startAfterPrefix),
}
objectList, err := c.s3Service.ListObjectsV2(listInput)
if err != nil {
return keys, err
}
for _, v := range objectList.Contents {
keys = append(keys, *v.Key)
}
return keys, err
}
func (c *Connection) GetRedditPostArchiveRecord(input RedditPostArchiveQueryInput) (record RedditPostArchiveRecord, err error) {
getObjectInput := &s3.GetObjectInput{
Bucket: aws.String(c.redditPostArchiveBucket),
Key: aws.String(input.Key),
}
object, err := c.s3Service.GetObject(getObjectInput)
if err != nil {
return record, err
}
defer object.Body.Close()
buf, err := ioutil.ReadAll(object.Body)
if err != nil {
return record, err
}
err = json.Unmarshal(buf, &record)
if err != nil {
return record, err
}
return record, err
}
func NewEtlResultsRecord(sr []report.StockReport, sub string, t time.Time) (e EtlResultsRecord) {
e.Data = sr
e.Id = fmt.Sprintf("%s_%s", sub, t.Format(DateFormat))
e.Hour = t.Hour()
return e
}
func (c *Connection) PutEtlResultsRecord(record EtlResultsRecord) (err error) {
av, err := dynamodbattribute.MarshalMap(record)
if err != nil {
return err
}
input := &dynamodb.PutItemInput{
Item: av,
TableName: aws.String(c.etlResultsTable),
}
_, err = c.service.PutItem(input)
return err
}
func (c *Connection) GetEtlResultsRecord(input EtlResultsQueryInput) (record []EtlResultsRecord, err error) {
qInput := &dynamodb.QueryInput{
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":v1": {
S: aws.String(fmt.Sprintf("%s_%s", input.Subreddit, input.Date.Format(DateFormat))),
},
},
KeyConditionExpression: aws.String("id = :v1"),
TableName: aws.String(c.etlResultsTable),
}
result, err := c.service.Query(qInput)
if err != nil {
return record, err
}
err = dynamodbattribute.UnmarshalListOfMaps(result.Items, &record)
return record, err
}
func (c *Connection) GetLatestEtlResultsRecord(input EtlResultsQueryInput) (record EtlResultsRecord, err error) {
// Starting from input.Date, loop backward by 1 day at a time until we get a result
// Take the latest results based off of the sort key
// If no date provided, start from time.Now()
if input.Date.IsZero() {
input.Date = time.Now()
}
var result *dynamodb.QueryOutput
count := int64(0)
lookbackCount := 0
for d := input.Date; count < 1; d = d.Add(-24 * time.Hour) {
if lookbackCount > GetLatestRedditMaxLookback {
return record, fmt.Errorf("Reached max lookback distance without finding primary key")
}
qInput := &dynamodb.QueryInput{
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":v1": {
S: aws.String(fmt.Sprintf("%s_%s", input.Subreddit, input.Date.Format(DateFormat))),
},
},
KeyConditionExpression: aws.String("id = :v1"),
ScanIndexForward: aws.Bool(false),
Limit: aws.Int64(1),
TableName: aws.String(c.etlResultsTable),
}
result, err = c.service.Query(qInput)
if err != nil {
return record, err
}
count = *result.Count
lookbackCount += 1
}
err = dynamodbattribute.UnmarshalMap(result.Items[0], &record)
return record, err
}