Skip to content

Commit

Permalink
Merge pull request #176 from rajnandan1/release/2.0.0
Browse files Browse the repository at this point in the history
Release/2.0.0 retry
  • Loading branch information
rajnandan1 authored Dec 4, 2024
2 parents ed52f28 + a7abf37 commit 392a0e8
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 66 deletions.
13 changes: 13 additions & 0 deletions src/lib/color.js
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 5 additions & 5 deletions src/lib/server/alerting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand Down
10 changes: 5 additions & 5 deletions src/lib/server/cron-minute.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down
42 changes: 21 additions & 21 deletions src/lib/server/github.js
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand All @@ -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 [];
Expand All @@ -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 [];
Expand All @@ -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 [];
Expand Down Expand Up @@ -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 [];
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/server/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
4 changes: 2 additions & 2 deletions src/lib/server/startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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": "✅"
};
Expand All @@ -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)) {
Expand Down
24 changes: 7 additions & 17 deletions src/lib/server/tool.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 () {
Expand Down Expand Up @@ -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("-");
Expand Down Expand Up @@ -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,
Expand All @@ -301,13 +291,13 @@ export {
checkIfDuplicateExists,
GetWordsStartingWithDollar,
StatusObj,
StatusColor,
ParseUptime,
ParsePercentage,
IsValidHost,
IsValidRecordType,
IsValidNameServer,
ReplaceAllOccurrences,
GetRequiredSecrets,
ValidateMonitorAlerts
ValidateMonitorAlerts,
GenerateRandomColor
};
2 changes: 1 addition & 1 deletion src/routes/(kener)/+page.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions src/routes/(kener)/api/incident/+server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down Expand Up @@ -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" },
Expand Down
Loading

0 comments on commit 392a0e8

Please sign in to comment.