mirror of
https://github.com/dec0dOS/zero-ui.git
synced 2025-07-06 04:51:44 -07:00
feat: last online
This commit is contained in:
parent
206d12ded3
commit
40f98cc9df
9 changed files with 82 additions and 64 deletions
|
@ -23,11 +23,15 @@ app.use(express.urlencoded({ extended: false }));
|
||||||
if (process.env.ZU_DISABLE_AUTH !== "true") {
|
if (process.env.ZU_DISABLE_AUTH !== "true") {
|
||||||
app.use(
|
app.use(
|
||||||
bearerToken({
|
bearerToken({
|
||||||
headerKey: "Bearer",
|
headerKey: "token",
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV === "production") {
|
||||||
|
console.debug = function () {};
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
process.env.NODE_ENV === "production" &&
|
process.env.NODE_ENV === "production" &&
|
||||||
process.env.ZU_SECURE_HEADERS !== "false"
|
process.env.ZU_SECURE_HEADERS !== "false"
|
||||||
|
@ -56,6 +60,18 @@ initAdmin().then(function (admin) {
|
||||||
db.defaults({ users: [admin], networks: [] }).write();
|
db.defaults({ users: [admin], networks: [] }).write();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (process.env.ZU_LAST_SEEN_FETCH !== "false") {
|
||||||
|
let schedule = process.env.ZU_LAST_SEEN_SCHEDULE || "*/5 * * * *";
|
||||||
|
cron.schedule(schedule, () => {
|
||||||
|
console.debug("Running scheduled job");
|
||||||
|
const networks = db.get("networks").value();
|
||||||
|
networks.forEach((network) => {
|
||||||
|
console.debug("Processing " + network.id);
|
||||||
|
pingAll(network);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const routerAPI = express.Router();
|
const routerAPI = express.Router();
|
||||||
const routerController = express.Router();
|
const routerController = express.Router();
|
||||||
|
|
||||||
|
@ -76,13 +92,4 @@ app.use(async function (err, req, res) {
|
||||||
res.status(500).json({ error: "500 Internal server error" });
|
res.status(500).json({ error: "500 Internal server error" });
|
||||||
});
|
});
|
||||||
|
|
||||||
// ping all networks every 5 minutes
|
|
||||||
cron.schedule('5 * * * *', () => {
|
|
||||||
const networks = db.get('networks').value();
|
|
||||||
networks.forEach((network) => {
|
|
||||||
pingAll(network);
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = app;
|
module.exports = app;
|
||||||
|
|
|
@ -25,13 +25,14 @@ async function getMemberAdditionalData(data) {
|
||||||
network.defaults({ members: [] }).get("members").write();
|
network.defaults({ members: [] }).get("members").write();
|
||||||
// END MIGRATION SECTION
|
// END MIGRATION SECTION
|
||||||
|
|
||||||
const additionalData = db
|
const member = db
|
||||||
.get("networks")
|
.get("networks")
|
||||||
.find({ id: data.nwid })
|
.find({ id: data.nwid })
|
||||||
.get("members")
|
.get("members")
|
||||||
.find({ id: data.id })
|
.find({ id: data.id });
|
||||||
.get("additionalConfig")
|
|
||||||
.value();
|
const additionalData = member.get("additionalConfig").value();
|
||||||
|
const lastOnline = member.get("lastOnline").value() || 0;
|
||||||
|
|
||||||
const peer = await getPeer(data.id);
|
const peer = await getPeer(data.id);
|
||||||
let peerData = {};
|
let peerData = {};
|
||||||
|
@ -57,11 +58,11 @@ async function getMemberAdditionalData(data) {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: data.nwid + "-" + data.id,
|
id: data.nwid + "-" + data.id,
|
||||||
type: "Member",
|
clock: new Date().getTime(),
|
||||||
clock: Math.floor(new Date().getTime() / 1000),
|
|
||||||
networkId: data.nwid,
|
networkId: data.nwid,
|
||||||
nodeId: data.id,
|
nodeId: data.id,
|
||||||
controllerId: ZT_ADDRESS,
|
controllerId: ZT_ADDRESS,
|
||||||
|
lastOnline: lastOnline,
|
||||||
...additionalData,
|
...additionalData,
|
||||||
...peerData,
|
...peerData,
|
||||||
config: data,
|
config: data,
|
||||||
|
|
|
@ -23,8 +23,7 @@ async function getNetworkAdditionalData(data) {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: data.id,
|
id: data.id,
|
||||||
type: "Network",
|
clock: new Date().getTime(),
|
||||||
clock: Math.floor(new Date().getTime() / 1000),
|
|
||||||
...additionalData.value(),
|
...additionalData.value(),
|
||||||
config: data,
|
config: data,
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,22 +1,29 @@
|
||||||
const api = require("./controller-api");
|
const api = require("./controller-api");
|
||||||
const db = require("./db");
|
const db = require("./db");
|
||||||
|
|
||||||
export default pingAll;
|
async function pingAll(network) {
|
||||||
async function pingAll (network) {
|
await Promise.all(
|
||||||
await Promise.all(network.members.map((async (member) => {
|
network.members.map(async (member) => {
|
||||||
try {
|
console.debug("Processing " + member.id);
|
||||||
await api.get("peer/" + member.id);
|
api
|
||||||
// write lastOnelineTime field in db
|
.get("peer/" + member.id)
|
||||||
db.get("networks")
|
.then(async function () {
|
||||||
.filter({ id: network.id })
|
// write lastOneline field in db
|
||||||
.map("members")
|
db.get("networks")
|
||||||
.first()
|
.filter({ id: network.id })
|
||||||
.filter({ id: member.id })
|
.map("members")
|
||||||
.first()
|
.first()
|
||||||
.set("lastOnlineTime", new Date().getTime())
|
.filter({ id: member.id })
|
||||||
.write();
|
.first()
|
||||||
} catch (err) {
|
.set("lastOnline", new Date().getTime())
|
||||||
return;
|
.write();
|
||||||
}
|
})
|
||||||
})));
|
.catch(function (err) {
|
||||||
};
|
console.debug("Couldn't fetch", member.id);
|
||||||
|
return;
|
||||||
|
});
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = pingAll;
|
||||||
|
|
|
@ -5,6 +5,8 @@ module.exports = async function () {
|
||||||
const res = await api.get("status");
|
const res = await api.get("status");
|
||||||
return res.data.address;
|
return res.data.address;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(
|
||||||
|
"Couldn't connect to the controller on " + err.config.baseURL
|
||||||
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
"@uiw/react-codemirror": "^3.1.0",
|
"@uiw/react-codemirror": "^3.1.0",
|
||||||
"axios": "^0.27.2",
|
"axios": "^0.27.2",
|
||||||
"codemirror": "^5.62.3",
|
"codemirror": "^5.62.3",
|
||||||
|
"date-fns": "^2.29.2",
|
||||||
"history": "^5.3.0",
|
"history": "^5.3.0",
|
||||||
"ipaddr.js": "^2.0.1",
|
"ipaddr.js": "^2.0.1",
|
||||||
"lodash": "^4.17.21",
|
"lodash": "^4.17.21",
|
||||||
|
|
|
@ -14,6 +14,7 @@ import DataTable from "react-data-table-component";
|
||||||
import { useParams } from "react-router-dom";
|
import { useParams } from "react-router-dom";
|
||||||
import API from "utils/API";
|
import API from "utils/API";
|
||||||
import { parseValue, replaceValue, setValue } from "utils/ChangeHelper";
|
import { parseValue, replaceValue, setValue } from "utils/ChangeHelper";
|
||||||
|
import { formatDistance } from "date-fns";
|
||||||
import AddMember from "./components/AddMember";
|
import AddMember from "./components/AddMember";
|
||||||
import DeleteMember from "./components/DeleteMember";
|
import DeleteMember from "./components/DeleteMember";
|
||||||
import ManagedIP from "./components/ManagedIP";
|
import ManagedIP from "./components/ManagedIP";
|
||||||
|
@ -98,40 +99,40 @@ function NetworkMembers({ network }) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "status",
|
id: "status",
|
||||||
name: "Peer status",
|
name: "Last seen",
|
||||||
minWidth: "100px",
|
minWidth: "100px",
|
||||||
cell: (row) =>
|
cell: (row) =>
|
||||||
row.online === 0 ? (
|
row.online === 1 ? (
|
||||||
<Typography color="error">OFFLINE</Typography>
|
<Typography style={{ color: "#008000" }}>{"ONLINE"}</Typography>
|
||||||
) : row.online === 1 ? (
|
) : row.controllerId === row.config.address ? (
|
||||||
<Typography style={{ color: "#008000" }}>
|
<Typography style={{ color: "#c5e31e" }}>{"CONTROLLER"}</Typography>
|
||||||
{"ONLINE (v" +
|
) : row.online === 0 ? (
|
||||||
row.config.vMajor +
|
<Typography color="error">
|
||||||
"." +
|
{row.lastOnline !== 0
|
||||||
row.config.vMinor +
|
? formatDistance(row.lastOnline, row.clock, {
|
||||||
"." +
|
includeSeconds: false,
|
||||||
row.config.vRev +
|
addSuffix: true,
|
||||||
")"}
|
})
|
||||||
|
: "OFFLINE"}
|
||||||
</Typography>
|
</Typography>
|
||||||
) : (
|
) : (
|
||||||
<Typography style={{ color: "#f1c232" }}>
|
<Typography style={{ color: "#f1c232" }}>{"RELAYED"}</Typography>
|
||||||
{"RELAYED (v" +
|
|
||||||
row.config.vMajor +
|
|
||||||
"." +
|
|
||||||
row.config.vMinor +
|
|
||||||
"." +
|
|
||||||
row.config.vRev +
|
|
||||||
")"}
|
|
||||||
</Typography>
|
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "physicalip",
|
id: "physicalip",
|
||||||
name: "Physical IP / Latency",
|
name: "Version / Physical IP / Latency",
|
||||||
minWidth: "220px",
|
minWidth: "220px",
|
||||||
cell: (row) =>
|
cell: (row) =>
|
||||||
row.online === 1 ? (
|
row.online === 1 ? (
|
||||||
<p>
|
<p>
|
||||||
|
{"v" +
|
||||||
|
row.config.vMajor +
|
||||||
|
"." +
|
||||||
|
row.config.vMinor +
|
||||||
|
"." +
|
||||||
|
row.config.vRev}
|
||||||
|
<br />
|
||||||
{row.physicalAddress + "/" + row.physicalPort}
|
{row.physicalAddress + "/" + row.physicalPort}
|
||||||
<br />
|
<br />
|
||||||
{"(" + row.latency + " ms)"}
|
{"(" + row.latency + " ms)"}
|
||||||
|
@ -181,7 +182,7 @@ function NetworkMembers({ network }) {
|
||||||
spacing={0}
|
spacing={0}
|
||||||
direction="column"
|
direction="column"
|
||||||
alignItems="center"
|
alignItems="center"
|
||||||
justify="center"
|
justifyContent="center"
|
||||||
style={{
|
style={{
|
||||||
minHeight: "50vh",
|
minHeight: "50vh",
|
||||||
}}
|
}}
|
||||||
|
|
|
@ -69,8 +69,8 @@ function NetworkSettings({ network, setNetwork }) {
|
||||||
value={network["description"]}
|
value={network["description"]}
|
||||||
onChange={handleChange("description")}
|
onChange={handleChange("description")}
|
||||||
multiline
|
multiline
|
||||||
rows={2}
|
minRows={2}
|
||||||
rowsMax={Infinity}
|
maxRows={Infinity}
|
||||||
label="Description"
|
label="Description"
|
||||||
variant="filled"
|
variant="filled"
|
||||||
InputLabelProps={{
|
InputLabelProps={{
|
||||||
|
|
|
@ -10,6 +10,6 @@ export default axios.create({
|
||||||
localStorage.getItem("disableAuth") === "true"
|
localStorage.getItem("disableAuth") === "true"
|
||||||
? {}
|
? {}
|
||||||
: {
|
: {
|
||||||
Authorization: `Bearer ${JSON.parse(localStorage.getItem("token"))}`,
|
Authorization: `token ${JSON.parse(localStorage.getItem("token"))}`,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue