Skip to content

Commit

Permalink
[feat]: provisioning dashboard cannot be edit (#107)
Browse files Browse the repository at this point in the history
Signed-off-by: kevin <[email protected]>
  • Loading branch information
kevin6025 authored Jul 24, 2023
1 parent ab50140 commit a0369fc
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 7 deletions.
18 changes: 16 additions & 2 deletions http/api/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,33 @@ func (api *DashboardAPI) DeleteDashboardByUID(c *gin.Context) {
// GetDashboardByUID gets dashboard by given uid.
func (api *DashboardAPI) GetDashboardByUID(c *gin.Context) {
uid := c.Param(constant.UID)
dashboard, err := api.deps.DashboardSrv.GetDashboardByUID(c.Request.Context(), uid)
ctx := c.Request.Context()
dashboard, err := api.deps.DashboardSrv.GetDashboardByUID(ctx, uid)
if err != nil {
httppkg.Error(c, err)
return
}
var dashboardMap map[string]any
if err := jsonUnmarshalFn(dashboard.Config, &dashboardMap); err != nil {
if err = jsonUnmarshalFn(dashboard.Config, &dashboardMap); err != nil {
httppkg.Error(c, err)
return
}
dashboardMap[constant.UID] = uid
provisioningDashboard, err := api.deps.DashboardSrv.GetProvisioningDashboard(ctx, uid)
if err != nil {
httppkg.Error(c, err)
return
}
meta := model.NewDashboardMeta()
if provisioningDashboard != nil {
meta.Provisioned = true
if !provisioningDashboard.AllowUIUpdates {
meta.CanEdit = false
}
}
httppkg.OK(c, gin.H{
"dashboard": dashboardMap,
"meta": meta,
})
}

Expand Down
15 changes: 15 additions & 0 deletions http/api/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,20 @@ func TestDashboardAPI_GetDashboardByUID(t *testing.T) {
assert.Equal(t, http.StatusInternalServerError, resp.Code)
},
},
{
name: "get provisioning dashboard failure",
prepare: func() {
var dashboard datatypes.JSON
_ = encoding.JSONUnmarshal([]byte("{}"), &dashboard)
dashboardSrv.EXPECT().GetDashboardByUID(gomock.Any(), "1234").Return(&model.Dashboard{
Config: dashboard,
}, nil)
dashboardSrv.EXPECT().GetProvisioningDashboard(gomock.Any(), "1234").Return(nil, fmt.Errorf("err"))
},
assert: func(resp *httptest.ResponseRecorder) {
assert.Equal(t, http.StatusInternalServerError, resp.Code)
},
},
{
name: "get dashboard successfully",
prepare: func() {
Expand All @@ -324,6 +338,7 @@ func TestDashboardAPI_GetDashboardByUID(t *testing.T) {
dashboardSrv.EXPECT().GetDashboardByUID(gomock.Any(), "1234").Return(&model.Dashboard{
Config: dashboard,
}, nil)
dashboardSrv.EXPECT().GetProvisioningDashboard(gomock.Any(), "1234").Return(&model.DashboardProvisioning{AllowUIUpdates: false}, nil)
},
assert: func(resp *httptest.ResponseRecorder) {
assert.Equal(t, http.StatusOK, resp.Code)
Expand Down
14 changes: 14 additions & 0 deletions model/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ type Dashboard struct {
IsStarred bool `json:"isStarred,omitempty" gorm:"-"`
}

// DashboardMeta represents dashboard metadata.
type DashboardMeta struct {
CanEdit bool `json:"canEdit"`
Provisioned bool `json:"provisioned"`
}

// NewDashboardMeta creates a dashboard metadata.
func NewDashboardMeta() DashboardMeta {
return DashboardMeta{
CanEdit: true,
Provisioned: false,
}
}

// DashboardProvisioning represents dashboard provisioning information.
type DashboardProvisioning struct {
BaseModel
Expand Down
6 changes: 6 additions & 0 deletions model/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,9 @@ func TestDashboard_GetCharts(t *testing.T) {
})
}
}

func TestDashboard_NewDashboardMeta(t *testing.T) {
meta := NewDashboardMeta()
assert.True(t, meta.CanEdit)
assert.False(t, meta.Provisioned)
}
18 changes: 18 additions & 0 deletions service/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ package service

import (
"context"
"errors"
"strings"

"gorm.io/gorm"

"github.com/lindb/linsight/constant"
"github.com/lindb/linsight/model"
dbpkg "github.com/lindb/linsight/pkg/db"
Expand Down Expand Up @@ -53,6 +56,8 @@ type DashboardService interface {
SaveProvisioningDashboard(ctx context.Context, req *model.SaveProvisioningDashboardRequest) error
// RemoveProvisioningDashboard removes provision dashboard if not exist.
RemoveProvisioningDashboard(ctx context.Context, req *model.RemoveProvisioningDashboardRequest) error
// GetProvisioningDashboard returns provisioning dashboard by given dashboard uid.
GetProvisioningDashboard(ctx context.Context, dashboardUID string) (*model.DashboardProvisioning, error)
}

// dashboardService implements DashboardService interface.
Expand Down Expand Up @@ -250,6 +255,19 @@ func (srv *dashboardService) RemoveProvisioningDashboard(ctx context.Context, re
})
}

// GetProvisioningDashboard returns provisioning dashboard by given dashboard uid.
func (srv *dashboardService) GetProvisioningDashboard(ctx context.Context, dashboardUID string) (*model.DashboardProvisioning, error) {
rs := &model.DashboardProvisioning{}
signedUser := util.GetUser(ctx)
if err := srv.db.Get(rs, "dashboard_uid=? and org_id=?", dashboardUID, signedUser.Org.ID); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, err
}
return rs, nil
}

// toggleStar toggles the dashboard star.
func (srv *dashboardService) toggleStar(ctx context.Context, uid string, star bool) error {
dashboard, err := srv.getDashboardByUID(ctx, uid)
Expand Down
48 changes: 48 additions & 0 deletions service/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
gomock "github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"gorm.io/datatypes"
"gorm.io/gorm"

"github.com/lindb/linsight/model"
"github.com/lindb/linsight/pkg/db"
Expand Down Expand Up @@ -511,3 +512,50 @@ func TestDashboardService_SaveProvisioningDashboard(t *testing.T) {
})
}
}

func TestDashboardService_GetProvisioningDashboard(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockDB := db.NewMockDB(ctrl)
srv := NewDashboardService(nil, mockDB)

cases := []struct {
name string
prepare func()
wantErr bool
}{
{
name: "get dashboard failure",
prepare: func() {
mockDB.EXPECT().Get(gomock.Any(), "dashboard_uid=? and org_id=?", "1234", int64(12)).Return(fmt.Errorf("err"))
},
wantErr: true,
},
{
name: "not found",
prepare: func() {
mockDB.EXPECT().Get(gomock.Any(), "dashboard_uid=? and org_id=?", "1234", int64(12)).Return(gorm.ErrRecordNotFound)
},
wantErr: false,
},
{
name: "found dashboard",
prepare: func() {
mockDB.EXPECT().Get(gomock.Any(), "dashboard_uid=? and org_id=?", "1234", int64(12)).Return(nil)
},
wantErr: false,
},
}

for _, tt := range cases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
tt.prepare()
_, err := srv.GetProvisioningDashboard(ctx, "1234")
if tt.wantErr != (err != nil) {
t.Fatal(tt.name)
}
})
}
}
4 changes: 2 additions & 2 deletions web/src/components/dashboard/Panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ const PanelHeader = forwardRef(
}}>
Explore
</Dropdown.Item>
{renderChartRepo()}
<Dropdown.Divider />
{!isStatic && renderChartRepo()}
{isStatic && <Dropdown.Divider />}
{!isStatic && (
<Dropdown.Item
icon={<IconDeleteStroked />}
Expand Down
2 changes: 1 addition & 1 deletion web/src/features/dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ const Dashboard: React.FC = () => {
}}
/>
)}
<SaveDashboard />
{DashboardStore.canEdit() && <SaveDashboard />}
<Button
type="tertiary"
icon={<IconSettingStroked />}
Expand Down
2 changes: 1 addition & 1 deletion web/src/features/dashboard/View.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const View: React.FC = () => {
case RowPanelType:
return <RowPanel key={`${panel.id}`} panel={panel} />;
default:
return <Panel key={`${panel.id}`} panel={panel} shortcutKey menu />;
return <Panel key={`${panel.id}`} panel={panel} shortcutKey menu isStatic={!DashboardStore.canEdit()} />;
}
};

Expand Down
12 changes: 11 additions & 1 deletion web/src/stores/dashboard.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
VisualizationAddPanelType,
} from '@src/constants';
import { DashboardSrv } from '@src/services';
import { Dashboard, GridPos, PanelSetting, Variable, Tracker, Chart } from '@src/types';
import { Dashboard, GridPos, PanelSetting, Variable, Tracker, Chart, DashboardMeta } from '@src/types';
import { ApiKit, ObjectKit } from '@src/utils';
import {
set,
Expand All @@ -49,6 +49,7 @@ class DashboardStore {
private panelSeq = 0;
private dashboardTracker: Tracker<Dashboard> = new Tracker<Dashboard>({});
public dashboard: Dashboard = {};
public meta: DashboardMeta = {} as any;

constructor() {
makeAutoObservable(this);
Expand Down Expand Up @@ -276,6 +277,10 @@ class DashboardStore {
});
}

canEdit(): boolean {
return get(this.meta, 'canEdit', false);
}

async loadDashbaord(dashboardId: string | null) {
try {
// reset panel seq when load dashboard
Expand All @@ -286,6 +291,10 @@ class DashboardStore {
this.dashboard = {
title: ChartPendingAddStore.dashboadTitle || 'New Dashboard',
};
this.meta = {
canEdit: true,
provisioned: false,
};
if (isEmpty(charts)) {
const panelId = this.assignPanelId();
panels.push({
Expand Down Expand Up @@ -315,6 +324,7 @@ class DashboardStore {
try {
const dashboard = await DashboardSrv.getDashboard(`${dashboardId}`);
this.initDashboard(dashboard.dashboard);
this.meta = dashboard.meta;
} catch (err) {
console.warn('load dashobard error', err);
Notification.error(ApiKit.getErrorMsg(err));
Expand Down
6 changes: 6 additions & 0 deletions web/src/types/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { PanelSetting, Query } from '@src/types';

export interface DashboardDetail {
dashboard: Dashboard;
meta: DashboardMeta;
}

export interface Variable {
Expand All @@ -45,6 +46,11 @@ export interface Dashboard {
templating?: Record<string, Variable>;
}

export interface DashboardMeta {
canEdit: boolean;
provisioned: boolean;
}

export interface SearchDashboard {
title?: string;
ownership?: string;
Expand Down

0 comments on commit a0369fc

Please sign in to comment.