edc50c7510
Size Import Parts * parts * think * workingpart * textout
88 lines
2.2 KiB
Go
88 lines
2.2 KiB
Go
package documenttext
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
"queryorchestration/internal/serviceconfig/build"
|
|
"queryorchestration/internal/serviceconfig/objectstore"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/textract"
|
|
"github.com/aws/aws-sdk-go-v2/service/textract/types"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
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 {
|
|
key := objectstore.BucketKey{
|
|
CreatedAt: time.Now().UTC(),
|
|
Location: objectstore.TextTextract,
|
|
ClientID: clean.Clientid,
|
|
}
|
|
|
|
currentPart, err := q.GetTextractOutputCurrentPart(ctx, &repository.GetTextractOutputCurrentPartParams{
|
|
Clientid: &clean.Clientid,
|
|
Querydate: pgtype.Timestamptz{
|
|
Time: key.CreatedAt,
|
|
Valid: true,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
part := s.cfg.GetDirectoryPart(currentPart.Part, currentPart.Count)
|
|
key.Part = &part
|
|
|
|
triggerId, err := s.cfg.GetDBQueries().AddDocumentTextTrigger(ctx, &repository.AddDocumentTextTriggerParams{
|
|
Cleanid: clean.ID,
|
|
Version: version,
|
|
Createdat: pgtype.Timestamp{
|
|
Time: key.CreatedAt,
|
|
Valid: true,
|
|
},
|
|
Part: *key.Part,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
key.EntityID = triggerId
|
|
keyStr := key.String()
|
|
jobTag := triggerId.String()
|
|
|
|
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,
|
|
S3Prefix: &keyStr,
|
|
},
|
|
})
|
|
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
|
|
})
|
|
}
|