DataSourceSettingsPage.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. import { hot } from 'react-hot-loader';
  4. import { connect } from 'react-redux';
  5. import isString from 'lodash/isString';
  6. // Components
  7. import Page from 'app/core/components/Page/Page';
  8. import { PluginSettings, GenericDataSourcePlugin } from './PluginSettings';
  9. import BasicSettings from './BasicSettings';
  10. import ButtonRow from './ButtonRow';
  11. // Services & Utils
  12. import appEvents from 'app/core/app_events';
  13. import { getBackendSrv } from 'app/core/services/backend_srv';
  14. import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
  15. // Actions & selectors
  16. import { getDataSource, getDataSourceMeta } from '../state/selectors';
  17. import { deleteDataSource, loadDataSource, setDataSourceName, setIsDefault, updateDataSource } from '../state/actions';
  18. import { getNavModel } from 'app/core/selectors/navModel';
  19. import { getRouteParamsId } from 'app/core/selectors/location';
  20. // Types
  21. import { StoreState, UrlQueryMap } from 'app/types/';
  22. import { NavModel, DataSourceSettings, DataSourcePluginMeta } from '@grafana/ui';
  23. import { getDataSourceLoadingNav } from '../state/navModel';
  24. import PluginStateinfo from 'app/features/plugins/PluginStateInfo';
  25. import { importDataSourcePlugin } from 'app/features/plugins/plugin_loader';
  26. export interface Props {
  27. navModel: NavModel;
  28. dataSource: DataSourceSettings;
  29. dataSourceMeta: DataSourcePluginMeta;
  30. pageId: number;
  31. deleteDataSource: typeof deleteDataSource;
  32. loadDataSource: typeof loadDataSource;
  33. setDataSourceName: typeof setDataSourceName;
  34. updateDataSource: typeof updateDataSource;
  35. setIsDefault: typeof setIsDefault;
  36. plugin?: GenericDataSourcePlugin;
  37. query: UrlQueryMap;
  38. page?: string;
  39. }
  40. interface State {
  41. dataSource: DataSourceSettings;
  42. plugin?: GenericDataSourcePlugin;
  43. isTesting?: boolean;
  44. testingMessage?: string;
  45. testingStatus?: string;
  46. loadError?: any;
  47. }
  48. export class DataSourceSettingsPage extends PureComponent<Props, State> {
  49. constructor(props: Props) {
  50. super(props);
  51. this.state = {
  52. dataSource: props.dataSource,
  53. plugin: props.plugin,
  54. };
  55. }
  56. async loadPlugin(pluginId?: string) {
  57. const { dataSourceMeta } = this.props;
  58. let importedPlugin: GenericDataSourcePlugin;
  59. try {
  60. importedPlugin = await importDataSourcePlugin(dataSourceMeta);
  61. } catch (e) {
  62. console.log('Failed to import plugin module', e);
  63. }
  64. this.setState({ plugin: importedPlugin });
  65. }
  66. async componentDidMount() {
  67. const { loadDataSource, pageId } = this.props;
  68. if (isNaN(pageId)) {
  69. this.setState({ loadError: 'Invalid ID' });
  70. return;
  71. }
  72. try {
  73. await loadDataSource(pageId);
  74. if (!this.state.plugin) {
  75. await this.loadPlugin();
  76. }
  77. } catch (err) {
  78. this.setState({ loadError: err });
  79. }
  80. }
  81. componentDidUpdate(prevProps: Props) {
  82. const { dataSource } = this.props;
  83. if (prevProps.dataSource !== dataSource) {
  84. this.setState({ dataSource });
  85. }
  86. }
  87. onSubmit = async (evt: React.FormEvent<HTMLFormElement>) => {
  88. evt.preventDefault();
  89. await this.props.updateDataSource({ ...this.state.dataSource });
  90. this.testDataSource();
  91. };
  92. onTest = async (evt: React.FormEvent<HTMLFormElement>) => {
  93. evt.preventDefault();
  94. this.testDataSource();
  95. };
  96. onDelete = () => {
  97. appEvents.emit('confirm-modal', {
  98. title: 'Delete',
  99. text: 'Are you sure you want to delete this data source?',
  100. yesText: 'Delete',
  101. icon: 'fa-trash',
  102. onConfirm: () => {
  103. this.confirmDelete();
  104. },
  105. });
  106. };
  107. confirmDelete = () => {
  108. this.props.deleteDataSource();
  109. };
  110. onModelChange = (dataSource: DataSourceSettings) => {
  111. this.setState({ dataSource });
  112. };
  113. isReadOnly() {
  114. return this.props.dataSource.readOnly === true;
  115. }
  116. renderIsReadOnlyMessage() {
  117. return (
  118. <div className="grafana-info-box span8">
  119. This datasource was added by config and cannot be modified using the UI. Please contact your server admin to
  120. update this datasource.
  121. </div>
  122. );
  123. }
  124. async testDataSource() {
  125. const dsApi = await getDatasourceSrv().get(this.state.dataSource.name);
  126. if (!dsApi.testDatasource) {
  127. return;
  128. }
  129. this.setState({ isTesting: true, testingMessage: 'Testing...', testingStatus: 'info' });
  130. getBackendSrv().withNoBackendCache(async () => {
  131. try {
  132. const result = await dsApi.testDatasource();
  133. this.setState({
  134. isTesting: false,
  135. testingStatus: result.status,
  136. testingMessage: result.message,
  137. });
  138. } catch (err) {
  139. let message = '';
  140. if (err.statusText) {
  141. message = 'HTTP Error ' + err.statusText;
  142. } else {
  143. message = err.message;
  144. }
  145. this.setState({
  146. isTesting: false,
  147. testingStatus: 'error',
  148. testingMessage: message,
  149. });
  150. }
  151. });
  152. }
  153. get hasDataSource() {
  154. return this.state.dataSource.id > 0;
  155. }
  156. renderLoadError(loadError: any) {
  157. let showDelete = false;
  158. let msg = loadError.toString();
  159. if (loadError.data) {
  160. if (loadError.data.message) {
  161. msg = loadError.data.message;
  162. }
  163. } else if (isString(loadError)) {
  164. showDelete = true;
  165. }
  166. const node = {
  167. text: msg,
  168. subTitle: 'Data Source Error',
  169. icon: 'fa fa-fw fa-warning',
  170. };
  171. const nav = {
  172. node: node,
  173. main: node,
  174. };
  175. return (
  176. <Page navModel={nav}>
  177. <Page.Contents>
  178. <div>
  179. <div className="gf-form-button-row">
  180. {showDelete && (
  181. <button type="submit" className="btn btn-danger" onClick={this.onDelete}>
  182. Delete
  183. </button>
  184. )}
  185. <a className="btn btn-inverse" href="datasources">
  186. Back
  187. </a>
  188. </div>
  189. </div>
  190. </Page.Contents>
  191. </Page>
  192. );
  193. }
  194. renderConfigPageBody(page: string) {
  195. const { plugin } = this.state;
  196. if (!plugin || !plugin.configPages) {
  197. return null; // still loading
  198. }
  199. for (const p of plugin.configPages) {
  200. if (p.id === page) {
  201. return <p.body plugin={plugin} query={this.props.query} />;
  202. }
  203. }
  204. return <div>Page Not Found: {page}</div>;
  205. }
  206. renderSettings() {
  207. const { dataSourceMeta, setDataSourceName, setIsDefault } = this.props;
  208. const { testingMessage, testingStatus, dataSource, plugin } = this.state;
  209. return (
  210. <form onSubmit={this.onSubmit}>
  211. {this.isReadOnly() && this.renderIsReadOnlyMessage()}
  212. {dataSourceMeta.state && (
  213. <div className="gf-form">
  214. <label className="gf-form-label width-10">Plugin state</label>
  215. <label className="gf-form-label gf-form-label--transparent">
  216. <PluginStateinfo state={dataSourceMeta.state} />
  217. </label>
  218. </div>
  219. )}
  220. <BasicSettings
  221. dataSourceName={dataSource.name}
  222. isDefault={dataSource.isDefault}
  223. onDefaultChange={state => setIsDefault(state)}
  224. onNameChange={name => setDataSourceName(name)}
  225. />
  226. {plugin && (
  227. <PluginSettings
  228. plugin={plugin}
  229. dataSource={this.state.dataSource}
  230. dataSourceMeta={dataSourceMeta}
  231. onModelChange={this.onModelChange}
  232. />
  233. )}
  234. <div className="gf-form-group">
  235. {testingMessage && (
  236. <div className={`alert-${testingStatus} alert`}>
  237. <div className="alert-icon">
  238. {testingStatus === 'error' ? (
  239. <i className="fa fa-exclamation-triangle" />
  240. ) : (
  241. <i className="fa fa-check" />
  242. )}
  243. </div>
  244. <div className="alert-body">
  245. <div className="alert-title">{testingMessage}</div>
  246. </div>
  247. </div>
  248. )}
  249. </div>
  250. <ButtonRow
  251. onSubmit={event => this.onSubmit(event)}
  252. isReadOnly={this.isReadOnly()}
  253. onDelete={this.onDelete}
  254. onTest={event => this.onTest(event)}
  255. />
  256. </form>
  257. );
  258. }
  259. render() {
  260. const { navModel, page } = this.props;
  261. const { loadError } = this.state;
  262. if (loadError) {
  263. return this.renderLoadError(loadError);
  264. }
  265. return (
  266. <Page navModel={navModel}>
  267. <Page.Contents isLoading={!this.hasDataSource}>
  268. {this.hasDataSource && <div>{page ? this.renderConfigPageBody(page) : this.renderSettings()}</div>}
  269. </Page.Contents>
  270. </Page>
  271. );
  272. }
  273. }
  274. function mapStateToProps(state: StoreState) {
  275. const pageId = getRouteParamsId(state.location);
  276. const dataSource = getDataSource(state.dataSources, pageId);
  277. const page = state.location.query.page as string;
  278. return {
  279. navModel: getNavModel(
  280. state.navIndex,
  281. page ? `datasource-page-${page}` : `datasource-settings-${pageId}`,
  282. getDataSourceLoadingNav('settings')
  283. ),
  284. dataSource: getDataSource(state.dataSources, pageId),
  285. dataSourceMeta: getDataSourceMeta(state.dataSources, dataSource.type),
  286. pageId: pageId,
  287. query: state.location.query,
  288. page,
  289. };
  290. }
  291. const mapDispatchToProps = {
  292. deleteDataSource,
  293. loadDataSource,
  294. setDataSourceName,
  295. updateDataSource,
  296. setIsDefault,
  297. };
  298. export default hot(module)(
  299. connect(
  300. mapStateToProps,
  301. mapDispatchToProps
  302. )(DataSourceSettingsPage)
  303. );