2020-04-15 09:51:51 -05:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import { chain } from 'lodash';
|
2020-04-24 01:48:04 -05:00
|
|
|
import { AppEvents, PanelData, SelectableValue } from '@grafana/data';
|
2020-06-29 12:58:47 -05:00
|
|
|
import { Button, CodeEditor, Field, Select } from '@grafana/ui';
|
|
|
|
import AutoSizer from 'react-virtualized-auto-sizer';
|
2020-04-27 02:09:05 -05:00
|
|
|
import { selectors } from '@grafana/e2e-selectors';
|
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';
|
|
|
|
import { getPanelInspectorStyles } from '../inspector/styles';
|
2020-04-15 09:51:51 -05:00
|
|
|
|
|
|
|
enum ShowContent {
|
|
|
|
PanelJSON = 'panel',
|
2021-03-18 11:38:09 -05:00
|
|
|
DataJSON = 'data',
|
2020-04-15 09:51:51 -05:00
|
|
|
DataStructure = 'structure',
|
|
|
|
}
|
|
|
|
|
|
|
|
const options: Array<SelectableValue<ShowContent>> = [
|
|
|
|
{
|
|
|
|
label: 'Panel JSON',
|
2020-05-06 00:40:52 -05:00
|
|
|
description: 'The model saved in the dashboard JSON that configures how everything works.',
|
2020-04-15 09:51:51 -05:00
|
|
|
value: ShowContent.PanelJSON,
|
|
|
|
},
|
|
|
|
{
|
2021-03-18 11:38:09 -05:00
|
|
|
label: 'Data',
|
2020-04-15 09:51:51 -05:00
|
|
|
description: 'The raw model passed to the panel visualization',
|
2021-03-18 11:38:09 -05:00
|
|
|
value: ShowContent.DataJSON,
|
2020-04-15 09:51:51 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'DataFrame structure',
|
|
|
|
description: 'Response info without any values',
|
|
|
|
value: ShowContent.DataStructure,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
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);
|
|
|
|
// If we are in panel, we want to show PanelJSON, otherwise show DataJSON
|
2020-04-15 09:51:51 -05:00
|
|
|
this.state = {
|
2021-03-18 11:38:09 -05:00
|
|
|
show: this.hasPanelJSON ? ShowContent.PanelJSON : ShowContent.DataJSON,
|
|
|
|
text: this.hasPanelJSON ? getPrettyJSON(props.panel!.getSaveModel()) : getPrettyJSON(props.data),
|
2020-04-15 09:51:51 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
onSelectChanged = (item: SelectableValue<ShowContent>) => {
|
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
|
|
|
const show = 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 });
|
|
|
|
};
|
|
|
|
|
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
|
|
|
getJSONObject(show: ShowContent) {
|
2021-03-18 11:38:09 -05:00
|
|
|
const { data, panel } = this.props;
|
|
|
|
if (show === ShowContent.DataJSON) {
|
|
|
|
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
|
|
|
|
2020-04-15 09:51:51 -05:00
|
|
|
if (show === ShowContent.DataStructure) {
|
2021-03-18 11:38:09 -05:00
|
|
|
const series = data?.series;
|
2020-04-15 09:51:51 -05:00
|
|
|
if (!series) {
|
|
|
|
return { note: 'Missing Response Data' };
|
|
|
|
}
|
2021-03-18 11:38:09 -05:00
|
|
|
return data!.series.map((frame) => {
|
2020-07-04 17:14:57 -05:00
|
|
|
const { table, fields, ...rest } = frame as any; // remove 'table' from arrow response
|
2020-04-15 09:51:51 -05:00
|
|
|
return {
|
2020-07-04 17:14:57 -05:00
|
|
|
...rest,
|
2021-01-20 00:59:48 -06:00
|
|
|
fields: frame.fields.map((field) => {
|
|
|
|
return chain(field).omit('values').omit('state').omit('display').value();
|
2020-07-04 17:14:57 -05:00
|
|
|
}),
|
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
|
|
|
}
|
|
|
|
|
2020-06-29 12:58:47 -05:00
|
|
|
return { note: `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
|
|
|
};
|
|
|
|
|
|
|
|
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 (
|
|
|
|
<>
|
2020-04-27 02:09:05 -05:00
|
|
|
<div className={styles.toolbar} aria-label={selectors.components.PanelInspector.Json.content}>
|
2020-04-15 09:51:51 -05:00
|
|
|
<Field label="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}
|
|
|
|
menuShouldPortal
|
|
|
|
/>
|
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>
|
|
|
|
)}
|
|
|
|
</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>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|