53ea7d34e6
Start adding Textract + UUID changes * base * startclient * ts * short * tests
99 lines
2.1 KiB
Go
99 lines
2.1 KiB
Go
package documenttext
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
"queryorchestration/internal/document"
|
|
"queryorchestration/internal/serviceconfig/build"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type extraction struct {
|
|
TextLocation document.Location
|
|
Hash string
|
|
}
|
|
|
|
func (s *Service) executeExtraction(ctx context.Context, cleanId uuid.UUID) (*extraction, error) {
|
|
entry, err := s.cfg.GetDBQueries().GetCleanEntry(ctx, cleanId)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if entry.Fail.Valid {
|
|
return nil, fmt.Errorf("no valid cleaning")
|
|
}
|
|
|
|
// analysis, detection - diff?
|
|
// split page by page?
|
|
// output format
|
|
// index
|
|
// pages
|
|
// block types
|
|
|
|
return &extraction{
|
|
Hash: "example",
|
|
TextLocation: document.Location{
|
|
Bucket: *entry.Bucket,
|
|
Key: *entry.Key,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func (s *Service) extract(ctx context.Context, cleanId uuid.UUID) error {
|
|
details, err := s.executeExtraction(ctx, cleanId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.storeExtraction(ctx, cleanId, details)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) storeExtraction(ctx context.Context, cleanId uuid.UUID, details *extraction) error {
|
|
version := build.GetVersionUnixTimestamp()
|
|
|
|
return s.cfg.ExecuteDBTransaction(ctx, func(ctx context.Context, q *repository.Queries) error {
|
|
existing, err := q.GetDocumentTextExtractionByHash(ctx, &repository.GetDocumentTextExtractionByHashParams{
|
|
Hash: details.Hash,
|
|
Cleanentryid: cleanId,
|
|
})
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
return err
|
|
}
|
|
|
|
var textId uuid.UUID
|
|
if existing == nil || errors.Is(err, sql.ErrNoRows) {
|
|
newTextId, err := q.AddDocumentText(ctx, &repository.AddDocumentTextParams{
|
|
Bucket: details.TextLocation.Bucket,
|
|
Key: details.TextLocation.Key,
|
|
Cleanentryid: cleanId,
|
|
Hash: details.Hash,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
textId = newTextId
|
|
} else {
|
|
textId = existing.ID
|
|
}
|
|
|
|
err = q.AddDocumentTextEntry(ctx, &repository.AddDocumentTextEntryParams{
|
|
Version: version,
|
|
Textid: textId,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|