Merged in feature/standardisefilepath (pull request #111)
Feature/standardisefilepath * baseprocessing * generalstructure
This commit is contained in:
@@ -2,13 +2,11 @@ package documentstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"regexp"
|
||||
|
||||
docinitrunner "queryorchestration/api/docInitRunner"
|
||||
doctextprocessrunner "queryorchestration/api/docTextProcessRunner"
|
||||
"queryorchestration/internal/client"
|
||||
"queryorchestration/internal/serviceconfig/objectstore"
|
||||
"queryorchestration/internal/serviceconfig/queue"
|
||||
)
|
||||
|
||||
@@ -31,25 +29,29 @@ func (s *Service) Process(ctx context.Context, params Params) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
task, clientId := getKeyMetadata(params.Key)
|
||||
key, err := objectstore.ParseBucketKey(params.Key)
|
||||
if err != nil {
|
||||
slog.Error("unable to parse key", "key", params.Key)
|
||||
return nil
|
||||
}
|
||||
|
||||
queueParams := &queue.SendParams{}
|
||||
switch task {
|
||||
case DocInit:
|
||||
switch key.Location {
|
||||
case objectstore.Import:
|
||||
queueParams.QueueURL = s.cfg.GetDocInitURL()
|
||||
queueParams.Body = docinitrunner.Body{
|
||||
Bucket: params.Bucket,
|
||||
Key: params.Key,
|
||||
Hash: params.Hash,
|
||||
ClientID: clientId,
|
||||
ClientID: key.ClientID,
|
||||
}
|
||||
case DocTextProcess:
|
||||
case objectstore.TextTextract:
|
||||
queueParams.QueueURL = s.cfg.GetDocumentTextProcessURL()
|
||||
queueParams.Body = doctextprocessrunner.Body{
|
||||
Bucket: params.Bucket,
|
||||
Key: params.Key,
|
||||
Hash: params.Hash,
|
||||
ClientID: clientId,
|
||||
ClientID: key.ClientID,
|
||||
}
|
||||
default:
|
||||
slog.Info("unsupported key", "key", params.Key)
|
||||
@@ -59,32 +61,6 @@ func (s *Service) Process(ctx context.Context, params Params) error {
|
||||
return s.cfg.SendToQueue(ctx, queueParams)
|
||||
}
|
||||
|
||||
type Task string
|
||||
|
||||
const (
|
||||
DocInit Task = "import"
|
||||
DocTextProcess Task = "text/textract"
|
||||
Invalid Task = ""
|
||||
)
|
||||
|
||||
func getKeyMetadata(key string) (Task, string) {
|
||||
regexStr := fmt.Sprintf(`^(%s)/(import|text/textract)/.+$`, client.CLIENT_ID_REGEX)
|
||||
re := regexp.MustCompile(regexStr)
|
||||
match := re.FindStringSubmatch(key)
|
||||
if len(match) < 3 {
|
||||
return Invalid, ""
|
||||
}
|
||||
|
||||
task := Task(match[2])
|
||||
if task == Invalid {
|
||||
return Invalid, ""
|
||||
}
|
||||
|
||||
clientId := match[1]
|
||||
|
||||
return task, clientId
|
||||
}
|
||||
|
||||
func (s *Service) isSupportedEvent(name EventS3) bool {
|
||||
return name == EventS3ObjectCreatedPut
|
||||
}
|
||||
|
||||
@@ -2,10 +2,11 @@ package documentstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/objectstore"
|
||||
"queryorchestration/internal/serviceconfig/queue/documentinit"
|
||||
"queryorchestration/internal/serviceconfig/queue/documenttextprocess"
|
||||
queuemock "queryorchestration/mocks/queue"
|
||||
@@ -56,9 +57,15 @@ func TestProcess(t *testing.T) {
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
location := objectstore.BucketKey{
|
||||
ClientID: "7db16095-9155-47d4-8004-b3b3ead93c83",
|
||||
Location: objectstore.Import,
|
||||
Filename: "aaa",
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
err := svc.Process(ctx, Params{
|
||||
Event: EventS3ObjectCreatedPut,
|
||||
Key: "7db16095-9155-47d4-8004-b3b3ead93c83/import/a",
|
||||
Key: location.String(),
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
@@ -73,9 +80,15 @@ func TestProcess(t *testing.T) {
|
||||
).
|
||||
Return(&sqs.SendMessageOutput{}, nil)
|
||||
|
||||
location := objectstore.BucketKey{
|
||||
ClientID: "7db16095-9155-47d4-8004-b3b3ead93c83",
|
||||
Location: objectstore.TextTextract,
|
||||
Filename: "aaa",
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
err := svc.Process(ctx, Params{
|
||||
Event: EventS3ObjectCreatedPut,
|
||||
Key: "7db16095-9155-47d4-8004-b3b3ead93c83/text/textract/a",
|
||||
Key: location.String(),
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
@@ -91,27 +104,3 @@ func TestIsSupportedEvent(t *testing.T) {
|
||||
assert.False(t, svc.isSupportedEvent("invalid"))
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetKeyMetadata(t *testing.T) {
|
||||
baseClientId := "hello"
|
||||
|
||||
task, clientId := getKeyMetadata(fmt.Sprintf("%s/import/a", baseClientId))
|
||||
assert.Equal(t, DocInit, task)
|
||||
assert.Equal(t, baseClientId, clientId)
|
||||
|
||||
task, clientId = getKeyMetadata(fmt.Sprintf("%s/text/textract/a", baseClientId))
|
||||
assert.Equal(t, DocTextProcess, task)
|
||||
assert.Equal(t, baseClientId, clientId)
|
||||
|
||||
task, clientId = getKeyMetadata("a")
|
||||
assert.Equal(t, Invalid, task)
|
||||
assert.Equal(t, "", clientId)
|
||||
|
||||
task, clientId = getKeyMetadata("fdee2e22-ad0f-4e30-91c8-d63c3501162f/something/a")
|
||||
assert.Equal(t, Invalid, task)
|
||||
assert.Equal(t, "", clientId)
|
||||
|
||||
task, clientId = getKeyMetadata("a/import/a")
|
||||
assert.Equal(t, DocInit, task)
|
||||
assert.Equal(t, "a", clientId)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user