Implement placeholder server responses

This commit is contained in:
Joscha 2023-10-22 16:29:20 +02:00
parent 3dc54738fa
commit f77ed130e1
10 changed files with 85 additions and 357 deletions

View file

@ -1,6 +1,3 @@
import { Requests } from "./graph/requests.js";
import { State } from "./graph/state.js";
import uPlot from "./uPlot.js";
/*
@ -121,11 +118,6 @@ const COLORS = [
// Initialization
const plot_div = document.getElementById("plot")!;
const plotDiv = document.getElementById("plot")!;
const metricsDiv = document.getElementById("metrics")!;
let plot: uPlot | null = null;
let requests = new Requests();
let state = new State(requests, metricsDiv);
state.update();

View file

@ -2,8 +2,7 @@
* `/graph/metrics` response data.
*/
export type MetricsResponse = {
// data_id: number; // TODO Uncomment
dataId: number;
metrics: string[];
};
@ -11,61 +10,38 @@ export type MetricsResponse = {
* `/graph/commits` response data.
*/
export type CommitsResponse = {
// graph_id: number; // TODO Uncomment
hash_by_hash: string[];
author_by_hash: number[];
committer_date_by_hash: string[];
message_by_hash: string[];
parents: [string, string][];
graphId: number;
hashByHash: string[];
authorByHash: string[];
committerDateByHash: number[];
messageByHash: string[];
parentsByHash: string[][];
};
/**
* `/graph/measurements` response data.
*/
export type MeasurementsResponse = {
// graph_id: number; // TODO Uncomment
// data_id: number; // TODO Uncomment
graphId: number;
dataId: number;
measurements: { [key: string]: (number | null)[]; };
};
/**
* Request different kinds of data from the server.
*
* This class has two main purposes:
*
* 1. Providing a nice interface for requesting data from the server
* 2. Preventing sending the same request again while still waiting for the server
*/
export class Requests {
#requesting_metrics: Promise<MetricsResponse> | null = null;
#requesting_commits: Promise<CommitsResponse> | null = null;
#requesting_measurements: Map<string, Promise<MeasurementsResponse>> = new Map();
async #request_data<R>(url: string): Promise<R> {
let response = await fetch(url);
let data: R = await response.json();
return data;
}
async get_metrics(): Promise<MetricsResponse | null> {
if (this.#requesting_metrics !== null) {
try {
return await this.#requesting_metrics;
} catch (error) {
return null;
}
}
this.#requesting_metrics = this.#request_data<MetricsResponse>("metrics");
try {
return await this.#requesting_metrics;
} catch (error) {
console.error("Could not get metrics:", error);
return null;
} finally {
this.#requesting_metrics = null;
}
}
async function getData<R>(url: string): Promise<R> {
const response = await fetch(url);
const data: R = await response.json();
return data;
}
export async function getMetrics(): Promise<MetricsResponse> {
return getData("metrics");
}
export async function getCommits(): Promise<CommitsResponse> {
return getData("commits");
}
export async function getMeasurements(metrics: string[]): Promise<MeasurementsResponse> {
const params = new URLSearchParams(metrics.map(m => ["metric", m]));
return getData(`measurements?${params}`);
}

View file

@ -1,15 +1,13 @@
import { updateMetricsDiv } from "./metrics.js";
import { Requests, MetricsResponse } from "./requests.js";
import { MetricsResponse, getMetrics } from "./requests.js";
export class State {
#requests: Requests;
#metricsDiv: HTMLElement;
#updating: boolean = false;
#metrics: MetricsResponse | null = null;
constructor(requests: Requests, metricsDiv: HTMLElement) {
this.#requests = requests;
constructor(metricsDiv: HTMLElement) {
this.#metricsDiv = metricsDiv;
}
@ -35,7 +33,7 @@ export class State {
}
async #update_metrics() {
this.#metrics = await this.#requests.get_metrics();
this.#metrics = await getMetrics();
if (this.#metrics === null) { return; }
updateMetricsDiv(this.#metricsDiv, this.#metrics.metrics);
}