Skip to content

Commit

Permalink
refactor(deployments): add ScaledStat interface
Browse files Browse the repository at this point in the history
Add a ScaledStat interface to provide a common type for ScaledNetworkStat and ScaledMemoryStat

fixes openshiftio/openshift.io#2962
  • Loading branch information
andrewazores authored and sanketpathak committed May 6, 2018
1 parent 81abcd1 commit 4d2fd91
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import { MemoryStat } from '../models/memory-stat';
import { NetworkStat } from '../models/network-stat';
import { Pods } from '../models/pods';
import { ScaledNetStat } from '../models/scaled-net-stat';
import {
instanceOfScaledStat,
ScaledStat
} from '../models/scaled-stat';
import { Stat } from '../models/stat';
import {
DeploymentStatusService,
Expand Down Expand Up @@ -321,7 +325,7 @@ export class DeploymentDetailsComponent {
private getNetStatValue(stat: NetworkStat): { sent: number, received: number } {
let sent: number = stat.sent.used;
let received: number = stat.received.used;
if ((stat.received instanceof ScaledNetStat) && (stat.sent instanceof ScaledNetStat)) {
if (instanceOfScaledStat(stat.sent) && instanceOfScaledStat(stat.received)) {
sent = stat.sent.raw;
received = stat.received.raw;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ import {
MemoryStat,
MemoryUnit
} from './memory-stat';
import { ScaledStat } from './scaled-stat';

import { round } from 'lodash';

export class ScaledMemoryStat implements MemoryStat {
export class ScaledMemoryStat implements MemoryStat, ScaledStat {

private static readonly UNITS = ['bytes', 'KB', 'MB', 'GB'];

public readonly raw: number;
public readonly units: MemoryUnit;

constructor(
public readonly used: number,
public readonly quota: number,
public readonly timestamp?: number
) {
this.raw = used;
let scale = 0;
if (this.used !== 0) {
while (this.used > 1024 && scale < ScaledMemoryStat.UNITS.length) {
Expand Down
3 changes: 2 additions & 1 deletion src/app/space/create/deployments/models/scaled-net-stat.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { MemoryUnit } from './memory-stat';
import { NetStat } from './network-stat';
import { ScaledStat } from './scaled-stat';

import { round } from 'lodash';

export class ScaledNetStat implements NetStat {
export class ScaledNetStat implements NetStat, ScaledStat {

private static readonly UNITS = ['bytes', 'KB', 'MB', 'GB'];

Expand Down
7 changes: 7 additions & 0 deletions src/app/space/create/deployments/models/scaled-stat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface ScaledStat {
readonly raw: number;
}

export function instanceOfScaledStat(object: any): object is ScaledStat {
return 'raw' in object;
}

0 comments on commit 4d2fd91

Please sign in to comment.