Skip to content

Commit 0496037

Browse files
authored
Merge pull request #1570 from rust-lang/npm-status
Port status page script to TypeScript
2 parents d360fb9 + 82283c9 commit 0496037

File tree

5 files changed

+54
-20
lines changed

5 files changed

+54
-20
lines changed

site/frontend/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
"dashboard": {
2626
"source": "src/pages/dashboard.ts",
2727
"distDir": "dist/scripts"
28+
},
29+
"status": {
30+
"source": "src/pages/status.ts",
31+
"distDir": "dist/scripts"
2832
}
2933
},
3034
"browserslist": "> 0.5%, last 3 years, not dead"

site/frontend/src/configuration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const BASE_URL = window.location.origin + "/perf";
22

33
export const DASHBOARD_DATA_URL = `${BASE_URL}/dashboard`;
4+
export const STATUS_DATA_URL = `${BASE_URL}/status_page`;

site/frontend/src/pages/dashboard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function populate_data(response: DashboardResponse) {
7474

7575
async function make_data() {
7676
const response = await fetch(DASHBOARD_DATA_URL, {});
77-
let data = await response.json();
77+
const data = await response.json();
7878
populate_data(data);
7979
}
8080

site/frontend/static/scripts/status.js renamed to site/frontend/src/pages/status.ts

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,44 @@
1-
function populate_data(data) {
1+
import {STATUS_DATA_URL} from "../configuration";
2+
3+
interface Commit {
4+
sha: string;
5+
date: string;
6+
type: "Try" | "Master";
7+
}
8+
9+
interface BenchmarkStatus {
10+
name: string;
11+
error: string;
12+
}
13+
14+
interface Step {
15+
step: string;
16+
is_done: boolean;
17+
expected_duration: number;
18+
current_progress: number;
19+
}
20+
21+
/**
22+
* The `any` types in the interface below were chosen because the types are quite complex
23+
* on the Rust side (they are modeled with enums encoded in a way that is not so simple to model in
24+
* TS).
25+
*/
26+
interface CurrentState {
27+
artifact: any;
28+
progress: [Step];
29+
}
30+
31+
interface StatusResponse {
32+
last_commit: Commit | null
33+
benchmarks: [BenchmarkStatus],
34+
missing: [[Commit, any]],
35+
current: CurrentState | null,
36+
most_recent_end: number | null,
37+
}
38+
39+
function populate_data(data: StatusResponse) {
240
let state_div = document.querySelector("#benchmark-state");
3-
if (data.last_commit) {
41+
if (data.last_commit !== null) {
442
let element = document.createElement("p");
543
element.innerHTML = `SHA: ${data.last_commit.sha}, date: ${data.last_commit.date}`;
644
state_div.appendChild(element);
@@ -11,7 +49,7 @@ function populate_data(data) {
1149
<summary>${benchmark.name} - error</summary>
1250
<pre class="benchmark-error"></pre>
1351
</details>`;
14-
element.querySelector(".benchmark-error").innerText = benchmark.error;
52+
element.querySelector<HTMLElement>(".benchmark-error").innerText = benchmark.error;
1553
state_div.appendChild(element);
1654
}
1755
let missing_div = document.querySelector("#data-insert-js");
@@ -39,9 +77,8 @@ function populate_data(data) {
3977
tr.appendChild(td);
4078
td = document.createElement("td");
4179
let progress = document.createElement("progress");
42-
progress.setAttribute("max", step.expected_duration);
43-
progress.setAttribute("value", step.is_done ?
44-
step.expected_duration : step.current_progress);
80+
progress.setAttribute("max", step.expected_duration.toString());
81+
progress.setAttribute("value", (step.is_done ? step.expected_duration : step.current_progress).toString());
4582
td.appendChild(progress);
4683
tr.appendChild(td);
4784
td = document.createElement("td");
@@ -75,7 +112,7 @@ function populate_data(data) {
75112
let end = new Date(data.most_recent_end * 1000);
76113
element.innerHTML = `No current collection in progress. Last one
77114
finished at ${end.toLocaleString()} local time,
78-
${format_duration(Math.trunc((new Date() - end) / 1000))} ago.`;
115+
${format_duration(Math.trunc((Date.now() - end.getTime()) / 1000))} ago.`;
79116
} else {
80117
element.innerHTML = "No current collection in progress.";
81118
}
@@ -163,16 +200,10 @@ function format_duration(seconds) {
163200
return s;
164201
}
165202

166-
function addHours(date, hours) {
167-
let ret = new Date(date);
168-
ret.setTime(ret.getTime() + (hours * 60 * 60 * 1000));
169-
return ret;
170-
}
171-
172-
function make_data() {
173-
fetch(BASE_URL + "/status_page", {}).then(function (response) {
174-
response.json().then(data => populate_data(data));
175-
});
203+
async function make_data() {
204+
const response = await fetch(STATUS_DATA_URL, {});
205+
const data = await response.json();
206+
populate_data(data);
176207
}
177208

178209
make_data();

site/frontend/templates/pages/status.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
padding: 0 0.5em;
1111
}
1212
</style>
13-
<script src="https://cdnjs.cloudflare.com/ajax/libs/highcharts/6.0.7/highcharts.js"></script>
1413
{% endblock %}
1514
{% block content %}
1615
<div>
@@ -23,6 +22,5 @@
2322
</div>
2423
{% endblock %}
2524
{% block script %}
26-
<script src="scripts/shared.js"></script>
2725
<script src="scripts/status.js"></script>
2826
{% endblock %}

0 commit comments

Comments
 (0)