fa95d733ca
Add short tests and Tidy internal directories * complete the tasks
74 lines
1.9 KiB
Go
74 lines
1.9 KiB
Go
package queryqueue
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"queryorchestration/internal/database"
|
|
"queryorchestration/internal/database/repository"
|
|
queryprocessor "queryorchestration/internal/query/processor"
|
|
"queryorchestration/internal/query/result"
|
|
contextfull "queryorchestration/internal/query/types/contextFull"
|
|
jsonextractor "queryorchestration/internal/query/types/jsonExtractor"
|
|
)
|
|
|
|
func (q *Queue) setResult(ctx context.Context, qu *queryprocessor.Query, resultValues []result.Value) error {
|
|
processor, err := q.getProcessor(qu.Type)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
value, err := processor.Process(ctx, qu, resultValues)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
id, err := result.Store(ctx, q.db.Queries, &result.ResultStore{
|
|
QueryID: qu.ID,
|
|
DocumentID: q.documentId,
|
|
Value: value,
|
|
CleanVersion: q.cleanVersion,
|
|
TextVersion: q.textVersion,
|
|
QueryVersion: qu.Version,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
q.results = append(q.results, &result.Result{
|
|
ID: id,
|
|
QueryID: qu.ID,
|
|
QueryVersion: qu.Version,
|
|
})
|
|
|
|
return nil
|
|
}
|
|
|
|
func (q *Queue) getProcessor(queryType queryprocessor.Type) (queryprocessor.Processor, error) {
|
|
switch queryType {
|
|
case queryprocessor.TypeJsonExtractor:
|
|
return jsonextractor.NewExtractor(q.db), nil
|
|
case queryprocessor.TypeContextFull:
|
|
return contextfull.NewExtractor(), nil
|
|
default:
|
|
return nil, fmt.Errorf("attempting to process invalid query type")
|
|
}
|
|
}
|
|
|
|
func (q *Queue) getResultValue(res *repository.ListResultValuesByIDRow) (result.Value, error) {
|
|
var queryType queryprocessor.Type
|
|
for _, qu := range q.collectorQueries {
|
|
if qu.ID == database.MustToUUID(res.Queryid) {
|
|
queryType = qu.Type
|
|
}
|
|
}
|
|
|
|
switch queryType {
|
|
case queryprocessor.TypeJsonExtractor:
|
|
return jsonextractor.NewResult(res.Value), nil
|
|
case queryprocessor.TypeContextFull:
|
|
return contextfull.NewResult(res.Value), nil
|
|
default:
|
|
return nil, fmt.Errorf("attempting to process invalid query type")
|
|
}
|
|
}
|