Browse Source

added actions

Peter Holmberg 7 years ago
parent
commit
6dc3e0399d
3 changed files with 56 additions and 1 deletions
  1. 1 0
      .gitignore
  2. 54 1
      public/app/features/datasources/state/actions.ts
  3. 1 0
      public/app/types/series.ts

+ 1 - 0
.gitignore

@@ -75,3 +75,4 @@ debug.test
 /devenv/bulk_alerting_dashboards/*.json
 
 /scripts/build/release_publisher/release_publisher
+*.patch

+ 54 - 1
public/app/features/datasources/state/actions.ts

@@ -1,11 +1,14 @@
 import { ThunkAction } from 'redux-thunk';
 import { DataSource, Plugin, StoreState } from 'app/types';
 import { getBackendSrv } from '../../../core/services/backend_srv';
+import { getDatasourceSrv } from '../../plugins/datasource_srv';
 import { LayoutMode } from '../../../core/components/LayoutSelector/LayoutSelector';
 import { updateLocation, updateNavIndex, UpdateNavIndexAction } from '../../../core/actions';
 import { UpdateLocationAction } from '../../../core/actions/location';
 import { buildNavModel } from './navModel';
 
+import config from '../../../core/config';
+
 export enum ActionTypes {
   LoadDataSources = 'LOAD_DATA_SOURCES',
   LoadDataSourceTypes = 'LOAD_DATA_SOURCE_TYPES',
@@ -159,7 +162,14 @@ export function loadDataSourceTypes(): ThunkResult<void> {
 
 export function updateDataSource(dataSource: DataSource): ThunkResult<void> {
   return async dispatch => {
-    await getBackendSrv().put(`/api/datasources/${dataSource.id}`, dataSource);
+    await getBackendSrv()
+      .put(`/api/datasources/${dataSource.id}`, dataSource)
+      .then(response => {
+        updateFrontendSettings().then(() => {
+          testDataSource(response.name);
+        });
+      });
+
     dispatch(loadDataSource(dataSource.id));
   };
 }
@@ -201,6 +211,49 @@ export function findNewName(dataSources, name) {
   return name;
 }
 
+function updateFrontendSettings() {
+  return getBackendSrv()
+    .get('/api/frontend/settings')
+    .then(settings => {
+      config.datasources = settings.datasources;
+      config.defaultDatasource = settings.defaultDatasource;
+      getDatasourceSrv().init();
+    });
+}
+
+function testDataSource(name) {
+  getDatasourceSrv()
+    .get(name)
+    .then(dataSource => {
+      if (!dataSource.testDatasource) {
+        return;
+      }
+
+      const testing = { done: false, status: 'error', message: '' };
+
+      // make test call in no backend cache context
+      getBackendSrv()
+        .withNoBackendCache(() => {
+          return dataSource
+            .testDatasource()
+            .then(result => {
+              testing.message = result.message;
+              testing.status = result.status;
+            })
+            .catch(err => {
+              if (err.statusText) {
+                testing.message = 'HTTP Error ' + err.statusText;
+              } else {
+                testing.message = err.message;
+              }
+            });
+        })
+        .finally(() => {
+          testing.done = true;
+        });
+    });
+}
+
 function nameHasSuffix(name) {
   return name.endsWith('-', name.length - 1);
 }

+ 1 - 0
public/app/types/series.ts

@@ -88,4 +88,5 @@ export interface DataQueryOptions {
 
 export interface DataSourceApi {
   query(options: DataQueryOptions): Promise<DataQueryResponse>;
+  testDatasource(): Promise<any>;
 }