From 6b50d4e5774ee5a351fea3bc110746005e176da1 Mon Sep 17 00:00:00 2001 From: Ashley Date: Fri, 30 Jun 2023 11:17:52 +0000 Subject: [PATCH] add statuspage support :3 --- src/libpoketube/init/pages-404-and-main.js | 114 +++++++++++++++++---- 1 file changed, 94 insertions(+), 20 deletions(-) diff --git a/src/libpoketube/init/pages-404-and-main.js b/src/libpoketube/init/pages-404-and-main.js index 25ad4412..f6a8dd36 100644 --- a/src/libpoketube/init/pages-404-and-main.js +++ b/src/libpoketube/init/pages-404-and-main.js @@ -18,6 +18,9 @@ const { getRandomArbitrary, } = require("../ptutils/libpt-coreutils.js"); +var http = require("https"); +var ping = require("ping"); + const sha384 = modules.hash; function getJson(str) { @@ -61,30 +64,101 @@ module.exports = function (app, config, renderTemplate) { j, }); }); - -app.get("/:v*?", async function (req, res) { - const uaos = req.useragent.os; - const browser = req.useragent.browser; - const isOldWindows = (uaos === "Windows 7" || uaos === "Windows 8") && browser === "Firefox"; - const rendermainpage = () => { - if (req.useragent.isMobile) { - return res.redirect("/discover"); + app.get("/:v*?", async function (req, res) { + const uaos = req.useragent.os; + const browser = req.useragent.browser; + const isOldWindows = + (uaos === "Windows 7" || uaos === "Windows 8") && browser === "Firefox"; + + if (process.env.STATUSPAGE_API) { + // The following 4 are the actual values that pertain to your account and this specific metric. + var apiKey = process.env.STATUSPAGE_API; + var pageId = process.env.STATUSPAGE_PAGEID; + var metricId = process.env.STATUSPAGE_METRICID; + var apiBase = "https://api.statuspage.io/v1"; + + var url = + apiBase + "/pages/" + pageId + "/metrics/" + metricId + "/data.json"; + var authHeader = { Authorization: "OAuth " + apiKey }; + var options = { method: "POST", headers: authHeader }; + + var totalPoints = (60 / 5) * 24; + var epochInSeconds = Math.floor(new Date() / 1000); + + var count = 0 + 1; + + if (count > totalPoints) return; + + var currentTimestamp = epochInSeconds - (count - 1) * 5 * 60; + + // Measure server ping here + var host = "poketube.fun"; // Replace with the server you want to ping + + ping.promise + .probe(host) + .then((result) => { + var ping = result.time !== "unknown" ? parseInt(result.time) : -1; + + ping = Math.min(Math.max(ping, 20), 250); + + var data = { + timestamp: currentTimestamp, + value: ping, + }; + + var request = http.request(url, options, function (res) { + if (res.statusMessage === "Unauthorized") { + const genericError = + "Error encountered. Please ensure that your page code and authorization key are correct."; + return console.error(genericError); + } + res.on("data", function () { + console.log("Submitted point " + count + " of " + totalPoints); + }); + res.on("end", function () { + + }); + res.on("error", (error) => { + console.error(`Error caught: ${error.message}`); + }); + }); + + request.end(JSON.stringify({ data: data })); + }) + .catch((error) => { + console.error("Ping failed:", error); + // Submit a default value if the ping fails + var data = { + timestamp: currentTimestamp, + value: -1, // Use -1 to indicate ping failure + }; + + var request = http.request(url, options, function (res) { + // Handle response + }); + + request.end(JSON.stringify({ data: data })); + }); } - return renderTemplate(res, req, "landing.ejs", { - isOldWindows, - }); - }; + const rendermainpage = () => { + if (req.useragent.isMobile) { + return res.redirect("/discover"); + } - if (req.params.v && /[a-zA-Z0-9]+/.test(req.params.v)) { - const isvld = await core.isvalidvideo(req.params.v); - if (isvld) { - return res.redirect(`/watch?v=${req.params.v}`); + return renderTemplate(res, req, "landing.ejs", { + isOldWindows, + }); + }; + + if (req.params.v && /[a-zA-Z0-9]+/.test(req.params.v)) { + const isvld = await core.isvalidvideo(req.params.v); + if (isvld) { + return res.redirect(`/watch?v=${req.params.v}`); + } } - } - - return rendermainpage(); -}); + return rendermainpage(); + }); };