Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/app/monitor-pings.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45// Monitor ping events from webapp_client and use them to set some6// ping state in the page store.78import { redux } from "../app-framework";9import { webapp_client } from "../webapp-client";10import * as prom_client from "../prom-client";1112export function init_ping(): void {13let prom_ping_time: any = undefined,14prom_ping_time_last: any = undefined;15if (prom_client.enabled) {16prom_ping_time = prom_client.new_histogram("ping_ms", "ping time", {17buckets: [50, 100, 150, 200, 300, 500, 1000, 2000, 5000],18});19prom_ping_time_last = prom_client.new_gauge(20"ping_last_ms",21"last reported ping time"22);23}2425webapp_client.on("ping", (ping_time: number): void => {26let ping_time_smooth = redux.getStore("page").get("avgping") ?? ping_time;2728// reset outside 3x29if (ping_time > 3 * ping_time_smooth || ping_time_smooth > 3 * ping_time) {30ping_time_smooth = ping_time;31} else {32const decay = 1 - Math.exp(-1);33ping_time_smooth = decay * ping_time_smooth + (1 - decay) * ping_time;34}35redux.getActions("page").set_ping(ping_time, Math.round(ping_time_smooth));3637if (prom_client.enabled) {38prom_ping_time?.observe(ping_time);39prom_ping_time_last?.set(ping_time);40}41});42}434445