-
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 #27 from StatCan/kubecost-estimates
feat: add cost estimate table
- Loading branch information
Showing
14 changed files
with
276 additions
and
21 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
27 changes: 27 additions & 0 deletions
27
frontend/src/app/main-table/cost-table/cost-table.component.html
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<div class="card mat-elevation-z2 mat-typography"> | ||
<div class="header"> | ||
<mat-icon>attach_money</mat-icon> | ||
<p>Cost</p> | ||
</div> | ||
|
||
<p> | ||
Estimated cost per day of notebook server resources – summed values may not | ||
match total due to rounding | ||
</p> | ||
|
||
<mat-divider></mat-divider> | ||
<table style="width: auto"> | ||
<tr class="mat-header-row" style="text-align: left;"> | ||
<th class="mat-header-cell">Compute</th> | ||
<th class="mat-header-cell">GPUs</th> | ||
<th class="mat-header-cell">Storage</th> | ||
<th class="mat-header-cell" width="99%">Total</th> | ||
</tr> | ||
<tr *ngIf="aggregatedCost?.data[currNamespace]; let cost" class="mat-row" style="text-align: left;"> | ||
<td class="mat-cell">{{ formatCost(cost.cpuCost + cost.ramCost) }}</td> | ||
<td class="mat-cell">{{ formatCost(cost.gpuCost) }}</td> | ||
<td class="mat-cell">{{ formatCost(cost.pvCost) }}</td> | ||
<td class="mat-cell" style="font-weight: 500;">{{ formatCost(cost.totalCost) }}</td> | ||
</tr> | ||
</table> | ||
</div> |
15 changes: 15 additions & 0 deletions
15
frontend/src/app/main-table/cost-table/cost-table.component.scss
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
p { | ||
padding-left: 24px; | ||
padding-right: 16px; | ||
} | ||
|
||
.header p { | ||
padding: 12px 0 0px; | ||
font-weight: 400; | ||
font-size: 20px; | ||
} | ||
|
||
.mat-cell, .mat-header-cell, .mat-footer-cell { | ||
padding-left: 12px; | ||
padding-right: 195px; | ||
} |
24 changes: 24 additions & 0 deletions
24
frontend/src/app/main-table/cost-table/cost-table.component.spec.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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { async, ComponentFixture, TestBed } from "@angular/core/testing"; | ||
|
||
import { CostTableComponent } from "./cost-table.component"; | ||
|
||
describe("CostTableComponent", () => { | ||
let component: CostTableComponent; | ||
let fixture: ComponentFixture<CostTableComponent>; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [CostTableComponent] | ||
}).compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(CostTableComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it("should create", () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
17 changes: 17 additions & 0 deletions
17
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Component, Input } from "@angular/core"; | ||
import { AggregateCostResponse } from 'src/app/services/kubecost.service'; | ||
|
||
@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; | ||
|
||
formatCost(value: number): string { | ||
return '$' + (value > 0 ? Math.max(value, 0.01) : 0).toFixed(2) | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
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"; | ||
|
||
export type AggregateCostResponse = { | ||
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 | ||
} | ||
|
||
@Injectable() | ||
export class KubecostService { | ||
|
||
constructor(private http: HttpClient, private snackBar: SnackBarService) { } | ||
|
||
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)) | ||
); | ||
} | ||
|
||
|
||
// ---------------------------Error Handling---------------------------------- | ||
private handleBackendError(response: { code: number }) { | ||
if (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); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.