frontend refactors

This commit is contained in:
Ryan McKinley 2024-07-01 16:04:53 -07:00
parent 1fb97cf417
commit b25e735c02
12 changed files with 41 additions and 44 deletions

View File

@ -34,7 +34,7 @@ func ProvideResourceServer(db db.DB, cfg *setting.Cfg, features featuremgmt.Feat
// Create a local blob filesystem blob store // Create a local blob filesystem blob store
if supportBlobs { if supportBlobs {
dir := filepath.Join(cfg.DataPath, "unistore", "blobs") dir := filepath.Join(cfg.DataPath, "unistore", "blobs")
if err := os.MkdirAll(dir, 0o755); err != nil { if err := os.MkdirAll(dir, 0o750); err != nil {
return nil, err return nil, err
} }
@ -77,7 +77,7 @@ func ProvideResourceServer(db db.DB, cfg *setting.Cfg, features featuremgmt.Feat
opts.Lifecycle = bridge opts.Lifecycle = bridge
} else { } else {
dir := filepath.Join(cfg.DataPath, "unistore", "resource") dir := filepath.Join(cfg.DataPath, "unistore", "resource")
if err := os.MkdirAll(dir, 0o755); err != nil { if err := os.MkdirAll(dir, 0o750); err != nil {
return nil, err return nil, err
} }

View File

@ -33,6 +33,7 @@ github.com/fullstorydev/grpchan v1.1.1 h1:heQqIJlAv5Cnks9a70GRL2EJke6QQoUB25VGR6
github.com/go-jose/go-jose/v3 v3.0.3 h1:fFKWeig/irsp7XD2zBxvnmA/XaRWp5V3CBsZXJF7G7k= github.com/go-jose/go-jose/v3 v3.0.3 h1:fFKWeig/irsp7XD2zBxvnmA/XaRWp5V3CBsZXJF7G7k=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=

View File

@ -25,7 +25,7 @@ import {
transformSaveModelToScene, transformSaveModelToScene,
} from '../serialization/transformSaveModelToScene'; } from '../serialization/transformSaveModelToScene';
import { DecoratedRevisionModel } from '../settings/VersionsEditView'; import { DecoratedRevisionModel } from '../settings/VersionsEditView';
import { historySrv } from '../settings/version-history/HistorySrv'; import { getHistorySrv } from '../settings/version-history/HistorySrv';
import { dashboardSceneGraph } from '../utils/dashboardSceneGraph'; import { dashboardSceneGraph } from '../utils/dashboardSceneGraph';
import { djb2Hash } from '../utils/djb2Hash'; import { djb2Hash } from '../utils/djb2Hash';
import { findVizPanelByKey } from '../utils/utils'; import { findVizPanelByKey } from '../utils/utils';
@ -1137,7 +1137,7 @@ describe('DashboardScene', () => {
version: 4, version: 4,
}); });
jest.mocked(historySrv.restoreDashboard).mockResolvedValue({ version: newVersion }); jest.mocked(getHistorySrv().restoreDashboard).mockResolvedValue({ version: newVersion });
jest.mocked(transformSaveModelToScene).mockReturnValue(mockScene); jest.mocked(transformSaveModelToScene).mockReturnValue(mockScene);
return scene.onRestore(getVersionMock()).then((res) => { return scene.onRestore(getVersionMock()).then((res) => {
@ -1150,7 +1150,7 @@ describe('DashboardScene', () => {
it('should return early if historySrv does not return a valid version number', () => { it('should return early if historySrv does not return a valid version number', () => {
jest jest
.mocked(historySrv.restoreDashboard) .mocked(getHistorySrv().restoreDashboard)
.mockResolvedValueOnce({ version: null }) .mockResolvedValueOnce({ version: null })
.mockResolvedValueOnce({ version: undefined }) .mockResolvedValueOnce({ version: undefined })
.mockResolvedValueOnce({ version: Infinity }) .mockResolvedValueOnce({ version: Infinity })

View File

@ -49,7 +49,7 @@ import {
import { gridItemToPanel } from '../serialization/transformSceneToSaveModel'; import { gridItemToPanel } from '../serialization/transformSceneToSaveModel';
import { DecoratedRevisionModel } from '../settings/VersionsEditView'; import { DecoratedRevisionModel } from '../settings/VersionsEditView';
import { DashboardEditView } from '../settings/utils'; import { DashboardEditView } from '../settings/utils';
import { historySrv } from '../settings/version-history'; import { getHistorySrv } from '../settings/version-history';
import { DashboardModelCompatibilityWrapper } from '../utils/DashboardModelCompatibilityWrapper'; import { DashboardModelCompatibilityWrapper } from '../utils/DashboardModelCompatibilityWrapper';
import { dashboardSceneGraph, getLibraryVizPanelFromVizPanel } from '../utils/dashboardSceneGraph'; import { dashboardSceneGraph, getLibraryVizPanelFromVizPanel } from '../utils/dashboardSceneGraph';
import { djb2Hash } from '../utils/djb2Hash'; import { djb2Hash } from '../utils/djb2Hash';
@ -355,19 +355,20 @@ export class DashboardScene extends SceneObjectBase<DashboardSceneState> {
} }
public onRestore = async (version: DecoratedRevisionModel): Promise<boolean> => { public onRestore = async (version: DecoratedRevisionModel): Promise<boolean> => {
const versionRsp = await historySrv.restoreDashboard(version.uid, version.version); const versionRsp = await getHistorySrv().restoreDashboard(version.uid, version.version);
if (!Number.isInteger(versionRsp.version)) { const rev = (versionRsp as SaveDashboardResponseDTO).version;
if (!Number.isInteger(version)) {
return false; return false;
} }
const dashboardDTO: DashboardDTO = { const dashboardDTO: DashboardDTO = {
dashboard: new DashboardModel(version.data), dashboard: new DashboardModel(version.data!),
meta: this.state.meta, meta: this.state.meta,
}; };
const dashScene = transformSaveModelToScene(dashboardDTO); const dashScene = transformSaveModelToScene(dashboardDTO);
const newState = sceneUtils.cloneSceneObjectState(dashScene.state); const newState = sceneUtils.cloneSceneObjectState(dashScene.state);
newState.version = versionRsp.version; newState.version = rev;
this.setState(newState); this.setState(newState);
this.exitEditMode({ skipConfirm: true, restoreInitialState: false }); this.exitEditMode({ skipConfirm: true, restoreInitialState: false });

View File

@ -4,7 +4,7 @@ import { DashboardScene } from '../scene/DashboardScene';
import { activateFullSceneTree } from '../utils/test-utils'; import { activateFullSceneTree } from '../utils/test-utils';
import { VERSIONS_FETCH_LIMIT, VersionsEditView } from './VersionsEditView'; import { VERSIONS_FETCH_LIMIT, VersionsEditView } from './VersionsEditView';
import { historySrv } from './version-history'; import { getHistorySrv } from './version-history';
jest.mock('./version-history/HistorySrv'); jest.mock('./version-history/HistorySrv');
@ -20,7 +20,7 @@ describe('VersionsEditView', () => {
} as unknown as React.FormEvent<HTMLInputElement>; } as unknown as React.FormEvent<HTMLInputElement>;
beforeEach(async () => { beforeEach(async () => {
jest.mocked(historySrv.getHistoryList).mockResolvedValue(getVersions()); jest.mocked(getHistorySrv().getHistoryList).mockResolvedValue(getVersions());
const result = await buildTestScene(); const result = await buildTestScene();
dashboard = result.dashboard; dashboard = result.dashboard;
@ -81,7 +81,7 @@ describe('VersionsEditView', () => {
versionsView.onCheck(mockEvent, 4); versionsView.onCheck(mockEvent, 4);
jest jest
.mocked(historySrv.getDashboardVersion) .mocked(getHistorySrv().getDashboardVersion)
.mockResolvedValueOnce({ data: 'lhs' }) .mockResolvedValueOnce({ data: 'lhs' })
.mockResolvedValue({ data: 'rhs' }); .mockResolvedValue({ data: 'rhs' });
@ -100,7 +100,7 @@ describe('VersionsEditView', () => {
versionsView.onCheck(mockEvent, 2); versionsView.onCheck(mockEvent, 2);
jest jest
.mocked(historySrv.getDashboardVersion) .mocked(getHistorySrv().getDashboardVersion)
.mockResolvedValueOnce({ data: 'lhs' }) .mockResolvedValueOnce({ data: 'lhs' })
.mockResolvedValue({ data: 'rhs' }); .mockResolvedValue({ data: 'rhs' });

View File

@ -16,16 +16,10 @@ import {
VersionHistoryTable, VersionHistoryTable,
VersionsHistoryButtons, VersionsHistoryButtons,
} from './version-history'; } from './version-history';
import { VersionModel, getHistorySrv } from './version-history/HistorySrv'; import { DecoratedRevisionModel, VersionModel, getHistorySrv } from './version-history/HistorySrv';
export const VERSIONS_FETCH_LIMIT = 10; export const VERSIONS_FETCH_LIMIT = 10;
export type DecoratedRevisionModel = VersionModel & {
checked: boolean;
createdDateString: string;
ageString: string;
};
export interface VersionsEditViewState extends DashboardEditViewState { export interface VersionsEditViewState extends DashboardEditViewState {
versions?: DecoratedRevisionModel[]; versions?: DecoratedRevisionModel[];
isLoading?: boolean; isLoading?: boolean;

View File

@ -7,19 +7,28 @@ export interface HistoryListOpts {
start: number; start: number;
} }
// The raw version from // The raw version returned from api
export interface VersionModel { export interface VersionModel {
uid: string; uid: string;
version: number | string; // resourceVersion in k8s version: number; // resourceVersion in k8s must be numeric
created: string; created: string;
createdBy: string; createdBy: string;
message: string; message: string;
} }
// The version used in UI components
export type DecoratedRevisionModel = VersionModel & {
checked: boolean;
createdDateString: string;
ageString: string;
data?: Dashboard
};
export interface HistorySrv { export interface HistorySrv {
getHistoryList(dashboardUID: string, options: HistoryListOpts): Promise<VersionModel[]>; getHistoryList(dashboardUID: string, options: HistoryListOpts): Promise<VersionModel[]>;
getDashboardVersion(dashboardUID: string, version: number | string): Promise<Dashboard | {}>; // Just the spec (for now) getDashboardVersion(dashboardUID: string, version: number | string): Promise<Dashboard | {}>; // Just the spec (for now)
restoreDashboard(dashboardUID: string, version: number | string): Promise<SaveDashboardResponseDTO | {}>; restoreDashboard(dashboardUID: string, version: number | string): Promise<SaveDashboardResponseDTO>;
} }
class LegacyHistorySrv implements HistorySrv { class LegacyHistorySrv implements HistorySrv {
@ -40,9 +49,9 @@ class LegacyHistorySrv implements HistorySrv {
return info.data; // the dashboard body return info.data; // the dashboard body
} }
restoreDashboard(dashboardUID: string, version: number): Promise<SaveDashboardResponseDTO | {}> { restoreDashboard(dashboardUID: string, version: number): Promise<SaveDashboardResponseDTO> {
if (typeof dashboardUID !== 'string') { if (typeof dashboardUID !== 'string') {
return Promise.resolve({}); return Promise.resolve({} as unknown as SaveDashboardResponseDTO);
} }
const url = `api/dashboards/uid/${dashboardUID}/restore`; const url = `api/dashboards/uid/${dashboardUID}/restore`;

View File

@ -1,7 +1,7 @@
import { ConfirmModal } from '@grafana/ui'; import { ConfirmModal } from '@grafana/ui';
import { useAppNotification } from 'app/core/copy/appNotification'; import { useAppNotification } from 'app/core/copy/appNotification';
import { DecoratedRevisionModel } from '../VersionsEditView'; import { DecoratedRevisionModel } from './HistorySrv';
export interface RevertDashboardModalProps { export interface RevertDashboardModalProps {
hideModal: () => void; hideModal: () => void;

View File

@ -1,4 +1,4 @@
export { HistorySrv, historySrv, RevisionsModel } from './HistorySrv'; export { HistorySrv, getHistorySrv } from './HistorySrv';
export { VersionHistoryTable } from './VersionHistoryTable'; export { VersionHistoryTable } from './VersionHistoryTable';
export { VersionHistoryHeader } from './VersionHistoryHeader'; export { VersionHistoryHeader } from './VersionHistoryHeader';
export { VersionsHistoryButtons } from './VersionHistoryButtons'; export { VersionsHistoryButtons } from './VersionHistoryButtons';

View File

@ -4,11 +4,10 @@ import * as React from 'react';
import { Spinner, HorizontalGroup } from '@grafana/ui'; import { Spinner, HorizontalGroup } from '@grafana/ui';
import { Page } from 'app/core/components/Page/Page'; import { Page } from 'app/core/components/Page/Page';
import { import {
RevisionsModel,
VersionHistoryHeader, VersionHistoryHeader,
VersionsHistoryButtons, VersionsHistoryButtons,
} from 'app/features/dashboard-scene/settings/version-history'; } from 'app/features/dashboard-scene/settings/version-history';
import { getHistorySrv } from 'app/features/dashboard-scene/settings/version-history/HistorySrv'; import { DecoratedRevisionModel, VersionModel, getHistorySrv } from 'app/features/dashboard-scene/settings/version-history/HistorySrv';
import { VersionHistoryComparison } from '../VersionHistory/VersionHistoryComparison'; import { VersionHistoryComparison } from '../VersionHistory/VersionHistoryComparison';
import { VersionHistoryTable } from '../VersionHistory/VersionHistoryTable'; import { VersionHistoryTable } from '../VersionHistory/VersionHistoryTable';
@ -28,11 +27,6 @@ type State = {
isNewLatest: boolean; isNewLatest: boolean;
}; };
export type DecoratedRevisionModel = RevisionsModel & {
createdDateString: string;
ageString: string;
};
export const VERSIONS_FETCH_LIMIT = 10; export const VERSIONS_FETCH_LIMIT = 10;
export class VersionsSettings extends PureComponent<Props, State> { export class VersionsSettings extends PureComponent<Props, State> {
@ -100,7 +94,7 @@ export class VersionsSettings extends PureComponent<Props, State> {
}); });
}; };
decorateVersions = (versions: RevisionsModel[]) => decorateVersions = (versions: VersionModel[]) =>
versions.map((version) => ({ versions.map((version) => ({
...version, ...version,
createdDateString: this.props.dashboard.formatDate(version.created), createdDateString: this.props.dashboard.formatDate(version.created),
@ -115,7 +109,7 @@ export class VersionsSettings extends PureComponent<Props, State> {
onCheck = (ev: React.FormEvent<HTMLInputElement>, versionId: number) => { onCheck = (ev: React.FormEvent<HTMLInputElement>, versionId: number) => {
this.setState({ this.setState({
versions: this.state.versions.map((version) => versions: this.state.versions.map((version) =>
version.id === versionId ? { ...version, checked: ev.currentTarget.checked } : version version.version === versionId ? { ...version, checked: ev.currentTarget.checked } : version
), ),
}); });
}; };

View File

@ -4,10 +4,9 @@ import { GrafanaTheme2 } from '@grafana/data';
import { Button, ModalsController, CollapsableSection, useStyles2, Stack, Icon, Box } from '@grafana/ui'; import { Button, ModalsController, CollapsableSection, useStyles2, Stack, Icon, Box } from '@grafana/ui';
import { DiffGroup } from 'app/features/dashboard-scene/settings/version-history/DiffGroup'; import { DiffGroup } from 'app/features/dashboard-scene/settings/version-history/DiffGroup';
import { DiffViewer } from 'app/features/dashboard-scene/settings/version-history/DiffViewer'; import { DiffViewer } from 'app/features/dashboard-scene/settings/version-history/DiffViewer';
import { DecoratedRevisionModel } from 'app/features/dashboard-scene/settings/version-history/HistorySrv';
import { jsonDiff } from 'app/features/dashboard-scene/settings/version-history/utils'; import { jsonDiff } from 'app/features/dashboard-scene/settings/version-history/utils';
import { DecoratedRevisionModel } from '../DashboardSettings/VersionsSettings';
import { RevertDashboardModal } from './RevertDashboardModal'; import { RevertDashboardModal } from './RevertDashboardModal';
type DiffViewProps = { type DiffViewProps = {

View File

@ -3,8 +3,7 @@ import * as React from 'react';
import { GrafanaTheme2 } from '@grafana/data'; import { GrafanaTheme2 } from '@grafana/data';
import { Checkbox, Button, Tag, ModalsController, useStyles2 } from '@grafana/ui'; import { Checkbox, Button, Tag, ModalsController, useStyles2 } from '@grafana/ui';
import { DecoratedRevisionModel } from 'app/features/dashboard-scene/settings/version-history/HistorySrv';
import { DecoratedRevisionModel } from '../DashboardSettings/VersionsSettings';
import { RevertDashboardModal } from './RevertDashboardModal'; import { RevertDashboardModal } from './RevertDashboardModal';
@ -32,7 +31,7 @@ export const VersionHistoryTable = ({ versions, canCompare, onCheck }: VersionsT
</thead> </thead>
<tbody> <tbody>
{versions.map((version, idx) => ( {versions.map((version, idx) => (
<tr key={version.id}> <tr key={version.version}>
<td> <td>
<Checkbox <Checkbox
aria-label={`Toggle selection of version ${version.version}`} aria-label={`Toggle selection of version ${version.version}`}
@ -40,7 +39,7 @@ export const VersionHistoryTable = ({ versions, canCompare, onCheck }: VersionsT
display: 'inline', display: 'inline',
})} })}
checked={version.checked} checked={version.checked}
onChange={(ev) => onCheck(ev, version.id)} onChange={(ev) => onCheck(ev, version.version)}
disabled={!version.checked && canCompare} disabled={!version.checked && canCompare}
/> />
</td> </td>