-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Timeseries data is sent to AWS in CSV format
- Loading branch information
1 parent
7e976ea
commit e39b988
Showing
10 changed files
with
256 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package awstools | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/aws/aws-sdk-go/service/s3/s3manager" | ||
) | ||
|
||
// Timeseries holds csv-buffer and upload features | ||
type Timeseries struct { | ||
reader io.Reader | ||
writer io.Writer | ||
upload AWSUploadFunc | ||
uploader *s3manager.Uploader | ||
key string | ||
} | ||
|
||
func (ts *Timeseries) Write(data []byte) (int, error) { | ||
if ts.writer == nil { | ||
return 0, fmt.Errorf("Timeseries %v already closed", ts.key) | ||
} | ||
return ts.writer.Write(data) | ||
} | ||
|
||
// Close uploads data to aws | ||
func (ts *Timeseries) Close() { | ||
ts.writer = nil | ||
ts.upload(ts.uploader, ts.key, ts.reader) | ||
} | ||
|
||
// NewTimeseries returns a timeseries that will invoke upload upon close | ||
func NewTimeseries(upload AWSUploadFunc, uploader *s3manager.Uploader, key string) *Timeseries { | ||
buf := bytes.NewBuffer([]byte{}) | ||
return &Timeseries{buf, buf, upload, uploader, key} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package awstools | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/s3/s3manager" | ||
) | ||
|
||
func TestNewTimeseries(t *testing.T) { | ||
sess := session.Must(session.NewSession(&aws.Config{Region: aws.String("localhost")})) | ||
upload := s3manager.NewUploader(sess) | ||
var idxUp = 0 | ||
uploads := make(map[string]int) | ||
|
||
var uploader = func(uploader *s3manager.Uploader, key string, bodyBuffer io.Reader) { | ||
buf, _ := ioutil.ReadAll(bodyBuffer) | ||
uploads[key] = len(buf) | ||
idxUp++ | ||
} | ||
|
||
ts := NewTimeseries(uploader, upload, "myfile") | ||
// Write several times possible | ||
for i := 0; i < 3; i++ { | ||
n, err := ts.Write([]byte("test")) | ||
if n != 4 || err != nil { | ||
t.Errorf("Timeseries.Write() = %v %v, want 4 <nil>", n, err) | ||
return | ||
} | ||
if idxUp != 0 { | ||
t.Errorf("Timesereis.Write() initiated an unexpected upload %v", uploads) | ||
return | ||
} | ||
} | ||
|
||
// Uploads file upon closing | ||
ts.Close() | ||
if idxUp != 1 { | ||
t.Errorf( | ||
"Timeseries.Close() didn't upload one file (%v files sent) with content %v", | ||
idxUp, | ||
uploads, | ||
) | ||
} | ||
l, ok := uploads["myfile"] | ||
if !ok || l != 4*3 { | ||
t.Errorf( | ||
"Timeseries.Close() didn't upload 'myfile' with 12 bytes (%v, %v)", | ||
l, | ||
ok, | ||
) | ||
} | ||
|
||
// Can't write after closing | ||
n, err := ts.Write([]byte("hello")) | ||
if n != 0 || err == nil { | ||
t.Errorf( | ||
"Timeseries.Write() should have written nothing and err after closing (%v, %v)", | ||
n, | ||
err, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package awstools | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/s3/s3manager" | ||
) | ||
|
||
const awsBucket = "mats-l0-artifacts" | ||
|
||
// AWSUpload uploads file content to target bucket | ||
func AWSUpload(uploader *s3manager.Uploader, key string, body io.Reader) { | ||
_, err := uploader.Upload(&s3manager.UploadInput{ | ||
Bucket: aws.String(awsBucket), | ||
Key: aws.String(strings.ReplaceAll(key, "\\", "/")), | ||
Body: body, | ||
}) | ||
if err != nil { | ||
log.Printf("Failed to upload file %v, %v", key, err) | ||
} | ||
|
||
} | ||
|
||
// AWSUploadFunc is the signature of an AWS upload function | ||
type AWSUploadFunc func(uploader *s3manager.Uploader, key string, body io.Reader) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.