2025-01-20 13:31:48 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"queryorchestration/internal/database"
|
|
|
|
|
queryprocessor "queryorchestration/internal/query/processor"
|
|
|
|
|
"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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) normalizeQueryIDs(ctx context.Context, ids RequiredQueryIDs) error {
|
|
|
|
|
if ids == nil || ids.GetRequiredQueryIDs() == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ide := ids.GetRequiredQueryIDs()
|
|
|
|
|
|
|
|
|
|
if len(*ide) == 0 {
|
|
|
|
|
ids.SetRequiredQueryIDs(nil)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbids := database.MustToDBUUIDArray(*ide)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) normalizeActiveVersion(current *Query, entity *queryprocessor.Update) error {
|
|
|
|
|
if current == nil {
|
|
|
|
|
return errors.New("current query required")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if entity == nil || entity.ActiveVersion == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if entity.ActiveVersion == ¤t.ActiveVersion {
|
|
|
|
|
entity.ActiveVersion = nil
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if *entity.ActiveVersion < 1 || *entity.ActiveVersion > current.LatestVersion+1 {
|
|
|
|
|
return fmt.Errorf("active version must be in the range: 1 <= activeVersion <= %d", current.LatestVersion+1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|