Merged in feature/jobcrud (pull request #35)

Job CRUD + Collector Integration

* startedcreate

* openapispec

* createjobintegration

* jobgetcontroller

* jobcreateclient

* updatejob

* job

* collector
This commit is contained in:
Michael McGuinness
2025-01-24 14:52:56 +00:00
parent 5b7160fe44
commit 4ec1d51a12
40 changed files with 1775 additions and 403 deletions
+168 -82
View File
@@ -16,6 +16,7 @@ import (
"github.com/getkin/kin-openapi/openapi3"
"github.com/labstack/echo/v4"
"github.com/oapi-codegen/runtime"
openapi_types "github.com/oapi-codegen/runtime/types"
)
// Defines values for ExportStatus.
@@ -61,7 +62,7 @@ type ClientUpdate struct {
// ExportDetails Payload for export trigger response.
type ExportDetails struct {
// JobId The job id relative to the export.
JobId *string `json:"job_id,omitempty"`
JobId *openapi_types.UUID `json:"job_id,omitempty"`
// OutputLocation The location in which the export zip file will be found.
OutputLocation *string `json:"output_location,omitempty"`
@@ -88,7 +89,7 @@ type ExportTrigger struct {
} `json:"ingestion_filters,omitempty"`
// JobId The job id of the query results to be exported.
JobId string `json:"job_id"`
JobId openapi_types.UUID `json:"job_id"`
}
// FieldFilter Filtering a column
@@ -109,7 +110,19 @@ type FieldFilterCondition string
// IdMessage defines model for IdMessage.
type IdMessage struct {
// Id Unique identifier for entity.
Id string `json:"id"`
Id openapi_types.UUID `json:"id"`
}
// Job defines model for Job.
type Job struct {
// CanSync Specifies whether the job is actively syncing
CanSync bool `json:"can_sync"`
// ClientId The client id the job belongs to
ClientId openapi_types.UUID `json:"client_id"`
// Id The job id
Id openapi_types.UUID `json:"id"`
}
// JobClient defines model for JobClient.
@@ -118,7 +131,7 @@ type JobClient struct {
CanSync bool `json:"can_sync"`
// Id The client id
Id string `json:"id"`
Id openapi_types.UUID `json:"id"`
// Name The client name
Name string `json:"name"`
@@ -133,7 +146,7 @@ type JobCollector struct {
Fields []JobCollectorField `json:"fields"`
// JobId The ID of the associated job.
JobId string `json:"job_id"`
JobId openapi_types.UUID `json:"job_id"`
// LatestVersion The latest version of the collector.
LatestVersion int32 `json:"latest_version"`
@@ -151,7 +164,7 @@ type JobCollectorField struct {
Name string `json:"name"`
// QueryId The query id that will populate the result.
QueryId string `json:"query_id"`
QueryId openapi_types.UUID `json:"query_id"`
}
// JobCollectorUpdate Payload for updating a JobCollector.
@@ -169,6 +182,18 @@ type JobCollectorUpdate struct {
MinimumTextVersion *int32 `json:"minimum_text_version,omitempty"`
}
// JobCreate defines model for JobCreate.
type JobCreate struct {
// ClientId The client id the job belongs to
ClientId openapi_types.UUID `json:"client_id"`
}
// JobUpdate defines model for JobUpdate.
type JobUpdate struct {
// CanSync Specifies whether the job is actively syncing
CanSync *bool `json:"can_sync,omitempty"`
}
// ListQueries defines model for ListQueries.
type ListQueries struct {
// Queries List of queries.
@@ -184,13 +209,13 @@ type Query struct {
Config *string `json:"config,omitempty"`
// Id Unique identifier for the query.
Id string `json:"id"`
Id openapi_types.UUID `json:"id"`
// LatestVersion The latest version of the query.
LatestVersion int32 `json:"latest_version"`
// RequiredQueries List of required query IDs.
RequiredQueries *[]string `json:"required_queries,omitempty"`
RequiredQueries *[]openapi_types.UUID `json:"required_queries,omitempty"`
// Type Specifies the type of the query.
Type QueryType `json:"type"`
@@ -202,7 +227,7 @@ type QueryCreate struct {
Config *string `json:"config,omitempty"`
// RequiredQueries List of required query IDs.
RequiredQueries *[]string `json:"required_queries,omitempty"`
RequiredQueries *[]openapi_types.UUID `json:"required_queries,omitempty"`
// Type Specifies the type of the query.
Type QueryType `json:"type"`
@@ -211,7 +236,7 @@ type QueryCreate struct {
// QueryTestRequest defines model for QueryTestRequest.
type QueryTestRequest struct {
// DocumentId ID of the document to test against.
DocumentId string `json:"document_id"`
DocumentId openapi_types.UUID `json:"document_id"`
// QueryVersion Version of the query to use for testing.
QueryVersion int32 `json:"query_version"`
@@ -235,7 +260,7 @@ type QueryUpdate struct {
Config *string `json:"config,omitempty"`
// RequiredQueries Updated list of required query IDs.
RequiredQueries *[]string `json:"required_queries,omitempty"`
RequiredQueries *[]openapi_types.UUID `json:"required_queries,omitempty"`
}
// CreateClientJSONRequestBody defines body for CreateClient for application/json ContentType.
@@ -244,9 +269,15 @@ type CreateClientJSONRequestBody = ClientCreate
// UpdateClientJSONRequestBody defines body for UpdateClient for application/json ContentType.
type UpdateClientJSONRequestBody = ClientUpdate
// CreateJobJSONRequestBody defines body for CreateJob for application/json ContentType.
type CreateJobJSONRequestBody = JobCreate
// TriggerExportJSONRequestBody defines body for TriggerExport for application/json ContentType.
type TriggerExportJSONRequestBody = ExportTrigger
// UpdateJobJSONRequestBody defines body for UpdateJob for application/json ContentType.
type UpdateJobJSONRequestBody = JobUpdate
// UpdateJobCollectorByJobIdJSONRequestBody defines body for UpdateJobCollectorByJobId for application/json ContentType.
type UpdateJobCollectorByJobIdJSONRequestBody = JobCollectorUpdate
@@ -262,41 +293,50 @@ type TestQueryJSONRequestBody = QueryTestRequest
// ServerInterface represents all server handlers.
type ServerInterface interface {
// Create a new client
// (POST /clients)
// (POST /client)
CreateClient(ctx echo.Context) error
// Get a client by ID
// (GET /clients/{id})
GetClient(ctx echo.Context, id string) error
// (GET /client/{id})
GetClient(ctx echo.Context, id openapi_types.UUID) error
// Update a client
// (PATCH /clients/{id})
UpdateClient(ctx echo.Context, id string) error
// (PATCH /client/{id})
UpdateClient(ctx echo.Context, id openapi_types.UUID) error
// Create a new job
// (POST /job)
CreateJob(ctx echo.Context) error
// Trigger an export
// (POST /job/export)
TriggerExport(ctx echo.Context) error
// Check export state.
// (GET /job/export/{id})
ExportState(ctx echo.Context, id string) error
ExportState(ctx echo.Context, id openapi_types.UUID) error
// Get a job by ID
// (GET /job/{id})
GetJob(ctx echo.Context, id openapi_types.UUID) error
// Update a job
// (PATCH /job/{id})
UpdateJob(ctx echo.Context, id openapi_types.UUID) error
// Get a job collector by ID
// (GET /job/{id}/collector)
GetJobCollectorByJobId(ctx echo.Context, id string) error
GetJobCollectorByJobId(ctx echo.Context, id openapi_types.UUID) error
// Update a job collector
// (PATCH /job/{id}/collector)
UpdateJobCollectorByJobId(ctx echo.Context, id string) error
UpdateJobCollectorByJobId(ctx echo.Context, id openapi_types.UUID) error
// List queries
// (GET /queries)
// (GET /query)
ListQueries(ctx echo.Context) error
// Create a new query
// (POST /queries)
// (POST /query)
CreateQuery(ctx echo.Context) error
// Get a query by ID
// (GET /queries/{id})
GetQuery(ctx echo.Context, id string) error
// (GET /query/{id})
GetQuery(ctx echo.Context, id openapi_types.UUID) error
// Update a query
// (PATCH /queries/{id})
UpdateQuery(ctx echo.Context, id string) error
// (PATCH /query/{id})
UpdateQuery(ctx echo.Context, id openapi_types.UUID) error
// Test a query
// (POST /queries/{id}/test)
TestQuery(ctx echo.Context, id string) error
// (POST /query/{id}/test)
TestQuery(ctx echo.Context, id openapi_types.UUID) error
}
// ServerInterfaceWrapper converts echo contexts to parameters.
@@ -317,7 +357,7 @@ func (w *ServerInterfaceWrapper) CreateClient(ctx echo.Context) error {
func (w *ServerInterfaceWrapper) GetClient(ctx echo.Context) error {
var err error
// ------------- Path parameter "id" -------------
var id string
var id openapi_types.UUID
err = runtime.BindStyledParameterWithOptions("simple", "id", ctx.Param("id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
@@ -333,7 +373,7 @@ func (w *ServerInterfaceWrapper) GetClient(ctx echo.Context) error {
func (w *ServerInterfaceWrapper) UpdateClient(ctx echo.Context) error {
var err error
// ------------- Path parameter "id" -------------
var id string
var id openapi_types.UUID
err = runtime.BindStyledParameterWithOptions("simple", "id", ctx.Param("id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
@@ -345,6 +385,15 @@ func (w *ServerInterfaceWrapper) UpdateClient(ctx echo.Context) error {
return err
}
// CreateJob converts echo context to params.
func (w *ServerInterfaceWrapper) CreateJob(ctx echo.Context) error {
var err error
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.CreateJob(ctx)
return err
}
// TriggerExport converts echo context to params.
func (w *ServerInterfaceWrapper) TriggerExport(ctx echo.Context) error {
var err error
@@ -358,7 +407,7 @@ func (w *ServerInterfaceWrapper) TriggerExport(ctx echo.Context) error {
func (w *ServerInterfaceWrapper) ExportState(ctx echo.Context) error {
var err error
// ------------- Path parameter "id" -------------
var id string
var id openapi_types.UUID
err = runtime.BindStyledParameterWithOptions("simple", "id", ctx.Param("id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
@@ -370,11 +419,43 @@ func (w *ServerInterfaceWrapper) ExportState(ctx echo.Context) error {
return err
}
// GetJob converts echo context to params.
func (w *ServerInterfaceWrapper) GetJob(ctx echo.Context) error {
var err error
// ------------- Path parameter "id" -------------
var id openapi_types.UUID
err = runtime.BindStyledParameterWithOptions("simple", "id", ctx.Param("id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter id: %s", err))
}
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.GetJob(ctx, id)
return err
}
// UpdateJob converts echo context to params.
func (w *ServerInterfaceWrapper) UpdateJob(ctx echo.Context) error {
var err error
// ------------- Path parameter "id" -------------
var id openapi_types.UUID
err = runtime.BindStyledParameterWithOptions("simple", "id", ctx.Param("id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter id: %s", err))
}
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.UpdateJob(ctx, id)
return err
}
// GetJobCollectorByJobId converts echo context to params.
func (w *ServerInterfaceWrapper) GetJobCollectorByJobId(ctx echo.Context) error {
var err error
// ------------- Path parameter "id" -------------
var id string
var id openapi_types.UUID
err = runtime.BindStyledParameterWithOptions("simple", "id", ctx.Param("id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
@@ -390,7 +471,7 @@ func (w *ServerInterfaceWrapper) GetJobCollectorByJobId(ctx echo.Context) error
func (w *ServerInterfaceWrapper) UpdateJobCollectorByJobId(ctx echo.Context) error {
var err error
// ------------- Path parameter "id" -------------
var id string
var id openapi_types.UUID
err = runtime.BindStyledParameterWithOptions("simple", "id", ctx.Param("id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
@@ -424,7 +505,7 @@ func (w *ServerInterfaceWrapper) CreateQuery(ctx echo.Context) error {
func (w *ServerInterfaceWrapper) GetQuery(ctx echo.Context) error {
var err error
// ------------- Path parameter "id" -------------
var id string
var id openapi_types.UUID
err = runtime.BindStyledParameterWithOptions("simple", "id", ctx.Param("id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
@@ -440,7 +521,7 @@ func (w *ServerInterfaceWrapper) GetQuery(ctx echo.Context) error {
func (w *ServerInterfaceWrapper) UpdateQuery(ctx echo.Context) error {
var err error
// ------------- Path parameter "id" -------------
var id string
var id openapi_types.UUID
err = runtime.BindStyledParameterWithOptions("simple", "id", ctx.Param("id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
@@ -456,7 +537,7 @@ func (w *ServerInterfaceWrapper) UpdateQuery(ctx echo.Context) error {
func (w *ServerInterfaceWrapper) TestQuery(ctx echo.Context) error {
var err error
// ------------- Path parameter "id" -------------
var id string
var id openapi_types.UUID
err = runtime.BindStyledParameterWithOptions("simple", "id", ctx.Param("id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
@@ -496,63 +577,68 @@ func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL
Handler: si,
}
router.POST(baseURL+"/clients", wrapper.CreateClient)
router.GET(baseURL+"/clients/:id", wrapper.GetClient)
router.PATCH(baseURL+"/clients/:id", wrapper.UpdateClient)
router.POST(baseURL+"/client", wrapper.CreateClient)
router.GET(baseURL+"/client/:id", wrapper.GetClient)
router.PATCH(baseURL+"/client/:id", wrapper.UpdateClient)
router.POST(baseURL+"/job", wrapper.CreateJob)
router.POST(baseURL+"/job/export", wrapper.TriggerExport)
router.GET(baseURL+"/job/export/:id", wrapper.ExportState)
router.GET(baseURL+"/job/:id", wrapper.GetJob)
router.PATCH(baseURL+"/job/:id", wrapper.UpdateJob)
router.GET(baseURL+"/job/:id/collector", wrapper.GetJobCollectorByJobId)
router.PATCH(baseURL+"/job/:id/collector", wrapper.UpdateJobCollectorByJobId)
router.GET(baseURL+"/queries", wrapper.ListQueries)
router.POST(baseURL+"/queries", wrapper.CreateQuery)
router.GET(baseURL+"/queries/:id", wrapper.GetQuery)
router.PATCH(baseURL+"/queries/:id", wrapper.UpdateQuery)
router.POST(baseURL+"/queries/:id/test", wrapper.TestQuery)
router.GET(baseURL+"/query", wrapper.ListQueries)
router.POST(baseURL+"/query", wrapper.CreateQuery)
router.GET(baseURL+"/query/:id", wrapper.GetQuery)
router.PATCH(baseURL+"/query/:id", wrapper.UpdateQuery)
router.POST(baseURL+"/query/:id/test", wrapper.TestQuery)
}
// Base64 encoded, gzipped, json marshaled Swagger object
var swaggerSpec = []string{
"H4sIAAAAAAAC/+RbW2/buBL+K4TOeTTsdLtPfusm6cJFz6anSQ8WWAQGTY5sZilSISmn3sD//YAX3SzR",
"UpxkkWKfGlvkDDnfNxfNuI8JkVkuBQijk/ljoskGMuz+POcMhDlXgA3Yz7mSOSjDwD0VOHPfUtBEsdww",
"KZJ5crMBRNw+5BZMErPLIZkn2igm1sl+P0kU3BdMAU3mf3gpt9UquboDYpL9JCj/ltNe5QSLpd4J0j3A",
"IkWmPgPTCHMuH5hYI0wM2wKy23R9rpWUHLCwKk+/Uef0l99zqcwFGMy47sr8gndcYopSqRC4pcgotl6D",
"Qgp0LoWGa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WHm1kStrlti3p",
"Hx3gC4SRrNl83vCaLHrVeJQzkk9b2gSTgkelesxRM79/kxJgwOkMfbkB9jtSgOVK2aDiHqvYlNF4ONKk",
"Q/UMBsbM0c13U2HDbKgmMUBKymc1YZivqmc5QbQdKoVPODymJ/A+KwdXiI6QdwvLn1+UuWvTkxVZEcXH",
"K8dCsXSUKuykt2EJ5jFp1F+NUDvA8FYGAvpXl1ll6h5QY42asc+prgKGG7O0OkmMsrAakICtE85YvYd/",
"njuWU/sds2ww/bS/lvEiGfvcVG4qVP2US2/6bgDuavgAj2zAcuyb5cqe4KTljUmzx/cK3Lba8hkO3MmJ",
"cUrwow7wczy72urYPj6AdHVvr8b3Ms4GgPui4dXr86LZlKu/sXWvXJniBhSnbVPXm4AjOnxdTYe/vzve",
"wcvmabyWDJrItVTuSxAKw7hZjRgw4HCLl7ZnH2+xtzmOd9WbV6/pQIRWGfmQe++BDRi1dLMnlLto6tN5",
"tbLrSI7l2+ohdEcpe3vcCdNsiU4jvbZ9avipp2yy/NXptObjA9Lo6K79zJtGtOaYmbKHL2WGPMtXZ6bo",
"Jr8aBnWmh6vvwHLMD65trXLXZ6bBTNH8o5OjGzTQZnQ+1bv1w0n10vp997sjst04GP2DxOckCieon3Z2",
"DqhdN8E+K5nkrGyIgq3sciWiebQxJtPz2YxmfOr/Wm3K5Ha2exNZtnhtx/I+FfbW+Jd3kBAj/RW8rtja",
"vII/TIaJuZNxTUbtwHmGgKqGbYpq1bBDZVaFlxfWwH+oFLwSq0lpXrAdbg//DwAA//8wRccXljoAAA==",
}
// GetSwagger returns the content of the embedded swagger specification file
+8 -18
View File
@@ -5,8 +5,8 @@ import (
"net/http"
"queryorchestration/internal/client"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/oapi-codegen/runtime/types"
)
func (s *Controllers) CreateClient(ctx echo.Context) error {
@@ -21,41 +21,31 @@ func (s *Controllers) CreateClient(ctx echo.Context) error {
}
return ctx.JSON(http.StatusCreated, IdMessage{
Id: id.String(),
Id: id,
})
}
func (s *Controllers) GetClient(ctx echo.Context, id string) error {
uid, err := uuid.Parse(id)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID")
}
client, err := s.svc.Client.Get(ctx.Request().Context(), uid)
func (s *Controllers) GetClient(ctx echo.Context, id types.UUID) error {
client, err := s.svc.Client.Get(ctx.Request().Context(), id)
if err != nil {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Unable to get client: %s", err))
}
return ctx.JSON(http.StatusOK, JobClient{
Id: client.ID.String(),
Id: client.ID,
Name: client.Name,
CanSync: client.CanSync,
})
}
func (s *Controllers) UpdateClient(ctx echo.Context, id string) error {
func (s *Controllers) UpdateClient(ctx echo.Context, id types.UUID) error {
req := ClientUpdate{}
if err := ctx.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err)
}
uid, err := uuid.Parse(id)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID")
}
err = s.svc.Client.Update(ctx.Request().Context(), &client.Update{
ID: uid,
err := s.svc.Client.Update(ctx.Request().Context(), &client.Update{
ID: id,
Name: req.Name,
CanSync: req.CanSync,
})
+3 -3
View File
@@ -88,7 +88,7 @@ func TestGetClient(t *testing.T) {
AddRow(database.MustToDBUUID(id), "client_name", true),
)
err = cons.GetClient(ctx, id.String())
err = cons.GetClient(ctx, id)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
@@ -96,7 +96,7 @@ func TestGetClient(t *testing.T) {
err = json.Unmarshal(rec.Body.Bytes(), &res)
assert.Nil(t, err)
assert.EqualExportedValues(t, queryservice.JobClient{
Id: id.String(),
Id: id,
Name: "client_name",
CanSync: true,
}, res)
@@ -139,7 +139,7 @@ func TestUpdateClient(t *testing.T) {
pool.ExpectExec("name: UpdateClient :exec").WithArgs("client_name", true, database.MustToDBUUID(id)).
WillReturnResult(pgxmock.NewResult("", 1))
err = cons.UpdateClient(ctx, id.String())
err = cons.UpdateClient(ctx, id)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Empty(t, rec.Body.String())
+2
View File
@@ -3,6 +3,7 @@ package queryservice
import (
"queryorchestration/internal/client"
"queryorchestration/internal/export"
"queryorchestration/internal/job"
"queryorchestration/internal/job/collector"
"queryorchestration/internal/query"
@@ -14,6 +15,7 @@ type Services struct {
JobCollector *collector.Service
Query *query.Service
Client *client.Service
Job *job.Service
}
type Controllers struct {
+2 -1
View File
@@ -4,12 +4,13 @@ import (
"net/http"
"github.com/labstack/echo/v4"
"github.com/oapi-codegen/runtime/types"
)
func (s *Controllers) TriggerExport(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, map[string]string{"status": "export triggered"})
}
func (s *Controllers) ExportState(ctx echo.Context, id string) error {
func (s *Controllers) ExportState(ctx echo.Context, id types.UUID) error {
return ctx.JSON(http.StatusOK, map[string]string{"status": "export triggered"})
}
+1 -1
View File
@@ -35,7 +35,7 @@ func TestExportState(t *testing.T) {
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{})
id := uuid.NewString()
id := uuid.New()
err := cons.ExportState(ctx, id)
assert.Nil(t, err)
+56
View File
@@ -0,0 +1,56 @@
package queryservice
import (
"fmt"
"net/http"
"queryorchestration/internal/job"
"github.com/labstack/echo/v4"
"github.com/oapi-codegen/runtime/types"
)
func (s *Controllers) CreateJob(ctx echo.Context) error {
req := JobCreate{}
if err := ctx.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err)
}
id, err := s.svc.Job.Create(ctx.Request().Context(), req.ClientId)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unable to create job: %s", err))
}
return ctx.JSON(http.StatusCreated, IdMessage{
Id: id,
})
}
func (s *Controllers) GetJob(ctx echo.Context, id types.UUID) error {
job, err := s.svc.Job.Get(ctx.Request().Context(), id)
if err != nil {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Unable to find job: %s", err))
}
return ctx.JSON(http.StatusOK, Job{
Id: job.ID,
ClientId: job.ClientID,
CanSync: job.CanSync,
})
}
func (s *Controllers) UpdateJob(ctx echo.Context, id types.UUID) error {
req := JobUpdate{}
if err := ctx.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err)
}
err := s.svc.Job.Update(ctx.Request().Context(), &job.Update{
ID: id,
CanSync: req.CanSync,
})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unable to update job: %s", err))
}
return ctx.NoContent(http.StatusOK)
}
+189
View File
@@ -0,0 +1,189 @@
package queryservice_test
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
queryservice "queryorchestration/api/queryService"
"queryorchestration/internal/client"
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
documentclean "queryorchestration/internal/document_clean"
"queryorchestration/internal/job"
"queryorchestration/internal/job/collector"
"queryorchestration/internal/query"
textextraction "queryorchestration/internal/text_extraction"
"strings"
"testing"
"github.com/go-playground/validator/v10"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/labstack/echo/v4"
"github.com/pashagolub/pgxmock/v3"
"github.com/stretchr/testify/assert"
)
func TestCreateJob(t *testing.T) {
pool, err := pgxmock.NewPool()
if err != nil {
t.Fatalf("failed to open pgxmock database: %v", err)
}
queries := repository.New(pool)
db := &database.Connection{
Queries: queries,
Pool: pool,
}
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
Job: job.New(db, &job.Services{
Collector: collector.New(db, &collector.Services{
Query: query.New(db),
DocumentClean: documentclean.New(),
TextExtraction: textextraction.New(),
}),
}),
})
body := queryservice.JobCreate{
ClientId: uuid.New(),
}
bodyBytes, err := json.Marshal(body)
assert.Nil(t, err)
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(bodyBytes)))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
id := uuid.New()
pool.ExpectQuery("-- name: CreateJob :one").WithArgs(database.MustToDBUUID(body.ClientId)).WillReturnRows(
pgxmock.NewRows([]string{"id"}).
AddRow(database.MustToDBUUID(id)),
)
pool.ExpectBeginTx(pgx.TxOptions{})
pool.ExpectQuery("name: CreateCollector :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows(
pgxmock.NewRows([]string{"id"}).
AddRow(database.MustToDBUUID(uuid.New())),
)
pool.ExpectCommit()
err = cons.CreateJob(ctx)
assert.Nil(t, err)
assert.Equal(t, http.StatusCreated, rec.Code)
assert.Equal(t, fmt.Sprintf("{\"id\":\"%s\"}\n", id), rec.Body.String())
}
func TestGetJob(t *testing.T) {
pool, err := pgxmock.NewPool()
if err != nil {
t.Fatalf("failed to open pgxmock database: %v", err)
}
queries := repository.New(pool)
db := &database.Connection{
Queries: queries,
Pool: pool,
}
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
Job: job.New(db, &job.Services{
Client: client.New(db),
}),
})
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
j := job.Job{
ID: uuid.New(),
ClientID: uuid.New(),
CanSync: true,
}
pool.ExpectQuery("name: GetJob :one").WithArgs(database.MustToDBUUID(j.ID)).WillReturnRows(
pgxmock.NewRows([]string{"id", "clientId", "canSync"}).
AddRow(database.MustToDBUUID(j.ID), database.MustToDBUUID(j.ClientID), j.CanSync),
)
pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(j.ClientID)).
WillReturnRows(
pgxmock.NewRows([]string{"id", "name", "canSync"}).
AddRow(database.MustToDBUUID(j.ClientID), "client_name", true),
)
err = cons.GetJob(ctx, j.ID)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
var res queryservice.Job
err = json.Unmarshal(rec.Body.Bytes(), &res)
assert.Nil(t, err)
assert.EqualExportedValues(t, queryservice.Job{
Id: j.ID,
ClientId: j.ClientID,
CanSync: j.CanSync,
}, res)
}
func TestUpdateJob(t *testing.T) {
pool, err := pgxmock.NewPool()
if err != nil {
t.Fatalf("failed to open pgxmock database: %v", err)
}
queries := repository.New(pool)
db := &database.Connection{
Queries: queries,
Pool: pool,
}
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
Job: job.New(db, &job.Services{
Client: client.New(db),
}),
})
j := job.Job{
ID: uuid.New(),
ClientID: uuid.New(),
CanSync: false,
}
ucs := !j.CanSync
body := queryservice.JobUpdate{
CanSync: &ucs,
}
bodyBytes, err := json.Marshal(body)
assert.Nil(t, err)
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(bodyBytes)))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
pool.ExpectQuery("name: GetJob :one").WithArgs(database.MustToDBUUID(j.ID)).WillReturnRows(
pgxmock.NewRows([]string{"id", "clientId", "canSync"}).
AddRow(database.MustToDBUUID(j.ID), database.MustToDBUUID(j.ClientID), j.CanSync),
)
pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(j.ClientID)).
WillReturnRows(
pgxmock.NewRows([]string{"id", "name", "canSync"}).
AddRow(database.MustToDBUUID(j.ClientID), "client_name", true),
)
pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(j.ClientID)).
WillReturnRows(
pgxmock.NewRows([]string{"id", "name", "canSync"}).
AddRow(database.MustToDBUUID(j.ClientID), "client_name", true),
)
pool.ExpectExec("name: UpdateJob :exec").WithArgs(ucs, database.MustToDBUUID(j.ID)).
WillReturnResult(pgxmock.NewResult("", 1))
err = cons.UpdateJob(ctx, j.ID)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Empty(t, rec.Body.String())
}
+9 -23
View File
@@ -7,15 +7,11 @@ import (
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/oapi-codegen/runtime/types"
)
func (s *Controllers) GetJobCollectorByJobId(ctx echo.Context, jobId string) error {
uid, err := uuid.Parse(jobId)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID")
}
coll, err := s.svc.JobCollector.GetByJobID(ctx.Request().Context(), uid)
func (s *Controllers) GetJobCollectorByJobId(ctx echo.Context, jobId types.UUID) error {
coll, err := s.svc.JobCollector.GetByJobID(ctx.Request().Context(), jobId)
if err != nil {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Unable to get collector: %s", err))
}
@@ -25,13 +21,13 @@ func (s *Controllers) GetJobCollectorByJobId(ctx echo.Context, jobId string) err
for name, id := range coll.Fields {
fields[index] = JobCollectorField{
Name: name,
QueryId: id.String(),
QueryId: id,
}
index++
}
return ctx.JSON(http.StatusOK, JobCollector{
JobId: coll.JobID.String(),
JobId: coll.JobID,
MinimumCleanerVersion: coll.MinCleanVersion,
MinimumTextVersion: coll.MinTextVersion,
ActiveVersion: coll.ActiveVersion,
@@ -40,32 +36,22 @@ func (s *Controllers) GetJobCollectorByJobId(ctx echo.Context, jobId string) err
})
}
func (s *Controllers) UpdateJobCollectorByJobId(ctx echo.Context, jobId string) error {
func (s *Controllers) UpdateJobCollectorByJobId(ctx echo.Context, jobId types.UUID) error {
req := JobCollectorUpdate{}
if err := ctx.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err)
}
uid, err := uuid.Parse(jobId)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID")
}
var fields *map[string]uuid.UUID
if req.Fields != nil {
fields := &map[string]uuid.UUID{}
for _, field := range *req.Fields {
queryId, err := uuid.Parse(field.QueryId)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID")
}
(*fields)[field.Name] = queryId
(*fields)[field.Name] = field.QueryId
}
}
err = s.svc.JobCollector.UpdateByJobId(ctx.Request().Context(), &collector.UpdateParams{
JobID: uid,
err := s.svc.JobCollector.UpdateByJobId(ctx.Request().Context(), &collector.UpdateParams{
JobID: jobId,
ActiveVersion: req.ActiveVersion,
MinCleanVersion: req.MinimumCleanerVersion,
MinTextVersion: req.MinimumTextVersion,
+3 -3
View File
@@ -73,7 +73,7 @@ func TestUpdateJobCollector(t *testing.T) {
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectCommit()
err = cons.UpdateJobCollectorByJobId(ctx, current.JobID.String())
err = cons.UpdateJobCollectorByJobId(ctx, current.JobID)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Empty(t, rec.Body.String())
@@ -115,7 +115,7 @@ func TestGetJobCollectorByJobId(t *testing.T) {
AddRow(database.MustToDBUUID(coll.ID), database.MustToDBUUID(coll.JobID), &coll.MinCleanVersion, &coll.MinTextVersion, int32(1), int32(2), []byte("")),
)
err = cons.GetJobCollectorByJobId(ctx, coll.JobID.String())
err = cons.GetJobCollectorByJobId(ctx, coll.JobID)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
@@ -123,7 +123,7 @@ func TestGetJobCollectorByJobId(t *testing.T) {
err = json.Unmarshal(rec.Body.Bytes(), &res)
assert.Nil(t, err)
assert.EqualExportedValues(t, queryservice.JobCollector{
JobId: coll.JobID.String(),
JobId: coll.JobID,
MinimumCleanerVersion: coll.MinCleanVersion,
MinimumTextVersion: coll.MinTextVersion,
ActiveVersion: int32(1),
+4 -10
View File
@@ -23,14 +23,8 @@ func parseQueries(queries []*query.Query) ([]Query, error) {
}
func parseQuery(query *query.Query) (*Query, error) {
var requiredQueries *[]string
if query.RequiredQueryIDs != nil && len(*query.RequiredQueryIDs) > 0 {
rQ := make([]string, len(*query.RequiredQueryIDs))
for index, id := range *query.RequiredQueryIDs {
rQ[index] = id.String()
}
requiredQueries = &rQ
if query.RequiredQueryIDs != nil && len(*query.RequiredQueryIDs) == 0 {
query.RequiredQueryIDs = nil
}
qt, err := parseQueryType(query.Type)
@@ -39,11 +33,11 @@ func parseQuery(query *query.Query) (*Query, error) {
}
q := &Query{
Id: query.ID.String(),
Id: query.ID,
Type: qt,
ActiveVersion: query.ActiveVersion,
LatestVersion: query.LatestVersion,
RequiredQueries: requiredQueries,
RequiredQueries: query.RequiredQueryIDs,
Config: query.Config,
}
+8 -7
View File
@@ -6,6 +6,7 @@ import (
"testing"
"github.com/google/uuid"
"github.com/oapi-codegen/runtime/types"
"github.com/stretchr/testify/assert"
)
@@ -28,12 +29,12 @@ func TestParseQueries(t *testing.T) {
assert.Len(t, out, len(in))
assert.ElementsMatch(t, []Query{
{
Id: in[0].ID.String(),
Id: in[0].ID,
Type: CONTEXTFULL,
ActiveVersion: 1,
LatestVersion: 2,
RequiredQueries: &[]string{
(*in[0].RequiredQueryIDs)[0].String(),
RequiredQueries: &[]types.UUID{
(*in[0].RequiredQueryIDs)[0],
},
Config: in[0].Config,
},
@@ -55,12 +56,12 @@ func TestParseQuery(t *testing.T) {
out, err := parseQuery(in)
assert.Nil(t, err)
assert.EqualExportedValues(t, Query{
Id: in.ID.String(),
Id: in.ID,
Type: CONTEXTFULL,
ActiveVersion: 1,
LatestVersion: 2,
RequiredQueries: &[]string{
(*in.RequiredQueryIDs)[0].String(),
RequiredQueries: &[]types.UUID{
(*in.RequiredQueryIDs)[0],
},
Config: in.Config,
},
@@ -77,7 +78,7 @@ func TestParseQueryMinimal(t *testing.T) {
out, err := parseQuery(in)
assert.Nil(t, err)
assert.EqualExportedValues(t, Query{
Id: in.ID.String(),
Id: in.ID,
Type: CONTEXTFULL,
ActiveVersion: 1,
LatestVersion: 2,
+12 -40
View File
@@ -6,8 +6,8 @@ import (
"queryorchestration/internal/query"
queryprocessor "queryorchestration/internal/query/processor"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/oapi-codegen/runtime/types"
)
func (s *Controllers) ListQueries(ctx echo.Context) error {
@@ -28,13 +28,8 @@ func (s *Controllers) ListQueries(ctx echo.Context) error {
})
}
func (s *Controllers) GetQuery(ctx echo.Context, id string) error {
uid, err := uuid.Parse(id)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID")
}
query, err := s.svc.Query.Get(ctx.Request().Context(), uid)
func (s *Controllers) GetQuery(ctx echo.Context, id types.UUID) error {
query, err := s.svc.Query.Get(ctx.Request().Context(), id)
if err != nil {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Unable to get query: %s", err))
}
@@ -53,11 +48,6 @@ func (s *Controllers) CreateQuery(ctx echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, err)
}
requiredQueryIDs, err := parseStringToUUIDArray(req.RequiredQueries)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid Required IDs")
}
qt, err := parseSpecQueryType(req.Type)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid Query Type")
@@ -65,7 +55,7 @@ func (s *Controllers) CreateQuery(ctx echo.Context) error {
id, err := s.svc.Query.Create(ctx.Request().Context(), &queryprocessor.Create{
Type: qt,
RequiredQueryIDs: requiredQueryIDs,
RequiredQueryIDs: req.RequiredQueries,
Config: req.Config,
})
if err != nil {
@@ -73,31 +63,21 @@ func (s *Controllers) CreateQuery(ctx echo.Context) error {
}
return ctx.JSON(http.StatusCreated, IdMessage{
Id: id.String(),
Id: id,
})
}
func (s *Controllers) UpdateQuery(ctx echo.Context, id string) error {
func (s *Controllers) UpdateQuery(ctx echo.Context, id types.UUID) error {
req := QueryUpdate{}
if err := ctx.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err)
}
uid, err := uuid.Parse(id)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID")
}
requiredQueryIDs, err := parseStringToUUIDArray(req.RequiredQueries)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid Required IDs")
}
err = s.svc.Query.Update(ctx.Request().Context(), &queryprocessor.Update{
err := s.svc.Query.Update(ctx.Request().Context(), &queryprocessor.Update{
ActiveVersion: req.ActiveVersion,
Config: req.Config,
RequiredQueryIDs: requiredQueryIDs,
ID: uid,
RequiredQueryIDs: req.RequiredQueries,
ID: id,
})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unable to update query: %s", err))
@@ -106,24 +86,16 @@ func (s *Controllers) UpdateQuery(ctx echo.Context, id string) error {
return ctx.NoContent(http.StatusOK)
}
func (s *Controllers) TestQuery(ctx echo.Context, id string) error {
queryId, err := uuid.Parse(id)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid Query ID")
}
func (s *Controllers) TestQuery(ctx echo.Context, id types.UUID) error {
req := QueryTestRequest{}
if err := ctx.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err)
}
docId, err := uuid.Parse(req.DocumentId)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid Doc ID")
}
value, err := s.svc.Query.Test(ctx.Request().Context(), query.Test{
QueryID: queryId,
QueryID: id,
QueryVersion: req.QueryVersion,
DocumentID: docId,
DocumentID: req.DocumentId,
})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unable to test query: %s", err))
+6 -6
View File
@@ -100,7 +100,7 @@ func TestListQueries(t *testing.T) {
assert.NotNil(t, res.Queries)
assert.ElementsMatch(t, res.Queries, []queryservice.Query{
{
Id: id.String(),
Id: id,
Type: queryservice.CONTEXTFULL,
ActiveVersion: 1,
LatestVersion: 2,
@@ -137,7 +137,7 @@ func TestGetQuery(t *testing.T) {
AddRow(database.MustToDBUUID(id), repository.QuerytypeContextFull, int32(1), int32(2), nil, []pgtype.UUID{}),
)
err = cons.GetQuery(ctx, id.String())
err = cons.GetQuery(ctx, id)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
@@ -145,7 +145,7 @@ func TestGetQuery(t *testing.T) {
err = json.Unmarshal(rec.Body.Bytes(), &res)
assert.Nil(t, err)
assert.EqualExportedValues(t, queryservice.Query{
Id: id.String(),
Id: id,
Type: queryservice.CONTEXTFULL,
ActiveVersion: 1,
LatestVersion: 2,
@@ -194,7 +194,7 @@ func TestUpdateQuery(t *testing.T) {
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectCommit()
err = cons.UpdateQuery(ctx, id.String())
err = cons.UpdateQuery(ctx, id)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Empty(t, rec.Body.String())
@@ -216,7 +216,7 @@ func TestTestQuery(t *testing.T) {
})
body := queryservice.QueryTestRequest{
DocumentId: uuid.NewString(),
DocumentId: uuid.New(),
}
bodyBytes, err := json.Marshal(body)
assert.Nil(t, err)
@@ -229,7 +229,7 @@ func TestTestQuery(t *testing.T) {
id := uuid.New()
err = cons.TestQuery(ctx, id.String())
err = cons.TestQuery(ctx, id)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
+6
View File
@@ -7,6 +7,7 @@ import (
"queryorchestration/internal/client"
documentclean "queryorchestration/internal/document_clean"
"queryorchestration/internal/export"
"queryorchestration/internal/job"
"queryorchestration/internal/job/collector"
"queryorchestration/internal/query"
"queryorchestration/internal/server"
@@ -31,12 +32,17 @@ func main() {
DocumentClean: clean,
})
cli := client.New(cfg.Database)
jbb := job.New(cfg.Database, &job.Services{
Collector: col,
Client: cli,
})
services := &queryservice.Services{
Export: exp,
JobCollector: col,
Query: que,
Client: cli,
Job: jbb,
}
cons := queryservice.NewControllers(cfg.Validator, services)
+4 -1
View File
@@ -2,4 +2,7 @@
SELECT id, clientId, canSync FROM jobs WHERE id = $1 LIMIT 1;
-- name: CreateJob :one
INSERT INTO jobs (clientId) VALUES ($1) RETURNING id;
INSERT INTO jobs (clientId) VALUES ($1) RETURNING id;
-- name: UpdateJob :exec
UPDATE jobs SET canSync = $1 WHERE id = $2;
+17
View File
@@ -38,3 +38,20 @@ func (q *Queries) GetJob(ctx context.Context, id pgtype.UUID) (*Job, error) {
err := row.Scan(&i.ID, &i.Clientid, &i.Cansync)
return &i, err
}
const updateJob = `-- name: UpdateJob :exec
UPDATE jobs SET canSync = $1 WHERE id = $2
`
type UpdateJobParams struct {
Cansync bool `db:"cansync"`
ID pgtype.UUID `db:"id"`
}
// UpdateJob
//
// UPDATE jobs SET canSync = $1 WHERE id = $2
func (q *Queries) UpdateJob(ctx context.Context, arg *UpdateJobParams) error {
_, err := q.db.Exec(ctx, updateJob, arg.Cansync, arg.ID)
return err
}
+14
View File
@@ -36,4 +36,18 @@ func TestJob(t *testing.T) {
Clientid: clientId,
Cansync: false,
}, job)
err = queries.UpdateJob(ctx, &repository.UpdateJobParams{
Cansync: true,
ID: id,
})
assert.Nil(t, err)
job, err = queries.GetJob(ctx, id)
assert.Nil(t, err)
assert.EqualExportedValues(t, &repository.Job{
ID: id,
Clientid: clientId,
Cansync: true,
}, job)
}
+13 -11
View File
@@ -14,7 +14,7 @@ type CreateParams struct {
JobID uuid.UUID
MinCleanVersion *int32
MinTextVersion *int32
Fields map[string]uuid.UUID
Fields *map[string]uuid.UUID
}
func (s *Service) Create(ctx context.Context, params *CreateParams) (uuid.UUID, error) {
@@ -35,7 +35,7 @@ type dbCreateParams struct {
JobID pgtype.UUID
MinCleanVersion *int32
MinTextVersion *int32
Fields map[string]pgtype.UUID
Fields *map[string]pgtype.UUID
}
type fields struct {
@@ -102,15 +102,17 @@ func (s *Service) submitCreate(ctx context.Context, params *dbCreateParams) (uui
}
}
for key, field := range params.Fields {
err = qtx.AddCollectorQuery(ctx, &repository.AddCollectorQueryParams{
Collectorid: dbID,
Name: key,
Queryid: field,
Addedversion: latestVersion,
})
if err != nil {
return err
if params.Fields != nil {
for key, field := range *params.Fields {
err = qtx.AddCollectorQuery(ctx, &repository.AddCollectorQueryParams{
Collectorid: dbID,
Name: key,
Queryid: field,
Addedversion: latestVersion,
})
if err != nil {
return err
}
}
}
+3 -3
View File
@@ -38,12 +38,12 @@ func TestCreate(t *testing.T) {
JobID: uuid.New(),
MinCleanVersion: &minCleanV,
MinTextVersion: &minTextV,
Fields: map[string]uuid.UUID{
Fields: &map[string]uuid.UUID{
"example_key": uuid.New(),
},
}
pool.ExpectQuery("name: AllQueriesExist :one").WithArgs(database.MustToDBUUIDArray([]uuid.UUID{create.Fields["example_key"]})).WillReturnRows(
pool.ExpectQuery("name: AllQueriesExist :one").WithArgs(database.MustToDBUUIDArray([]uuid.UUID{(*create.Fields)["example_key"]})).WillReturnRows(
pgxmock.NewRows([]string{"all_exist"}).
AddRow(true),
)
@@ -54,7 +54,7 @@ func TestCreate(t *testing.T) {
)
pool.ExpectExec("name: AddCollectorCodeVersion :exec").WithArgs(database.MustToDBUUID(id), int32(1), *create.MinCleanVersion, *create.MinTextVersion).
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectExec("name: AddCollectorQuery :exec").WithArgs(database.MustToDBUUID(id), "example_key", database.MustToDBUUID(create.Fields["example_key"]), int32(1)).
pool.ExpectExec("name: AddCollectorQuery :exec").WithArgs(database.MustToDBUUID(id), "example_key", database.MustToDBUUID((*create.Fields)["example_key"]), int32(1)).
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectCommit()
+8 -8
View File
@@ -40,12 +40,12 @@ func TestGetCreateParams(t *testing.T) {
JobID: uuid.New(),
MinCleanVersion: &minCleanV,
MinTextVersion: &minTextV,
Fields: map[string]uuid.UUID{
Fields: &map[string]uuid.UUID{
"example_key": uuid.New(),
},
}
pool.ExpectQuery("name: AllQueriesExist :one").WithArgs(database.MustToDBUUIDArray([]uuid.UUID{params.Fields["example_key"]})).WillReturnRows(
pool.ExpectQuery("name: AllQueriesExist :one").WithArgs(database.MustToDBUUIDArray([]uuid.UUID{(*params.Fields)["example_key"]})).WillReturnRows(
pgxmock.NewRows([]string{"all_exist"}).
AddRow(true),
)
@@ -56,13 +56,13 @@ func TestGetCreateParams(t *testing.T) {
JobID: database.MustToDBUUID(params.JobID),
MinCleanVersion: &minCleanV,
MinTextVersion: &minTextV,
Fields: map[string]pgtype.UUID{
"example_key": database.MustToDBUUID(params.Fields["example_key"]),
Fields: &map[string]pgtype.UUID{
"example_key": database.MustToDBUUID((*params.Fields)["example_key"]),
},
}, dbparams)
params.Fields["second_key"] = params.Fields["example_key"]
assert.Len(t, params.Fields, 2)
(*params.Fields)["second_key"] = (*params.Fields)["example_key"]
assert.Len(t, *params.Fields, 2)
_, err = svc.getCreateParams(ctx, &params)
assert.Error(t, err)
}
@@ -94,7 +94,7 @@ func TestSubmitCreate(t *testing.T) {
JobID: database.MustToDBUUID(uuid.New()),
MinCleanVersion: &minCleanV,
MinTextVersion: &minTextV,
Fields: map[string]pgtype.UUID{
Fields: &map[string]pgtype.UUID{
"example_key": database.MustToDBUUID(uuid.New()),
},
}
@@ -106,7 +106,7 @@ func TestSubmitCreate(t *testing.T) {
)
pool.ExpectExec("name: AddCollectorCodeVersion :exec").WithArgs(database.MustToDBUUID(id), int32(1), *params.MinCleanVersion, *params.MinTextVersion).
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectExec("name: AddCollectorQuery :exec").WithArgs(database.MustToDBUUID(id), "example_key", params.Fields["example_key"], int32(1)).
pool.ExpectExec("name: AddCollectorQuery :exec").WithArgs(database.MustToDBUUID(id), "example_key", (*params.Fields)["example_key"], int32(1)).
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectCommit()
+18 -17
View File
@@ -52,13 +52,9 @@ func (s *Service) getUpdateParams(ctx context.Context, current *Collector, param
return nil, err
}
var ufields *map[string]pgtype.UUID
if params.Fields != nil {
ufs, err := s.normalizeFieldsToDB(ctx, *params.Fields)
if err != nil {
return nil, err
}
ufields = &ufs
fs, err := s.normalizeFieldsToDB(ctx, params.Fields)
if err != nil {
return nil, err
}
err = s.normalizeActiveVersion(current, params)
@@ -69,7 +65,7 @@ func (s *Service) getUpdateParams(ctx context.Context, current *Collector, param
if params.ActiveVersion == nil &&
params.MinCleanVersion == nil &&
params.MinTextVersion == nil &&
ufields == nil {
fs == nil {
return nil, errors.New("no changes")
}
@@ -78,7 +74,7 @@ func (s *Service) getUpdateParams(ctx context.Context, current *Collector, param
ActiveVersion: params.ActiveVersion,
MinCleanVersion: params.MinCleanVersion,
MinTextVersion: params.MinTextVersion,
Fields: ufields,
Fields: fs,
}, nil
}
@@ -136,9 +132,15 @@ func (s *Service) normalizeActiveVersion(current *Collector, params *UpdateParam
return nil
}
func (s *Service) normalizeFieldsToDB(ctx context.Context, ofields map[string]uuid.UUID) (map[string]pgtype.UUID, error) {
func (s *Service) normalizeFieldsToDB(ctx context.Context, ofields *map[string]uuid.UUID) (*map[string]pgtype.UUID, error) {
if ofields == nil || *ofields == nil {
return nil, nil
} else if len(*ofields) == 0 {
return nil, nil
}
fids := []uuid.UUID{}
for _, id := range ofields {
for _, id := range *ofields {
fids = append(fids, id)
}
vfields := fields{
@@ -148,17 +150,16 @@ func (s *Service) normalizeFieldsToDB(ctx context.Context, ofields map[string]uu
err := s.svc.Query.NormalizeQueryIDs(ctx, &vfields)
if err != nil {
return nil, err
}
if len(ofields) != len(*vfields.values) {
} else if len(*ofields) != len(*vfields.values) {
return nil, errors.New("duplicate output fields")
}
ufields := map[string]pgtype.UUID{}
for key, value := range ofields {
ufields[key] = database.MustToDBUUID(value)
fs := map[string]pgtype.UUID{}
for name, id := range *ofields {
fs[name] = database.MustToDBUUID(id)
}
return ufields, nil
return &fs, nil
}
func (s *Service) submitUpdate(ctx context.Context, current *Collector, params *dbUpdateParams) error {
+4 -3
View File
@@ -138,6 +138,7 @@ func TestSubmitUpdate(t *testing.T) {
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectExec("name: RemoveCollectorQuery :exec").WithArgs(&rv, database.MustToDBUUID(current.Fields["original_key"]), database.MustToDBUUID(current.ID)).
WillReturnResult(pgxmock.NewResult("", 1))
pool.MatchExpectationsInOrder(false)
pool.ExpectExec("name: AddCollectorQuery :exec").WithArgs(database.MustToDBUUID(current.ID), "example_key", (*params.Fields)["example_key"], int32(2)).
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectExec("name: AddCollectorQuery :exec").WithArgs(database.MustToDBUUID(current.ID), "second_key", (*params.Fields)["second_key"], int32(2)).
@@ -181,9 +182,9 @@ func TestNormalizeFieldsToDB(t *testing.T) {
AddRow(true),
)
dbparams, err := svc.normalizeFieldsToDB(ctx, fields)
dbparams, err := svc.normalizeFieldsToDB(ctx, &fields)
assert.Nil(t, err)
assert.EqualExportedValues(t, map[string]pgtype.UUID{
assert.EqualExportedValues(t, &map[string]pgtype.UUID{
"example_key": database.MustToDBUUID(fields["example_key"]),
}, dbparams)
@@ -194,7 +195,7 @@ func TestNormalizeFieldsToDB(t *testing.T) {
AddRow(true),
)
_, err = svc.normalizeFieldsToDB(ctx, fields)
_, err = svc.normalizeFieldsToDB(ctx, &fields)
assert.Error(t, err)
}
+27
View File
@@ -0,0 +1,27 @@
package job
import (
"context"
"queryorchestration/internal/database"
"queryorchestration/internal/job/collector"
"github.com/google/uuid"
)
func (s *Service) Create(ctx context.Context, clientID uuid.UUID) (uuid.UUID, error) {
did, err := s.db.Queries.CreateJob(ctx, database.MustToDBUUID(clientID))
if err != nil {
return uuid.Nil, err
}
id := database.MustToUUID(did)
_, err = s.svc.Collector.Create(ctx, &collector.CreateParams{
JobID: id,
})
if err != nil {
return uuid.Nil, err
}
return id, nil
}
+53
View File
@@ -0,0 +1,53 @@
package job_test
import (
"context"
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/job"
"queryorchestration/internal/job/collector"
"testing"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/pashagolub/pgxmock/v3"
"github.com/stretchr/testify/assert"
)
func TestCreate(t *testing.T) {
ctx := context.Background()
pool, err := pgxmock.NewPool()
if err != nil {
t.Fatalf("failed to open pgxmock database: %v", err)
}
queries := repository.New(pool)
db := &database.Connection{
Queries: queries,
Pool: pool,
}
svc := job.New(db, &job.Services{
Collector: collector.New(db, &collector.Services{}),
})
job := job.Job{
ID: uuid.New(),
ClientID: uuid.New(),
}
pool.ExpectQuery("name: CreateJob :one").WithArgs(database.MustToDBUUID(job.ClientID)).WillReturnRows(
pgxmock.NewRows([]string{"id"}).
AddRow(database.MustToDBUUID(job.ID)),
)
pool.ExpectBeginTx(pgx.TxOptions{})
pool.ExpectQuery("name: CreateCollector :one").WithArgs(database.MustToDBUUID(job.ID)).WillReturnRows(
pgxmock.NewRows([]string{"id"}).
AddRow(database.MustToDBUUID(uuid.New())),
)
pool.ExpectCommit()
aid, err := svc.Create(ctx, job.ClientID)
assert.Nil(t, err)
assert.Equal(t, job.ID, aid)
}
+31
View File
@@ -0,0 +1,31 @@
package job
import (
"context"
"queryorchestration/internal/database"
"github.com/google/uuid"
)
func (s *Service) Get(ctx context.Context, id uuid.UUID) (*Job, error) {
job, err := s.db.Queries.GetJob(ctx, database.MustToDBUUID(id))
if err != nil {
return nil, err
}
client, err := s.svc.Client.Get(ctx, database.MustToUUID(job.Clientid))
if err != nil {
return nil, err
}
canSync := job.Cansync
if !client.CanSync {
canSync = false
}
return &Job{
ID: id,
ClientID: database.MustToUUID(job.Clientid),
CanSync: canSync,
}, nil
}
+76
View File
@@ -0,0 +1,76 @@
package job_test
import (
"context"
"queryorchestration/internal/client"
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/job"
"queryorchestration/internal/job/collector"
"testing"
"github.com/google/uuid"
"github.com/pashagolub/pgxmock/v3"
"github.com/stretchr/testify/assert"
)
func TestGet(t *testing.T) {
ctx := context.Background()
pool, err := pgxmock.NewPool()
if err != nil {
t.Fatalf("failed to open pgxmock database: %v", err)
}
queries := repository.New(pool)
db := &database.Connection{
Queries: queries,
Pool: pool,
}
svc := job.New(db, &job.Services{
Collector: collector.New(db, &collector.Services{}),
Client: client.New(db),
})
j := job.Job{
ID: uuid.New(),
ClientID: uuid.New(),
CanSync: true,
}
pool.ExpectQuery("name: GetJob :one").WithArgs(database.MustToDBUUID(j.ID)).WillReturnRows(
pgxmock.NewRows([]string{"id", "clientId", "canSync"}).
AddRow(database.MustToDBUUID(j.ID), database.MustToDBUUID(j.ClientID), j.CanSync),
)
pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(j.ClientID)).
WillReturnRows(
pgxmock.NewRows([]string{"id", "name", "canSync"}).
AddRow(database.MustToDBUUID(j.ClientID), "client_name", false),
)
ajob, err := svc.Get(ctx, j.ID)
assert.Nil(t, err)
assert.EqualExportedValues(t, &job.Job{
ID: j.ID,
ClientID: j.ClientID,
CanSync: false,
}, ajob)
pool.ExpectQuery("name: GetJob :one").WithArgs(database.MustToDBUUID(j.ID)).WillReturnRows(
pgxmock.NewRows([]string{"id", "clientId", "canSync"}).
AddRow(database.MustToDBUUID(j.ID), database.MustToDBUUID(j.ClientID), j.CanSync),
)
pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(j.ClientID)).
WillReturnRows(
pgxmock.NewRows([]string{"id", "name", "canSync"}).
AddRow(database.MustToDBUUID(j.ClientID), "client_name", true),
)
ajob, err = svc.Get(ctx, j.ID)
assert.Nil(t, err)
assert.EqualExportedValues(t, &job.Job{
ID: j.ID,
ClientID: j.ClientID,
CanSync: true,
}, ajob)
}
+32
View File
@@ -0,0 +1,32 @@
package job
import (
"queryorchestration/internal/client"
"queryorchestration/internal/database"
"queryorchestration/internal/job/collector"
"github.com/google/uuid"
)
type Job struct {
ID uuid.UUID
ClientID uuid.UUID
CanSync bool
}
type Services struct {
Collector *collector.Service
Client *client.Service
}
type Service struct {
db *database.Connection
svc *Services
}
func New(db *database.Connection, svc *Services) *Service {
return &Service{
db,
svc,
}
}
+26
View File
@@ -0,0 +1,26 @@
package job_test
import (
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/job"
"testing"
"github.com/pashagolub/pgxmock/v3"
"github.com/stretchr/testify/assert"
)
func TestService(t *testing.T) {
pool, err := pgxmock.NewPool()
if err != nil {
t.Fatalf("failed to open pgxmock database: %v", err)
}
queries := repository.New(pool)
db := &database.Connection{
Queries: queries,
Pool: pool,
}
svc := job.New(db, &job.Services{})
assert.NotNil(t, svc)
}
+63
View File
@@ -0,0 +1,63 @@
package job
import (
"context"
"errors"
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
"github.com/google/uuid"
)
type Update struct {
ID uuid.UUID
CanSync *bool
}
func (s *Service) Update(ctx context.Context, update *Update) error {
if update == nil {
return errors.New("Update required")
}
current, err := s.Get(ctx, update.ID)
if err != nil {
return err
}
err = s.normalizeCanSync(ctx, current, update)
if err != nil {
return err
}
if update.CanSync == nil {
return errors.New("no changes")
}
err = s.db.Queries.UpdateJob(ctx, &repository.UpdateJobParams{
ID: database.MustToDBUUID(update.ID),
Cansync: *update.CanSync,
})
if err != nil {
return err
}
return nil
}
func (s *Service) normalizeCanSync(ctx context.Context, current *Job, update *Update) error {
if update.CanSync == nil {
return nil
} else if current.CanSync == *update.CanSync {
update.CanSync = nil
return nil
}
client, err := s.svc.Client.Get(ctx, current.ClientID)
if err != nil {
return err
} else if !client.CanSync && *update.CanSync {
return errors.New("client not allowing sync")
}
return nil
}
+62
View File
@@ -0,0 +1,62 @@
package job_test
import (
"context"
"queryorchestration/internal/client"
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/job"
"testing"
"github.com/google/uuid"
"github.com/pashagolub/pgxmock/v3"
"github.com/stretchr/testify/assert"
)
func TestUpdate(t *testing.T) {
ctx := context.Background()
pool, err := pgxmock.NewPool()
if err != nil {
t.Fatalf("failed to open pgxmock database: %v", err)
}
queries := repository.New(pool)
db := &database.Connection{
Queries: queries,
Pool: pool,
}
svc := job.New(db, &job.Services{
Client: client.New(db),
})
j := job.Job{
ID: uuid.New(),
ClientID: uuid.New(),
CanSync: true,
}
pool.ExpectQuery("name: GetJob :one").WithArgs(database.MustToDBUUID(j.ID)).WillReturnRows(
pgxmock.NewRows([]string{"id", "clientId", "canSync"}).
AddRow(database.MustToDBUUID(j.ID), database.MustToDBUUID(j.ClientID), j.CanSync),
)
pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(j.ClientID)).
WillReturnRows(
pgxmock.NewRows([]string{"id", "name", "canSync"}).
AddRow(database.MustToDBUUID(j.ClientID), "client_name", true),
)
pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(j.ClientID)).
WillReturnRows(
pgxmock.NewRows([]string{"id", "name", "canSync"}).
AddRow(database.MustToDBUUID(j.ClientID), "client_name", true),
)
ucs := !j.CanSync
pool.ExpectExec("name: UpdateJob :exec").WithArgs(ucs, database.MustToDBUUID(j.ID)).
WillReturnResult(pgxmock.NewResult("", 1))
err = svc.Update(ctx, &job.Update{
ID: j.ID,
CanSync: &ucs,
})
assert.Nil(t, err)
}
+93
View File
@@ -0,0 +1,93 @@
package job
import (
"context"
"queryorchestration/internal/client"
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
"testing"
"github.com/google/uuid"
"github.com/pashagolub/pgxmock/v3"
"github.com/stretchr/testify/assert"
)
func TestNormalizeCanSync(t *testing.T) {
ctx := context.Background()
pool, err := pgxmock.NewPool()
if err != nil {
t.Fatalf("failed to open pgxmock database: %v", err)
}
queries := repository.New(pool)
db := &database.Connection{
Queries: queries,
Pool: pool,
}
svc := New(db, &Services{
Client: client.New(db),
})
j := Job{
ID: uuid.New(),
ClientID: uuid.New(),
CanSync: false,
}
update := Update{
ID: j.ID,
}
err = svc.normalizeCanSync(ctx, &j, &update)
assert.Nil(t, err)
assert.Nil(t, update.CanSync)
cs := false
update.CanSync = &cs
err = svc.normalizeCanSync(ctx, &j, &update)
assert.Nil(t, err)
assert.Nil(t, update.CanSync)
pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(j.ClientID)).
WillReturnRows(
pgxmock.NewRows([]string{"id", "name", "canSync"}).
AddRow(database.MustToDBUUID(j.ClientID), "client_name", false),
)
cs = true
update.CanSync = &cs
err = svc.normalizeCanSync(ctx, &j, &update)
assert.Error(t, err)
pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(j.ClientID)).
WillReturnRows(
pgxmock.NewRows([]string{"id", "name", "canSync"}).
AddRow(database.MustToDBUUID(j.ClientID), "client_name", true),
)
cs = false
update.CanSync = &cs
err = svc.normalizeCanSync(ctx, &j, &update)
assert.Nil(t, err)
assert.Nil(t, update.CanSync)
pool.ExpectQuery("name: GetClient :one").WithArgs(database.MustToDBUUID(j.ClientID)).
WillReturnRows(
pgxmock.NewRows([]string{"id", "name", "canSync"}).
AddRow(database.MustToDBUUID(j.ClientID), "client_name", true),
)
cs = true
update.CanSync = &cs
err = svc.normalizeCanSync(ctx, &j, &update)
assert.Nil(t, err)
assert.True(t, *update.CanSync)
j.CanSync = true
cs = true
update.CanSync = &cs
err = svc.normalizeCanSync(ctx, &j, &update)
assert.Nil(t, err)
assert.Nil(t, update.CanSync)
}
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -37,7 +37,7 @@ tasks:
- task build
- task lint
- task test:unit
- task test:integration
- task test:integration:nocache
build:
deps:
- generate
+3
View File
@@ -52,6 +52,9 @@ tasks:
cmds:
- task generate
- task docker:build
- task test:integration:nocache
integration:nocache:
cmds:
- go test -count=1 -v ./test/...
integration:nobuild:
cmds:
+163 -61
View File
@@ -10,6 +10,8 @@ servers:
tags:
- name: ClientService
description: Operations related to clients
- name: JobService
description: Operations related to jobs
- name: JobCollectorService
description: Operations related to job collectors
- name: QueryService
@@ -18,7 +20,7 @@ tags:
description: Operations related to exports
paths:
/clients:
/client:
post:
operationId: createClient
tags:
@@ -41,20 +43,21 @@ paths:
'400':
description: Invalid request body.
/clients/{id}:
/client/{id}:
parameters:
- in: path
name: id
required: true
schema:
type: string
format: uuid
description: The ID of the client to retrieve.
get:
operationId: getClient
tags:
- ClientService
summary: Get a client by ID
description: Retrieves a specific client by its ID.
parameters:
- in: path
name: id
required: true
schema:
type: string
description: The ID of the client to retrieve.
responses:
'200':
description: Client details.
@@ -72,13 +75,6 @@ paths:
- ClientService
summary: Update a client
description: Updates an existing client with new details.
parameters:
- in: path
name: id
required: true
schema:
type: string
description: The ID of the client to update.
requestBody:
required: true
content:
@@ -93,7 +89,7 @@ paths:
'404':
description: Client not found
/queries:
/query:
get:
operationId: listQueries
tags:
@@ -133,20 +129,21 @@ paths:
'400':
description: Invalid request body.
/queries/{id}:
/query/{id}:
parameters:
- in: path
name: id
required: true
schema:
type: string
format: uuid
description: The ID of the query.
get:
operationId: getQuery
tags:
- QueryService
summary: Get a query by ID
description: Retrieves a specific query by its ID.
parameters:
- in: path
name: id
required: true
schema:
type: string
description: The ID of the query to retrieve.
responses:
'200':
description: Query details.
@@ -164,13 +161,6 @@ paths:
- QueryService
summary: Update a query
description: Updates an existing query with new details.
parameters:
- in: path
name: id
required: true
schema:
type: string
description: The ID of the query to update.
requestBody:
required: true
content:
@@ -185,20 +175,21 @@ paths:
'404':
description: Query not found.
/queries/{id}/test:
/query/{id}/test:
parameters:
- in: path
name: id
required: true
schema:
type: string
format: uuid
description: The ID of the query.
post:
operationId: testQuery
tags:
- QueryService
summary: Test a query
description: Executes a test run of a query with the provided parameters.
parameters:
- in: path
name: id
required: true
schema:
type: string
description: The ID of the query to test.
requestBody:
required: true
content:
@@ -214,21 +205,91 @@ paths:
$ref: '#/components/schemas/QueryTestResponse'
'400':
description: Invalid request body.
/job:
post:
operationId: createJob
tags:
- JobService
summary: Create a new job
description: Creates a new job with the provided details.
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/JobCreate'
responses:
'201':
description: Job created successfully.
content:
application/json:
schema:
$ref: '#/components/schemas/IdMessage'
'400':
description: Invalid request body.
/job/{id}:
parameters:
- in: path
name: id
required: true
schema:
type: string
format: uuid
description: The job ID.
get:
operationId: getJob
tags:
- JobService
summary: Get a job by ID
description: Retrieves a specific job by its ID.
responses:
'200':
description: Job details.
content:
application/json:
schema:
$ref: '#/components/schemas/Job'
'400':
description: Invalid request parameters.
'404':
description: Job not found.
patch:
operationId: updateJob
tags:
- JobService
summary: Update a job
description: Updates an existing job with new details.
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/JobUpdate'
responses:
'204':
description: Job updated successfully.
'400':
description: Invalid request body.
'404':
description: Job not found.
/job/{id}/collector:
parameters:
- in: path
name: id
required: true
schema:
type: string
format: uuid
description: The job ID for the collector.
get:
operationId: getJobCollectorByJobId
tags:
- JobCollectorService
summary: Get a job collector by ID
description: Retrieves a specific job collector by its ID.
parameters:
- in: path
name: id
required: true
schema:
type: string
description: The job ID for the collector.
responses:
'200':
description: Job collector details.
@@ -244,13 +305,6 @@ paths:
- JobCollectorService
summary: Update a job collector
description: Updates an existing job collector with new details.
parameters:
- in: path
name: id
required: true
schema:
type: string
description: The ID of the job collector to update.
requestBody:
required: true
content:
@@ -291,19 +345,20 @@ paths:
description: Job not found.
/job/export/{id}:
parameters:
- in: path
name: id
required: true
schema:
type: string
format: uuid
description: The ID of the export.
get:
operationId: exportState
tags:
- ExportService
summary: Check export state.
description: Checks the current state of an export.
parameters:
- in: path
name: id
required: true
schema:
type: string
description: The ID of the export.
responses:
'200':
description: Export has been completed.
@@ -329,6 +384,7 @@ components:
properties:
id:
type: string
format: uuid
description: Unique identifier for the query.
type:
$ref: '#/components/schemas/QueryType'
@@ -348,6 +404,7 @@ components:
type: array
items:
type: string
format: uuid
description: List of required query IDs.
required:
- id
@@ -379,6 +436,7 @@ components:
type: array
items:
type: string
format: uuid
description: List of required query IDs.
required:
- type
@@ -397,6 +455,7 @@ components:
type: array
items:
type: string
format: uuid
description: Updated list of required query IDs.
QueryTestRequest:
@@ -404,6 +463,7 @@ components:
properties:
document_id:
type: string
format: uuid
description: ID of the document to test against.
query_version:
type: integer
@@ -427,6 +487,7 @@ components:
properties:
id:
type: string
format: uuid
description: The client id
name:
type: string
@@ -439,6 +500,42 @@ components:
- name
- can_sync
Job:
type: object
properties:
id:
type: string
format: uuid
description: The job id
client_id:
type: string
format: uuid
description: The client id the job belongs to
can_sync:
type: boolean
description: Specifies whether the job is actively syncing
required:
- id
- client_id
- can_sync
JobCreate:
type: object
properties:
client_id:
type: string
format: uuid
description: The client id the job belongs to
required:
- client_id
JobUpdate:
type: object
properties:
can_sync:
type: boolean
description: Specifies whether the job is actively syncing
ClientCreate:
type: object
properties:
@@ -463,6 +560,7 @@ components:
properties:
id:
type: string
format: uuid
description: Unique identifier for entity.
required:
- id
@@ -472,6 +570,7 @@ components:
properties:
job_id:
type: string
format: uuid
description: The ID of the associated job.
active_version:
type: integer
@@ -535,6 +634,7 @@ components:
description: The output field name.
query_id:
type: string
format: uuid
description: The query id that will populate the result.
required:
- name
@@ -545,6 +645,7 @@ components:
properties:
job_id:
type: string
format: uuid
description: The job id of the query results to be exported.
ingestion_filters:
type: object
@@ -610,6 +711,7 @@ components:
properties:
job_id:
type: string
format: uuid
description: The job id relative to the export.
status:
$ref: '#/components/schemas/ExportStatus'
+52
View File
@@ -0,0 +1,52 @@
package integration_test
import (
"context"
"queryorchestration/internal/test"
queryservice "queryorchestration/pkg/queryService"
"testing"
"github.com/stretchr/testify/assert"
)
func TestJob(t *testing.T) {
ctx := context.Background()
address, cleanup := test.CreateAPIWithDependencies(t, ctx, "queryService")
defer cleanup()
client, err := queryservice.NewClientWithResponses(address)
assert.Nil(t, err)
clientRes, err := client.CreateClientWithResponse(ctx, queryservice.ClientCreate{
Name: "example_name",
})
assert.Nil(t, err)
idRes, err := client.CreateJobWithResponse(ctx, queryservice.JobCreate{
ClientId: clientRes.JSON201.Id,
})
assert.Nil(t, err)
assert.NotNil(t, idRes)
assert.NotNil(t, idRes.JSON201)
assert.NotNil(t, idRes.JSON201.Id)
id := idRes.JSON201.Id
jobRes, err := client.GetJobWithResponse(ctx, id)
assert.Nil(t, err)
assert.Equal(t, id, jobRes.JSON200.Id)
assert.Equal(t, clientRes.JSON201.Id, jobRes.JSON200.ClientId)
assert.False(t, jobRes.JSON200.CanSync)
updateCanSync := !jobRes.JSON200.CanSync
updateRes, err := client.UpdateJobWithResponse(ctx, id, queryservice.JobUpdate{
CanSync: &updateCanSync,
})
assert.Nil(t, err)
assert.NotNil(t, updateRes)
jobRes, err = client.GetJobWithResponse(ctx, id)
assert.Nil(t, err)
assert.Equal(t, id, jobRes.JSON200.Id)
assert.Equal(t, clientRes.JSON201.Id, jobRes.JSON200.ClientId)
assert.False(t, jobRes.JSON200.CanSync)
}
+38 -22
View File
@@ -6,7 +6,7 @@ import (
queryservice "queryorchestration/pkg/queryService"
"testing"
"github.com/google/uuid"
"github.com/oapi-codegen/runtime/types"
"github.com/stretchr/testify/assert"
)
@@ -29,13 +29,22 @@ func TestJobCollectorService(t *testing.T) {
jsonRes, err := client.CreateQueryWithResponse(ctx, queryservice.QueryCreate{
Type: queryservice.JSONEXTRACTOR,
Config: &jsoncfg,
RequiredQueries: &[]string{
RequiredQueries: &[]types.UUID{
contextRes.JSON201.Id,
},
})
assert.Nil(t, err)
jobID := uuid.NewString()
clientRes, err := client.CreateClientWithResponse(ctx, queryservice.ClientCreate{
Name: "example_name",
})
assert.Nil(t, err)
jobRes, err := client.CreateJobWithResponse(ctx, queryservice.JobCreate{
ClientId: clientRes.JSON201.Id,
})
assert.Nil(t, err)
id := jobRes.JSON201.Id
fields := []queryservice.JobCollectorField{
{
Name: "json_output",
@@ -43,27 +52,34 @@ func TestJobCollectorService(t *testing.T) {
},
}
collRes, err := client.GetJobCollectorByJobIdWithResponse(ctx, jobID)
collRes, err := client.GetJobCollectorByJobIdWithResponse(ctx, id)
assert.Nil(t, err)
assert.NotNil(t, collRes.JSON200)
assert.EqualExportedValues(t, queryservice.JobCollector{
JobId: jobID,
Fields: fields,
}, *collRes.JSON200)
assert.Equal(t, id, collRes.JSON200.JobId)
assert.Equal(t, 1, collRes.JSON200.ActiveVersion)
assert.Equal(t, 1, collRes.JSON200.LatestVersion)
assert.Equal(t, 1, collRes.JSON200.MinimumCleanerVersion)
assert.Equal(t, 1, collRes.JSON200.MinimumTextVersion)
assert.Len(t, collRes.JSON200.Fields, 0)
fields = []queryservice.JobCollectorField{
{
Name: "json",
QueryId: jsonRes.JSON201.Id,
},
}
res, err := client.UpdateJobCollectorByJobIdWithResponse(ctx, jobID, queryservice.JobCollectorUpdate{
Fields: &fields,
av := int32(2)
minClean := int32(2)
minText := int32(2)
uRes, err := client.UpdateJobCollectorByJobIdWithResponse(ctx, id, queryservice.JobCollectorUpdate{
ActiveVersion: &av,
MinimumCleanerVersion: &minClean,
MinimumTextVersion: &minText,
Fields: &fields,
})
assert.Nil(t, err)
assert.EqualExportedValues(t, queryservice.JobCollector{
JobId: jobID,
Fields: fields,
}, *collRes.JSON200)
assert.NotNil(t, res)
assert.Nil(t, uRes)
collRes, err = client.GetJobCollectorByJobIdWithResponse(ctx, id)
assert.Nil(t, err)
assert.Equal(t, id, collRes.JSON200.JobId)
assert.Equal(t, 2, collRes.JSON200.ActiveVersion)
assert.Equal(t, 2, collRes.JSON200.LatestVersion)
assert.Equal(t, 2, collRes.JSON200.MinimumCleanerVersion)
assert.Equal(t, 2, collRes.JSON200.MinimumTextVersion)
assert.Len(t, collRes.JSON200.Fields, 1)
assert.ElementsMatch(t, fields, collRes.JSON200.Fields)
}
+3 -2
View File
@@ -6,6 +6,7 @@ import (
queryservice "queryorchestration/pkg/queryService"
"testing"
"github.com/oapi-codegen/runtime/types"
"github.com/stretchr/testify/assert"
)
@@ -49,7 +50,7 @@ func TestQueryService(t *testing.T) {
aV := int32(2)
res, err := client.UpdateQueryWithResponse(ctx, jsonID, queryservice.QueryUpdate{
ActiveVersion: &aV,
RequiredQueries: &[]string{
RequiredQueries: &[]types.UUID{
contextID,
},
})
@@ -63,5 +64,5 @@ func TestQueryService(t *testing.T) {
assert.Equal(t, int32(2), queryRes.JSON200.ActiveVersion)
assert.Equal(t, int32(2), queryRes.JSON200.LatestVersion)
assert.Nil(t, queryRes.JSON200.Config)
assert.ElementsMatch(t, []string{contextID}, *queryRes.JSON200.RequiredQueries)
assert.ElementsMatch(t, []types.UUID{contextID}, *queryRes.JSON200.RequiredQueries)
}
+1 -1
View File
@@ -29,7 +29,7 @@ func TestQueryServiceTest(t *testing.T) {
docId := uuid.New()
testRes, err := client.TestQueryWithResponse(ctx, id, queryservice.QueryTestRequest{
DocumentId: docId.String(),
DocumentId: docId,
QueryVersion: int32(1),
})
assert.Nil(t, err)