Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth More authored and Siddharth More committed May 6, 2024
1 parent 6efc495 commit 6f33481
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 0 deletions.
15 changes: 15 additions & 0 deletions disperser/dataapi/blobs_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ func (s *server) getBlobs(ctx context.Context, limit int) ([]*BlobMetadataRespon
return s.convertBlobMetadatasToBlobMetadataResponse(ctx, blobMetadatas)
}

func (s *server) getBlobCountByAccountId(ctx context.Context, accountID string) (*BlobCountForAccountIdResponse, error) {
s.logger.Info("Calling get blob", "AccountId", accountID)

metadataCount, err := s.blobstore.GetBlobMetadataCountByAccountID(ctx, accountID)
if err != nil {
return nil, err
}

s.logger.Debug("Got blob metadata count for AccountId", "AccountId", accountID, "metadataCount", metadataCount)
return &BlobCountForAccountIdResponse{
Count: metadataCount,
AccountId: accountID,
}, nil
}

func (s *server) convertBlobMetadatasToBlobMetadataResponse(ctx context.Context, metadatas []*disperser.BlobMetadata) ([]*BlobMetadataResponse, error) {
var (
err error
Expand Down
57 changes: 57 additions & 0 deletions disperser/dataapi/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,52 @@ const docTemplate = `{
}
}
},
"/feed/blobs/{accountId}": {
"get": {
"produces": [
"application/json"
],
"tags": [
"Feed"
],
"summary": "Fetch blob metadata count by AccountId",
"parameters": [
{
"type": "string",
"description": "AccountId",
"name": "accountId",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/dataapi.BlobCountForAccountIdResponse"
}
},
"400": {
"description": "error: Bad request",
"schema": {
"$ref": "#/definitions/dataapi.ErrorResponse"
}
},
"404": {
"description": "error: Not found",
"schema": {
"$ref": "#/definitions/dataapi.ErrorResponse"
}
},
"500": {
"description": "error: Server error",
"schema": {
"$ref": "#/definitions/dataapi.ErrorResponse"
}
}
}
}
},
"/feed/blobs/{blob_key}": {
"get": {
"produces": [
Expand Down Expand Up @@ -632,6 +678,17 @@ const docTemplate = `{
}
}
},
"dataapi.BlobCountForAccountIdResponse": {
"type": "object",
"properties": {
"account_id": {
"type": "string"
},
"count": {
"type": "integer"
}
}
},
"dataapi.BlobMetadataResponse": {
"type": "object",
"properties": {
Expand Down
57 changes: 57 additions & 0 deletions disperser/dataapi/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,52 @@
}
}
},
"/feed/blobs/{accountId}": {
"get": {
"produces": [
"application/json"
],
"tags": [
"Feed"
],
"summary": "Fetch blob metadata count by AccountId",
"parameters": [
{
"type": "string",
"description": "AccountId",
"name": "accountId",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/dataapi.BlobCountForAccountIdResponse"
}
},
"400": {
"description": "error: Bad request",
"schema": {
"$ref": "#/definitions/dataapi.ErrorResponse"
}
},
"404": {
"description": "error: Not found",
"schema": {
"$ref": "#/definitions/dataapi.ErrorResponse"
}
},
"500": {
"description": "error: Server error",
"schema": {
"$ref": "#/definitions/dataapi.ErrorResponse"
}
}
}
}
},
"/feed/blobs/{blob_key}": {
"get": {
"produces": [
Expand Down Expand Up @@ -628,6 +674,17 @@
}
}
},
"dataapi.BlobCountForAccountIdResponse": {
"type": "object",
"properties": {
"account_id": {
"type": "string"
},
"count": {
"type": "integer"
}
}
},
"dataapi.BlobMetadataResponse": {
"type": "object",
"properties": {
Expand Down
37 changes: 37 additions & 0 deletions disperser/dataapi/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ definitions:
data was posted to the DA node.
type: integer
type: object
dataapi.BlobCountForAccountIdResponse:
properties:
account_id:
type: string
count:
type: integer
type: object
dataapi.BlobMetadataResponse:
properties:
batch_header_hash:
Expand Down Expand Up @@ -305,6 +312,36 @@ paths:
summary: Fetch blobs metadata list
tags:
- Feed
/feed/blobs/{accountId}:
get:
parameters:
- description: AccountId
in: path
name: accountId
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/dataapi.BlobCountForAccountIdResponse'
"400":
description: 'error: Bad request'
schema:
$ref: '#/definitions/dataapi.ErrorResponse'
"404":
description: 'error: Not found'
schema:
$ref: '#/definitions/dataapi.ErrorResponse'
"500":
description: 'error: Server error'
schema:
$ref: '#/definitions/dataapi.ErrorResponse'
summary: Fetch blob metadata count by AccountId
tags:
- Feed
/feed/blobs/{blob_key}:
get:
parameters:
Expand Down
41 changes: 41 additions & 0 deletions disperser/dataapi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ type (
DispersalOnline bool `json:"dispersal_online"`
RetrievalOnline bool `json:"retrieval_online"`
}

BlobCountForAccountIdResponse struct {
Count int32 `json:"count"`
AccountId string `json:"account_id"`
}

ErrorResponse struct {
Error string `json:"error"`
}
Expand Down Expand Up @@ -244,6 +250,7 @@ func (s *server) Start() error {
{
feed.GET("/blobs", s.FetchBlobsHandler)
feed.GET("/blobs/:blob_key", s.FetchBlobHandler)
feed.GET("/blobs/:accountId", s.FetchBlobCountByAccountIdHandler)
}
operatorsInfo := v1.Group("/operators-info")
{
Expand Down Expand Up @@ -446,6 +453,40 @@ func (s *server) FetchBlobsHandler(c *gin.Context) {
})
}

// FetchBlobCountByAccountIdHandler godoc
//
// @Summary Fetch blob metadata count by AccountId
// @Tags Feed
// @Produce json
// @Param accountId path string true "AccountId"
// @Success 200 {object} BlobCountForAccountIdResponse
// @Failure 400 {object} ErrorResponse "error: Bad request"
// @Failure 404 {object} ErrorResponse "error: Not found"
// @Failure 500 {object} ErrorResponse "error: Server error"
// @Router /feed/blobs/{accountId} [get]
func (s *server) FetchBlobCountByAccountIdHandler(c *gin.Context) {
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(f float64) {
s.metrics.ObserveLatency("FetchBlobCountByAccountIdHandler", f*1000) // make milliseconds
}))
defer timer.ObserveDuration()

accountId := c.Param("accountId")
if accountId == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "account id empty string is invalid"})
}

