DataSourceSettingsPage.tsx 7.6 KB

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