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/client/tracking.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
import { WebappClient } from "./client";
7
import * as message from "@cocalc/util/message";
8
9
export class TrackingClient {
10
private client: WebappClient;
11
private log_error_cache: { [error: string]: number } = {};
12
13
constructor(client: WebappClient) {
14
this.client = client;
15
}
16
17
// Send metrics to the hub this client is connected to.
18
// There is no confirmation or response that this succeeded,
19
// which is fine, since dropping some metrics is fine.
20
public send_metrics(metrics: object): void {
21
this.client.hub_client.send(message.metrics({ metrics }));
22
}
23
24
public async user_tracking(evt: string, value: object): Promise<void> {
25
await this.client.async_call({
26
message: message.user_tracking({ evt, value }),
27
});
28
}
29
30
public log_error(error: any): void {
31
if (typeof error != "string") {
32
error = JSON.stringify(error);
33
}
34
const last = this.log_error_cache[error];
35
if (last != null && Date.now() - last <= 1000 * 60 * 15) {
36
return;
37
}
38
this.log_error_cache[error] = Date.now();
39
this.client.call({
40
message: message.log_client_error({ error }),
41
});
42
}
43
44
public async webapp_error(opts: object): Promise<void> {
45
await this.client.async_call({ message: message.webapp_error(opts) });
46
}
47
}
48
49