diff --git a/builddashboard.js b/dashboard/build.js similarity index 73% rename from builddashboard.js rename to dashboard/build.js index 15ef7a7..04608ad 100644 --- a/builddashboard.js +++ b/dashboard/build.js @@ -4,16 +4,20 @@ const { minify: minifyCSS } = require('csso'); const { rollup } = require('rollup'); const commonjs = require('@rollup/plugin-commonjs'); const css = require('rollup-plugin-css-only'); +const replace = require('@rollup/plugin-replace'); const { default: resolve } = require('@rollup/plugin-node-resolve'); const svelte = require('rollup-plugin-svelte'); const { terser } = require('rollup-plugin-terser'); -async function build() { +async function build(server) { + const serverDomain = server.settings.domain || 'localhost'; + const serverPort = server.settings.port || 80; + const serverBase = `${serverDomain}:${serverPort}`; let cssOutput = ''; try { const bundle = await rollup({ - input: __dirname + '/gui/dashboard/index.js', + input: __dirname + '/../gui/dashboard/index.js', plugins: [ // Svelte svelte({ @@ -39,6 +43,14 @@ async function build() { // Minify terser(), + + // Replace env vars + replace({ + preventAssignment: false, + values: { + '__SERVER__': serverBase, + }, + }), ], }); diff --git a/dashboard/socket.js b/dashboard/socket.js new file mode 100644 index 0000000..88ed900 --- /dev/null +++ b/dashboard/socket.js @@ -0,0 +1,72 @@ +'use strict'; + +const { makeId } = require('core/makeid'); + +const decoder = new TextDecoder('utf-8'); +let uws; + +async function createDashboardSocket(server) { + uws = server.ws({ + route: '/statusdashboard/socket', + onOpen: async ws => { + function sendTime() { + ws.send(JSON.stringify({ + cmd: 'time', + time: new Date().getTime(), + })); + } + + sendTime(); + setInterval(sendTime, 5000); + + async function sendStatuses() { + const services = await server.storage + .store('smartyellow/webservice') + .find() + .toArray(); + const heartbeats = await server.storage + .store('smartyellow/webserviceheartbeat') + .find({ webservice: { $in: services.map(s => s.id) } }) + .sort({ date: -1 }) + .toArray(); + const mappedServices = {}; + + for (const s of services) { + const lastBeat = heartbeats.find(h => h.webservice === s.id); + mappedServices[s.id] = { + name: s.name, + lastBeat: lastBeat, + }; + } + + ws.send(JSON.stringify({ + cmd: 'data', + data: mappedServices, + })); + } + + sendStatuses(); + setInterval(sendStatuses, 5000); + }, + onUpgrade: async () => ({ id: makeId(10) }), + onMessage: async (ws, msg) => { + msg = JSON.parse(decoder.decode(msg)); + console.log('msg', msg); + + if (!msg || !msg.command) { + return; + } + + switch (msg.command) { + case 'data': + ws.send('data'); + return; + + default: + return; + } + }, + }); +} + +module.exports = createDashboardSocket; diff --git a/gui/dashboard/app.svelte b/gui/dashboard/app.svelte index 8a1636f..c92e58b 100644 --- a/gui/dashboard/app.svelte +++ b/gui/dashboard/app.svelte @@ -1,19 +1,69 @@