DataSourceSettingsPage.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. import { hot } from 'react-hot-loader';
  4. import { connect } from 'react-redux';
  5. // Components
  6. import Page from 'app/core/components/Page/Page';
  7. import PluginSettings from './PluginSettings';
  8. import BasicSettings from './BasicSettings';
  9. import ButtonRow from './ButtonRow';
  10. // Services & Utils
  11. import appEvents from 'app/core/app_events';
  12. import { getBackendSrv } from 'app/core/services/backend_srv';
  13. import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
  14. // Actions & selectors
  15. import { getDataSource, getDataSourceMeta } from '../state/selectors';
  16. import { deleteDataSource, loadDataSource, setDataSourceName, setIsDefault, updateDataSource } from '../state/actions';
  17. import { getNavModel } from 'app/core/selectors/navModel';
  18. import { getRouteParamsId } from 'app/core/selectors/location';
  19. // Types
  20. import { NavModel, Plugin, StoreState } from 'app/types/';
  21. import { DataSourceSettings } from '@grafana/ui/src/types/';
  22. import { getDataSourceLoadingNav } from '../state/navModel';
  23. import PluginStateinfo from 'app/features/plugins/PluginStateInfo';
  24. export interface Props {
  25. navModel: NavModel;
  26. dataSource: DataSourceSettings;
  27. dataSourceMeta: Plugin;
  28. pageId: number;
  29. deleteDataSource: typeof deleteDataSource;
  30. loadDataSource: typeof loadDataSource;
  31. setDataSourceName: typeof setDataSourceName;
  32. updateDataSource: typeof updateDataSource;
  33. setIsDefault: typeof setIsDefault;
  34. }
  35. interface State {
  36. dataSource: DataSourceSettings;
  37. isTesting?: boolean;
  38. testingMessage?: string;
  39. testingStatus?: string;
  40. }
  41. export class DataSourceSettingsPage extends PureComponent<Props, State> {
  42. constructor(props: Props) {
  43. super(props);
  44. this.state = {
  45. dataSource: {} as DataSourceSettings,
  46. };
  47. }
  48. async componentDidMount() {
  49. const { loadDataSource, pageId } = this.props;
  50. await loadDataSource(pageId);
  51. }
  52. componentDidUpdate(prevProps: Props) {
  53. const { dataSource } = this.props;
  54. if (prevProps.dataSource !== dataSource) {
  55. this.setState({ dataSource });
  56. }
  57. }
  58. onSubmit = async (evt: React.FormEvent<HTMLFormElement>) => {
  59. evt.preventDefault();
  60. await this.props.updateDataSource({ ...this.state.dataSource, name: this.props.dataSource.name });
  61. this.testDataSource();
  62. };
  63. onTest = async (evt: React.FormEvent<HTMLFormElement>) => {
  64. evt.preventDefault();
  65. this.testDataSource();
  66. };
  67. onDelete = () => {
  68. appEvents.emit('confirm-modal', {
  69. title: 'Delete',
  70. text: 'Are you sure you want to delete this data source?',
  71. yesText: 'Delete',
  72. icon: 'fa-trash',
  73. onConfirm: () => {
  74. this.confirmDelete();
  75. },
  76. });
  77. };
  78. confirmDelete = () => {
  79. this.props.deleteDataSource();
  80. };
  81. onModelChange = (dataSource: DataSourceSettings) => {
  82. this.setState({ dataSource });
  83. };
  84. isReadOnly() {
  85. return this.props.dataSource.readOnly === true;
  86. }
  87. renderIsReadOnlyMessage() {
  88. return (
  89. <div className="grafana-info-box span8">
  90. This datasource was added by config and cannot be modified using the UI. Please contact your server admin to
  91. update this datasource.
  92. </div>
  93. );
  94. }
  95. async testDataSource() {
  96. const dsApi = await getDatasourceSrv().get(this.state.dataSource.name);
  97. if (!dsApi.testDatasource) {
  98. return;
  99. }
  100. this.setState({ isTesting: true, testingMessage: 'Testing...', testingStatus: 'info' });
  101. getBackendSrv().withNoBackendCache(async () => {
  102. try {
  103. const result = await dsApi.testDatasource();
  104. this.setState({
  105. isTesting: false,
  106. testingStatus: result.status,
  107. testingMessage: result.message,
  108. });
  109. } catch (err) {
  110. let message = '';
  111. if (err.statusText) {
  112. message = 'HTTP Error ' + err.statusText;
  113. } else {
  114. message = err.message;
  115. }
  116. this.setState({
  117. isTesting: false,
  118. testingStatus: 'error',
  119. testingMessage: message,
  120. });
  121. }
  122. });
  123. }
  124. get hasDataSource() {
  125. return Object.keys(this.props.dataSource).length > 0;
  126. }
  127. render() {
  128. const { dataSource, dataSourceMeta, navModel, setDataSourceName, setIsDefault } = this.props;
  129. const { testingMessage, testingStatus } = this.state;
  130. return (
  131. <Page navModel={navModel}>
  132. <Page.Contents isLoading={!this.hasDataSource}>
  133. {this.hasDataSource && (
  134. <div>
  135. <form onSubmit={this.onSubmit}>
  136. {this.isReadOnly() && this.renderIsReadOnlyMessage()}
  137. <PluginStateinfo state={dataSourceMeta.state} />
  138. <BasicSettings
  139. dataSourceName={dataSource.name}
  140. isDefault={dataSource.isDefault}
  141. onDefaultChange={state => setIsDefault(state)}
  142. onNameChange={name => setDataSourceName(name)}
  143. />
  144. {dataSourceMeta.module && (
  145. <PluginSettings
  146. dataSource={dataSource}
  147. dataSourceMeta={dataSourceMeta}
  148. onModelChange={this.onModelChange}
  149. />
  150. )}
  151. <div className="gf-form-group">
  152. {testingMessage && (
  153. <div className={`alert-${testingStatus} alert`}>
  154. <div className="alert-icon">
  155. {testingStatus === 'error' ? (
  156. <i className="fa fa-exclamation-triangle" />
  157. ) : (
  158. <i className="fa fa-check" />
  159. )}
  160. </div>
  161. <div className="alert-body">
  162. <div className="alert-title">{testingMessage}</div>
  163. </div>
  164. </div>
  165. )}
  166. </div>
  167. <ButtonRow
  168. onSubmit={event => this.onSubmit(event)}
  169. isReadOnly={this.isReadOnly()}
  170. onDelete={this.onDelete}
  171. onTest={event => this.onTest(event)}
  172. />
  173. </form>
  174. </div>
  175. )}
  176. </Page.Contents>
  177. </Page>
  178. );
  179. }
  180. }
  181. function mapStateToProps(state: StoreState) {
  182. const pageId = getRouteParamsId(state.location);
  183. const dataSource = getDataSource(state.dataSources, pageId);
  184. return {
  185. navModel: getNavModel(state.navIndex, `datasource-settings-${pageId}`, getDataSourceLoadingNav('settings')),
  186. dataSource: getDataSource(state.dataSources, pageId),
  187. dataSourceMeta: getDataSourceMeta(state.dataSources, dataSource.type),
  188. pageId: pageId,
  189. };
  190. }
  191. const mapDispatchToProps = {
  192. deleteDataSource,
  193. loadDataSource,
  194. setDataSourceName,
  195. updateDataSource,
  196. setIsDefault,
  197. };
  198. export default hot(module)(
  199. connect(
  200. mapStateToProps,
  201. mapDispatchToProps
  202. )(DataSourceSettingsPage)
  203. );