-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from StatCan/fix-kubecost-error
fix: add check for empty response
- Loading branch information
Showing
10 changed files
with
140 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 25 additions & 6 deletions
31
frontend/src/app/main-table/cost-table/cost-table.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,36 @@ | ||
import { Component, Input } from "@angular/core"; | ||
import { AggregateCostResponse } from 'src/app/services/kubecost.service'; | ||
import {Component, Input} from "@angular/core"; | ||
import {AggregateCostResponse} from "src/app/services/kubecost.service"; | ||
|
||
enum AsyncStatus { | ||
PENDING, | ||
SUCCESS, | ||
FAILURE | ||
} | ||
|
||
@Component({ | ||
selector: "app-cost-table", | ||
templateUrl: "./cost-table.component.html", | ||
styleUrls: ["./cost-table.component.scss", "../main-table.component.scss"] | ||
}) | ||
export class CostTableComponent{ | ||
@Input() aggregatedCost: AggregateCostResponse; | ||
@Input() currNamespace:string; | ||
export class CostTableComponent { | ||
@Input() aggregatedCost: AggregateCostResponse; | ||
@Input() currNamespace: string; | ||
|
||
AsyncStatus = AsyncStatus; | ||
|
||
formatCost(value: number): string { | ||
return '$' + (value > 0 ? Math.max(value, 0.01) : 0).toFixed(2) | ||
return "$" + (value > 0 ? Math.max(value, 0.01) : 0).toFixed(2); | ||
} | ||
|
||
getStatus(): AsyncStatus { | ||
if (this.aggregatedCost == null) { | ||
return AsyncStatus.PENDING; | ||
} | ||
|
||
if (this.aggregatedCost instanceof Error) { | ||
return AsyncStatus.FAILURE; | ||
} | ||
|
||
return AsyncStatus.SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,73 @@ | ||
import { Injectable } from "@angular/core"; | ||
import { HttpClient, HttpErrorResponse } from "@angular/common/http"; | ||
import {Injectable} from "@angular/core"; | ||
import {HttpClient, HttpErrorResponse} from "@angular/common/http"; | ||
|
||
import { Observable, throwError } from "rxjs"; | ||
import { tap, catchError } from "rxjs/operators"; | ||
import { environment } from "src/environments/environment"; | ||
|
||
import { | ||
Resp, | ||
SnackType | ||
} from "../utils/types"; | ||
import { SnackBarService } from "./snack-bar.service"; | ||
import {Observable, throwError} from "rxjs"; | ||
import {tap, catchError} from "rxjs/operators"; | ||
import {environment} from "src/environments/environment"; | ||
import {Resp} from "../utils/types"; | ||
|
||
export type AggregateCostResponse = { | ||
code: number, | ||
code: number; | ||
data: { | ||
[namespace: string]: { | ||
aggregation: string, | ||
environment: string, | ||
cpuAllocationAverage: number, | ||
cpuCost: number, | ||
cpuEfficiency: number, | ||
efficiency: number, | ||
gpuAllocationAverage: number, | ||
gpuCost: number, | ||
ramAllocationAverage: number, | ||
ramCost: number, | ||
ramEfficiency: number, | ||
pvAllocationAverage: number, | ||
pvCost: number, | ||
networkCost: number, | ||
sharedCost: number, | ||
totalCost: number | ||
} | ||
}, | ||
message: string | ||
} | ||
aggregation: string; | ||
environment: string; | ||
cpuAllocationAverage: number; | ||
cpuCost: number; | ||
cpuEfficiency: number; | ||
efficiency: number; | ||
gpuAllocationAverage: number; | ||
gpuCost: number; | ||
ramAllocationAverage: number; | ||
ramCost: number; | ||
ramEfficiency: number; | ||
pvAllocationAverage: number; | ||
pvCost: number; | ||
networkCost: number; | ||
sharedCost: number; | ||
totalCost: number; | ||
}; | ||
}; | ||
message: string; | ||
}; | ||
|
||
@Injectable() | ||
export class KubecostService { | ||
|
||
constructor(private http: HttpClient, private snackBar: SnackBarService) { } | ||
constructor(private http: HttpClient) {} | ||
|
||
getAggregateCost(ns: string): Observable<AggregateCostResponse> { | ||
const url = environment.apiUrl + `/api/namespaces/${ns}/cost/aggregated` | ||
|
||
return this.http.get<AggregateCostResponse>(url, { | ||
params: { | ||
aggregation: 'namespace', | ||
namespace: ns, | ||
window: '1d' | ||
} | ||
}).pipe( | ||
tap(res => this.handleBackendError(res)), | ||
catchError(err => this.handleError(err)) | ||
); | ||
} | ||
const url = environment.apiUrl + `/api/namespaces/${ns}/cost/aggregated`; | ||
|
||
return this.http | ||
.get<AggregateCostResponse | Resp>(url, { | ||
params: { | ||
aggregation: "namespace", | ||
namespace: ns, | ||
window: "1d" | ||
} | ||
}) | ||
.pipe( | ||
tap(res => this.handleBackendError(res)), | ||
catchError(err => this.handleError(err)) | ||
) as Observable<AggregateCostResponse>; | ||
} | ||
|
||
// ---------------------------Error Handling---------------------------------- | ||
private handleBackendError(response: { code: number }) { | ||
if (response.code < 200 || response.code >= 300) { | ||
private handleBackendError(response: AggregateCostResponse | Resp) { | ||
if (this.isResp(response) || response.code < 200 || response.code >= 300) { | ||
throw response; | ||
} | ||
} | ||
|
||
private handleError(error: HttpErrorResponse | Resp): Observable<never> { | ||
// The backend returned an unsuccessful response code. | ||
// The response body may contain clues as to what went wrong, | ||
if (error instanceof HttpErrorResponse) { | ||
this.snackBar.show( | ||
`${error.status}: There was an error trying to connect ` + | ||
`to the backend API. ${error.message}`, | ||
SnackType.Error | ||
); | ||
return throwError(error.message); | ||
} else { | ||
// Backend error thrown from handleBackendError | ||
const backendError = error as Resp; | ||
this.snackBar.show(backendError.log, SnackType.Error); | ||
return throwError(backendError.log); | ||
} | ||
private handleError( | ||
error: HttpErrorResponse | AggregateCostResponse | Resp | ||
): Observable<never> { | ||
const message = this.isResp(error) ? error.log : error.message; | ||
return throwError(new Error(message)); | ||
} | ||
|
||
private isResp( | ||
obj: HttpErrorResponse | AggregateCostResponse | Resp | ||
): obj is Resp { | ||
return (obj as Resp).success !== undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters