Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

feat: list the topic and dates for a dlq-bucket #64

Merged
merged 14 commits into from
Sep 18, 2023
17 changes: 13 additions & 4 deletions internal/server/gcs/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

const clientTimeout = time.Second * 120

func NewClient(keyFilePath string) (*SClient, error) {
func NewClient(keyFilePath string) (BlobObjectClient, error) {
client, err := storage.NewClient(context.Background(), option.WithCredentialsFile(keyFilePath))
if err != nil {
log.Printf("Failed to create GCSClient storageClient: %v\n", err)
Expand All @@ -24,12 +24,11 @@ func NewClient(keyFilePath string) (*SClient, error) {
return &SClient{gcsClient: client}, nil
}

func (client Client) ListTopicDates(bucketInfo BucketInfo) (map[string]map[string]int64, error) {
func (client Client) ListTopicDates(bucketInfo BucketInfo) ([]TopicMetaData, error) {
bucket := bucketInfo.BucketName
prefix := bucketInfo.Prefix
delim := bucketInfo.Delim
ctx := context.Background()
// map(topic -> map(Date -> size))
topicDateMap := make(map[string]map[string]int64)
ctx, cancel := context.WithTimeout(ctx, clientTimeout)
defer cancel()
Expand Down Expand Up @@ -57,5 +56,15 @@ func (client Client) ListTopicDates(bucketInfo BucketInfo) (map[string]map[strin
}
topicDateMap[topicName][date] += attrs.Size
}
return topicDateMap, nil
var returnVal []TopicMetaData
for topic, dates := range topicDateMap {
for date, size := range dates {
returnVal = append(returnVal, TopicMetaData{
Topic: topic,
Date: date,
SizeInBytes: size,
})
}
}
return returnVal, nil
}
39 changes: 33 additions & 6 deletions internal/server/gcs/gcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gcs_test

import (
"fmt"
"sort"
"testing"

"cloud.google.com/go/storage"
Expand Down Expand Up @@ -31,12 +32,38 @@ func TestListTopicDates(t *testing.T) {
Delim: "",
})
assert.NoError(t, err)
assert.Equal(t, 2, len(topicDates))
assert.Equal(t, 2, len(topicDates["test-topic1"]))
assert.Equal(t, 1, len(topicDates["test-topic2"]))
assert.Equal(t, int64(579), topicDates["test-topic1"]["2023-08-26"])
assert.Equal(t, int64(890), topicDates["test-topic1"]["2023-08-27"])
assert.Equal(t, int64(1696), topicDates["test-topic2"]["2023-08-28"])
expected := []gcs.TopicMetaData{
{
Topic: "test-topic1",
Date: "2023-08-26",
SizeInBytes: 579,
},
{
Topic: "test-topic2",
Date: "2023-08-28",
SizeInBytes: 1696,
},
{
Topic: "test-topic1",
Date: "2023-08-27",
SizeInBytes: 890,
},
}
sortMetadata(expected)
sortMetadata(topicDates)
assert.Equal(t, expected, topicDates)
}

func sortMetadata(data []gcs.TopicMetaData) {
sort.Slice(data, func(i, j int) bool {
if data[i].Topic != data[j].Topic {
return data[i].Topic < data[j].Topic
}
if data[i].Date != data[j].Date {
return data[i].Date < data[j].Date
}
return data[i].SizeInBytes < data[j].SizeInBytes
})
}

func TestErrorOnListTopic(t *testing.T) {
Expand Down
8 changes: 7 additions & 1 deletion internal/server/gcs/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import (

// BlobStorageClient This is used in service
type BlobStorageClient interface {
ListTopicDates(bucketInfo BucketInfo) (map[string]map[string]int64, error)
ListTopicDates(bucketInfo BucketInfo) ([]TopicMetaData, error)
}

type TopicMetaData struct {
StewartJingga marked this conversation as resolved.
Show resolved Hide resolved
Topic string `json:"topic"`
Date string `json:"date"`
SizeInBytes int64 `json:"size_in_bytes"`
}

// BlobObjectClient This is used to abstract actual gcs client
Expand Down
25 changes: 18 additions & 7 deletions internal/server/v1/dlq/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,30 @@ func TestListTopicDates(t *testing.T) {
UpdatedBy: "",
},
}, nil)
topicDates := make(map[string]map[string]int64)
topicDates["topic-1"] = make(map[string]int64)
topicDates["topic-2"] = make(map[string]int64)
topicDates["topic-1"]["2023-08-26"] = int64(1234)
topicDates["topic-1"]["2023-12-10"] = int64(4321)
topicDates["topic-2"]["2023-09-20"] = int64(99)
topicDates := []gcs.TopicMetaData{
{
Topic: "topic-1",
Date: "2023-08-26",
SizeInBytes: 1234,
},
{
Topic: "test-topic2",
Date: "2023-12-10",
SizeInBytes: 4321,
},
{
Topic: "topic-2",
Date: "2023-09-20",
SizeInBytes: 99,
},
}
gClient.On("ListTopicDates", gcs.BucketInfo{
BucketName: "test-bucket",
Prefix: "test-prefix",
Delim: "",
}).Return(topicDates, nil)
handler.ListFirehoseDLQ(httpWriter, httpRequest)
expectedMap := make(map[string]map[string]map[string]int64)
expectedMap := make(map[string][]gcs.TopicMetaData)
err := json.Unmarshal([]byte(httpWriter.messages[0]), &expectedMap)
require.NoError(t, err)
assert.Equal(t, topicDates, expectedMap["dlq_list"])
lavkesh marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
10 changes: 5 additions & 5 deletions mocks/BlobStorageClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.