Skip to content

Commit

Permalink
implement Documents.GetStatus (#349)
Browse files Browse the repository at this point in the history
* implement Documents.GetStatus

* use proper API release
  • Loading branch information
hugowetterberg authored Nov 29, 2024
1 parent 02d935c commit 9e01d68
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 24 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ require (
github.com/rakutentech/jwk-go v1.1.3
github.com/tmaxmax/go-sse v0.9.0-pre.2
github.com/ttab/darknut v0.1.0
github.com/ttab/elephant-api v0.17.3
github.com/ttab/elephantine v0.16.0
github.com/ttab/elephant-api v0.17.4
github.com/ttab/elephantine v0.16.1
github.com/ttab/langos v0.1.1
github.com/ttab/mage v0.7.1
github.com/ttab/newsdoc v0.6.0
github.com/ttab/newsdoc v0.7.0
github.com/ttab/revisor v0.9.1
github.com/ttab/revisorschemas v0.5.0
github.com/ttab/revisorschemas v0.5.1
github.com/twitchtv/twirp v8.1.3+incompatible
github.com/urfave/cli/v2 v2.27.5
golang.org/x/mod v0.22.0
Expand Down
18 changes: 8 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -275,22 +275,20 @@ github.com/tmaxmax/go-sse v0.9.0-pre.2 h1:6X6xiu9PAc9VmCvLO2c/1pqw4Dqx4RC8dcqw57
github.com/tmaxmax/go-sse v0.9.0-pre.2/go.mod h1:HLoxqxdH+7oSUItjtnpxjzJedfr/+Rrm/dNWBcTxJFM=
github.com/ttab/darknut v0.1.0 h1:0EvIc82YzRYmxk8x/nEU374ci0TpGYaFOaGMKdggkYg=
github.com/ttab/darknut v0.1.0/go.mod h1:G7jxtatph/rpttr2XOoElJpcUX25hWr+Uql5PhRf7ao=
github.com/ttab/elephant-api v0.17.3 h1:sP76XXIxf3dIsAL+RR3dNQw+cBWNbhOXXm6APQYToeI=
github.com/ttab/elephant-api v0.17.3/go.mod h1:b0b6Y6LjV4WOyNK9oCVOITnJgQKWOCQKzYMKrkl+Zzk=
github.com/ttab/elephantine v0.16.0 h1:lewdYLbB4Bt978WjRHSHU2kxssaM68IMCCDpmNkIRbc=
github.com/ttab/elephantine v0.16.0/go.mod h1:PNpQa4bpa5JLmKd9SEKlaXslTocCYVo5oA9dV00jv3I=
github.com/ttab/elephant-api v0.17.4 h1:BGk24NBi1ob/5r+1Eg8UXx1ivOu+YokyrOzCzha7ITg=
github.com/ttab/elephant-api v0.17.4/go.mod h1:b0b6Y6LjV4WOyNK9oCVOITnJgQKWOCQKzYMKrkl+Zzk=
github.com/ttab/elephantine v0.16.1 h1:TZHFetBqxAzkYIR0B7WLQ8D7eb2sxbKRpS6OsF2qM1Y=
github.com/ttab/elephantine v0.16.1/go.mod h1:PNpQa4bpa5JLmKd9SEKlaXslTocCYVo5oA9dV00jv3I=
github.com/ttab/langos v0.1.1 h1:87AksvnO5AWdXEKnb5Ga5H+dXvigDX8I1y46pTc8vDE=
github.com/ttab/langos v0.1.1/go.mod h1:+E1sOSvBJIbE5VM0m2rfr83aqw0kZbfe/clqNtwSP8w=
github.com/ttab/mage v0.7.1 h1:40etoCHYQ4MHofkuQ1A0aIXnH/tDI2PvkeGlPtf2/FA=
github.com/ttab/mage v0.7.1/go.mod h1:TrsadmQ9OK/Yg0FbX+ntShyp1X3tGe8zxdWNgDOmBmw=
github.com/ttab/newsdoc v0.6.0 h1:xe6RYGBTXOJqXeIv7k/SeAMtocZlK+q4eRElw6jjVGQ=
github.com/ttab/newsdoc v0.6.0/go.mod h1:9hqqHiu77aEp90HBNrHLWECjsfU5QI9hATpR0ZSGMgA=
github.com/ttab/newsdoc v0.7.0 h1:mzj0VUQJ+XqrQv5MiQHODetvsHQ2lI1cwKwCkHIbPdE=
github.com/ttab/newsdoc v0.7.0/go.mod h1:AXwLv9THaZTTPVOzP4dZ22FzOsVhL5662RQqfTPtvGs=
github.com/ttab/revisor v0.9.1 h1:2Urwfm90qv2sXsGdYGBeXLBJt3NRrp/1mGzcFAMHnNQ=
github.com/ttab/revisor v0.9.1/go.mod h1:Gohi7gsBHRmG0ADLjsu0yZLvvxbubgUX8H0GcM7tfG4=
github.com/ttab/revisorschemas v0.4.10 h1:SI1r9JA2IG5g+jOy7IgTzvyL45/nsxceF6AO9anxUls=
github.com/ttab/revisorschemas v0.4.10/go.mod h1:0BqAiXV3BfVhjeny4FU3yel98Nvz/ezgpn8Say3zVLM=
github.com/ttab/revisorschemas v0.5.0 h1:N5AdI3u4WFgs+VHPlhAcJCFy2nOXXy+pWYTvoTN+IUw=
github.com/ttab/revisorschemas v0.5.0/go.mod h1:vXZOqJzJ6edm31V9ZE9QlAjZ9FqBPnnlDztZzkhs4lE=
github.com/ttab/revisorschemas v0.5.1 h1:jCPJbnhefrNJwQobvAZ6y0hFbpezz4tVZcT+YzqtFB0=
github.com/ttab/revisorschemas v0.5.1/go.mod h1:vXZOqJzJ6edm31V9ZE9QlAjZ9FqBPnnlDztZzkhs4lE=
github.com/twitchtv/twirp v8.1.3+incompatible h1:+F4TdErPgSUbMZMwp13Q/KgDVuI7HJXP61mNV3/7iuU=
github.com/twitchtv/twirp v8.1.3+incompatible/go.mod h1:RRJoFSAmTEh2weEqWtpPE3vFK5YBhA6bqp2l1kfCC5A=
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
Expand Down
9 changes: 9 additions & 0 deletions postgres/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,15 @@ WHERE uuid = @uuid AND name = @name
ORDER BY id DESC
LIMIT @count;

-- name: GetStatus :one
SELECT id, version, created, creator_uri, meta
FROM document_status
WHERE uuid = @uuid AND name = @name
AND (@id::bigint = 0 OR id = @id::bigint)
ORDER BY id DESC
LIMIT 1;


-- name: RegisterMetricKind :exec
INSERT INTO metric_kind(name, aggregation)
VALUES (@name, @aggregation);
Expand Down
36 changes: 36 additions & 0 deletions postgres/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions repository/docstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ type DocStore interface {
OnEventlog(
ctx context.Context, ch chan int64,
)
GetStatus(
ctx context.Context, uuid uuid.UUID,
name string, id int64,
) (Status, error)
// GetStatusHistory of a document. Count cannot be greater than
// StatusHistoryMaxCount.
GetStatusHistory(
Expand Down
70 changes: 60 additions & 10 deletions repository/documents_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,54 @@ func NewDocumentsService(
// Interface guard.
var _ repository.Documents = &DocumentsService{}

// GetStatus implements repository.Documents.
func (a *DocumentsService) GetStatus(
ctx context.Context, req *repository.GetStatusRequest,
) (*repository.GetStatusResponse, error) {
elephantine.SetLogMetadata(ctx,
elephantine.LogKeyDocumentUUID, req.Uuid,
)

auth, err := RequireAnyScope(ctx,
ScopeDocumentRead, ScopeDocumentReadAll, ScopeDocumentAdmin,
)
if err != nil {
return nil, err
}

docUUID, err := validateRequiredUUIDParam(req.Uuid)
if err != nil {
return nil, err
}

err = a.accessCheck(ctx, auth, docUUID, ReadPermission)
if err != nil {
return nil, err
}

if req.Name == "" {
return nil, twirp.RequiredArgumentError("name")
}

status, err := a.store.GetStatus(ctx, docUUID, req.Name, req.Id)
if IsDocStoreErrorCode(err, ErrCodeNotFound) {
return nil, twirp.NotFoundError(err.Error())
} else if err != nil {
return nil, twirp.InternalErrorf("load status information: %v", err)
}

return &repository.GetStatusResponse{
Status: &repository.Status{
Id: status.ID,
Version: status.Version,
Creator: status.Creator,
Created: status.Created.Format(time.RFC3339),
Meta: status.Meta,
MetaDocVersion: status.MetaDocVersion,
},
}, nil
}

// GetStatusOverview implements repository.Documents.
func (a *DocumentsService) GetStatusOverview(
ctx context.Context, req *repository.GetStatusOverviewRequest,
Expand Down Expand Up @@ -126,11 +174,12 @@ func (a *DocumentsService) GetStatusOverview(

for name, status := range di.Heads {
ri.Heads[name] = &repository.Status{
Id: status.ID,
Version: status.Version,
Creator: status.Creator,
Created: status.Created.Format(time.RFC3339),
Meta: status.Meta,
Id: status.ID,
Version: status.Version,
Creator: status.Creator,
Created: status.Created.Format(time.RFC3339),
Meta: status.Meta,
MetaDocVersion: status.MetaDocVersion,
}
}

Expand Down Expand Up @@ -188,11 +237,12 @@ func (a *DocumentsService) GetStatusHistory(

for i := range history {
res.Statuses[i] = &repository.Status{
Id: history[i].ID,
Version: history[i].Version,
Creator: history[i].Creator,
Created: history[i].Created.Format(time.RFC3339),
Meta: history[i].Meta,
Id: history[i].ID,
Version: history[i].Version,
Creator: history[i].Creator,
Created: history[i].Created.Format(time.RFC3339),
Meta: history[i].Meta,
MetaDocVersion: history[i].MetaDocVersion,
}
}

Expand Down
35 changes: 35 additions & 0 deletions repository/pgstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,41 @@ func (s *PGDocStore) GetVersionHistory(
return updates, nil
}

func (s *PGDocStore) GetStatus(
ctx context.Context, uuid uuid.UUID,
name string, id int64,
) (Status, error) {
row, err := s.reader.GetStatus(ctx, postgres.GetStatusParams{
UUID: uuid,
Name: name,
ID: id,
})
if errors.Is(err, pgx.ErrNoRows) {
return Status{}, DocStoreErrorf(ErrCodeNotFound, "no status found")
} else if err != nil {
return Status{}, fmt.Errorf("database error: %w", err)
}

var meta newsdoc.DataMap

if row.Meta != nil {
err := json.Unmarshal(row.Meta, &meta)
if err != nil {
return Status{}, fmt.Errorf(
"failed to unmarshal metadata for status %d: %w",
row.ID, err)
}
}

return Status{
ID: row.ID,
Version: row.Version,
Creator: row.CreatorUri,
Created: row.Created.Time,
Meta: meta,
}, nil
}

func (s *PGDocStore) GetStatusHistory(
ctx context.Context, uuid uuid.UUID,
name string, before int64, count int,
Expand Down

0 comments on commit 9e01d68

Please sign in to comment.