blobMetadataCountByAccountIdResp, err := s.getBlobCountByAccountId(c.Request.Context(), accountId)
if err != nil {
s.metrics.IncrementFailedRequestNum("FetchBlobCountByAccountIdHandler")
errorResponse(c, err)
return
}

s.metrics.IncrementSuccessfulRequestNum("FetchBlobCountByAccountIdHandler")
c.Writer.Header().Set(cacheControlParam, fmt.Sprintf("max-age=%d", maxFeedBlobAage))
c.JSON(http.StatusOK, blobMetadataCountByAccountIdResp)
}

// FetchMetricsHandler godoc
//
// @Summary Fetch metrics
Expand Down
61 changes: 61 additions & 0 deletions disperser/dataapi/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,65 @@ func TestFetchBlobsHandler(t *testing.T) {
assert.Equal(t, 2, len(response.Data))
}

func TestFetchBlobCountByAccountIdHandler(t *testing.T) {
r := setUpRouter()

blob := makeTestBlob(0, 80)
key := queueBlob(t, &blob, blobstore)
markBlobConfirmed(t, &blob, key, expectedBatchHeaderHash, blobstore)
accountd := "test"
r.GET("/v1/feed/blobs/:accountId", testDataApiServer.FetchBlobCountByAccountIdHandler)

w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/v1/feed/blobs/"+accountd, nil)
r.ServeHTTP(w, req)

res := w.Result()
defer res.Body.Close()

data, err := io.ReadAll(res.Body)
assert.NoError(t, err)

var response dataapi.BlobCountForAccountIdResponse
err = json.Unmarshal(data, &response)
assert.NoError(t, err)
assert.NotNil(t, response)

assert.Equal(t, http.StatusOK, res.StatusCode)
assert.Equal(t, int32(1), response.Count)
assert.Equal(t, "test", response.AccountId)
}

func TestFetchBlobCountByAccountIdInvalidHandler(t *testing.T) {
r := setUpRouter()

blob := makeTestBlob(0, 80)
key := queueBlob(t, &blob, blobstore)
markBlobConfirmed(t, &blob, key, expectedBatchHeaderHash, blobstore)
// BlobAccountId is "test"
// Search by AccountId test1
accountd := "test1"
r.GET("/v1/feed/blobs/:accountId", testDataApiServer.FetchBlobCountByAccountIdHandler)

w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/v1/feed/blobs/"+accountd, nil)
r.ServeHTTP(w, req)

res := w.Result()
defer res.Body.Close()

data, err := io.ReadAll(res.Body)
assert.NoError(t, err)

var response dataapi.BlobCountForAccountIdResponse
err = json.Unmarshal(data, &response)
assert.NoError(t, err)
assert.NotNil(t, response)

assert.Equal(t, http.StatusOK, res.StatusCode)
assert.Equal(t, int32(0), response.Count)
}

func TestFetchMetricsHandler(t *testing.T) {
defer goleak.VerifyNone(t)

Expand Down Expand Up @@ -1529,9 +1588,11 @@ func markBlobConfirmed(t *testing.T, blob *core.Blob, key disperser.BlobKey, bat
BlobStatus: disperser.Confirmed,
Expiry: 0,
NumRetries: 0,
AccountID: "test",
RequestMetadata: &disperser.RequestMetadata{
BlobRequestHeader: core.BlobRequestHeader{
SecurityParams: blob.RequestHeader.SecurityParams,
BlobAuthHeader: blob.RequestHeader.BlobAuthHeader,
},
RequestedAt: expectedRequestedAt,
BlobSize: uint(len(blob.Data)),
Expand Down

0 comments on commit 6f33481

Please sign in to comment.