DataSourceSettings.tsx 6.8 KB

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