Skip to content

Commit

Permalink
Add CreatedAt and CreatedBy to DatabaseInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
ostafen committed Jun 7, 2024
1 parent a147294 commit a36e4bc
Show file tree
Hide file tree
Showing 9 changed files with 644 additions and 597 deletions.
17 changes: 10 additions & 7 deletions cmd/immuadmin/command/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ func (cl *commandline) database(cmd *cobra.Command) {
}
c.PrintTable(
cmd.OutOrStdout(),
[]string{"Database Name", "Status", "Is Replica", "Disk Size", "Transactions"},
[]string{"Database Name", "Created At", "Created By", "Status", "Is Replica", "Disk Size", "Transactions"},
len(resp.Databases),
func(i int) []string {
row := make([]string, 5)
row := make([]string, 7)

db := resp.Databases[i]

Expand All @@ -120,17 +120,20 @@ func (cl *commandline) database(cmd *cobra.Command) {
}
row[0] += db.Name

row[1] = time.Unix(int64(db.CreatedAt), 0).Format("2006-01-02")
row[2] = db.CreatedBy

if db.GetLoaded() {
row[1] += "LOADED"
row[3] += "LOADED"
} else {
row[1] += "UNLOADED"
row[3] += "UNLOADED"
}

isReplica := db.Settings.ReplicationSettings.Replica != nil && db.Settings.ReplicationSettings.Replica.Value

row[2] = strings.ToUpper(strconv.FormatBool(isReplica))
row[3] = helper.FormatByteSize(db.DiskSize)
row[4] = strconv.FormatUint(db.NumTransactions, 10)
row[4] = strings.ToUpper(strconv.FormatBool(isReplica))
row[5] = helper.FormatByteSize(db.DiskSize)
row[6] = strconv.FormatUint(db.NumTransactions, 10)

return row
},
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/schema/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ DEPRECATED
| loaded | [bool](#bool) | | If true, this database is currently loaded into memory |
| diskSize | [uint64](#uint64) | | database disk size |
| numTransactions | [uint64](#uint64) | | total number of transactions |
| created_at | [uint64](#uint64) | | the time when the db was created |
| created_by | [string](#string) | | the user who created the database |



Expand Down
1,180 changes: 601 additions & 579 deletions pkg/api/schema/schema.pb.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions pkg/api/schema/schema.proto
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,12 @@ message DatabaseInfo {

// total number of transactions
uint64 numTransactions = 5;

// the time when the db was created
uint64 created_at = 6;

// the user who created the database
string created_by = 7;
}

message Chunk {
Expand Down
9 changes: 9 additions & 0 deletions pkg/api/schema/schema.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2087,6 +2087,15 @@
"type": "string",
"format": "uint64",
"title": "total number of transactions"
},
"created_at": {
"type": "string",
"format": "uint64",
"title": "the time when the db was created"
},
"created_by": {
"type": "string",
"title": "the user who created the database"
}
}
},
Expand Down
13 changes: 8 additions & 5 deletions pkg/server/db_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,12 @@ type ahtOptions struct {
WriteBufferSize int `json:"writeBufferSize"`
}

const DefaultMaxValueLen = 1 << 25 //32Mb
const DefaultStoreFileSize = 1 << 29 //512Mb
const (
DefaultMaxValueLen = 1 << 25 //32Mb
DefaultStoreFileSize = 1 << 29 //512Mb
)

func (s *ImmuServer) defaultDBOptions(dbName string) *dbOptions {
func (s *ImmuServer) defaultDBOptions(dbName, userName string) *dbOptions {
dbOpts := &dbOptions{
Database: dbName,

Expand Down Expand Up @@ -170,6 +172,7 @@ func (s *ImmuServer) defaultDBOptions(dbName string) *dbOptions {
Autoload: unspecifiedState,

CreatedAt: time.Now(),
CreatedBy: userName,
TruncationFrequency: Milliseconds(database.DefaultTruncationFrequency.Milliseconds()),
}

Expand Down Expand Up @@ -862,14 +865,14 @@ func (s *ImmuServer) deleteDBOptionsFor(db string) error {

func (s *ImmuServer) loadDBOptions(database string, createIfNotExists bool) (*dbOptions, error) {
if database == s.Options.systemAdminDBName || database == s.Options.defaultDBName {
return s.defaultDBOptions(database), nil
return s.defaultDBOptions(database, s.Options.systemAdminDBName), nil
}

optionsKey := make([]byte, 1+len(database))
optionsKey[0] = KeyPrefixDBSettings
copy(optionsKey[1:], []byte(database))

options := s.defaultDBOptions(database)
options := s.defaultDBOptions(database, "")

e, err := s.sysDB.Get(context.Background(), &schema.KeyRequest{Key: optionsKey})
if errors.Is(err, store.ErrKeyNotFound) && createIfNotExists {
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/db_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestDefaultOptions(t *testing.T) {
s, closer := testServer(DefaultOptions().WithDir(dir))
defer closer()

opts := s.defaultDBOptions("db1")
opts := s.defaultDBOptions("db1", "user")

require.NoError(t, opts.Validate())

Expand All @@ -43,7 +43,7 @@ func TestReplicaOptions(t *testing.T) {
s, closer := testServer(DefaultOptions().WithDir(dir))
defer closer()

opts := s.defaultDBOptions("db1")
opts := s.defaultDBOptions("db1", "user")

opts.Replica = true

Expand All @@ -69,7 +69,7 @@ func TestPrimaryOptions(t *testing.T) {
s, closer := testServer(DefaultOptions().WithDir(dir))
defer closer()

opts := s.defaultDBOptions("db1")
opts := s.defaultDBOptions("db1", "user")

opts.Replica = false

Expand Down
2 changes: 1 addition & 1 deletion pkg/server/remote_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ func testAppendableIsUploadedToRemoteStorage(t *testing.T) (string, remotestorag

s.remoteStorage = memory.Open()

stOpts := s.databaseOptionsFrom(s.defaultDBOptions("testdb")).GetStoreOptions().WithEmbeddedValues(false)
stOpts := s.databaseOptionsFrom(s.defaultDBOptions("testdb", "")).GetStoreOptions().WithEmbeddedValues(false)

path := filepath.Join(dir, "testdb")
st, err := store.Open(path, stOpts)
Expand Down
6 changes: 4 additions & 2 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ func (s *ImmuServer) CreateDatabaseV2(ctx context.Context, req *schema.CreateDat
s.dbListMutex.Lock()
defer s.dbListMutex.Unlock()

//check if database exists
// check if database exists
if s.dbList.GetId(req.Name) >= 0 {
if !req.IfNotExists {
return nil, database.ErrDatabaseAlreadyExists
Expand All @@ -984,7 +984,7 @@ func (s *ImmuServer) CreateDatabaseV2(ctx context.Context, req *schema.CreateDat
}, nil
}

dbOpts := s.defaultDBOptions(req.Name)
dbOpts := s.defaultDBOptions(req.Name, user.Username)

if req.Settings != nil {
err = s.overwriteWith(dbOpts, req.Settings, false)
Expand Down Expand Up @@ -1476,6 +1476,8 @@ func (s *ImmuServer) DatabaseListV2(ctx context.Context, req *schema.DatabaseLis
Loaded: !db.IsClosed(),
DiskSize: size,
NumTransactions: txCount,
CreatedAt: uint64(dbOpts.CreatedAt.Unix()),
CreatedBy: dbOpts.CreatedBy,
}
resp.Databases = append(resp.Databases, info)
}
Expand Down

0 comments on commit a36e4bc

Please sign in to comment.