Merged in feature/docresult (pull request #105)

Document Result Endpoint

* testing

* cleantesting

* progress

* query

* lint

* readme

* dockerclient

* api

* passtest

* tests

* test
This commit is contained in:
Michael McGuinness
2025-03-17 18:14:15 +00:00
parent f7c41c4ef3
commit 555b6d420b
105 changed files with 3276 additions and 2484 deletions
+97 -48
View File
@@ -48,20 +48,6 @@ func (q *Queries) CreateDocument(ctx context.Context, arg *CreateDocumentParams)
return id, err
}
const getDocument = `-- name: GetDocument :one
SELECT id, clientId, hash FROM documents WHERE id = $1 LIMIT 1
`
// GetDocument
//
// SELECT id, clientId, hash FROM documents WHERE id = $1 LIMIT 1
func (q *Queries) GetDocument(ctx context.Context, id pgtype.UUID) (*Document, error) {
row := q.db.QueryRow(ctx, getDocument, id)
var i Document
err := row.Scan(&i.ID, &i.Clientid, &i.Hash)
return &i, err
}
const getDocumentEntry = `-- name: GetDocumentEntry :one
SELECT documentId, bucket, key FROM documentEntries WHERE documentId = $1 ORDER BY id DESC LIMIT 1
`
@@ -82,6 +68,80 @@ func (q *Queries) GetDocumentEntry(ctx context.Context, documentid pgtype.UUID)
return &i, err
}
const getDocumentExternal = `-- name: GetDocumentExternal :one
WITH
docs AS (
SELECT id, hash, clientId
FROM documents
WHERE id = $1
),
namedResults AS (
SELECT
q.name,
dd.id,
d.value
FROM currentCollectorQueries AS q
JOIN docs as dd on dd.clientId = q.clientId
LEFT JOIN listValidDocumentResults($1) AS d
ON d.queryId = q.queryId and q.name is not null
)
SELECT
d.id,
c.externalId AS clientId,
d.hash,
COALESCE(jsonb_object_agg(r.name, r.value) FILTER (WHERE r.name IS NOT NULL), '{}') AS fields
FROM docs AS d
JOIN clients AS c ON c.id = d.clientId
LEFT JOIN namedResults AS r ON d.id = r.id
GROUP BY d.id, c.externalId, d.hash
`
type GetDocumentExternalRow struct {
ID pgtype.UUID `db:"id"`
Clientid string `db:"clientid"`
Hash string `db:"hash"`
Fields []byte `db:"fields"`
}
// GetDocumentExternal
//
// WITH
// docs AS (
// SELECT id, hash, clientId
// FROM documents
// WHERE id = $1
// ),
// namedResults AS (
// SELECT
// q.name,
// dd.id,
// d.value
// FROM currentCollectorQueries AS q
// JOIN docs as dd on dd.clientId = q.clientId
// LEFT JOIN listValidDocumentResults($1) AS d
// ON d.queryId = q.queryId and q.name is not null
// )
// SELECT
// d.id,
// c.externalId AS clientId,
// d.hash,
// COALESCE(jsonb_object_agg(r.name, r.value) FILTER (WHERE r.name IS NOT NULL), '{}') AS fields
// FROM docs AS d
// JOIN clients AS c ON c.id = d.clientId
// LEFT JOIN namedResults AS r ON d.id = r.id
// GROUP BY d.id, c.externalId, d.hash
func (q *Queries) GetDocumentExternal(ctx context.Context, documentid pgtype.UUID) (*GetDocumentExternalRow, error) {
row := q.db.QueryRow(ctx, getDocumentExternal, documentid)
var i GetDocumentExternalRow
err := row.Scan(
&i.ID,
&i.Clientid,
&i.Hash,
&i.Fields,
)
return &i, err
}
const getDocumentIDByHash = `-- name: GetDocumentIDByHash :one
SELECT id FROM documents WHERE hash = $1 and clientId = $2
`
@@ -101,6 +161,20 @@ func (q *Queries) GetDocumentIDByHash(ctx context.Context, arg *GetDocumentIDByH
return id, err
}
const getDocumentSummary = `-- name: GetDocumentSummary :one
SELECT id, clientId, hash FROM documents WHERE id = $1
`
// GetDocumentSummary
//
// SELECT id, clientId, hash FROM documents WHERE id = $1
func (q *Queries) GetDocumentSummary(ctx context.Context, id pgtype.UUID) (*Document, error) {
row := q.db.QueryRow(ctx, getDocumentSummary, id)
var i Document
err := row.Scan(&i.ID, &i.Clientid, &i.Hash)
return &i, err
}
const listDocumentIDsBatch = `-- name: ListDocumentIDsBatch :many
SELECT id, totalCount FROM listDocumentIDs($1, $2, $3)
`
@@ -142,50 +216,25 @@ func (q *Queries) ListDocumentIDsBatch(ctx context.Context, arg *ListDocumentIDs
const listDocumentsByClientExternalId = `-- name: ListDocumentsByClientExternalId :many
WITH client as (
SELECT id from clients where externalId = $1
),
docs as (
SELECT d.id
from documents as d
JOIN client as c on d.clientId = c.id
),
entries as (
SELECT
de.documentId,
de.bucket,
de.key,
ROW_NUMBER() OVER (PARTITION BY de.documentId ORDER BY de.id DESC) as rowNumber
FROM docs d
JOIN documentEntries de on d.id = de.documentId
)
SELECT documentId as id, bucket, key from entries WHERE rowNumber = 1
SELECT d.id, d.hash
from documents as d
JOIN client as c on d.clientId = c.id
`
type ListDocumentsByClientExternalIdRow struct {
ID pgtype.UUID `db:"id"`
Bucket string `db:"bucket"`
Key string `db:"key"`
ID pgtype.UUID `db:"id"`
Hash string `db:"hash"`
}
// ListDocumentsByClientExternalId
//
// WITH client as (
// SELECT id from clients where externalId = $1
// ),
// docs as (
// SELECT d.id
// from documents as d
// JOIN client as c on d.clientId = c.id
// ),
// entries as (
// SELECT
// de.documentId,
// de.bucket,
// de.key,
// ROW_NUMBER() OVER (PARTITION BY de.documentId ORDER BY de.id DESC) as rowNumber
// FROM docs d
// JOIN documentEntries de on d.id = de.documentId
// )
// SELECT documentId as id, bucket, key from entries WHERE rowNumber = 1
// SELECT d.id, d.hash
// from documents as d
// JOIN client as c on d.clientId = c.id
func (q *Queries) ListDocumentsByClientExternalId(ctx context.Context, clientid string) ([]*ListDocumentsByClientExternalIdRow, error) {
rows, err := q.db.Query(ctx, listDocumentsByClientExternalId, clientid)
if err != nil {
@@ -195,7 +244,7 @@ func (q *Queries) ListDocumentsByClientExternalId(ctx context.Context, clientid
items := []*ListDocumentsByClientExternalIdRow{}
for rows.Next() {
var i ListDocumentsByClientExternalIdRow
if err := rows.Scan(&i.ID, &i.Bucket, &i.Key); err != nil {
if err := rows.Scan(&i.ID, &i.Hash); err != nil {
return nil, err
}
items = append(items, &i)