-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sync status (kvdb stats) p2p endpoints to auth, tns and patrick (#…
…175)
- Loading branch information
Showing
26 changed files
with
580 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package auth | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/taubyte/p2p/streams/command" | ||
kvdbIface "github.com/taubyte/tau/core/kvdb" | ||
iface "github.com/taubyte/tau/core/services/auth" | ||
"github.com/taubyte/tau/pkg/kvdb" | ||
) | ||
|
||
func (c *Client) Stats() iface.Stats { | ||
return (*Stats)(c) | ||
} | ||
|
||
func (h *Stats) Database() (kvdbIface.Stats, error) { | ||
response, err := h.client.Send("stats", command.Body{"action": "db"}) | ||
if err != nil { | ||
return nil, fmt.Errorf("sending stats.db request failed with %w", err) | ||
} | ||
|
||
idata, err := response.Get("stats") | ||
if err != nil { | ||
return nil, fmt.Errorf("getting stats from response failed with %w", err) | ||
} | ||
|
||
data, ok := idata.([]byte) | ||
if !ok { | ||
return nil, errors.New("incorrect stats type") | ||
} | ||
|
||
s := kvdb.NewStats() | ||
err = s.Decode(data) | ||
if err != nil { | ||
return nil, fmt.Errorf("decoding stats failed with %w", err) | ||
} | ||
|
||
return s, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package auth_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
commonIface "github.com/taubyte/tau/core/common" | ||
"github.com/taubyte/tau/core/kvdb" | ||
"github.com/taubyte/tau/dream" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestStats(t *testing.T) { | ||
defer os.Remove(testDir) | ||
|
||
u := dream.New(dream.UniverseConfig{Name: t.Name()}) | ||
defer u.Stop() | ||
|
||
err := u.StartWithConfig(&dream.Config{ | ||
Services: map[string]commonIface.ServiceConfig{ | ||
"auth": {}, | ||
}, | ||
Simples: map[string]dream.SimpleConfig{ | ||
"client": { | ||
Clients: dream.SimpleConfigClients{ | ||
Auth: &commonIface.ClientConfig{}, | ||
}.Compat(), | ||
}, | ||
}, | ||
}) | ||
assert.NilError(t, err) | ||
|
||
simple, err := u.Simple("client") | ||
assert.NilError(t, err) | ||
|
||
auth, err := simple.Auth() | ||
assert.NilError(t, err) | ||
|
||
// make sure database has a head | ||
injectCert(t, auth) | ||
|
||
stats, err := auth.Stats().Database() | ||
assert.NilError(t, err) | ||
|
||
assert.Equal(t, stats.Type(), kvdb.TypeCRDT) | ||
|
||
assert.Equal(t, len(stats.Heads()), 1) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package patrick | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/taubyte/p2p/streams/command" | ||
|
||
kvdbIface "github.com/taubyte/tau/core/kvdb" | ||
"github.com/taubyte/tau/pkg/kvdb" | ||
) | ||
|
||
func (client *Client) DatabaseStats() (kvdbIface.Stats, error) { | ||
response, err := client.Send("stats", command.Body{"action": "db"}) | ||
if err != nil { | ||
return nil, fmt.Errorf("sending stats.db request failed with %w", err) | ||
} | ||
|
||
idata, err := response.Get("stats") | ||
if err != nil { | ||
return nil, fmt.Errorf("getting stats from response failed with %w", err) | ||
} | ||
|
||
data, ok := idata.([]byte) | ||
if !ok { | ||
return nil, errors.New("incorrect stats type") | ||
} | ||
|
||
s := kvdb.NewStats() | ||
err = s.Decode(data) | ||
if err != nil { | ||
return nil, fmt.Errorf("decoding stats failed with %w", err) | ||
} | ||
|
||
return s, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package patrick_test | ||
|
||
import ( | ||
"testing" | ||
|
||
commonIface "github.com/taubyte/tau/core/common" | ||
"github.com/taubyte/tau/core/kvdb" | ||
"github.com/taubyte/tau/dream" | ||
"gotest.tools/v3/assert" | ||
|
||
_ "github.com/taubyte/tau/services/patrick" | ||
) | ||
|
||
func TestStats(t *testing.T) { | ||
u := dream.New(dream.UniverseConfig{Name: t.Name()}) | ||
defer u.Stop() | ||
|
||
err := u.StartWithConfig(&dream.Config{ | ||
Services: map[string]commonIface.ServiceConfig{ | ||
"patrick": {}, | ||
}, | ||
Simples: map[string]dream.SimpleConfig{ | ||
"client": { | ||
Clients: dream.SimpleConfigClients{ | ||
Patrick: &commonIface.ClientConfig{}, | ||
}.Compat(), | ||
}, | ||
}, | ||
}) | ||
assert.NilError(t, err) | ||
|
||
simple, err := u.Simple("client") | ||
assert.NilError(t, err) | ||
|
||
pat, err := simple.Patrick() | ||
assert.NilError(t, err) | ||
|
||
stats, err := pat.DatabaseStats() | ||
assert.NilError(t, err) | ||
|
||
assert.Equal(t, stats.Type(), kvdb.TypeCRDT) | ||
|
||
assert.Equal(t, len(stats.Heads()), 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package tns | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/taubyte/p2p/streams/command" | ||
kvdbIface "github.com/taubyte/tau/core/kvdb" | ||
iface "github.com/taubyte/tau/core/services/tns" | ||
"github.com/taubyte/tau/pkg/kvdb" | ||
) | ||
|
||
func (c *Client) Stats() iface.Stats { | ||
return (*Stats)(c) | ||
} | ||
|
||
func (h *Stats) Database() (kvdbIface.Stats, error) { | ||
response, err := h.client.Send("stats", command.Body{"action": "db"}) | ||
if err != nil { | ||
return nil, fmt.Errorf("sending stats.db request failed with %w", err) | ||
} | ||
|
||
idata, err := response.Get("stats") | ||
if err != nil { | ||
return nil, fmt.Errorf("getting stats from response failed with %w", err) | ||
} | ||
|
||
data, ok := idata.([]byte) | ||
if !ok { | ||
return nil, errors.New("incorrect stats type") | ||
} | ||
|
||
s := kvdb.NewStats() | ||
err = s.Decode(data) | ||
if err != nil { | ||
return nil, fmt.Errorf("decoding stats failed with %w", err) | ||
} | ||
|
||
return s, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package tns_test | ||
|
||
import ( | ||
"testing" | ||
|
||
commonIface "github.com/taubyte/tau/core/common" | ||
"github.com/taubyte/tau/core/kvdb" | ||
"github.com/taubyte/tau/dream" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestStats(t *testing.T) { | ||
u := dream.New(dream.UniverseConfig{Name: t.Name()}) | ||
defer u.Stop() | ||
|
||
err := u.StartWithConfig(&dream.Config{ | ||
Services: map[string]commonIface.ServiceConfig{ | ||
"tns": {}, | ||
}, | ||
Simples: map[string]dream.SimpleConfig{ | ||
"client": { | ||
Clients: dream.SimpleConfigClients{ | ||
TNS: &commonIface.ClientConfig{}, | ||
}.Compat(), | ||
}, | ||
}, | ||
}) | ||
assert.NilError(t, err) | ||
|
||
simple, err := u.Simple("client") | ||
assert.NilError(t, err) | ||
|
||
tns, err := simple.TNS() | ||
assert.NilError(t, err) | ||
|
||
stats, err := tns.Stats().Database() | ||
assert.NilError(t, err) | ||
|
||
assert.Equal(t, stats.Type(), kvdb.TypeCRDT) | ||
|
||
assert.Equal(t, len(stats.Heads()), 0) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.