Skip to content

Commit

Permalink
Refactor to use TS alias types
Browse files Browse the repository at this point in the history
  • Loading branch information
mtho11 committed Apr 20, 2015
1 parent bc69a2d commit 8b40b80
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 34 deletions.
45 changes: 24 additions & 21 deletions plugins/metrics/plugins/metrics/ts/metricsAvailabilityPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
/// See the License for the specific language governing permissions and
/// limitations under the License.

/// <reference path="metricsTypes.ts"/>
/// <reference path="metricsPlugin.ts"/>
/// <reference path="../../includes.ts"/>

module HawkularMetrics {


//


export interface IAvailabilitySummary {
start: number;
end: number;
Expand All @@ -29,7 +33,6 @@ module HawkularMetrics {
empty:boolean;
}


export class MetricsAvailabilityController {
/// for minification only
public static $inject = ['$scope', '$interval', '$log', 'HawkularMetric', '$routeParams'];
Expand All @@ -39,47 +42,47 @@ module HawkularMetrics {
private $log:ng.ILogService,
private HawkularMetric:any,
private $routeParams:any,
public startTimeStamp:number,
public endTimeStamp:number) {
public startTimeStamp:TimestampInMillis,
public endTimeStamp:TimestampInMillis) {
$scope.vm = this;

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

this.metricId = $scope.hkParams.resourceId;
this.resourceId = $scope.hkParams.resourceId;

$scope.$watch('hkParams.resourceId', (resourceId) => {
$scope.$watch('hkParams.resourceId', (resourceId:ResourceId) => {
/// made a selection from url switcher
if (resourceId) {
this.metricId = resourceId;
this.refreshAvailPageNow(this.getRawMetricId());
this.resourceId = resourceId;
this.refreshAvailPageNow(this.getResourceId());
}
});

$scope.$on('RefreshAvailabilityChart', (event) => {
this.refreshAvailPageNow(this.getRawMetricId());
this.refreshAvailPageNow(this.getResourceId());
});

this.autoRefreshAvailability(20);
}

private availabilityDataPoints:IChartDataPoint[] = [];
private autoRefreshPromise:ng.IPromise<number>;
private metricId;
private resourceId:ResourceId;

uptimeRatio = 0;
downtimeDuration = 0;
lastDowntime:Date;
downtimeCount = 0;
empty = true;

refreshAvailPageNow(rawMetricId:string, startTime?:number):void {
refreshAvailPageNow(resourceId:ResourceId, startTime?:number):void {
this.$scope.hkEndTimestamp = +moment();
var adjStartTimeStamp:number = +moment().subtract(this.$scope.hkParams.timeOffset, 'milliseconds');
this.endTimeStamp = this.$scope.hkEndTimestamp;
if (rawMetricId) {
this.refreshSummaryAvailabilityData(rawMetricId, startTime ? startTime : adjStartTimeStamp, this.endTimeStamp);
this.refreshAvailDataForTimestamp(rawMetricId, startTime ? startTime : adjStartTimeStamp, this.endTimeStamp);
if (resourceId) {
this.refreshSummaryAvailabilityData(resourceId, startTime ? startTime : adjStartTimeStamp, this.endTimeStamp);
this.refreshAvailDataForTimestamp(resourceId, startTime ? startTime : adjStartTimeStamp, this.endTimeStamp);
}
}

Expand All @@ -88,17 +91,17 @@ module HawkularMetrics {
toastr.info('Canceling Auto Refresh Availability');
}

autoRefreshAvailability(intervalInSeconds:number):void {
autoRefreshAvailability(intervalInSeconds:TimestampInMillis):void {
this.endTimeStamp = this.$scope.hkEndTimestamp;
this.startTimeStamp = this.$scope.hkStartTimestamp;
this.refreshAvailPageNow(this.getRawMetricId());
this.refreshAvailPageNow(this.getResourceId());
this.autoRefreshPromise = this.$interval(() => {
console.info('Autorefresh Availabilty for: ' + this.getRawMetricId());
console.info('Autorefresh Availabilty for: ' + this.getResourceId());
this.$scope.hkEndTimestamp = +moment();
this.endTimeStamp = this.$scope.hkEndTimestamp;
this.$scope.hkStartTimestamp = +moment().subtract(this.$scope.hkParams.timeOffset, 'milliseconds');
this.startTimeStamp = this.$scope.hkStartTimestamp;
this.refreshAvailPageNow(this.getRawMetricId());
this.refreshAvailPageNow(this.getResourceId());
}, intervalInSeconds * 1000);

this.$scope.$on('$destroy', () => {
Expand All @@ -111,7 +114,7 @@ module HawkularMetrics {
///toastr.warning('No Data found for id: ' + id);
}

refreshSummaryAvailabilityData(metricId:string, startTime:number, endTime:number):void {
refreshSummaryAvailabilityData(metricId:MetricId, startTime:TimestampInMillis, endTime:TimestampInMillis):void {

if (metricId) {
this.HawkularMetric.AvailabilityMetricData.query({
Expand Down Expand Up @@ -143,12 +146,12 @@ module HawkularMetrics {
}


getRawMetricId():string {
return this.metricId;
getResourceId():ResourceId {
return this.resourceId;
}


refreshAvailDataForTimestamp(metricId:string, startTime:number, endTime:number):void {
refreshAvailDataForTimestamp(metricId:MetricId, startTime:TimestampInMillis, endTime:TimestampInMillis):void {

if (metricId) {
this.HawkularMetric.AvailabilityMetricData.query({
Expand Down
27 changes: 14 additions & 13 deletions plugins/metrics/plugins/metrics/ts/metricsResponsePage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
/// See the License for the specific language governing permissions and
/// limitations under the License.

/// <reference path="metricsTypes.ts"/>
/// <reference path="metricsPlugin.ts"/>
/// <reference path="../../includes.ts"/>

Expand Down Expand Up @@ -59,23 +60,23 @@ module HawkularMetrics {
private HawkularMetric:any,
private HawkularAlert:any,
private $routeParams:any,
public startTimeStamp:number,
public endTimeStamp:number) {
public startTimeStamp:TimestampInMillis,
public endTimeStamp:TimestampInMillis) {
$scope.vm = this;

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

this.metricId = $scope.hkParams.resourceId;
this.resourceId = $scope.hkParams.resourceId;

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

$scope.$watch('hkParams.resourceId', (resourceId) => {
$scope.$watch('hkParams.resourceId', (resourceId:ResourceId) => {
/// made a selection from url switcher
if (resourceId) {
this.metricId = resourceId;
this.resourceId = resourceId;
this.refreshChartDataNow(this.getMetricId());
}
});
Expand All @@ -88,7 +89,7 @@ module HawkularMetrics {
private chartData:any;
private autoRefreshPromise:ng.IPromise<number>;

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

median = 0;
Expand Down Expand Up @@ -117,13 +118,13 @@ module HawkularMetrics {
});
}

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


refreshChartDataNow(metricId:string, startTime?:number):void {
refreshChartDataNow(metricId:string, startTime?:TimestampInMillis):void {
this.$scope.hkEndTimestamp = +moment();
var adjStartTimeStamp:number = moment().subtract(this.$scope.hkParams.timeOffset, 'milliseconds').valueOf();
this.endTimeStamp = this.$scope.hkEndTimestamp;
Expand All @@ -132,8 +133,8 @@ module HawkularMetrics {
this.retrieveThreshold();
}

getMetricId():string {
return this.metricId + '.status.duration';
getMetricId():ResourceId {
return this.resourceId + '.status.duration';
}


Expand All @@ -149,7 +150,7 @@ module HawkularMetrics {
});
}

refreshSummaryData(metricId:string, startTime?:number, endTime?:number):void {
refreshSummaryData(metricId:string, startTime?:TimestampInMillis, endTime?:TimestampInMillis):void {
var dataPoints:IChartDataPoint[];
// calling refreshChartData without params use the model values
if (!endTime) {
Expand Down Expand Up @@ -185,7 +186,7 @@ module HawkularMetrics {
}


refreshHistoricalChartDataForTimestamp(metricId:string, startTime?:number, endTime?:number):void {
refreshHistoricalChartDataForTimestamp(metricId:string, startTime?:TimestampInMillis, endTime?:TimestampInMillis):void {
// calling refreshChartData without params use the model values
if (!endTime) {
endTime = this.endTimeStamp;
Expand Down
32 changes: 32 additions & 0 deletions plugins/metrics/plugins/metrics/ts/metricsTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
/// and other contributors as indicated by the @author tags.
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.

/// <reference path="metricsPlugin.ts"/>
/// <reference path="../../includes.ts"/>
/// <reference path="errorManager.ts"/>


module HawkularMetrics {

/// Typescript 1.4 introduces type aliases: http://blogs.msdn.com/b/typescript/archive/2015/01/16/announcing-typescript-1-4.aspx
/// Some Type aliases to make things more type safe than just string or number. Implies how it is being used and is especially useful for refactoring.

export type TenantId = string;
export type ResourceId = string;
export type MetricId = string;
export type TimestampInMillis = number;


}

0 comments on commit 8b40b80

Please sign in to comment.