Skip to content

Commit

Permalink
Add basic uptime visualisation (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcbloch committed Jun 2, 2022
1 parent b2a3f27 commit 327d19a
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ npm-debug.log
.idea/
*.iml
/tmp/

*.swp
7 changes: 7 additions & 0 deletions assets/css/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,10 @@
0% { opacity: 1; }
100% { opacity: 0; }
}

.loading-container {
margin: 20px;
width: 100px;
height: 100px;
position: relative;
}
66 changes: 54 additions & 12 deletions assets/js/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// We import the CSS which is extracted to its own file by esbuild.
// Remove this line if you add a your own CSS build pipeline (e.g postcss).
import "../css/app.scss"
import "../css/app.scss";

// If you want to use Phoenix channels, run `mix help phx.gen.channel`
// to get started and then uncomment the line below.
Expand All @@ -20,26 +20,68 @@ import "../css/app.scss"
//

// Include phoenix_html to handle method=PUT/DELETE in forms and buttons.
import "phoenix_html"
import "phoenix_html";
// Establish Phoenix Socket and LiveView configuration.
import {Socket} from "phoenix"
import {LiveSocket} from "phoenix_live_view"
import topbar from "../vendor/topbar"
import { Socket } from "phoenix";
import { LiveSocket } from "phoenix_live_view";
import topbar from "../vendor/topbar";
import ProgressBar from "progressbar.js";

let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}})
let csrfToken = document
.querySelector("meta[name='csrf-token']")
.getAttribute("content");
let liveSocket = new LiveSocket("/live", Socket, {
params: { _csrf_token: csrfToken },
});

// Show progress bar on live navigation and form submits
topbar.config({barColors: {0: "#29d"}, shadowColor: "rgba(0, 0, 0, .3)"})
window.addEventListener("phx:page-loading-start", info => topbar.show())
window.addEventListener("phx:page-loading-stop", info => topbar.hide())
topbar.config({ barColors: { 0: "#29d" }, shadowColor: "rgba(0, 0, 0, .3)" });
window.addEventListener("phx:page-loading-start", (info) => topbar.show());
window.addEventListener("phx:page-loading-stop", (info) => topbar.hide());

// connect if there are any LiveViews on the page
liveSocket.connect()
liveSocket.connect();

// expose liveSocket on window for web console debug logs and latency simulation:
// >> liveSocket.enableDebug()
// >> liveSocket.enableLatencySim(1000) // enabled for duration of browser session
// >> liveSocket.disableLatencySim()
window.liveSocket = liveSocket
window.liveSocket = liveSocket;

// [email protected] version is used
// Docs: http://progressbarjs.readthedocs.org/en/1.0.0/

window.loadStatusProgressBar = function(divId) {
console.log(`Loading bar for id ${divId}`);
let bar = new ProgressBar.Circle(`#${divId}`, {
color: "#aaa",
// This has to be the same size as the maximum width to
// prevent clipping
strokeWidth: 4,
trailWidth: 1,
easing: "easeInOut",
duration: 1400,
text: {
autoStyleContainer: false,
},
from: { color: "#aaa", width: 1 },
to: { color: "#5cb571", width: 4 }, // #333

// Set default step function for all animate calls
step: function (state, circle) {
circle.path.setAttribute("stroke", state.color);
circle.path.setAttribute("stroke-width", state.width);

var value = Math.round(circle.value() * 100);
if (value === 0) {
circle.setText("");
} else {
circle.setText(value);
}
},
});
bar.text.style.fontFamily = '"Raleway", Helvetica, sans-serif';
bar.text.style.fontSize = "2rem";

bar.animate(1.0); // Number from 0.0 to 1.0
}
37 changes: 34 additions & 3 deletions assets/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"font-awesome": "^4.7.0",
"phoenix": "file:../deps/phoenix",
"phoenix_html": "file:../deps/phoenix_html",
"phoenix_live_view": "file:../deps/phoenix_live_view"
"phoenix_live_view": "file:../deps/phoenix_live_view",
"progressbar.js": "^1.1.0"
}
}
1 change: 1 addition & 0 deletions lib/zout_web/templates/layout/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<meta name="csrf-token" content={csrf_token_value()}>
<%= live_title_tag assigns[:page_title] || "Zout", suffix: " · Phoenix Framework" %>
<link phx-track-static rel="stylesheet" href={Routes.static_path(@conn, "/assets/app.css")}/>
<link href="https://fonts.googleapis.com/css?family=Raleway:400,300,600,800,900" rel="stylesheet" type="text/css">
<script defer phx-track-static type="text/javascript" src={Routes.static_path(@conn, "/assets/app.js")}></script>
</head>
<body>
Expand Down
7 changes: 7 additions & 0 deletions lib/zout_web/templates/project/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<tr>
<th>Project</th>
<th>Status</th>
<th>Uptime</th>
</tr>
</thead>
<tbody>
Expand All @@ -14,7 +15,13 @@
<td>
<%= render_status(downtime) %>
</td>
<td><div class="loading-container" id={"loading-container-#{ project.id }"}></div></td>
</tr>
<script>
window.addEventListener("load",function(event) {
loadStatusProgressBar("loading-container-<%= project.id %>");
});
</script>
<% end %>
</tbody>
</table>

0 comments on commit 327d19a

Please sign in to comment.