CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/app/monitor-pings.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
// Monitor ping events from webapp_client and use them to set some
7
// ping state in the page store.
8
9
import { redux } from "../app-framework";
10
import { webapp_client } from "../webapp-client";
11
import * as prom_client from "../prom-client";
12
13
export function init_ping(): void {
14
let prom_ping_time: any = undefined,
15
prom_ping_time_last: any = undefined;
16
if (prom_client.enabled) {
17
prom_ping_time = prom_client.new_histogram("ping_ms", "ping time", {
18
buckets: [50, 100, 150, 200, 300, 500, 1000, 2000, 5000],
19
});
20
prom_ping_time_last = prom_client.new_gauge(
21
"ping_last_ms",
22
"last reported ping time"
23
);
24
}
25
26
webapp_client.on("ping", (ping_time: number): void => {
27
let ping_time_smooth = redux.getStore("page").get("avgping") ?? ping_time;
28
29
// reset outside 3x
30
if (ping_time > 3 * ping_time_smooth || ping_time_smooth > 3 * ping_time) {
31
ping_time_smooth = ping_time;
32
} else {
33
const decay = 1 - Math.exp(-1);
34
ping_time_smooth = decay * ping_time_smooth + (1 - decay) * ping_time;
35
}
36
redux.getActions("page").set_ping(ping_time, Math.round(ping_time_smooth));
37
38
if (prom_client.enabled) {
39
prom_ping_time?.observe(ping_time);
40
prom_ping_time_last?.set(ping_time);
41
}
42
});
43
}
44
45