2022-07-15 08:38:14 -05:00
|
|
|
import { t } from '@lingui/macro';
|
2020-04-15 09:51:51 -05:00
|
|
|
import React, { PureComponent } from 'react';
|
2020-06-29 12:58:47 -05:00
|
|
|
import AutoSizer from 'react-virtualized-auto-sizer';
|
2022-08-18 17:06:33 -05:00
|
|
|
import { firstValueFrom } from 'rxjs';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2022-09-02 17:38:35 -05:00
|
|
|
import { AppEvents, PanelData, SelectableValue, LoadingState } from '@grafana/data';
|
2020-04-27 02:09:05 -05:00
|
|
|
import { selectors } from '@grafana/e2e-selectors';
|
2022-09-02 17:38:35 -05:00
|
|
|
import { locationService } from '@grafana/runtime';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { Button, CodeEditor, Field, Select } from '@grafana/ui';
|
2020-04-15 09:51:51 -05:00
|
|
|
import { appEvents } from 'app/core/core';
|
2021-03-18 11:38:09 -05:00
|
|
|
import { DashboardModel, PanelModel } from 'app/features/dashboard/state';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2022-09-02 17:38:35 -05:00
|
|
|
import { getPanelDataFrames } from '../dashboard/components/DebugWizard/utils';
|
2021-03-18 11:38:09 -05:00
|
|
|
import { getPanelInspectorStyles } from '../inspector/styles';
|
2020-04-15 09:51:51 -05:00
|
|
|
|
2022-09-02 17:38:35 -05:00
|
|
|
import { InspectTab } from './types';
|
|
|
|
|
2020-04-15 09:51:51 -05:00
|
|
|
enum ShowContent {
|
|
|
|
PanelJSON = 'panel',
|
2022-03-30 12:58:49 -05:00
|
|
|
PanelData = 'data',
|
|
|
|
DataFrames = 'frames',
|
2020-04-15 09:51:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const options: Array<SelectableValue<ShowContent>> = [
|
|
|
|
{
|
2022-07-15 08:38:14 -05:00
|
|
|
label: t({ id: 'dashboard.inspect-json.panel-json-label', message: 'Panel JSON' }),
|
|
|
|
description: t({
|
|
|
|
id: 'dashboard.inspect-json.panel-json-description',
|
|
|
|
message: 'The model saved in the dashboard JSON that configures how everything works.',
|
|
|
|
}),
|
2020-04-15 09:51:51 -05:00
|
|
|
value: ShowContent.PanelJSON,
|
|
|
|
},
|
|
|
|
{
|
2022-07-15 08:38:14 -05:00
|
|
|
label: t({ id: 'dashboard.inspect-json.panel-data-label', message: 'Panel data' }),
|
|
|
|
description: t({
|
|
|
|
id: 'dashboard.inspect-json.panel-data-description',
|
|
|
|
message: 'The raw model passed to the panel visualization',
|
|
|
|
}),
|
2022-03-30 12:58:49 -05:00
|
|
|
value: ShowContent.PanelData,
|
2020-04-15 09:51:51 -05:00
|
|
|
},
|
|
|
|
{
|
2022-08-18 17:06:33 -05:00
|
|
|
label: t({ id: 'dashboard.inspect-json.dataframe-label', message: 'DataFrame JSON (from Query)' }),
|
|
|
|
description: t({
|
|
|
|
id: 'dashboard.inspect-json.dataframe-description',
|
|
|
|
message: 'Raw data without transformations and field config applied. ',
|
|
|
|
}),
|
2022-03-30 12:58:49 -05:00
|
|
|
value: ShowContent.DataFrames,
|
2020-04-15 09:51:51 -05:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
onClose: () => void;
|
2021-03-18 11:38:09 -05:00
|
|
|
dashboard?: DashboardModel;
|
|
|
|
panel?: PanelModel;
|
|
|
|
data?: PanelData;
|
2020-04-15 09:51:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
show: ShowContent;
|
|
|
|
text: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class InspectJSONTab extends PureComponent<Props, State> {
|
2021-03-18 11:38:09 -05:00
|
|
|
hasPanelJSON: boolean;
|
|
|
|
|
2020-04-15 09:51:51 -05:00
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
2021-03-18 11:38:09 -05:00
|
|
|
this.hasPanelJSON = !!(props.panel && props.dashboard);
|
2022-03-30 12:58:49 -05:00
|
|
|
// If we are in panel, we want to show PanelJSON, otherwise show DataFrames
|
2020-04-15 09:51:51 -05:00
|
|
|
this.state = {
|
2022-03-30 12:58:49 -05:00
|
|
|
show: this.hasPanelJSON ? ShowContent.PanelJSON : ShowContent.DataFrames,
|
2021-03-18 11:38:09 -05:00
|
|
|
text: this.hasPanelJSON ? getPrettyJSON(props.panel!.getSaveModel()) : getPrettyJSON(props.data),
|
2020-04-15 09:51:51 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-18 17:06:33 -05:00
|
|
|
onSelectChanged = async (item: SelectableValue<ShowContent>) => {
|
|
|
|
const show = await this.getJSONObject(item.value!);
|
2020-06-29 12:58:47 -05:00
|
|
|
const text = getPrettyJSON(show);
|
Chore: Fix all Typescript strict null errors (#26204)
* Chore: Fix typescript strict null errors
* Added new limit
* Fixed ts issue
* fixed tests
* trying to fix type inference
* Fixing more ts errors
* Revert tsconfig option
* Fix
* Fixed code
* More fixes
* fix tests
* Updated snapshot
* Chore: More ts strict null fixes
* More fixes in some really messed up azure config components
* More fixes, current count: 441
* 419
* More fixes
* Fixed invalid initial state in explore
* Fixing tests
* Fixed tests
* Explore fix
* More fixes
* Progress
* Sub 300
* Now at 218
* Progress
* Update
* Progress
* Updated tests
* at 159
* fixed tests
* Progress
* YAy blow 100! at 94
* 10,9,8,7,6,5,4,3,2,1... lift off
* Fixed tests
* Fixed more type errors
Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
2020-07-10 05:46:59 -05:00
|
|
|
this.setState({ text, show: item.value! });
|
2020-04-15 09:51:51 -05:00
|
|
|
};
|
|
|
|
|
2020-06-29 12:58:47 -05:00
|
|
|
// Called onBlur
|
|
|
|
onTextChanged = (text: string) => {
|
2020-04-15 09:51:51 -05:00
|
|
|
this.setState({ text });
|
|
|
|
};
|
|
|
|
|
2022-08-18 17:06:33 -05:00
|
|
|
async getJSONObject(show: ShowContent) {
|
2021-03-18 11:38:09 -05:00
|
|
|
const { data, panel } = this.props;
|
2022-03-30 12:58:49 -05:00
|
|
|
if (show === ShowContent.PanelData) {
|
2021-03-18 11:38:09 -05:00
|
|
|
return data;
|
2020-04-15 09:51:51 -05:00
|
|
|
}
|
Chore: Fix all Typescript strict null errors (#26204)
* Chore: Fix typescript strict null errors
* Added new limit
* Fixed ts issue
* fixed tests
* trying to fix type inference
* Fixing more ts errors
* Revert tsconfig option
* Fix
* Fixed code
* More fixes
* fix tests
* Updated snapshot
* Chore: More ts strict null fixes
* More fixes in some really messed up azure config components
* More fixes, current count: 441
* 419
* More fixes
* Fixed invalid initial state in explore
* Fixing tests
* Fixed tests
* Explore fix
* More fixes
* Progress
* Sub 300
* Now at 218
* Progress
* Update
* Progress
* Updated tests
* at 159
* fixed tests
* Progress
* YAy blow 100! at 94
* 10,9,8,7,6,5,4,3,2,1... lift off
* Fixed tests
* Fixed more type errors
Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
2020-07-10 05:46:59 -05:00
|
|
|
|
2022-03-30 12:58:49 -05:00
|
|
|
if (show === ShowContent.DataFrames) {
|
2022-08-18 17:06:33 -05:00
|
|
|
let d = data;
|
|
|
|
|
|
|
|
// do not include transforms and
|
|
|
|
if (panel && data?.state === LoadingState.Done) {
|
|
|
|
d = await firstValueFrom(
|
|
|
|
panel.getQueryRunner().getData({
|
|
|
|
withFieldConfig: false,
|
|
|
|
withTransforms: false,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return getPanelDataFrames(d);
|
2020-04-15 09:51:51 -05:00
|
|
|
}
|
Chore: Fix all Typescript strict null errors (#26204)
* Chore: Fix typescript strict null errors
* Added new limit
* Fixed ts issue
* fixed tests
* trying to fix type inference
* Fixing more ts errors
* Revert tsconfig option
* Fix
* Fixed code
* More fixes
* fix tests
* Updated snapshot
* Chore: More ts strict null fixes
* More fixes in some really messed up azure config components
* More fixes, current count: 441
* 419
* More fixes
* Fixed invalid initial state in explore
* Fixing tests
* Fixed tests
* Explore fix
* More fixes
* Progress
* Sub 300
* Now at 218
* Progress
* Update
* Progress
* Updated tests
* at 159
* fixed tests
* Progress
* YAy blow 100! at 94
* 10,9,8,7,6,5,4,3,2,1... lift off
* Fixed tests
* Fixed more type errors
Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
2020-07-10 05:46:59 -05:00
|
|
|
|
2021-03-18 11:38:09 -05:00
|
|
|
if (this.hasPanelJSON && show === ShowContent.PanelJSON) {
|
|
|
|
return panel!.getSaveModel();
|
2020-04-15 09:51:51 -05:00
|
|
|
}
|
|
|
|
|
2022-07-15 08:38:14 -05:00
|
|
|
return { note: t({ id: 'dashboard.inspect-json.unknown', message: `Unknown Object: ${show}` }) };
|
Chore: Fix all Typescript strict null errors (#26204)
* Chore: Fix typescript strict null errors
* Added new limit
* Fixed ts issue
* fixed tests
* trying to fix type inference
* Fixing more ts errors
* Revert tsconfig option
* Fix
* Fixed code
* More fixes
* fix tests
* Updated snapshot
* Chore: More ts strict null fixes
* More fixes in some really messed up azure config components
* More fixes, current count: 441
* 419
* More fixes
* Fixed invalid initial state in explore
* Fixing tests
* Fixed tests
* Explore fix
* More fixes
* Progress
* Sub 300
* Now at 218
* Progress
* Update
* Progress
* Updated tests
* at 159
* fixed tests
* Progress
* YAy blow 100! at 94
* 10,9,8,7,6,5,4,3,2,1... lift off
* Fixed tests
* Fixed more type errors
Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
2020-07-10 05:46:59 -05:00
|
|
|
}
|
2020-04-15 09:51:51 -05:00
|
|
|
|
|
|
|
onApplyPanelModel = () => {
|
|
|
|
const { panel, dashboard, onClose } = this.props;
|
2021-03-18 11:38:09 -05:00
|
|
|
if (this.hasPanelJSON) {
|
|
|
|
try {
|
|
|
|
if (!dashboard!.meta.canEdit) {
|
|
|
|
appEvents.emit(AppEvents.alertError, ['Unable to apply']);
|
|
|
|
} else {
|
|
|
|
const updates = JSON.parse(this.state.text);
|
2021-06-15 07:12:32 -05:00
|
|
|
dashboard!.shouldUpdateDashboardPanelFromJSON(updates, panel!);
|
2021-03-18 11:38:09 -05:00
|
|
|
panel!.restoreModel(updates);
|
|
|
|
panel!.refresh();
|
|
|
|
appEvents.emit(AppEvents.alertSuccess, ['Panel model updated']);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Error applying updates', err);
|
|
|
|
appEvents.emit(AppEvents.alertError, ['Invalid JSON text']);
|
2020-04-15 09:51:51 -05:00
|
|
|
}
|
|
|
|
|
2021-03-18 11:38:09 -05:00
|
|
|
onClose();
|
|
|
|
}
|
2020-04-15 09:51:51 -05:00
|
|
|
};
|
|
|
|
|
2022-09-02 17:38:35 -05:00
|
|
|
onShowSupportWizard = () => {
|
|
|
|
const queryParms = locationService.getSearch();
|
|
|
|
queryParms.set('inspectTab', InspectTab.Debug.toString());
|
|
|
|
locationService.push('?' + queryParms.toString());
|
|
|
|
};
|
|
|
|
|
2020-04-15 09:51:51 -05:00
|
|
|
render() {
|
|
|
|
const { dashboard } = this.props;
|
2020-06-30 16:05:34 -05:00
|
|
|
const { show, text } = this.state;
|
2021-03-18 11:38:09 -05:00
|
|
|
const jsonOptions = this.hasPanelJSON ? options : options.slice(1, options.length);
|
2021-01-20 00:59:48 -06:00
|
|
|
const selected = options.find((v) => v.value === show);
|
2020-04-15 09:51:51 -05:00
|
|
|
const isPanelJSON = show === ShowContent.PanelJSON;
|
2021-03-18 11:38:09 -05:00
|
|
|
const canEdit = dashboard && dashboard.meta.canEdit;
|
2020-04-15 09:51:51 -05:00
|
|
|
const styles = getPanelInspectorStyles();
|
|
|
|
|
|
|
|
return (
|
2022-03-16 11:28:09 -05:00
|
|
|
<div className={styles.wrap}>
|
2020-04-27 02:09:05 -05:00
|
|
|
<div className={styles.toolbar} aria-label={selectors.components.PanelInspector.Json.content}>
|
2022-07-15 08:38:14 -05:00
|
|
|
<Field
|
|
|
|
label={t({ id: 'dashboard.inspect-json.select-source', message: 'Select source' })}
|
|
|
|
className="flex-grow-1"
|
|
|
|
>
|
2021-11-01 09:45:23 -05:00
|
|
|
<Select
|
|
|
|
inputId="select-source-dropdown"
|
|
|
|
options={jsonOptions}
|
|
|
|
value={selected}
|
|
|
|
onChange={this.onSelectChanged}
|
|
|
|
/>
|
2020-04-15 09:51:51 -05:00
|
|
|
</Field>
|
2021-03-18 11:38:09 -05:00
|
|
|
{this.hasPanelJSON && isPanelJSON && canEdit && (
|
2020-04-15 09:51:51 -05:00
|
|
|
<Button className={styles.toolbarItem} onClick={this.onApplyPanelModel}>
|
|
|
|
Apply
|
|
|
|
</Button>
|
|
|
|
)}
|
2022-09-02 17:38:35 -05:00
|
|
|
{show === ShowContent.DataFrames && (
|
|
|
|
<Button className={styles.toolbarItem} onClick={this.onShowSupportWizard}>
|
|
|
|
Debug
|
|
|
|
</Button>
|
|
|
|
)}
|
2020-04-15 09:51:51 -05:00
|
|
|
</div>
|
|
|
|
<div className={styles.content}>
|
2020-06-29 12:58:47 -05:00
|
|
|
<AutoSizer disableWidth>
|
|
|
|
{({ height }) => (
|
|
|
|
<CodeEditor
|
|
|
|
width="100%"
|
|
|
|
height={height}
|
|
|
|
language="json"
|
2020-06-30 16:05:34 -05:00
|
|
|
showLineNumbers={true}
|
Chore: Fix all Typescript strict null errors (#26204)
* Chore: Fix typescript strict null errors
* Added new limit
* Fixed ts issue
* fixed tests
* trying to fix type inference
* Fixing more ts errors
* Revert tsconfig option
* Fix
* Fixed code
* More fixes
* fix tests
* Updated snapshot
* Chore: More ts strict null fixes
* More fixes in some really messed up azure config components
* More fixes, current count: 441
* 419
* More fixes
* Fixed invalid initial state in explore
* Fixing tests
* Fixed tests
* Explore fix
* More fixes
* Progress
* Sub 300
* Now at 218
* Progress
* Update
* Progress
* Updated tests
* at 159
* fixed tests
* Progress
* YAy blow 100! at 94
* 10,9,8,7,6,5,4,3,2,1... lift off
* Fixed tests
* Fixed more type errors
Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
2020-07-10 05:46:59 -05:00
|
|
|
showMiniMap={(text && text.length) > 100}
|
2020-06-30 16:05:34 -05:00
|
|
|
value={text || ''}
|
2020-06-29 12:58:47 -05:00
|
|
|
readOnly={!isPanelJSON}
|
|
|
|
onBlur={this.onTextChanged}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</AutoSizer>
|
2020-04-15 09:51:51 -05:00
|
|
|
</div>
|
2022-03-16 11:28:09 -05:00
|
|
|
</div>
|
2020-04-15 09:51:51 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 12:58:47 -05:00
|
|
|
function getPrettyJSON(obj: any): string {
|
|
|
|
return JSON.stringify(obj, null, 2);
|
2020-04-15 09:51:51 -05:00
|
|
|
}
|