diff --git a/src/lib/color.js b/src/lib/color.js new file mode 100644 index 00000000..770bb684 --- /dev/null +++ b/src/lib/color.js @@ -0,0 +1,13 @@ +// @ts-nocheck +import { siteStore } from "./server/stores/site.js"; +import { get } from "svelte/store"; +const site = get(siteStore); + +const StatusColor = { + UP: site.colors?.UP || "#00dfa2", + DEGRADED: site.colors?.DEGRADED || "#e6ca61", + DOWN: site.colors?.DOWN || "#ca3038", + NO_DATA: "#f1f5f8" +}; + +export default StatusColor; diff --git a/src/lib/server/alerting.js b/src/lib/server/alerting.js index 85c1b378..618ca1c7 100644 --- a/src/lib/server/alerting.js +++ b/src/lib/server/alerting.js @@ -77,14 +77,14 @@ async function createGHIncident(monitor, alert, commonData) { } githubLabels.push("auto"); - let resp = await CreateIssue(title, body, githubLabels); + let resp = await CreateIssue(siteData, title, body, githubLabels); return GHIssueToKenerIncident(resp); } async function closeGHIncident(alert) { let incidentNumber = alert.incidentNumber; - let issue = await GetIncidentByNumber(incidentNumber); + let issue = await GetIncidentByNumber(siteData, incidentNumber); if (issue === null) { return; } @@ -100,17 +100,17 @@ async function closeGHIncident(alert) { body = body.trim(); body = body + " " + `[end_datetime:${endDatetime}]`; - let resp = await UpdateIssueLabels(incidentNumber, labels, body); + let resp = await UpdateIssueLabels(siteData, incidentNumber, labels, body); if (resp === null) { return; } - await CloseIssue(incidentNumber); + await CloseIssue(siteData, incidentNumber); return GHIssueToKenerIncident(resp); } //add comment to incident async function addCommentToIncident(alert, comment) { - let resp = await AddComment(alert.incidentNumber, comment); + let resp = await AddComment(siteData, alert.incidentNumber, comment); return resp; } diff --git a/src/lib/server/cron-minute.js b/src/lib/server/cron-minute.js index c6c94b6b..18d8afad 100644 --- a/src/lib/server/cron-minute.js +++ b/src/lib/server/cron-minute.js @@ -44,8 +44,8 @@ const alertingQueue = new Queue({ autostart: true // Automatically start the queue (optional) }); -async function manualIncident(monitor, githubConfig) { - let incidentsResp = await GetIncidentsManual(monitor.tag, "open"); +async function manualIncident(monitor, site) { + let incidentsResp = await GetIncidentsManual(site, monitor.tag, "open"); let manualData = {}; if (incidentsResp.length == 0) { @@ -81,7 +81,7 @@ async function manualIncident(monitor, githubConfig) { if (end_time <= GetNowTimestampUTC() && incident.state === "open") { //close the issue after 30 secs setTimeout(async () => { - await CloseIssue(incidentNumber); + await CloseIssue(site, incidentNumber); }, 30000); } } else { @@ -325,7 +325,7 @@ async function dsnChecker(dnsResolver, host, recordType, matchType, values) { } } -const Minuter = async (monitor, githubConfig) => { +const Minuter = async (monitor, site) => { if (apiQueue.length > 0) { console.log("Queue length is " + apiQueue.length); } @@ -396,7 +396,7 @@ const Minuter = async (monitor, githubConfig) => { dnsData[startOfMinute] = dnsResponse; } - manualData = await manualIncident(monitor, githubConfig); + manualData = await manualIncident(monitor, site); //merge noData, apiData, webhookData, dayData let mergedData = {}; if (monitor.defaultStatus !== undefined && monitor.defaultStatus !== null) { diff --git a/src/lib/server/github.js b/src/lib/server/github.js index bdd7ca94..b317a5ff 100644 --- a/src/lib/server/github.js +++ b/src/lib/server/github.js @@ -1,15 +1,12 @@ // @ts-nocheck import axios from "axios"; -import { GetMinuteStartNowTimestampUTC } from "./tool.js"; +import { GetMinuteStartNowTimestampUTC, GenerateRandomColor } from "./tool.js"; import { marked } from "marked"; import { fileURLToPath } from "url"; -import { siteStore } from "./stores/site.js"; -import { get } from "svelte/store"; import { dirname } from "path"; import dotenv from "dotenv"; dotenv.config(); -let site = get(siteStore); const GH_TOKEN = process.env.GH_TOKEN; @@ -76,18 +73,14 @@ const GetAllGHLabels = async function (site) { } return labels; }; -function generateRandomColor() { - var randomColor = Math.floor(Math.random() * 16777215).toString(16); - return randomColor; -} + const CreateGHLabel = async function (site, label, description, color) { - site = get(siteStore); if (!site.hasGithub || GH_TOKEN === undefined) { console.warn(GhNotConfiguredMsg); return null; } if (color === undefined) { - color = generateRandomColor(); + color = GenerateRandomColor(); } const options = postAxiosOptions( @@ -128,7 +121,7 @@ const GetEndTimeFromBody = function (text) { } return null; }; -const GetIncidentByNumber = async function (incidentNumber) { +const GetIncidentByNumber = async function (site, incidentNumber) { if (!site.hasGithub || GH_TOKEN === undefined) { console.warn(GhNotConfiguredMsg); return null; @@ -143,7 +136,7 @@ const GetIncidentByNumber = async function (incidentNumber) { return null; } }; -const GetIncidents = async function (tagName, state = "all") { +const GetIncidents = async function (site, tagName, state = "all") { if (!site.hasGithub || GH_TOKEN === undefined) { console.warn(GhNotConfiguredMsg); return []; @@ -168,7 +161,7 @@ const GetIncidents = async function (tagName, state = "all") { return []; } }; -const GetIncidentsManual = async function (tagName, state = "all") { +const GetIncidentsManual = async function (site, tagName, state = "all") { if (!site.hasGithub || GH_TOKEN === undefined) { console.warn(GhNotConfiguredMsg); return []; @@ -193,7 +186,7 @@ const GetIncidentsManual = async function (tagName, state = "all") { return []; } }; -const GetOpenIncidents = async function () { +const GetOpenIncidents = async function (site) { if (!site.hasGithub || GH_TOKEN === undefined) { console.warn(GhNotConfiguredMsg); return []; @@ -273,7 +266,7 @@ function Mapper(issue) { return res; } -async function GetCommentsForIssue(issueID) { +async function GetCommentsForIssue(site, issueID) { if (!site.hasGithub || GH_TOKEN === undefined) { console.warn(GhNotConfiguredMsg); return []; @@ -287,7 +280,7 @@ async function GetCommentsForIssue(issueID) { return []; } } -async function CreateIssue(issueTitle, issueBody, issueLabels) { +async function CreateIssue(site, issueTitle, issueBody, issueLabels) { if (!site.hasGithub || GH_TOKEN === undefined) { console.warn(GhNotConfiguredMsg); return null; @@ -306,7 +299,14 @@ async function CreateIssue(issueTitle, issueBody, issueLabels) { return null; } } -async function UpdateIssue(incidentNumber, issueTitle, issueBody, issueLabels, state = "open") { +async function UpdateIssue( + site, + incidentNumber, + issueTitle, + issueBody, + issueLabels, + state = "open" +) { if (!site.hasGithub || GH_TOKEN === undefined) { console.warn(GhNotConfiguredMsg); return null; @@ -326,7 +326,7 @@ async function UpdateIssue(incidentNumber, issueTitle, issueBody, issueLabels, s return null; } } -async function CloseIssue(incidentNumber) { +async function CloseIssue(site, incidentNumber) { if (!site.hasGithub || GH_TOKEN === undefined) { console.warn(GhNotConfiguredMsg); return null; @@ -343,7 +343,7 @@ async function CloseIssue(incidentNumber) { return null; } } -async function AddComment(incidentNumber, commentBody) { +async function AddComment(site, incidentNumber, commentBody) { if (!site.hasGithub || GH_TOKEN === undefined) { console.warn(GhNotConfiguredMsg); return null; @@ -361,7 +361,7 @@ async function AddComment(incidentNumber, commentBody) { } } //update issue labels -async function UpdateIssueLabels(incidentNumber, issueLabels, body, state = "open") { +async function UpdateIssueLabels(site, incidentNumber, issueLabels, body, state = "open") { if (!site.hasGithub || GH_TOKEN === undefined) { console.warn(GhNotConfiguredMsg); return null; @@ -382,7 +382,7 @@ async function UpdateIssueLabels(incidentNumber, issueLabels, body, state = "ope } //search issue -async function SearchIssue(query, page, per_page) { +async function SearchIssue(site, query, page, per_page) { if (GH_TOKEN === undefined) { console.warn(GhNotConfiguredMsg); return null; diff --git a/src/lib/server/page.js b/src/lib/server/page.js index b545de99..3fff3fb6 100644 --- a/src/lib/server/page.js +++ b/src/lib/server/page.js @@ -5,10 +5,10 @@ import { GetMinuteStartNowTimestampUTC, BeginningOfDay, StatusObj, - StatusColor, ParseUptime, GetDayStartTimestampUTC } from "$lib/server/tool.js"; +import StatusColor from "$lib/color.js"; import { siteStore } from "$lib/server/stores/site"; import { get } from "svelte/store"; diff --git a/src/lib/server/startup.js b/src/lib/server/startup.js index a75a0cc3..b284dfa9 100644 --- a/src/lib/server/startup.js +++ b/src/lib/server/startup.js @@ -30,7 +30,7 @@ const Startup = async () => { // init monitors for (let i = 0; i < monitors.length; i++) { const monitor = monitors[i]; - await Minuter(monitor, site.github, site); + await Minuter(monitor, site); startUPLog[monitor.name] = { "Initial Fetch": "✅" }; @@ -44,7 +44,7 @@ const Startup = async () => { cronExpression = monitor.cron; } Cron(cronExpression, async () => { - await Minuter(monitor, site.github, site); + await Minuter(monitor, site); }); startUPLog[monitor.name]["Monitoring At"] = cronExpression; if (monitor.alerts && ValidateMonitorAlerts(monitor.alerts)) { diff --git a/src/lib/server/tool.js b/src/lib/server/tool.js index cdba415a..5c0a3823 100644 --- a/src/lib/server/tool.js +++ b/src/lib/server/tool.js @@ -1,8 +1,6 @@ // @ts-nocheck import { AllRecordTypes } from "./constants.js"; -import { siteStore } from "./stores/site.js"; -import { get } from "svelte/store"; -const site = get(siteStore); + import dotenv from "dotenv"; dotenv.config(); const IsValidURL = function (url) { @@ -15,11 +13,6 @@ const IsStringURLSafe = function (str) { const IsValidHTTPMethod = function (method) { return /^(GET|POST|PUT|DELETE|HEAD|OPTIONS|PATCH)$/.test(method); }; -function generateRandomColor() { - var randomColor = Math.floor(Math.random() * 16777215).toString(16); - return randomColor; - //random color will be freshly served -} //return given timestamp in UTC const GetNowTimestampUTC = function () { @@ -151,12 +144,6 @@ const StatusObj = { DOWN: "api-down", NO_DATA: "api-nodata" }; -const StatusColor = { - UP: site.colors?.UP || "#00dfa2", - DEGRADED: site.colors?.DEGRADED || "#e6ca61", - DOWN: site.colors?.DOWN || "#ca3038", - NO_DATA: "#f1f5f8" -}; // @ts-ignore const ParseUptime = function (up, all) { if (all === 0) return String("-"); @@ -285,7 +272,10 @@ function ValidateMonitorAlerts(alerts) { } return true; } - +function GenerateRandomColor() { + var randomColor = Math.floor(Math.random() * 16777215).toString(16); + return randomColor; +} export { IsValidURL, IsValidHTTPMethod, @@ -301,7 +291,6 @@ export { checkIfDuplicateExists, GetWordsStartingWithDollar, StatusObj, - StatusColor, ParseUptime, ParsePercentage, IsValidHost, @@ -309,5 +298,6 @@ export { IsValidNameServer, ReplaceAllOccurrences, GetRequiredSecrets, - ValidateMonitorAlerts + ValidateMonitorAlerts, + GenerateRandomColor }; diff --git a/src/routes/(kener)/+page.server.js b/src/routes/(kener)/+page.server.js index 81b0363c..17b1a591 100644 --- a/src/routes/(kener)/+page.server.js +++ b/src/routes/(kener)/+page.server.js @@ -29,7 +29,7 @@ export async function load({ parent }) { monitors[i].activeIncidents = []; monitorsActive.push(monitors[i]); } - let openIncidents = await GetOpenIncidents(); + let openIncidents = await GetOpenIncidents(siteData); let openIncidentsReduced = openIncidents.map(Mapper); return { diff --git a/src/routes/(kener)/api/incident/+server.js b/src/routes/(kener)/api/incident/+server.js index 34c3c6bf..dfc37d52 100644 --- a/src/routes/(kener)/api/incident/+server.js +++ b/src/routes/(kener)/api/incident/+server.js @@ -30,7 +30,7 @@ export async function POST({ request }) { let site = get(siteStore); let github = site.github; githubLabels.push("manual"); - let resp = await CreateIssue(title, body, githubLabels); + let resp = await CreateIssue(site, title, body, githubLabels); if (resp === null) { return json( { error: "github error" }, @@ -111,8 +111,7 @@ export async function GET({ request, url }) { if (titleLike) { filterArray.unshift(`${titleLike} in:title`); } - - const resp = await SearchIssue(filterArray, page, per_page); + const resp = await SearchIssue(site, filterArray, page, per_page); const incidents = resp.items.map((issue) => GHIssueToKenerIncident(issue)); diff --git a/src/routes/(kener)/api/incident/[incidentNumber]/+server.js b/src/routes/(kener)/api/incident/[incidentNumber]/+server.js index 38e30572..ebc968c6 100644 --- a/src/routes/(kener)/api/incident/[incidentNumber]/+server.js +++ b/src/routes/(kener)/api/incident/[incidentNumber]/+server.js @@ -43,7 +43,7 @@ export async function PATCH({ request, params }) { let site = get(siteStore); let github = site.github; - let resp = await UpdateIssue(incidentNumber, title, body, githubLabels); + let resp = await UpdateIssue(site, incidentNumber, title, body, githubLabels); if (resp === null) { return json( { error: "github error" }, diff --git a/src/routes/(kener)/api/incident/[incidentNumber]/comment/+server.js b/src/routes/(kener)/api/incident/[incidentNumber]/comment/+server.js index 66c89f28..460de617 100644 --- a/src/routes/(kener)/api/incident/[incidentNumber]/comment/+server.js +++ b/src/routes/(kener)/api/incident/[incidentNumber]/comment/+server.js @@ -29,7 +29,7 @@ export async function GET({ request, params }) { let site = get(siteStore); let github = site.github; - let resp = await GetCommentsForIssue(incidentNumber); + let resp = await GetCommentsForIssue(site, incidentNumber); return json( resp.map((comment) => { return { @@ -78,7 +78,7 @@ export async function POST({ request, params }) { let site = get(siteStore); let github = site.github; - let resp = await AddComment(incidentNumber, body); + let resp = await AddComment(site, incidentNumber, body); if (resp === null) { return json( { error: "github error" }, diff --git a/src/routes/(kener)/api/incident/[incidentNumber]/status/+server.js b/src/routes/(kener)/api/incident/[incidentNumber]/status/+server.js index 9807a393..80c3b5a9 100644 --- a/src/routes/(kener)/api/incident/[incidentNumber]/status/+server.js +++ b/src/routes/(kener)/api/incident/[incidentNumber]/status/+server.js @@ -46,7 +46,7 @@ export async function POST({ request, params }) { let site = get(siteStore); let github = site.github; - let issue = await GetIncidentByNumber(incidentNumber); + let issue = await GetIncidentByNumber(site, incidentNumber); if (issue === null) { return json( { error: "github error" }, @@ -78,7 +78,7 @@ export async function POST({ request, params }) { body = body + " " + `[end_datetime:${endDatetime}]`; } - let resp = await UpdateIssueLabels(incidentNumber, labels, body); + let resp = await UpdateIssueLabels(site, incidentNumber, labels, body); if (resp === null) { return json( { error: "github error" }, diff --git a/src/routes/(kener)/badge/[tag]/dot/+server.js b/src/routes/(kener)/badge/[tag]/dot/+server.js index f0cfc58d..8dbaa026 100644 --- a/src/routes/(kener)/badge/[tag]/dot/+server.js +++ b/src/routes/(kener)/badge/[tag]/dot/+server.js @@ -1,6 +1,6 @@ // @ts-nocheck import { monitorsStore } from "$lib/server/stores/monitors"; -import { StatusColor } from "$lib/server/tool.js"; +import StatusColor from "$lib/color.js"; import { makeBadge } from "badge-maker"; import { get } from "svelte/store"; import db from "$lib/server/db/db.js"; diff --git a/src/routes/(kener)/badge/[tag]/status/+server.js b/src/routes/(kener)/badge/[tag]/status/+server.js index f6e1f073..f77a453c 100644 --- a/src/routes/(kener)/badge/[tag]/status/+server.js +++ b/src/routes/(kener)/badge/[tag]/status/+server.js @@ -1,6 +1,6 @@ // @ts-nocheck import { monitorsStore } from "$lib/server/stores/monitors"; -import { StatusColor } from "$lib/server/tool.js"; +import StatusColor from "$lib/color.js"; import { makeBadge } from "badge-maker"; import { get } from "svelte/store"; import db from "$lib/server/db/db.js"; diff --git a/src/routes/(kener)/category-[category]/+page.server.js b/src/routes/(kener)/category-[category]/+page.server.js index aad49140..0f3e748a 100644 --- a/src/routes/(kener)/category-[category]/+page.server.js +++ b/src/routes/(kener)/category-[category]/+page.server.js @@ -26,7 +26,7 @@ export async function load({ params, route, url, parent }) { monitors[i].embed = false; monitorsActive.push(monitors[i]); } - let openIncidents = await GetOpenIncidents(); + let openIncidents = await GetOpenIncidents(siteData); let openIncidentsReduced = openIncidents.map(Mapper); return { monitors: monitorsActive, diff --git a/src/routes/(kener)/incident/[id]/+page.server.js b/src/routes/(kener)/incident/[id]/+page.server.js index eeef4b06..497404b9 100644 --- a/src/routes/(kener)/incident/[id]/+page.server.js +++ b/src/routes/(kener)/incident/[id]/+page.server.js @@ -17,7 +17,7 @@ export async function load({ params, route, url, parent }) { const { description, name, tag, image } = monitors.find( (monitor) => monitor.folderName === params.id ); - const allIncidents = await GetIncidents(tag, "all"); + const allIncidents = await GetIncidents(siteData, tag, "all"); const gitHubActiveIssues = allIncidents.filter((issue) => { return issue.state === "open"; }); diff --git a/src/routes/(kener)/incident/[id]/comments/+server.js b/src/routes/(kener)/incident/[id]/comments/+server.js index bd49a24b..892d4647 100644 --- a/src/routes/(kener)/incident/[id]/comments/+server.js +++ b/src/routes/(kener)/incident/[id]/comments/+server.js @@ -8,7 +8,7 @@ import { get } from "svelte/store"; export async function GET({ params }) { const incidentNumber = params.id; let siteData = get(siteStore); - let comments = await GetCommentsForIssue(incidentNumber); + let comments = await GetCommentsForIssue(siteData, incidentNumber); comments = comments.map( ( /** @type {{ body: string | import("markdown-it/lib/token")[]; created_at: any; updated_at: any; html_url: any; }} */ comment diff --git a/src/routes/(kener)/monitor-[tag]/+page.server.js b/src/routes/(kener)/monitor-[tag]/+page.server.js index 9be62fee..e3441aa5 100644 --- a/src/routes/(kener)/monitor-[tag]/+page.server.js +++ b/src/routes/(kener)/monitor-[tag]/+page.server.js @@ -23,7 +23,7 @@ export async function load({ params, route, url, parent }) { monitors[i].embed = false; monitorsActive.push(monitors[i]); } - let openIncidents = await GetOpenIncidents(); + let openIncidents = await GetOpenIncidents(siteData); let openIncidentsReduced = openIncidents.map(Mapper); return { monitors: monitorsActive,