Browse Source

"Server is offline" error instead of "Unexpected error" #48 (#87)

master
sanke1 6 years ago committed by rozetko
parent
commit
ae597c4644
  1. 3
      src/controllers/analytic_controller.ts
  2. 17
      src/module.ts
  3. 12
      src/partials/tab_analytics.html
  4. 98
      src/services/analytic_service.ts

3
src/controllers/analytic_controller.ts

@ -402,6 +402,9 @@ export class AnalyticController {
return this._serverInfo;
}
public get serverStatus() {
return this._analyticService.isUp;
}
}
function addAlphaToRGB(colorString: string, alpha: number): string {

17
src/module.ts

@ -16,7 +16,6 @@ import { PanelInfo } from './models/info';
import { axesEditorComponent } from './axes_editor';
import { MetricsPanelCtrl, alertTab } from 'grafana/app/plugins/sdk';
import { BackendSrv } from 'grafana/app/core/services/backend_srv';
import { appEvents } from 'grafana/app/core/core'
import config from 'grafana/app/core/config';
@ -28,6 +27,7 @@ const BACKEND_VARIABLE_NAME = 'HASTIC_SERVER_URL';
class GraphCtrl extends MetricsPanelCtrl {
static template = template;
analyticService: AnalyticService;
hiddenSeries: any = {};
seriesList: any = [];
dataList: any = [];
@ -148,7 +148,7 @@ class GraphCtrl extends MetricsPanelCtrl {
/** @ngInject */
constructor(
$scope, $injector, private annotationsSrv,
$scope, $injector, $http, private annotationsSrv,
private keybindingSrv, private backendSrv,
private popoverSrv, private contextSrv,
private alertSrv
@ -163,11 +163,11 @@ class GraphCtrl extends MetricsPanelCtrl {
this.processor = new DataProcessor(this.panel);
var anomalyService = new AnalyticService(this.backendURL, backendSrv as BackendSrv);
this.analyticService = new AnalyticService(this.backendURL, $http, this.alertSrv);
this.runBackendConnectivityCheck();
this.analyticsController = new AnalyticController(this.panel, anomalyService, this.events);
this.analyticsController = new AnalyticController(this.panel, this.analyticService, this.events);
this.anomalyTypes = this.panel.anomalyTypes;
keybindingSrv.bind('d', this.onDKey.bind(this));
@ -224,11 +224,12 @@ class GraphCtrl extends MetricsPanelCtrl {
return;
}
var as = new AnalyticService(this.backendURL, this.backendSrv);
var isOK = await as.isBackendOk();
if(!isOK) {
let connected = await this.analyticService.isBackendOk();
if(connected) {
this.alertSrv.set(
'Can`t connect to Hastic server', `Hastic server: "${this.backendURL}"`, 'warning', 4000
'Connected to Hastic server',
`Hastic server: "${this.backendURL}"`,
'success', 4000
);
}
}

12
src/partials/tab_analytics.html

@ -1,3 +1,12 @@
<div class="gf-form-button-row" ng-if="ctrl.analyticsController.serverStatus === false">
<h5>Hastic server at "{{ctrl.backendURL}}" is not available</h5>
<button class="btn btn-inverse" ng-click="ctrl.runBackendConnectivityCheck()">
<i class="fa fa-plug"></i>
Reconnect to Hastic server
</button>
</div>
<div ng-if="ctrl.analyticsController.serverStatus === true">
<h5> Analytic Units </h5>
<div class="editor-row">
<div class="gf-form" ng-repeat="analyticUnit in ctrl.analyticsController.analyticUnits">
@ -145,4 +154,7 @@
<i class="fa fa-plus"></i>
Add Analytic Unit
</button>
</div>
</div>

98
src/services/analytic_service.ts

@ -5,46 +5,41 @@ import { SegmentsSet } from '../models/segment_set';
import { AnalyticUnitId, AnalyticUnit, AnalyticSegment } from '../models/analytic_unit';
import { ServerInfo } from '../models/info';
import { BackendSrv } from 'grafana/app/core/services/backend_srv';
export class AnalyticService {
constructor(private _backendURL: string, private _backendSrv: BackendSrv) {
private _isUp = false;
constructor(private _backendURL: string, private $http, private alertSrv) {
this.isBackendOk();
}
async postNewItem(
metric: MetricExpanded, datasourceRequest: DatasourceRequest,
newItem: AnalyticUnit, panelId: number
): Promise<AnalyticUnitId> {
let datasource = await this._backendSrv.get(`/api/datasources/name/${metric.datasource}`);
let datasource = await this.get(`/api/datasources/name/${metric.datasource}`);
datasourceRequest.type = datasource.type;
return this._backendSrv.post(
this._backendURL + '/analyticUnits',
{
const response = await this.post(this._backendURL + '/analyticUnits', {
panelUrl: window.location.origin + window.location.pathname + `?panelId=${panelId}&fullscreen`,
type: newItem.type,
name: newItem.name,
metric: metric.toJSON(),
datasource: datasourceRequest
});
return response.id as AnalyticUnitId;
}
).then(res => res.id as AnalyticUnitId);
};
async isBackendOk(): Promise<boolean> {
try {
var data = await this._backendSrv.get(this._backendURL);
// TODO: check version
return true;
} catch(e) {
return false;
}
await this.get(this._backendURL);
return this._isUp;
}
async updateSegments(
id: AnalyticUnitId, addedSegments: SegmentsSet<Segment>, removedSegments: SegmentsSet<Segment>
): Promise<SegmentId[]> {
const getJSONs = (segs: SegmentsSet<Segment>) => segs.getSegments().map(segment => ({
from: segment.from,
to: segment.to
@ -56,11 +51,10 @@ export class AnalyticService {
removedSegments: removedSegments.getSegments().map(s => s.id)
};
var data = await this._backendSrv.patch(this._backendURL + '/segments', payload);
var data = await this.patch(this._backendURL + '/segments', payload);
if(data.addedIds === undefined) {
throw new Error('Server didn`t send addedIds');
}
return data.addedIds as SegmentId[];
}
@ -75,7 +69,7 @@ export class AnalyticService {
if(to !== undefined) {
payload['to'] = to;
}
var data = await this._backendSrv.get(this._backendURL + '/segments', payload);
var data = await this.get(this._backendURL + '/segments', payload);
if(data.segments === undefined) {
throw new Error('Server didn`t return segments array');
}
@ -88,9 +82,7 @@ export class AnalyticService {
throw new Error('id is undefined');
}
let statusCheck = async () => {
var data = await this._backendSrv.get(
this._backendURL + '/analyticUnits/status', { id }
);
var data = await this.get(this._backendURL + '/analyticUnits/status', { id });
return data;
}
@ -102,34 +94,29 @@ export class AnalyticService {
yield await statusCheck();
await timeout();
}
}
async getAlertEnabled(id: AnalyticUnitId): Promise<boolean> {
if(id === undefined) {
throw new Error('id is undefined');
}
var data = await this._backendSrv.get(
this._backendURL + '/alerts', { id }
);
var data = await this.get(this._backendURL + '/alerts', { id });
if(data.enabled === undefined) {
throw new Error('Server didn`t return "enabled"');
}
return data.enabled as boolean;
}
async setAlertEnabled(id: AnalyticUnitId, enabled: boolean): Promise<void> {
if(id === undefined) {
throw new Error('id is undefined');
}
return this._backendSrv.post(
this._backendURL + '/alerts', { id, enabled }
);
return await this.post(this._backendURL + '/alerts', { id, enabled });
}
async getServerInfo(): Promise<ServerInfo> {
let data = await this._backendSrv.get(this._backendURL);
let data = await this.get(this._backendURL);
console.log(data);
return {
nodeVersion: data.nodeVersion,
packageVersion: data.packageVersion,
@ -142,4 +129,51 @@ export class AnalyticService {
};
}
private async get(url, params?) {
try {
let response = await this.$http({ method: 'GET', url, params });
this._isUp = true;
return response.data;
} catch(error) {
this.displayConnectionAlert();
console.error(error);
this._isUp = false;
}
}
private async post(url, data) {
try {
let response = await this.$http({ method: 'POST', url, data });
this._isUp = true;
return response.data;
} catch(error) {
this.displayConnectionAlert();
console.error(error);
this._isUp = false;
}
}
private async patch(url, data) {
try {
let response = await this.$http({ method: 'PATCH', url, data });
this._isUp = true;
return response.data;
} catch(error) {
this.displayConnectionAlert();
console.error(error);
this._isUp = false;
}
}
private displayConnectionAlert() {
this.alertSrv.set(
'No connection to Hastic server',
`Hastic server: "${this._backendURL}"`,
'warning', 4000
);
}
public get isUp() {
return this._isUp;
}
}

Loading…
Cancel
Save