2025-04-02 18:50:03 +00:00
|
|
|
package documenttext
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
2025-04-03 12:13:16 +00:00
|
|
|
"time"
|
2025-04-02 18:50:03 +00:00
|
|
|
|
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
|
|
|
"queryorchestration/internal/serviceconfig/build"
|
2025-04-03 12:13:16 +00:00
|
|
|
"queryorchestration/internal/serviceconfig/objectstore"
|
2025-04-02 18:50:03 +00:00
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/textract"
|
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/textract/types"
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (s *Service) triggerExtract(ctx context.Context, clean *repository.Currentcleanentry) error {
|
|
|
|
|
version := build.GetVersionUnixTimestamp()
|
|
|
|
|
|
|
|
|
|
return s.cfg.ExecuteDBTransaction(ctx, func(ctx context.Context, q *repository.Queries) error {
|
|
|
|
|
triggerId, err := s.cfg.GetDBQueries().AddDocumentTextTrigger(ctx, &repository.AddDocumentTextTriggerParams{
|
|
|
|
|
Cleanid: clean.ID,
|
|
|
|
|
Version: version,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jobTag := triggerId.String()
|
|
|
|
|
|
2025-04-03 12:13:16 +00:00
|
|
|
filename, err := s.getOutputFilename(triggerId)
|
2025-04-02 18:50:03 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-03 12:13:16 +00:00
|
|
|
key := objectstore.BucketKey{
|
|
|
|
|
CreatedAt: time.Now().UTC(),
|
|
|
|
|
Location: objectstore.TextTextract,
|
|
|
|
|
ClientID: clean.Clientid,
|
|
|
|
|
Filename: filename,
|
|
|
|
|
}
|
|
|
|
|
keyStr := key.String()
|
|
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
res, err := s.cfg.GetTextractClient().StartDocumentTextDetection(ctx, &textract.StartDocumentTextDetectionInput{
|
|
|
|
|
DocumentLocation: &types.DocumentLocation{
|
|
|
|
|
S3Object: &types.S3Object{
|
|
|
|
|
Bucket: clean.Bucket,
|
|
|
|
|
Name: clean.Key,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
JobTag: &jobTag,
|
|
|
|
|
OutputConfig: &types.OutputConfig{
|
|
|
|
|
S3Bucket: clean.Bucket,
|
2025-04-03 12:13:16 +00:00
|
|
|
S3Prefix: &keyStr,
|
2025-04-02 18:50:03 +00:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else if res.JobId == nil {
|
|
|
|
|
return errors.New("No job id returned from textract")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = s.cfg.GetDBQueries().AddDocumentTextTriggerJobId(ctx, &repository.AddDocumentTextTriggerJobIdParams{
|
|
|
|
|
ID: triggerId,
|
|
|
|
|
Textractjobid: res.JobId,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-03 12:13:16 +00:00
|
|
|
func (s *Service) getOutputFilename(triggerId uuid.UUID) (string, error) {
|
|
|
|
|
if triggerId == uuid.Nil {
|
2025-04-02 18:50:03 +00:00
|
|
|
return "", errors.New("trigger id required")
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-03 12:13:16 +00:00
|
|
|
return triggerId.String(), nil
|
2025-04-02 18:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
2025-04-03 12:13:16 +00:00
|
|
|
func (s *Service) getTriggerIdFromKey(key objectstore.BucketKey) (uuid.UUID, error) {
|
|
|
|
|
triggerId, err := uuid.Parse(key.Filename)
|
2025-04-02 18:50:03 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return uuid.Nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return triggerId, nil
|
|
|
|
|
}
|