From 483a47e4e04eb61681cdf11d2384714a7e71abdf Mon Sep 17 00:00:00 2001 From: Michael McGuinness Date: Fri, 3 Jan 2025 14:08:04 +0000 Subject: [PATCH] getandlist --- database/queries/query.sql | 13 +++++- internal/database/repository/query.sql.go | 48 ++++++++++++++++++++++- internal/query/list.go | 21 ++++++++-- internal/query/service.go | 32 +++------------ internal/query/test.go | 4 +- internal/queryProcessor/parse.go | 42 +++++++++++++++++++- internal/queryProcessor/service.go | 6 +-- internal/queryQueue/create.go | 2 +- 8 files changed, 129 insertions(+), 39 deletions(-) diff --git a/database/queries/query.sql b/database/queries/query.sql index 15bb71fc..eb7f59c4 100644 --- a/database/queries/query.sql +++ b/database/queries/query.sql @@ -5,7 +5,7 @@ SELECT id, config FROM queryConfigs where queryId = $1 and addedVersion >= $2 an SELECT type FROM queries where id = $1; -- name: DeprecateQuery :exec -INSERT INTO queryDeprecations (queryId) VALUES ($1) RETURNING id; +INSERT INTO queryDeprecations (queryId) VALUES ($1); -- name: IsQueryDeprecated :one SELECT EXISTS ( @@ -22,4 +22,15 @@ SELECT q.id, q.type, q.activeVersion, c.config, ARRAY_AGG(r.requiredQueryId) AS and COALESCE(c.removedVersion, q.activeVersion - 1) < q.activeVersion and r.addedVersion >= q.activeVersion and COALESCE(r.removedVersion, q.activeVersion - 1) < q.activeVersion + GROUP BY q.id, c.config; + +-- name: ListQueries :many +SELECT q.id, q.type, q.activeVersion, c.config, ARRAY_AGG(r.requiredQueryId) AS requiredIds + FROM queries AS q + JOIN queryConfigs AS c ON q.id = c.queryId + JOIN requiredQueries AS r ON q.id = r.queryId + WHERE c.addedVersion >= q.activeVersion + and COALESCE(c.removedVersion, q.activeVersion - 1) < q.activeVersion + and r.addedVersion >= q.activeVersion + and COALESCE(r.removedVersion, q.activeVersion - 1) < q.activeVersion GROUP BY q.id, c.config; \ No newline at end of file diff --git a/internal/database/repository/query.sql.go b/internal/database/repository/query.sql.go index 0403c2fd..f226a62b 100644 --- a/internal/database/repository/query.sql.go +++ b/internal/database/repository/query.sql.go @@ -12,7 +12,7 @@ import ( ) const deprecateQuery = `-- name: DeprecateQuery :exec -INSERT INTO queryDeprecations (queryId) VALUES ($1) RETURNING id +INSERT INTO queryDeprecations (queryId) VALUES ($1) ` func (q *Queries) DeprecateQuery(ctx context.Context, queryid pgtype.UUID) error { @@ -98,3 +98,49 @@ func (q *Queries) IsQueryDeprecated(ctx context.Context, queryid pgtype.UUID) (b err := row.Scan(&exists) return exists, err } + +const listQueries = `-- name: ListQueries :many +SELECT q.id, q.type, q.activeVersion, c.config, ARRAY_AGG(r.requiredQueryId) AS requiredIds + FROM queries AS q + JOIN queryConfigs AS c ON q.id = c.queryId + JOIN requiredQueries AS r ON q.id = r.queryId + WHERE c.addedVersion >= q.activeVersion + and COALESCE(c.removedVersion, q.activeVersion - 1) < q.activeVersion + and r.addedVersion >= q.activeVersion + and COALESCE(r.removedVersion, q.activeVersion - 1) < q.activeVersion + GROUP BY q.id, c.config +` + +type ListQueriesRow struct { + ID pgtype.UUID + Type Querytype + Activeversion int32 + Config []byte + Requiredids []pgtype.UUID +} + +func (q *Queries) ListQueries(ctx context.Context) ([]ListQueriesRow, error) { + rows, err := q.db.Query(ctx, listQueries) + if err != nil { + return nil, err + } + defer rows.Close() + var items []ListQueriesRow + for rows.Next() { + var i ListQueriesRow + if err := rows.Scan( + &i.ID, + &i.Type, + &i.Activeversion, + &i.Config, + &i.Requiredids, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/query/list.go b/internal/query/list.go index 81932355..2e498931 100644 --- a/internal/query/list.go +++ b/internal/query/list.go @@ -2,9 +2,24 @@ package query import ( "context" + queryprocessor "queryorchestration/internal/queryProcessor" ) -func (s *Service) List(ctx context.Context, filters ListFilters) (*[]Query, error) { - // TODO - return nil, nil +func (s *Service) List(ctx context.Context, filters ListFilters) (*[]queryprocessor.Query, error) { + dbQueries, err := s.db.ListQueries(ctx) + if err != nil { + return nil, err + } + + queries := make([]queryprocessor.Query, len(dbQueries)) + for index, query := range dbQueries { + q, err := queryprocessor.ParseDBListQuery(&query) + if err != nil { + return nil, err + } + + queries[index] = *q + } + + return &queries, nil } diff --git a/internal/query/service.go b/internal/query/service.go index a6be2760..7760cd28 100644 --- a/internal/query/service.go +++ b/internal/query/service.go @@ -9,20 +9,14 @@ import ( "github.com/google/uuid" ) -type Query struct { - ID uuid.UUID - Type queryprocessor.Type - RequiredQueryIDs []uuid.UUID - Version int32 - Config interface{} -} - type ListFilters struct { Types []queryprocessor.Type } type Test struct { - ID uuid.UUID + QueryID uuid.UUID + DocumentID uuid.UUID + QueryVersion int32 } type Service struct { @@ -33,27 +27,11 @@ func New(db *repository.Queries) *Service { return &Service{db} } -func (s *Service) Get(ctx context.Context, id uuid.UUID) (*Query, error) { +func (s *Service) Get(ctx context.Context, id uuid.UUID) (*queryprocessor.Query, error) { query, err := s.db.GetQuery(ctx, database.MustToDBUUID(id)) if err != nil { return nil, err } - queryType, err := queryprocessor.ParseDBType(query.Type) - if err != nil { - return nil, err - } - - reqIDs := make([]uuid.UUID, len(query.Requiredids)) - for index, id := range query.Requiredids { - reqIDs[index] = database.MustToUUID(id) - } - - return &Query{ - ID: id, - Type: queryType, - Version: query.Activeversion, - RequiredQueryIDs: reqIDs, - Config: string(query.Config), - }, nil + return queryprocessor.ParseDBGetQuery(&query) } diff --git a/internal/query/test.go b/internal/query/test.go index 42fe2673..0264d5c9 100644 --- a/internal/query/test.go +++ b/internal/query/test.go @@ -6,8 +6,8 @@ import ( func (s *Service) Test(ctx context.Context, filters Test) (string, error) { // TODO - // Sync doc - // Run test + // Sync doc - documentID + // Run test - queryID, queryVersion return "", nil } diff --git a/internal/queryProcessor/parse.go b/internal/queryProcessor/parse.go index 70cd71e5..b82738c9 100644 --- a/internal/queryProcessor/parse.go +++ b/internal/queryProcessor/parse.go @@ -42,7 +42,7 @@ func ToDBQueryType(t Type) (repository.NullQuerytype, error) { return repository.NullQuerytype{Querytype: dbType, Valid: true}, nil } -func ParseDBQuery(q *repository.GetCollectorQueriesRow) (*Query, error) { +func ParseDBCollectorQuery(q *repository.GetCollectorQueriesRow) (*Query, error) { reqQueryIDs := make([]uuid.UUID, len(q.Requiredids)) for index, id := range q.Requiredids { reqQueryIDs[index] = database.MustToUUID(id) @@ -60,3 +60,43 @@ func ParseDBQuery(q *repository.GetCollectorQueriesRow) (*Query, error) { RequiredQueryIDs: reqQueryIDs, }, nil } + +func ParseDBListQuery(q *repository.ListQueriesRow) (*Query, error) { + reqQueryIDs := make([]uuid.UUID, len(q.Requiredids)) + for index, id := range q.Requiredids { + reqQueryIDs[index] = database.MustToUUID(id) + } + + qType, err := ParseDBType(q.Type) + if err != nil { + return nil, err + } + + return &Query{ + ID: database.MustToUUID(q.ID), + Version: q.Activeversion, + Type: qType, + RequiredQueryIDs: reqQueryIDs, + Config: string(q.Config), + }, nil +} + +func ParseDBGetQuery(q *repository.GetQueryRow) (*Query, error) { + reqQueryIDs := make([]uuid.UUID, len(q.Requiredids)) + for index, id := range q.Requiredids { + reqQueryIDs[index] = database.MustToUUID(id) + } + + qType, err := ParseDBType(q.Type) + if err != nil { + return nil, err + } + + return &Query{ + ID: database.MustToUUID(q.ID), + Version: q.Activeversion, + Type: qType, + RequiredQueryIDs: reqQueryIDs, + Config: string(q.Config), + }, nil +} diff --git a/internal/queryProcessor/service.go b/internal/queryProcessor/service.go index 7603a924..9addc10b 100644 --- a/internal/queryProcessor/service.go +++ b/internal/queryProcessor/service.go @@ -17,13 +17,13 @@ const ( type Create struct { Type Type RequiredQueryIDs []uuid.UUID - Config interface{} + Config string } type Update struct { ID uuid.UUID RequiredQueryIDs []uuid.UUID - Config interface{} + Config string } type Query struct { @@ -31,7 +31,7 @@ type Query struct { Type Type Version int32 RequiredQueryIDs []uuid.UUID - Config interface{} + Config string } type Creator interface { diff --git a/internal/queryQueue/create.go b/internal/queryQueue/create.go index bfeb0618..e9a1aa30 100644 --- a/internal/queryQueue/create.go +++ b/internal/queryQueue/create.go @@ -38,7 +38,7 @@ func (c *Queue) getCollectorQueries(ctx context.Context) error { cleanQueries := make([]queryprocessor.Query, len(queries)) for index, dbQuery := range queries { - cleanQuery, err := queryprocessor.ParseDBQuery(&dbQuery) + cleanQuery, err := queryprocessor.ParseDBCollectorQuery(&dbQuery) if err != nil { return err }