2025-02-11 15:22:59 +00:00
|
|
|
package resultset
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-02-20 19:02:44 +00:00
|
|
|
"database/sql"
|
|
|
|
|
"errors"
|
2025-02-11 15:22:59 +00:00
|
|
|
"log/slog"
|
2025-03-05 12:05:46 +00:00
|
|
|
|
2025-02-11 15:22:59 +00:00
|
|
|
"queryorchestration/internal/database/repository"
|
|
|
|
|
"queryorchestration/internal/query/result"
|
2025-03-20 11:06:41 +00:00
|
|
|
resultprocessor "queryorchestration/internal/query/result/processor"
|
2025-02-11 15:22:59 +00:00
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Set struct {
|
|
|
|
|
DocumentID uuid.UUID `json:"document_id" validate:"required,uuid"`
|
|
|
|
|
QueryID uuid.UUID `json:"query_id" validate:"required,uuid"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) Set(ctx context.Context, params *Set) error {
|
2025-03-20 11:06:41 +00:00
|
|
|
dbid := params.DocumentID
|
2025-03-05 19:49:03 +00:00
|
|
|
textVersion, err := s.cfg.GetDBQueries().GetTextEntryByDocId(ctx, dbid)
|
2025-02-11 15:22:59 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
query, err := s.svc.Query.Get(ctx, params.QueryID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
value, err := s.svc.Result.Process(ctx, &result.Process{
|
|
|
|
|
DocumentID: params.DocumentID,
|
|
|
|
|
QueryID: query.ID,
|
|
|
|
|
QueryVersion: query.ActiveVersion,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-20 11:06:41 +00:00
|
|
|
err = s.storeResult(ctx, textVersion, params, query.ActiveVersion, value)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
slog.Debug("set query result", "query_id", params.QueryID.String(), "document_id", params.DocumentID.String())
|
|
|
|
|
|
|
|
|
|
return s.informQueryDependents(ctx, params)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) storeResult(ctx context.Context, textVersion *repository.Currenttextentry, params *Set, activeVersion int32, value resultprocessor.Value) error {
|
|
|
|
|
return s.cfg.ExecuteDBTransaction(ctx, func(ctx context.Context, q *repository.Queries) error {
|
2025-02-20 19:02:44 +00:00
|
|
|
id, err := q.AddResult(ctx, &repository.AddResultParams{
|
2025-03-20 11:06:41 +00:00
|
|
|
Queryid: params.QueryID,
|
2025-02-20 19:02:44 +00:00
|
|
|
Value: value.GetStoreValue(),
|
2025-03-20 11:06:41 +00:00
|
|
|
Queryversion: activeVersion,
|
2025-02-20 19:02:44 +00:00
|
|
|
Textentryid: textVersion.ID,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qResults, err := q.ListQueryRequirementValues(ctx, &repository.ListQueryRequirementValuesParams{
|
2025-03-20 11:06:41 +00:00
|
|
|
Queryid: ¶ms.QueryID,
|
|
|
|
|
Documentid: ¶ms.DocumentID,
|
|
|
|
|
Version: &activeVersion,
|
2025-02-20 19:02:44 +00:00
|
|
|
})
|
|
|
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, result := range qResults {
|
|
|
|
|
err = q.AddResultDependency(ctx, &repository.AddResultDependencyParams{
|
|
|
|
|
Resultid: id,
|
2025-03-20 11:06:41 +00:00
|
|
|
Requiredresultid: *result.ID,
|
2025-02-20 19:02:44 +00:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
2025-02-11 15:22:59 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) informQueryDependents(ctx context.Context, params *Set) error {
|
|
|
|
|
ids, err := s.cfg.GetDBQueries().ListQueryDirectDependentsByDocumentID(ctx, &repository.ListQueryDirectDependentsByDocumentIDParams{
|
2025-05-06 01:59:52 +00:00
|
|
|
Documentid: ¶ms.DocumentID,
|
|
|
|
|
Queryid: ¶ms.QueryID,
|
2025-02-11 15:22:59 +00:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-20 11:06:41 +00:00
|
|
|
return s.svc.Sync.TriggerMultiSync(ctx, params.DocumentID, ids)
|
2025-02-11 15:22:59 +00:00
|
|
|
}
|