Skip to content

Commit

Permalink
Implement MsgDBStats for Tigris (#1047)
Browse files Browse the repository at this point in the history
Closes #774.
  • Loading branch information
rumyantseva committed Aug 17, 2022
1 parent 1533c4b commit cb89ab3
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 12 deletions.
12 changes: 2 additions & 10 deletions integration/commands_administration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,10 +680,8 @@ func TestCommandsAdministrationDataSizeCollectionNotExist(t *testing.T) {
}

func TestCommandsAdministrationDBStats(t *testing.T) {
setup.SkipForTigris(t)

t.Parallel()
ctx, collection := setup.Setup(t, shareddata.Scalars, shareddata.Composites)
ctx, collection := setup.Setup(t, shareddata.DocumentsStrings)

var actual bson.D
command := bson.D{{"dbStats", int32(1)}}
Expand All @@ -705,8 +703,6 @@ func TestCommandsAdministrationDBStats(t *testing.T) {
}

func TestCommandsAdministrationDBStatsEmpty(t *testing.T) {
setup.SkipForTigris(t)

t.Parallel()
ctx, collection := setup.Setup(t)

Expand All @@ -730,10 +726,8 @@ func TestCommandsAdministrationDBStatsEmpty(t *testing.T) {
}

func TestCommandsAdministrationDBStatsWithScale(t *testing.T) {
setup.SkipForTigris(t)

t.Parallel()
ctx, collection := setup.Setup(t, shareddata.Scalars, shareddata.Composites)
ctx, collection := setup.Setup(t, shareddata.DocumentsStrings)

var actual bson.D
command := bson.D{{"dbStats", int32(1)}, {"scale", float64(1_000)}}
Expand All @@ -755,8 +749,6 @@ func TestCommandsAdministrationDBStatsWithScale(t *testing.T) {
}

func TestCommandsAdministrationDBStatsEmptyWithScale(t *testing.T) {
setup.SkipForTigris(t)

t.Parallel()
ctx, collection := setup.Setup(t)

Expand Down
87 changes: 85 additions & 2 deletions internal/handlers/tigris/msg_dbstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,95 @@ package tigris
import (
"context"

"github.com/tigrisdata/tigris-client-go/driver"

"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/tigris/tigrisdb"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/must"
"github.com/FerretDB/FerretDB/internal/wire"
)

// MsgDBStats implements HandlerInterface.
func (h *Handler) MsgDBStats(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
// TODO https://github.com/FerretDB/FerretDB/issues/774
return nil, notImplemented(must.NotFail(msg.Document()).Command())
document, err := msg.Document()
if err != nil {
return nil, lazyerrors.Error(err)
}

var db string

if db, err = common.GetRequiredParam[string](document, "$db"); err != nil {
return nil, err
}

m := document.Map()
scale, ok := m["scale"].(float64)

if !ok {
scale = 1
}

stats, err := h.db.Driver.DescribeDatabase(ctx, db)
switch err := err.(type) {
case nil:
// do nothing
case *driver.Error:
if !tigrisdb.IsNotFound(err) {
return nil, lazyerrors.Error(err)
}

// If DB doesn't exist just return empty stats.
stats = &driver.DescribeDatabaseResponse{
Db: db,
Size: 0,
}

default:
return nil, lazyerrors.Error(err)
}

// TODO We need a better way to get the number of documents in all collections.
var objects int32

for _, collection := range stats.Collections {
f := fetchParam{db: db, collection: collection.Collection}

docs, err := h.fetch(ctx, f)
if err != nil {
return nil, lazyerrors.Error(err)
}
objects += int32(len(docs))
}

var avgObjSize float64
if objects > 0 {
avgObjSize = float64(stats.Size) / float64(objects)
}

var reply wire.OpMsg
err = reply.SetSections(wire.OpMsgSection{
Documents: []*types.Document{must.NotFail(types.NewDocument(
"db", db,
"collections", int32(len(stats.Collections)),
// TODO https://github.com/FerretDB/FerretDB/issues/176
"views", int32(0),
"objects", int32(objects),
"avgObjSize", float64(avgObjSize),
"dataSize", float64(stats.Size),
// Tigris indexes all the fields https://docs.tigrisdata.com/apidocs/#operation/Tigris_Read
"indexes", int32(0),
"indexSize", int32(0),
"totalSize", int32(0),
"scaleFactor", scale,
"ok", float64(1),
))},
})

if err != nil {
return nil, lazyerrors.Error(err)
}

return &reply, nil
}

0 comments on commit cb89ab3

Please sign in to comment.