Browse Source

Move hastic server url to app config #156 (#158)

master
Alexey Velikiy 6 years ago committed by rozetko
parent
commit
12b2d5ffa7
  1. 4
      build/webpack.base.conf.js
  2. 10
      package.json
  3. 14
      src/config/config_ctrl.ts
  4. 7
      src/config/template.html
  5. 4
      src/module.ts
  6. 4
      src/module_raw.d.ts
  7. 128
      src/panel/graph_panel/graph_ctrl.ts
  8. 4
      src/panel/graph_panel/template.html

4
build/webpack.base.conf.js

@ -50,6 +50,10 @@ module.exports = {
], ],
exclude: /node_modules/, exclude: /node_modules/,
}, },
{
test: /\.html$/,
use: 'raw-loader'
},
{ {
test: /jquery\.flot\.(?!events)/, test: /jquery\.flot\.(?!events)/,
loaders: [ loaders: [

10
package.json

@ -33,16 +33,16 @@
"jquery": "^3.3.1", "jquery": "^3.3.1",
"loader-utils": "^1.1.0", "loader-utils": "^1.1.0",
"md5": "^2.2.1", "md5": "^2.2.1",
"perfect-scrollbar": "^1.3.0",
"raw-loader": "^1.0.0",
"tether-drop": "^1.4.2",
"tinycolor": "0.0.1", "tinycolor": "0.0.1",
"tinycolor2": "^1.4.1",
"ts-jest": "^22.4.6", "ts-jest": "^22.4.6",
"ts-loader": "^4.2.0", "ts-loader": "^4.2.0",
"typescript": "^2.8.3", "typescript": "^2.8.3",
"webpack": "4.7.0", "webpack": "4.7.0",
"webpack-cli": "^2.1.2" "webpack-cli": "^2.1.2"
}, },
"dependencies": { "dependencies": {}
"perfect-scrollbar": "^1.3.0",
"tether-drop": "^1.4.2",
"tinycolor2": "^1.4.1"
}
} }

14
src/config/config_ctrl.ts

@ -0,0 +1,14 @@
import template from './template.html';
class ConfigCtrl {
static template = template;
appModel: any;
constructor() {
if(this.appModel.jsonData === undefined) {
this.appModel.jsonData = {};
}
}
}
export { ConfigCtrl };

7
src/config/template.html

@ -0,0 +1,7 @@
<h3 class="page-heading">Enter your Hastic config</h3>
<div class="gf-form-group">
<div class="gf-form">
<label class="gf-form-label width-10">Hastic server url</label>
<input type="url" class="gf-form-input max-width-20" ng-model='ctrl.appModel.jsonData.hasticServerUrl'/>
</div>
</div>

4
src/module.ts

@ -1 +1,3 @@
export { }; import { ConfigCtrl } from './config/config_ctrl'
export { ConfigCtrl };

4
src/module_raw.d.ts vendored

@ -0,0 +1,4 @@
declare module '*.html' {
const contents: string
export = contents
}

128
src/panel/graph_panel/graph_ctrl.ts

@ -1,6 +1,6 @@
import './series_overrides_ctrl'; import './series_overrides_ctrl';
import template from './template'; import template from './template.html';
import { GraphRenderer } from './graph_renderer'; import { GraphRenderer } from './graph_renderer';
import { GraphLegend } from './graph_legend'; import { GraphLegend } from './graph_legend';
@ -31,6 +31,8 @@ class GraphCtrl extends MetricsPanelCtrl {
hiddenSeries: any = {}; hiddenSeries: any = {};
seriesList: any = []; seriesList: any = [];
dataList: any = []; dataList: any = [];
_backendUrl: string = undefined;
// annotations: any = []; // annotations: any = [];
private _datasourceRequest: DatasourceRequest; private _datasourceRequest: DatasourceRequest;
@ -140,7 +142,7 @@ class GraphCtrl extends MetricsPanelCtrl {
/** @ngInject */ /** @ngInject */
constructor( constructor(
$scope, $injector, $http, $scope, $injector, private $http,
private annotationsSrv, private annotationsSrv,
private keybindingSrv, private keybindingSrv,
private backendSrv: BackendSrv, private backendSrv: BackendSrv,
@ -154,52 +156,7 @@ class GraphCtrl extends MetricsPanelCtrl {
_.defaults(this.panel.legend, this.panelDefaults.legend); _.defaults(this.panel.legend, this.panelDefaults.legend);
_.defaults(this.panel.xaxis, this.panelDefaults.xaxis); _.defaults(this.panel.xaxis, this.panelDefaults.xaxis);
this.processor = new DataProcessor(this.panel);
this.analyticService = new AnalyticService(this.backendURL, $http);
this.runBackendConnectivityCheck();
this.analyticsController = new AnalyticController(this.panel, this.analyticService, this.events);
this.bindDKey();
this.events.on('render', this.onRender.bind(this));
this.events.on('data-received', this.onDataReceived.bind(this));
this.events.on('data-error', this.onDataError.bind(this));
this.events.on('data-snapshot-load', this.onDataSnapshotLoad.bind(this));
this.events.on('init-edit-mode', this.onInitEditMode.bind(this));
this.events.on('init-panel-actions', this.onInitPanelActions.bind(this));
this.events.on('analytic-unit-status-change', async (analyticUnit: AnalyticUnit) => {
if(analyticUnit === undefined) {
throw new Error('analyticUnit is undefined');
}
if(analyticUnit.status === '404') {
await this.analyticsController.removeAnalyticUnit(analyticUnit.id, true);
}
if(analyticUnit.status === 'READY') {
await this.analyticsController.fetchSegments(analyticUnit, +this.range.from, +this.range.to);
}
this.render(this.seriesList);
this.$scope.$digest();
});
this._datasources = {};
appEvents.on('ds-request-response', data => {
let requestConfig = data.config;
this._datasourceRequest = {
url: requestConfig.url,
method: requestConfig.method,
data: requestConfig.data,
params: requestConfig.params,
type: undefined
};
});
this.analyticsController.fetchAnalyticUnitsStatuses();
} }
@ -212,11 +169,18 @@ class GraphCtrl extends MetricsPanelCtrl {
this.bindDKey(); this.bindDKey();
} }
get backendURL(): string { async getBackendURL(): Promise<string> {
if(this.templateSrv.index[BACKEND_VARIABLE_NAME] === undefined) { if(this._backendUrl !== undefined) {
return this._backendUrl;
}
var data = await this.backendSrv.get('/api/plugins/hastic-app/settings');
if(data.jsonData === undefined) {
return undefined;
}
let val = data.jsonData.hasticServerUrl;
if(val === undefined) {
return undefined; return undefined;
} }
let val = this.templateSrv.index[BACKEND_VARIABLE_NAME].current.value;
val = val.replace(/\/+$/, ""); val = val.replace(/\/+$/, "");
return val; return val;
} }
@ -235,12 +199,13 @@ class GraphCtrl extends MetricsPanelCtrl {
} }
async runBackendConnectivityCheck() { async runBackendConnectivityCheck() {
if(this.backendURL === '' || this.backendURL === undefined) { let backendURL = await this.getBackendURL();
if(backendURL === '' || backendURL === undefined) {
appEvents.emit( appEvents.emit(
'alert-warning', 'alert-warning',
[ [
`Dashboard variable $${BACKEND_VARIABLE_NAME} is missing`, `hasticServerUrl is missing`,
`Please set $${BACKEND_VARIABLE_NAME}` `Please set it in config`
] ]
); );
return; return;
@ -253,13 +218,63 @@ class GraphCtrl extends MetricsPanelCtrl {
'alert-success', 'alert-success',
[ [
'Connected to Hastic server', 'Connected to Hastic server',
`Hastic server: "${this.backendURL}"` `Hastic server: "${backendURL}"`
] ]
); );
} }
} }
link(scope, elem, attrs, ctrl) { async link(scope, elem, attrs, ctrl) {
this.processor = new DataProcessor(this.panel);
let backendURL = await this.getBackendURL();
this.analyticService = new AnalyticService(backendURL, this.$http);
this.runBackendConnectivityCheck();
this.analyticsController = new AnalyticController(this.panel, this.analyticService, this.events);
this.bindDKey();
this.events.on('render', this.onRender.bind(this));
this.events.on('data-received', this.onDataReceived.bind(this));
this.events.on('data-error', this.onDataError.bind(this));
this.events.on('data-snapshot-load', this.onDataSnapshotLoad.bind(this));
this.events.on('init-edit-mode', this.onInitEditMode.bind(this));
this.events.on('init-panel-actions', this.onInitPanelActions.bind(this));
this.events.on('analytic-unit-status-change', async (analyticUnit: AnalyticUnit) => {
if(analyticUnit === undefined) {
throw new Error('analyticUnit is undefined');
}
if(analyticUnit.status === '404') {
await this.analyticsController.removeAnalyticUnit(analyticUnit.id, true);
}
if(analyticUnit.status === 'READY') {
await this.analyticsController.fetchSegments(analyticUnit, +this.range.from, +this.range.to);
}
this.render(this.seriesList);
this.$scope.$digest();
});
this._datasources = {};
appEvents.on('ds-request-response', data => {
let requestConfig = data.config;
this._datasourceRequest = {
url: requestConfig.url,
method: requestConfig.method,
data: requestConfig.data,
params: requestConfig.params,
type: undefined
};
});
this.analyticsController.fetchAnalyticUnitsStatuses();
var $graphElem = $(elem[0]).find('#graphPanel'); var $graphElem = $(elem[0]).find('#graphPanel');
var $legendElem = $(elem[0]).find('#graphLegend'); var $legendElem = $(elem[0]).find('#graphLegend');
this._graphRenderer = new GraphRenderer( this._graphRenderer = new GraphRenderer(
@ -607,12 +622,13 @@ class GraphCtrl extends MetricsPanelCtrl {
private async _updatePanelInfo() { private async _updatePanelInfo() {
const datasource = await this._getDatasourceByName(this.panel.datasource); const datasource = await this._getDatasourceByName(this.panel.datasource);
const backendUrl = await this.getBackendURL();
this._panelInfo = { this._panelInfo = {
grafanaVersion: this.contextSrv.version, grafanaVersion: this.contextSrv.version,
grafanaUrl: window.location.host, grafanaUrl: window.location.host,
datasourceType: datasource.type, datasourceType: datasource.type,
hasticServerUrl: this.backendURL hasticServerUrl: backendUrl
}; };
} }

4
src/panel/graph_panel/template.ts → src/panel/graph_panel/template.html

@ -1,8 +1,4 @@
var template = `
<div class="graph-panel" ng-class="{'graph-panel--legend-right': ctrl.panel.legend.rightSide}"> <div class="graph-panel" ng-class="{'graph-panel--legend-right': ctrl.panel.legend.rightSide}">
<div class="graph-panel__chart" id="graphPanel" ng-dblclick="ctrl.zoomOut()" /> <div class="graph-panel__chart" id="graphPanel" ng-dblclick="ctrl.zoomOut()" />
<div class="hastic-graph-legend" id="graphLegend" /> <div class="hastic-graph-legend" id="graphLegend" />
</div> </div>
`;
export default template;
Loading…
Cancel
Save