Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Merge pull request #329 from hawkular/codereview4
Browse files Browse the repository at this point in the history
Refactoring from codereview.
  • Loading branch information
mtho11 committed Jul 16, 2015
2 parents 7495cba + f4a6470 commit 565b035
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
/// limitations under the License.
///

/// <reference path="../../includes.ts"/>
module HawkularAccounts {
export var pluginName = 'hawkular-accounts';
export var log:Logging.Logger = Logger.get(pluginName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
/// limitations under the License.
///

/// <reference path='../../includes.ts'/>
/// <reference path='accountsGlobals.ts'/>
module HawkularAccounts {
export var _module = angular.module(HawkularAccounts.pluginName, ['ui.bootstrap']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ <h3 class="pull-left">Response Time Status</h3>
<!-- Alerts -->
<div class="hk-info-top clearfix">
<h3 class="pull-left" data-toggle="tooltip" data-placement="top" title="" data-original-title="Expand/collapse">
<button class="btn btn-link hk-trigger" ng-click="showAllAlerts = !showAllAlerts"><i class="fa" ng-show="vm.alertList.length > 0" ng-class="showAllAlerts ? 'fa-minus-square-o' : 'fa-plus-square-o'"></i> Alerts <span ng-show="vm.alertList.length > 0">({{showAllAlerts ? vm.alertList.length : vm.math.min(vm.alertList.length, 3)}} of {{vm.alertList.length}})</span></button>
<button class="btn btn-link hk-trigger" ng-click="showAllAlerts = !showAllAlerts">
<i class="fa" ng-show="vm.alertList.length > 0" ng-class="showAllAlerts ? 'fa-minus-square-o' : 'fa-plus-square-o'"></i>
Alerts <span ng-show="vm.alertList.length > 0">({{showAllAlerts ? vm.alertList.length : MetricsViewController.min(vm.alertList.length, 3)}} of {{vm.alertList.length}})</span></button>
</h3>
<span class="hk-settings pull-right"><a href="#" ng-controller="MetricsAlertController as mac" ng-click="mac.openSetup()"><i class="fa fa-cog"></i>Alert Settings</a></span>
</div>
Expand Down
73 changes: 38 additions & 35 deletions console/src/main/scripts/plugins/metrics/ts/alertsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,40 @@ module HawkularMetrics {
}

export interface IHawkularAlertsManager {
addEmailAction(email: string): ng.IPromise<void>;
createAction(email: string): ng.IPromise<void>;
updateTrigger(triggerId: string, data: any): ng.IPromise<void>;
createTrigger(triggerName: string, enabled: boolean, conditionType: string, email: string): ng.IPromise<void>;
deleteTrigger(triggerId: string): ng.IPromise<void>;
createCondition(triggerId: string, condition: any): ng.IPromise<void>;
updateCondition(triggerId: string, conditionId: string, condition: any): ng.IPromise<void>;
createDampening(triggerId: string, duration: number, triggerMode?: string): ng.IPromise<void>;
updateDampening(triggerId: string, dampeningId: string, dampening: any): ng.IPromise<void>;
getAction(email: string): ng.IPromise<void>;
getActions(triggerId: string): ng.IPromise<void>;
getTrigger(triggerId: string): ng.IPromise<void>;
setEmail(triggerId: string, email: string): ng.IPromise<void>;
setResponseTime(triggerId: string, treshold: number, duration: number, enabled: boolean): ng.IPromise<void>;
setDowntime(triggerId: string, duration: number, enabled: boolean): ng.IPromise<void>;
queryConsoleAlerts(metricId: string, startTime?:TimestampInMillis, endTime?:TimestampInMillis, type?:AlertType,
addEmailAction(email: EmailType): ng.IPromise<void>;
createAction(email: EmailType): ng.IPromise<void>;
updateTrigger(triggerId: TriggerId, data: any): ng.IPromise<void>;
createTrigger(triggerName: string, enabled: boolean, conditionType: string, email: EmailType): ng.IPromise<void>;
deleteTrigger(triggerId: TriggerId): ng.IPromise<void>;
createCondition(triggerId: TriggerId, condition: any): ng.IPromise<void>;
updateCondition(triggerId: TriggerId, conditionId: ConditionId, condition: any): ng.IPromise<void>;
createDampening(triggerId: TriggerId, duration: number, triggerMode?: string): ng.IPromise<void>;
updateDampening(triggerId: TriggerId, dampeningId: DampeningId, dampening: any): ng.IPromise<void>;
getAction(email: EmailType): ng.IPromise<void>;
getActions(triggerId: TriggerId): ng.IPromise<void>;
getTrigger(triggerId: TriggerId): ng.IPromise<void>;
setEmail(triggerId: TriggerId, email: EmailType): ng.IPromise<void>;
setResponseTime(triggerId: TriggerId, treshold: number, duration: number, enabled: boolean): ng.IPromise<void>;
setDowntime(triggerId: TriggerId, duration: number, enabled: boolean): ng.IPromise<void>;
queryConsoleAlerts(metricId: MetricId, startTime?:TimestampInMillis, endTime?:TimestampInMillis, type?:AlertType,
currentPage?:number, perPage?:number): any;
}

export class HawkularAlertsManager implements IHawkularAlertsManager{

public static $inject = ['HawkularAlert', '$q', '$log', '$moment'];
public static $inject = ['HawkularAlert', '$q', '$log', '$moment','AlertService'];

constructor(private HawkularAlert: any,
private $q: ng.IQService,
private $log: ng.ILogService,
private $moment: any) {
private $moment: any,
private AlertService:any) {
}

public createTrigger(triggerName: string, enabled: boolean,
conditionType: string, email: string): ng.IPromise<void> {
conditionType: string, email: EmailType): ng.IPromise<void> {
// Create a trigger
var triggerId: string;
var triggerId: TriggerId;
var DEFAULT_RESOLVE_THRESHOLD = 1000;
var DEFAULT_DAMPENING_INTERVAL = 7 * 60000;
var DEFAULT_AUTORESOLVE_INTERVAL = 5 * 60000;
Expand Down Expand Up @@ -121,23 +122,23 @@ module HawkularMetrics {
});
}

public deleteTrigger(triggerId: string): ng.IPromise<void> {
public deleteTrigger(triggerId: TriggerId): ng.IPromise<void> {
return this.HawkularAlert.Trigger.delete({triggerId: triggerId}).$promise;
}

public updateTrigger(triggerId: string, data: any): ng.IPromise<void> {
public updateTrigger(triggerId: TriggerId, data: any): ng.IPromise<void> {
data.id = triggerId;
return this.HawkularAlert.Trigger.put({triggerId: triggerId}, data).$promise;
}

public getAction(email: string): ng.IPromise<void> {
public getAction(email: EmailType): ng.IPromise<void> {
return this.HawkularAlert.Action.get({
pluginId: 'email',
actionId: email
}).$promise;
}

public createAction(email: string): ng.IPromise<void> {
public createAction(email: EmailType): ng.IPromise<void> {
return this.HawkularAlert.Action.save({
actionPlugin: 'email',
actionId: email,
Expand All @@ -146,7 +147,7 @@ module HawkularMetrics {
}).$promise;
}

public addEmailAction(email: string): ng.IPromise<void> {
public addEmailAction(email: EmailType): ng.IPromise<void> {
return this.getAction(email).then((promiseValue: any) => {
return promiseValue;
}, (reason: any) => {
Expand All @@ -158,7 +159,7 @@ module HawkularMetrics {
});
}

public updateAction(email: string): ng.IPromise<void> {
public updateAction(email: EmailType): ng.IPromise<void> {
return this.HawkularAlert.Action.put({
actionPlugin: 'email',
actionId: email,
Expand All @@ -167,15 +168,15 @@ module HawkularMetrics {
}).$promise;
}

public createCondition(triggerId: string, condition: any): ng.IPromise<void> {
public createCondition(triggerId: TriggerId, condition: any): ng.IPromise<void> {
return this.HawkularAlert.Condition.save({triggerId: triggerId}, condition).$promise;
}

public updateCondition(triggerId: string, conditionId: string, condition: any): ng.IPromise<void> {
public updateCondition(triggerId: TriggerId, conditionId: ConditionId, condition: any): ng.IPromise<void> {
return this.HawkularAlert.Condition.put({triggerId: triggerId, conditionId: conditionId}, condition).$promise;
}

public createDampening(triggerId: string, duration: number, triggerMode?: string): ng.IPromise<void> {
public createDampening(triggerId: TriggerId, duration: number, triggerMode?: string): ng.IPromise<void> {
return this.HawkularAlert.Dampening.save({ triggerId: triggerId }, {
triggerId: triggerId,
evalTimeSetting: duration,
Expand All @@ -184,20 +185,20 @@ module HawkularMetrics {
}).$promise;
}

public updateDampening(triggerId: string, dampeningId: string, dampening: any): ng.IPromise<void> {
public updateDampening(triggerId: TriggerId, dampeningId: DampeningId, dampening: any): ng.IPromise<void> {
dampening.dampeningId = dampeningId;
return this.HawkularAlert.Dampening.put({ triggerId: triggerId, dampeningId: dampeningId }, dampening).$promise;
}

public getActions(triggerId:string): ng.IPromise<void> {
public getActions(triggerId:TriggerId): ng.IPromise<void> {
return undefined;
}

public getTrigger(triggerId: string): ng.IPromise<void> {
public getTrigger(triggerId: TriggerId): ng.IPromise<void> {
return this.HawkularAlert.Trigger.get({ triggerId: triggerId }).$promise;
}

public setEmail(triggerId:string, email:string):ng.IPromise<void> {
public setEmail(triggerId:TriggerId, email:EmailType):ng.IPromise<void> {
var actions = this.getActions(triggerId);
return actions.then((actions)=> {

Expand All @@ -219,11 +220,11 @@ module HawkularMetrics {
});
}

public setResponseTime(triggerId:string, treshold:number, duration:number, enabled:boolean):ng.IPromise<void> {
public setResponseTime(triggerId:TriggerId, treshold:number, duration:number, enabled:boolean):ng.IPromise<void> {
return undefined;
}

public setDowntime(triggerId:string, duration:number, enabled:boolean):ng.IPromise<void> {
public setDowntime(triggerId:TriggerId, duration:number, enabled:boolean):ng.IPromise<void> {
return undefined;
}

Expand Down Expand Up @@ -331,6 +332,8 @@ module HawkularMetrics {
};
});
}


}

_module.service('HawkularAlertsManager', HawkularAlertsManager);
Expand Down
78 changes: 43 additions & 35 deletions console/src/main/scripts/plugins/metrics/ts/metricsResponsePage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,29 @@ module HawkularMetrics {
median: number;
}

declare var window: any;
export interface IChartData {
id: MetricId;
startTimeStamp: TimestampInMillis;
endTimeStamp: TimestampInMillis;
dataPoints: IChartDataPoint[];
contextDataPoints: IChartDataPoint[];
annotationDataPoints: IChartDataPoint[];
}

/**
* @ngdoc controller
* @name ChartController
* @description This controller is responsible for handling activity related to the Chart tab.
* @name MetricsViewController
* @description This controller is responsible for handling activity related to the metrics response tab.
* @param $scope
* @param $rootScope for publishing $broadcast events only
* @param $interval
* @param $log
* @param HawkularMetric
* @param HawkularAlert
* @param $routeParams
* @param HawkularAlertsManager
* @param HawkularErrorManager
* @param AlertService
*/
export class MetricsViewController {
/// for minification only
Expand All @@ -59,16 +69,17 @@ module HawkularMetrics {

private bucketedDataPoints:IChartDataPoint[] = [];
private contextDataPoints:IChartDataPoint[] = [];
private chartData:any;
private autoRefreshPromise:ng.IPromise<number>;

private resourceId:ResourceId;
public threshold = 5000; // default to 5 seconds some high number

public chartData:IChartData; // essentially the dto for the screen
public threshold:TimestampInMillis = 5000; // default to 5 seconds some high number
public median = 0;
public percentile95th = 0;
public average = 0;
public math;
public alertList;
public startTimeStamp:TimestampInMillis;
public endTimeStamp:TimestampInMillis;

constructor(private $scope:any,
private $rootScope:any,
Expand All @@ -79,19 +90,16 @@ module HawkularMetrics {
private $routeParams:any,
private HawkularAlertsManager: IHawkularAlertsManager,
private HawkularErrorManager: IHawkularErrorManager,
private AlertService: IAlertService,
public alertList:any,
public startTimeStamp:TimestampInMillis,
public endTimeStamp:TimestampInMillis) {
private AlertService: IAlertService ) {
$scope.vm = this;
this.math = window.Math;

this.startTimeStamp = moment().subtract(1, 'hours').valueOf();
this.endTimeStamp = +moment();

this.resourceId = $scope.hkParams.resourceId;

$scope.$on('RefreshChart', (event) => {
this.$log.debug('RefreshChart Event');
this.refreshChartDataNow(this.getMetricId());
});

Expand All @@ -115,7 +123,7 @@ module HawkularMetrics {
this.autoRefresh(20);
}

private getAlerts(metricId: string, startTime:TimestampInMillis, endTime:TimestampInMillis):void {
private getAlerts(metricId: MetricId, startTime:TimestampInMillis, endTime:TimestampInMillis):void {
this.HawkularAlertsManager.queryConsoleAlerts(metricId, startTime, endTime,
HawkularMetrics.AlertType.THRESHOLD).then((data)=> {
this.alertList = data.alertList;
Expand Down Expand Up @@ -157,10 +165,6 @@ module HawkularMetrics {
});
}

private noDataFoundForId(resourceId:ResourceId):void {
this.$log.warn('No Data found for id: ' + resourceId);
///this.AlertService.warning('No Data found for id: ' + id);
}

private refreshChartDataNow(metricId:MetricId, startTime?:TimestampInMillis):void {
this.$scope.hkEndTimestamp = +moment();
Expand All @@ -177,21 +181,10 @@ module HawkularMetrics {
return this.resourceId + '.status.duration';
}

public retrieveThreshold() {
this.HawkularAlert.Condition.query({triggerId: this.$routeParams.resourceId + '_trigger_thres'}).$promise
.then((response) => {

if (response[0]) {
this.threshold = response[0].threshold;
}

}, (error) => {
this.AlertService.error('Error Loading Threshold Data: ' + error);
});
public static min(a:number, b:number):number {
return Math.min(a,b);
}



public refreshSummaryData(metricId:MetricId,
startTime?:TimestampInMillis,
endTime?:TimestampInMillis):void {
Expand All @@ -214,7 +207,7 @@ module HawkularMetrics {
.then((response) => {

dataPoints = this.formatBucketedChartOutput(response);
console.dir(dataPoints);
///console.dir(dataPoints);

this.median = Math.round(_.last(dataPoints).median);
this.percentile95th = Math.round(_.last(dataPoints).percentile95th);
Expand All @@ -227,10 +220,25 @@ module HawkularMetrics {
}
}

private retrieveThreshold() {
this.HawkularAlert.Condition.query({triggerId: this.$routeParams.resourceId + '_trigger_thres'}).$promise
.then((response) => {

if (response[0]) {
this.threshold = response[0].threshold;
}

}, (error) => {
this.$log.error('Error Loading Threshold data');
toastr.error('Error Loading Threshold Data: ' + error);
});
}


public refreshHistoricalChartDataForTimestamp(metricId:MetricId,
startTime?:TimestampInMillis,
endTime?:TimestampInMillis):void {
// calling refreshChartData without params use the model values
/// calling refreshChartData without params use the model values
if (!endTime) {
endTime = this.endTimeStamp;
}
Expand All @@ -249,7 +257,7 @@ module HawkularMetrics {

// we want to isolate the response from the data we are feeding to the chart
this.bucketedDataPoints = this.formatBucketedChartOutput(response);
console.dir(this.bucketedDataPoints);
///console.dir(this.bucketedDataPoints);

if (this.bucketedDataPoints.length) {
// this is basically the DTO for the chart
Expand All @@ -259,11 +267,11 @@ module HawkularMetrics {
endTimeStamp: endTime,
dataPoints: this.bucketedDataPoints,
contextDataPoints: this.contextDataPoints,
annotationDataPoints: []
annotationDataPoints: null
};

} else {
this.noDataFoundForId(metricId);
this.$log.warn('No Data found for id: ' + metricId);
}

}, (error) => {
Expand Down

0 comments on commit 565b035

Please sign in to comment.