forked from freifunk/meshviewer
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnode.ts
More file actions
236 lines (209 loc) · 5.96 KB
/
Copy pathnode.ts
File metadata and controls
236 lines (209 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { h } from "snabbdom";
import moment, { Moment } from "moment";
import { _ } from "./language.js";
import * as helper from "./helper.js";
export type LinkId = string;
export interface Link {
type: string; // wifi, vpn etc
id: LinkId;
distance: number;
source: Node;
target: Node;
source_addr: string;
target_addr: string;
source_mac?: string; // Same as _addr
target_mac?: string; // Same as _addr
source_tq?: number; // Batman IV link quality (0..1)
target_tq?: number;
source_tp?: number; // Batman V link quality (kbps)
target_tp?: number;
}
export interface Neighbour {
node: Node;
link: Link;
}
export interface Firmware {
release: string;
base: string;
}
export type IPAddress = string;
export interface Autoupdater {
enabled: boolean;
branch: string;
}
export type NodeId = string;
export interface Node {
node_id: NodeId;
hostname: string;
domain?: string;
firstseen: Moment;
lastseen: Moment;
is_online: boolean;
location: LatLon;
neighbours: Neighbour[];
firmware: Firmware;
uptime: number;
nproc: number;
loadavg: number;
memory_usage: number;
model?: string;
clients: number;
clients_wifi24: number;
clients_wifi5: number;
clients_other: number;
is_gateway: boolean;
addresses: IPAddress[];
gateway_nexthop: NodeId;
gateway: NodeId;
gateway6: string; // mac address for some reason
autoupdater: Autoupdater;
}
const self = {
showStatus: undefined,
showGeoURI: undefined,
showGateway: undefined,
showFirmware: undefined,
showUptime: undefined,
showFirstSeen: undefined,
showLoad: undefined,
showRAM: undefined,
showDomain: undefined,
countLocalClients: undefined,
showClients: undefined,
showIPs: undefined,
showAutoupdate: undefined,
};
function showBar(value: string, width: number, warning: boolean) {
return h("span", { props: { className: "bar" + (warning ? " warning" : "") } }, [
h("span", {
style: { width: width * 100 + "%" },
}),
h("label", value),
]);
}
self.showStatus = function showStatus(node: Node) {
return h(
"td",
{ props: { className: node.is_online ? "online" : "offline" } },
_.t(node.is_online ? "node.lastOnline" : "node.lastOffline", {
time: node.lastseen.fromNow(),
date: node.lastseen.format("DD.MM.YYYY, H:mm:ss"),
}),
);
};
self.showGeoURI = function showGeoURI(data: Node) {
if (!helper.hasLocation(data)) {
return undefined;
}
return h(
"td",
h(
"a",
{ props: { href: "geo:" + data.location.latitude + "," + data.location.longitude } },
Number(data.location.latitude.toFixed(6)) + ", " + Number(data.location.longitude.toFixed(6)),
),
);
};
self.showGateway = function showGateway(node: Node) {
return node.is_gateway ? _.t("yes") : undefined;
};
self.showFirmware = function showFirmware(node: Node) {
return (
[helper.dictGet(node, ["firmware", "release"]), helper.dictGet(node, ["firmware", "base"])]
.filter(function (value) {
return value !== null;
})
.join(" / ") || undefined
);
};
self.showUptime = function showUptime(node: Node) {
return moment.utc(node.uptime).local().fromNow(true);
};
self.showFirstSeen = function showFirstSeen(node: Node) {
return moment.utc(node.firstseen).local().fromNow();
};
self.showLoad = function showLoad(node: Node) {
return showBar(node.loadavg.toFixed(2), node.loadavg / (node.nproc || 1), node.loadavg >= node.nproc);
};
self.showRAM = function showRAM(node: Node) {
return showBar(Math.round(node.memory_usage * 100) + " %", node.memory_usage, node.memory_usage >= 0.8);
};
self.showDomain = function showDomain(node: Node) {
let domainTitle = node.domain;
let config = window.config;
if (config.domainNames) {
config.domainNames.some(function (domain) {
if (domainTitle === domain.domain) {
domainTitle = domain.name;
return true;
}
});
}
return domainTitle;
};
self.countLocalClients = function countLocalClients(node: Node, visited = {}) {
if (node.node_id in visited) return 0;
visited[node.node_id] = 1;
let count = node.clients || 0;
node.neighbours.forEach(function (neighbour) {
if (neighbour.link.type === "vpn") return;
count += self.countLocalClients(neighbour.node, visited);
});
return count;
};
self.showClients = function showClients(node: Node) {
if (!node.is_online) {
return undefined;
}
let localClients = self.countLocalClients(node);
let clients = [
h("span", [
node.clients > 0 ? node.clients : _.t("none"),
h("br"),
h("i", { props: { className: "ion-people", title: _.t("node.clients") } }),
]),
h("span", { props: { className: "legend-24ghz" } }, [
node.clients_wifi24,
h("br"),
h("span", { props: { className: "symbol", title: "2,4 GHz" } }),
]),
h("span", { props: { className: "legend-5ghz" } }, [
node.clients_wifi5,
h("br"),
h("span", { props: { className: "symbol", title: "5 GHz" } }),
]),
h("span", { props: { className: "legend-others" } }, [
node.clients_other,
h("br"),
h("span", { props: { className: "symbol", title: _.t("others") } }),
]),
h("span", [
localClients > 0 ? localClients : _.t("none"),
h("br"),
h("i", { props: { className: "ion-share-alt", title: _.t("node.localClients") } }),
]),
];
return h("td", { props: { className: "clients" } }, clients);
};
self.showIPs = function showIPs(node: Node) {
let string = [];
let ips = node.addresses;
ips.sort();
ips.forEach(function (ip, i) {
if (i > 0) {
string.push(h("br"));
}
if (ip.indexOf("fe80:") !== 0) {
string.push(h("a", { props: { href: "http://[" + ip + "]/", target: "_blank" } }, ip));
} else {
string.push(ip);
}
});
return h("td", string);
};
self.showAutoupdate = function showAutoupdate(node: Node) {
return node.autoupdater.enabled
? _.t("node.activated", { branch: node.autoupdater.branch })
: _.t("node.deactivated");
};
export default self;