fix(inmem): remove view service
parent
0a4ef01294
commit
d7c702671d
|
@ -303,7 +303,10 @@ func (s *Service) RemoveDashboardCell(ctx context.Context, dashboardID platform.
|
|||
}
|
||||
|
||||
if err := s.DeleteView(ctx, d.Cells[idx].ID); err != nil {
|
||||
return err
|
||||
return &platform.Error{
|
||||
Err: err,
|
||||
Op: op,
|
||||
}
|
||||
}
|
||||
|
||||
d.Cells = append(d.Cells[:idx], d.Cells[idx+1:]...)
|
||||
|
@ -442,3 +445,50 @@ func (s *Service) UpdateDashboardCellView(ctx context.Context, dashboardID, cell
|
|||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (s *Service) loadView(ctx context.Context, id platform.ID) (*platform.View, *platform.Error) {
|
||||
i, ok := s.viewKV.Load(id.String())
|
||||
if !ok {
|
||||
return nil, &platform.Error{
|
||||
Code: platform.ENotFound,
|
||||
Msg: "view not found",
|
||||
}
|
||||
}
|
||||
|
||||
d, ok := i.(*platform.View)
|
||||
if !ok {
|
||||
return nil, &platform.Error{
|
||||
Code: platform.EInvalid,
|
||||
Msg: fmt.Sprintf("type %T is not a view", i),
|
||||
}
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// FindViewByID returns a single view by ID.
|
||||
func (s *Service) FindViewByID(ctx context.Context, id platform.ID) (*platform.View, error) {
|
||||
v, pe := s.loadView(ctx, id)
|
||||
if pe != nil {
|
||||
return nil, pe
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// PutView sets view with the current ID.
|
||||
func (s *Service) PutView(ctx context.Context, c *platform.View) error {
|
||||
if c.Properties == nil {
|
||||
c.Properties = platform.EmptyViewProperties{}
|
||||
}
|
||||
s.viewKV.Store(c.ID.String(), c)
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteView removes a view by ID.
|
||||
func (s *Service) DeleteView(ctx context.Context, id platform.ID) error {
|
||||
if _, err := s.FindViewByID(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.viewKV.Delete(id.String())
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -18,11 +18,6 @@ func initDashboardService(f platformtesting.DashboardFields, t *testing.T) (plat
|
|||
t.Fatalf("failed to populate Dashboards")
|
||||
}
|
||||
}
|
||||
for _, b := range f.Views {
|
||||
if err := s.PutView(ctx, b); err != nil {
|
||||
t.Fatalf("failed to populate views")
|
||||
}
|
||||
}
|
||||
return s, OpPrefix, func() {}
|
||||
}
|
||||
|
||||
|
|
139
inmem/view.go
139
inmem/view.go
|
@ -1,139 +0,0 @@
|
|||
package inmem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
platform "github.com/influxdata/influxdb"
|
||||
)
|
||||
|
||||
func (s *Service) loadView(ctx context.Context, id platform.ID) (*platform.View, *platform.Error) {
|
||||
i, ok := s.viewKV.Load(id.String())
|
||||
if !ok {
|
||||
return nil, &platform.Error{
|
||||
Code: platform.ENotFound,
|
||||
Msg: "view not found",
|
||||
}
|
||||
}
|
||||
|
||||
d, ok := i.(*platform.View)
|
||||
if !ok {
|
||||
return nil, &platform.Error{
|
||||
Code: platform.EInvalid,
|
||||
Msg: fmt.Sprintf("type %T is not a view", i),
|
||||
}
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// FindViewByID returns a single view by ID.
|
||||
func (s *Service) FindViewByID(ctx context.Context, id platform.ID) (*platform.View, error) {
|
||||
v, pe := s.loadView(ctx, id)
|
||||
if pe != nil {
|
||||
return nil, &platform.Error{
|
||||
Err: pe,
|
||||
Op: OpPrefix + platform.OpFindViewByID,
|
||||
}
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func filterViewFn(filter platform.ViewFilter) func(d *platform.View) bool {
|
||||
if filter.ID != nil {
|
||||
return func(d *platform.View) bool {
|
||||
return d.ID == *filter.ID
|
||||
}
|
||||
}
|
||||
|
||||
return func(d *platform.View) bool { return true }
|
||||
}
|
||||
|
||||
// FindViews implements platform.ViewService interface.
|
||||
func (s *Service) FindViews(ctx context.Context, filter platform.ViewFilter) ([]*platform.View, int, error) {
|
||||
var ds []*platform.View
|
||||
if filter.ID != nil {
|
||||
d, err := s.FindViewByID(ctx, *filter.ID)
|
||||
if err != nil && platform.ErrorCode(err) != platform.ENotFound {
|
||||
return nil, 0, &platform.Error{
|
||||
Err: err,
|
||||
Op: OpPrefix + platform.OpFindViews,
|
||||
}
|
||||
}
|
||||
if d != nil {
|
||||
ds = append(ds, d)
|
||||
}
|
||||
|
||||
return ds, len(ds), nil
|
||||
}
|
||||
|
||||
var err error
|
||||
filterF := filterViewFn(filter)
|
||||
s.viewKV.Range(func(k, v interface{}) bool {
|
||||
d, ok := v.(*platform.View)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
if filterF(d) {
|
||||
ds = append(ds, d)
|
||||
}
|
||||
return true
|
||||
})
|
||||
return ds, len(ds), err
|
||||
}
|
||||
|
||||
// CreateView implements platform.ViewService interface.
|
||||
func (s *Service) CreateView(ctx context.Context, c *platform.View) error {
|
||||
c.ID = s.IDGenerator.ID()
|
||||
if err := s.PutView(ctx, c); err != nil {
|
||||
return &platform.Error{
|
||||
Err: err,
|
||||
Op: OpPrefix + platform.OpCreateView,
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PutView implements platform.ViewService interface.
|
||||
func (s *Service) PutView(ctx context.Context, c *platform.View) error {
|
||||
if c.Properties == nil {
|
||||
c.Properties = platform.EmptyViewProperties{}
|
||||
}
|
||||
s.viewKV.Store(c.ID.String(), c)
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateView implements platform.ViewService interface.
|
||||
func (s *Service) UpdateView(ctx context.Context, id platform.ID, upd platform.ViewUpdate) (*platform.View, error) {
|
||||
c, err := s.FindViewByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, &platform.Error{
|
||||
Err: err,
|
||||
Op: OpPrefix + platform.OpUpdateView,
|
||||
}
|
||||
}
|
||||
|
||||
if upd.Name != nil {
|
||||
c.Name = *upd.Name
|
||||
}
|
||||
|
||||
if upd.Properties != nil {
|
||||
c.Properties = upd.Properties
|
||||
}
|
||||
|
||||
s.viewKV.Store(c.ID.String(), c)
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// DeleteView implements platform.ViewService interface.
|
||||
func (s *Service) DeleteView(ctx context.Context, id platform.ID) error {
|
||||
if _, err := s.FindViewByID(ctx, id); err != nil {
|
||||
return &platform.Error{
|
||||
Err: err,
|
||||
Op: OpPrefix + platform.OpDeleteView,
|
||||
}
|
||||
}
|
||||
s.viewKV.Delete(id.String())
|
||||
return nil
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
package inmem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
platform "github.com/influxdata/influxdb"
|
||||
platformtesting "github.com/influxdata/influxdb/testing"
|
||||
)
|
||||
|
||||
func initViewService(f platformtesting.ViewFields, t *testing.T) (platform.ViewService, string, func()) {
|
||||
s := NewService()
|
||||
s.IDGenerator = f.IDGenerator
|
||||
ctx := context.TODO()
|
||||
for _, b := range f.Views {
|
||||
if err := s.PutView(ctx, b); err != nil {
|
||||
t.Fatalf("failed to populate Views")
|
||||
}
|
||||
}
|
||||
return s, OpPrefix, func() {}
|
||||
}
|
||||
|
||||
func TestViewService_CreateView(t *testing.T) {
|
||||
platformtesting.CreateView(initViewService, t)
|
||||
}
|
||||
|
||||
func TestViewService_FindViewByID(t *testing.T) {
|
||||
platformtesting.FindViewByID(initViewService, t)
|
||||
}
|
||||
func TestViewService_FindViews(t *testing.T) {
|
||||
platformtesting.FindViews(initViewService, t)
|
||||
}
|
||||
|
||||
func TestViewService_DeleteView(t *testing.T) {
|
||||
platformtesting.DeleteView(initViewService, t)
|
||||
}
|
||||
|
||||
func TestViewService_UpdateView(t *testing.T) {
|
||||
platformtesting.UpdateView(initViewService, t)
|
||||
}
|
Loading…
Reference in New Issue