2025-01-20 13:31:48 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"queryorchestration/internal/database"
|
2025-01-29 11:52:37 +00:00
|
|
|
resultprocessor "queryorchestration/internal/query/result/processor"
|
2025-01-23 14:56:20 +00:00
|
|
|
"queryorchestration/internal/server/validation"
|
2025-01-20 13:31:48 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Config interface {
|
|
|
|
|
GetConfig() *string
|
|
|
|
|
SetConfig(*string)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) normalizeConfig(config Config) error {
|
|
|
|
|
if config == nil || config.GetConfig() == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trim := strings.TrimSpace(*config.GetConfig())
|
|
|
|
|
|
|
|
|
|
if trim == "" {
|
|
|
|
|
config.SetConfig(nil)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !json.Valid([]byte(trim)) {
|
|
|
|
|
return errors.New("invalid config JSON")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var data map[string]interface{}
|
|
|
|
|
if err := json.Unmarshal([]byte(trim), &data); err != nil {
|
|
|
|
|
return fmt.Errorf("error unmarshalling JSON: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prettyJSON, err := json.Marshal(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("error marshalling JSON: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strJSON := string(prettyJSON)
|
|
|
|
|
|
2025-01-21 18:24:14 +00:00
|
|
|
config.SetConfig(&strJSON)
|
2025-01-20 13:31:48 +00:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type RequiredQueryIDs interface {
|
|
|
|
|
GetRequiredQueryIDs() *[]uuid.UUID
|
|
|
|
|
SetRequiredQueryIDs(*[]uuid.UUID)
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-23 14:56:20 +00:00
|
|
|
func (s *Service) NormalizeQueryIDs(ctx context.Context, ids RequiredQueryIDs) error {
|
2025-01-20 13:31:48 +00:00
|
|
|
if ids == nil || ids.GetRequiredQueryIDs() == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ide := ids.GetRequiredQueryIDs()
|
|
|
|
|
|
|
|
|
|
if len(*ide) == 0 {
|
|
|
|
|
ids.SetRequiredQueryIDs(nil)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-23 14:56:20 +00:00
|
|
|
dedup := validation.DeduplicateArray(*ide)
|
|
|
|
|
ids.SetRequiredQueryIDs(&dedup)
|
|
|
|
|
|
|
|
|
|
dbids := database.MustToDBUUIDArray(dedup)
|
2025-01-20 13:31:48 +00:00
|
|
|
|
|
|
|
|
exist, err := s.db.Queries.AllQueriesExist(ctx, dbids)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
} else if !exist {
|
|
|
|
|
return errors.New("not all required ids are present")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-29 11:52:37 +00:00
|
|
|
func (s *Service) normalizeActiveVersion(current *Query, entity *resultprocessor.Update) error {
|
2025-01-20 13:31:48 +00:00
|
|
|
if current == nil {
|
|
|
|
|
return errors.New("current query required")
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-23 14:56:20 +00:00
|
|
|
if entity == nil {
|
2025-01-20 13:31:48 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-23 14:56:20 +00:00
|
|
|
err := validation.NormalizeInClosedInterval(&entity.ActiveVersion, current.ActiveVersion, 1, current.LatestVersion+1)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2025-01-20 13:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|