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/connection-info.tsx
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 { Modal } from "antd";
7
import { FormattedMessage, useIntl } from "react-intl";
8
9
import { Button, Col, Row } from "@cocalc/frontend/antd-bootstrap";
10
import {
11
React,
12
useActions,
13
useTypedRedux,
14
} from "@cocalc/frontend/app-framework";
15
import { Icon } from "@cocalc/frontend/components";
16
import { labels } from "@cocalc/frontend/i18n";
17
import { webapp_client } from "@cocalc/frontend/webapp-client";
18
import { COLORS } from "@cocalc/util/theme";
19
20
export const ConnectionInfo: React.FC = React.memo(() => {
21
const intl = useIntl();
22
23
const ping = useTypedRedux("page", "ping");
24
const avgping = useTypedRedux("page", "avgping");
25
const status = useTypedRedux("page", "connection_status");
26
const hub = useTypedRedux("account", "hub");
27
const page_actions = useActions("page");
28
29
function close() {
30
page_actions.show_connection(false);
31
}
32
33
return (
34
<Modal
35
width={900}
36
open
37
onCancel={close}
38
onOk={close}
39
title={
40
<>
41
<Icon name="wifi" style={{ marginRight: "1em" }} />{" "}
42
{intl.formatMessage(labels.connection)}
43
</>
44
}
45
>
46
<div>
47
{ping ? (
48
<Row>
49
<Col sm={3}>
50
<h4>
51
<FormattedMessage
52
id="connection-info.ping"
53
defaultMessage="Ping time"
54
description={"Ping how long a server takes to respond"}
55
/>
56
</h4>
57
</Col>
58
<Col sm={6}>
59
<pre>
60
<FormattedMessage
61
id="connection-info.ping_info"
62
defaultMessage="{avgping}ms (latest: {ping}ms)"
63
description={
64
"Short string stating the average and the most recent ping in milliseconds."
65
}
66
values={{ avgping, ping }}
67
/>
68
</pre>
69
</Col>
70
</Row>
71
) : undefined}
72
<Row>
73
<Col sm={3}>
74
<h4>
75
<FormattedMessage
76
id="connection-info.hub_server"
77
defaultMessage="Hub server"
78
description={"Ping how long a server takes to respond"}
79
/>
80
</h4>
81
</Col>
82
<Col sm={6}>
83
<pre>{hub != null ? hub : "Not signed in"}</pre>
84
</Col>
85
<Col sm={2}>
86
<Button onClick={webapp_client.hub_client.fix_connection}>
87
<Icon name="repeat" spin={status === "connecting"} />{" "}
88
{intl.formatMessage(labels.reconnect)}
89
</Button>
90
</Col>
91
</Row>
92
<Row>
93
<Col sm={3}>
94
<h4>{intl.formatMessage(labels.message_plural, { num: 10 })}</h4>
95
</Col>
96
<Col sm={6}>
97
<MessageInfo />
98
</Col>
99
</Row>
100
</div>
101
</Modal>
102
);
103
});
104
105
function bytes_to_str(bytes: number): string {
106
const x = Math.round(bytes / 1000);
107
if (x < 1000) {
108
return x + "K";
109
}
110
return x / 1000 + "M";
111
}
112
113
const MessageInfo: React.FC = React.memo(() => {
114
const intl = useIntl();
115
116
const info = useTypedRedux("account", "mesg_info");
117
118
if (info == null) {
119
return <span></span>;
120
}
121
122
function messages(num: number): string {
123
return `${num} ${intl.formatMessage(labels.message_plural, { num })}`;
124
}
125
126
const sent = intl.formatMessage({
127
id: "connection-info.messages_sent",
128
defaultMessage: "sent",
129
description: "Messages sent",
130
});
131
132
const received = intl.formatMessage({
133
id: "connection-info.messages_received",
134
defaultMessage: "received",
135
description: "Messages received",
136
});
137
138
return (
139
<div>
140
<pre>
141
{messages(info.get("sent"))} {sent} (
142
{bytes_to_str(info.get("sent_length"))})
143
<br />
144
{messages(info.get("recv"))} {received} (
145
{bytes_to_str(info.get("recv_length"))})
146
<br />
147
<span
148
style={
149
info.get("count") > 0
150
? { color: "#08e", fontWeight: "bold" }
151
: undefined
152
}
153
>
154
{messages(info.get("count"))} in flight
155
</span>
156
<br />
157
{messages(info.get("enqueued"))} queued to send
158
</pre>
159
<div style={{ color: COLORS.GRAY_M }}>
160
<FormattedMessage
161
id="connection-info.info"
162
defaultMessage={`Connection icon color changes as the number of messages in flight to a hub increases.
163
Usually, no action is needed, but the counts are helpful for diagnostic purposes.
164
The maximum number of messages that can be sent at the same time is {max}.`}
165
values={{ max: info.get("max_concurrent") }}
166
/>
167
</div>
168
</div>
169
);
170
});
171
172