Skip to content

Commit adc42a6

Browse files
committed
add token usage status message
1 parent be63f5f commit adc42a6

3 files changed

Lines changed: 44 additions & 10 deletions

File tree

angular/src/app/pages/tools/token_usage/usage.component.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
</div>
99
<button class="stroked xlarge" [disabled]="!api_token" (click)="check_usage()">Get Info</button>
1010
</div>
11-
<div class="usage" >
11+
<div class="info">
12+
<span *ngIf="invalid" class="error">Invalid Token</span>
13+
<span *ngIf="working">Check {{check_index}} / {{check_total}} </span>
14+
</div>
15+
<div class="usage" >
1216
<div class="text-container">
1317
<div class="label">API request usage: </div>
1418
<div *ngIf="usage.requests > -1" class="value">{{usage.requests}}</div>
@@ -17,10 +21,10 @@
1721
<div *ngIf="usage.request_limit > -1" class="value">{{usage.request_limit}}</div>
1822
<div *ngIf="usage.request_limit == -1" class="value">-</div>
1923
</div>
20-
<div *ngIf="usage.request_percentage>=100" class="progress-bar" role="progressbar" [ngStyle]="{'background': 'radial-gradient(closest-side, #161c26 79%, transparent 80% 100%), conic-gradient(#ff6969 '+usage.request_percentage+'%, #698bc7 0)'}">
24+
<div *ngIf="usage.request_percentage>=100" class="progress-bar" role="progressbar" [ngStyle]="{'background': 'radial-gradient(closest-side, #202836 79%, transparent 80% 100%), conic-gradient(#ff6969 '+usage.request_percentage+'%, #698bc7 0)'}">
2125
{{usage.request_percentage | number: '1.0-0' }}%
2226
</div>
23-
<div *ngIf="usage.request_percentage<100" class="progress-bar" role="progressbar" [ngStyle]="{'background': 'radial-gradient(closest-side, #161c26 79%, transparent 80% 100%), conic-gradient(#7dff69 '+usage.request_percentage+'%, #698bc7 0)'}">
27+
<div *ngIf="usage.request_percentage<100" class="progress-bar" role="progressbar" [ngStyle]="{'background': 'radial-gradient(closest-side, #202836 79%, transparent 80% 100%), conic-gradient(#7dff69 '+usage.request_percentage+'%, #698bc7 0)'}">
2428
{{usage.request_percentage | number: '1.0-0' }}%
2529
</div>
2630
</div>

angular/src/app/pages/tools/token_usage/usage.component.scss

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
.box-token {
22
margin: 1em;
33
}
4+
5+
.info{
6+
font-size: larger;
7+
text-align: center;
8+
height: 1.5em;
9+
}
10+
.info .error {
11+
color: #ff0000d7;
12+
}
413
.usage{
514
display: flex;
615
flex-direction: row;
716
justify-content: space-between;
8-
padding: 1em;
17+
padding: 0 1em 1em;
918
font-size: small;
1019
}
1120

angular/src/app/pages/tools/token_usage/usage.component.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,38 +42,59 @@ export class TokenUsageComponent implements OnInit {
4242
request_limit: -1,
4343
request_percentage: 0
4444
};
45+
working:boolean = false;
46+
invalid: boolean = false;
47+
check_index:number=0;
48+
check_total:number=0;
4549
api_token: string = "";
4650

4751
ngOnInit() {
52+
this.invalid = false;
53+
this.check_index = 0;
54+
this.api_token = "";
55+
this._cd.detectChanges()
4856
}
49-
57+
5058
check_usage() {
5159
const api_hosts = this._browser.getHostApi()
60+
this.working = true;
61+
this.invalid = false;
62+
this.check_index = 0;
63+
this.check_total = api_hosts.length;
5264
this.check_cloud_usage(api_hosts);
5365
}
5466

55-
private check_cloud_usage(api_hosts, index = 0): void {
56-
if (index < api_hosts.length) {
67+
private check_cloud_usage(api_hosts): void {
68+
this._cd.detectChanges()
69+
if (this.check_index < api_hosts.length) {
5770
this._http.get(
58-
"https://" + api_hosts[index] + "/api/v1/self/usage",
71+
"https://" + api_hosts[this.check_index] + "/api/v1/self/usage",
5972
{ headers: { "Authorization": "Token " + this.api_token }, observe: 'response' }
6073
).subscribe(data => {
6174
if (data.status == 200) {
6275
this.usage.requests = data.body["requests"];
6376
this.usage.request_limit = data.body["request_limit"];
6477
this.usage.request_percentage = (this.usage.requests / this.usage.request_limit) * 100;
78+
this.working =false;
6579
this._cd.detectChanges()
6680
} else if (data.status == 429) {
6781
this.usage.requests = 5000;
6882
this.usage.request_limit = 5000;
6983
this.usage.request_percentage = 100;
84+
this.working =false;
7085
this._cd.detectChanges()
7186
} else {
72-
this.check_cloud_usage(api_hosts, index + 1);
87+
this.check_index +=1;
88+
this.check_cloud_usage(api_hosts);
7389
}
7490
}, err => {
75-
this.check_cloud_usage(api_hosts, index + 1);
91+
this.check_index +=1;
92+
this.check_cloud_usage(api_hosts);
7693
})
94+
} else {
95+
this.working =false;
96+
this.invalid = true;
97+
this._cd.detectChanges()
7798
}
7899
}
79100

0 commit comments

Comments
 (0)