2022-09-28 02:56:58 -05:00
|
|
|
import { isEqual } from 'lodash';
|
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';
|
2022-10-06 10:34:04 -05:00
|
|
|
import { t } from 'app/core/internationalization';
|
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-16 10:51:14 -05:00
|
|
|
import { getPanelDataFrames } from '../dashboard/components/HelpWizard/utils';
|
2021-03-18 11:38:09 -05:00
|
|
|
import { getPanelInspectorStyles } from '../inspector/styles';
|
2022-09-28 02:56:58 -05:00
|
|
|
import { reportPanelInspectInteraction } from '../search/page/reporting';
|
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-10-06 10:34:04 -05:00
|
|
|
label: t('dashboard.inspect-json.panel-json-label', 'Panel JSON'),
|
|
|
|
description: t(
|
|
|
|
'dashboard.inspect-json.panel-json-description',
|
|
|
|
'The model saved in the dashboard JSON that configures how everything works.'
|
|
|
|
),
|
2020-04-15 09:51:51 -05:00
|
|
|
value: ShowContent.PanelJSON,
|
|
|
|
},
|
|
|
|
{
|
2022-10-06 10:34:04 -05:00
|
|
|
label: t('dashboard.inspect-json.panel-data-label', 'Panel data'),
|
|
|
|
description: t('dashboard.inspect-json.panel-data-description', '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-10-06 10:34:04 -05:00
|
|
|
label: t('dashboard.inspect-json.dataframe-label', 'DataFrame JSON (from Query)'),
|
|
|
|
description: t(
|
|
|
|
'dashboard.inspect-json.dataframe-description',
|
|
|
|
'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-09-28 02:56:58 -05:00
|
|
|
componentDidMount() {
|
|
|
|
// when opening the inspector we want to report the interaction
|
|
|
|
reportPanelInspectInteraction(InspectTab.JSON, 'panelJSON');
|
|
|
|
}
|
|
|
|
|
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) {
|
2022-09-28 02:56:58 -05:00
|
|
|
reportPanelInspectInteraction(InspectTab.JSON, '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-09-28 02:56:58 -05:00
|
|
|
reportPanelInspectInteraction(InspectTab.JSON, 'dataFrame');
|
|
|
|
|
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) {
|
2022-09-28 02:56:58 -05:00
|
|
|
reportPanelInspectInteraction(InspectTab.JSON, 'panelJSON');
|
2021-03-18 11:38:09 -05:00
|
|
|
return panel!.getSaveModel();
|
2020-04-15 09:51:51 -05:00
|
|
|
}
|
|
|
|
|
2022-10-06 10:34:04 -05:00
|
|
|
return { note: t('dashboard.inspect-json.unknown', 'Unknown Object: {{show}}', { 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!);
|
2022-09-28 02:56:58 -05:00
|
|
|
|
|
|
|
//Report relevant updates
|
|
|
|
reportPanelInspectInteraction(InspectTab.JSON, 'apply', {
|
|
|
|
panel_type_changed: panel!.type !== updates.type,
|
|
|
|
panel_id_changed: panel!.id !== updates.id,
|
|
|
|
panel_grid_pos_changed: !isEqual(panel!.gridPos, updates.gridPos),
|
|
|
|
panel_targets_changed: !isEqual(panel!.targets, updates.targets),
|
|
|
|
});
|
|
|
|
|
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-16 10:51:14 -05:00
|
|
|
onShowHelpWizard = () => {
|
2022-09-28 02:56:58 -05:00
|
|
|
reportPanelInspectInteraction(InspectTab.JSON, 'supportWizard');
|
2022-09-02 17:38:35 -05:00
|
|
|
const queryParms = locationService.getSearch();
|
2022-09-16 10:51:14 -05:00
|
|
|
queryParms.set('inspectTab', InspectTab.Help.toString());
|
2022-09-02 17:38:35 -05:00
|
|
|
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-10-06 10:34:04 -05:00
|
|
|
<Field label={t('dashboard.inspect-json.select-source', '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 && (
|
2022-09-16 10:51:14 -05:00
|
|
|
<Button className={styles.toolbarItem} onClick={this.onShowHelpWizard}>
|
2022-09-09 16:35:52 -05:00
|
|
|
Support
|
2022-09-02 17:38:35 -05:00
|
|
|
</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 {
|
2022-10-12 02:52:23 -05:00
|
|
|
let r = '';
|
|
|
|
try {
|
|
|
|
r = JSON.stringify(obj, null, 2);
|
|
|
|
} catch (e) {
|
|
|
|
if (
|
|
|
|
e instanceof Error &&
|
|
|
|
(e.toString().includes('RangeError') || e.toString().includes('allocation size overflow'))
|
|
|
|
) {
|
|
|
|
appEvents.emit(AppEvents.alertError, [e.toString(), 'Cannot display JSON, the object is too big.']);
|
|
|
|
} else {
|
|
|
|
appEvents.emit(AppEvents.alertError, [e instanceof Error ? e.toString() : e]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
2020-04-15 09:51:51 -05:00
|
|
|
}
|