mirror of
https://github.com/rajnandan1/kener.git
synced 2026-04-23 03:27:24 -05:00
refactored whole code
This commit is contained in:
+7
-10
@@ -1,7 +1,8 @@
|
||||
import axios from "axios";
|
||||
import fs from "fs-extra";
|
||||
import { UP, DOWN, DEGRADED } from "./constants.js";
|
||||
import { GetIncidentsOpen, GetStartTimeFromBody, GetEndTimeFromBody, GetNowTimestampUTC, GetMinuteStartNowTimestampUTC,GetMinuteStartTimestampUTC, GetDayStartTimestampUTC } from "./tool.js";
|
||||
import { GetNowTimestampUTC, GetMinuteStartNowTimestampUTC,GetMinuteStartTimestampUTC, GetDayStartTimestampUTC } from "./tool.js";
|
||||
import { GetIncidentsOpen, GetEndTimeFromBody, GetStartTimeFromBody } from "./github.js";
|
||||
import Randomstring from "randomstring";
|
||||
import Queue from "queue";
|
||||
|
||||
@@ -212,8 +213,9 @@ const getDayData = async (monitor) => {
|
||||
}
|
||||
return originalData;
|
||||
};
|
||||
const updateDayData = async (mergedData, startOfMinute, monitor, since) => {
|
||||
let mxBackDate = startOfMinute - (since * 3600);
|
||||
const updateDayData = async (mergedData, startOfMinute, monitor) => {
|
||||
let since = 48
|
||||
let mxBackDate = startOfMinute - since * 3600;
|
||||
let _0Day = {};
|
||||
for (const ts in mergedData) {
|
||||
const element = mergedData[ts];
|
||||
@@ -227,7 +229,7 @@ const updateDayData = async (mergedData, startOfMinute, monitor, since) => {
|
||||
keys.sort();
|
||||
let sortedDay0 = {};
|
||||
keys.reverse() //reverse to keep 90days data
|
||||
.slice(0, since * 60)
|
||||
.slice(0, since * 60)
|
||||
.reverse() //reverse to keep 0day data
|
||||
.forEach((key) => {
|
||||
sortedDay0[key] = _0Day[key];
|
||||
@@ -330,26 +332,21 @@ const Minuter = async (envSecrets, monitor, githubConfig) => {
|
||||
//merge apiData, webhookData, dayData
|
||||
let mergedData = {};
|
||||
// console.log(Object.keys(dayData).length);;
|
||||
console.log(Object.keys(mergedData).length);
|
||||
for (const timestamp in dayData) {
|
||||
mergedData[timestamp] = dayData[timestamp];
|
||||
}
|
||||
console.log(Object.keys(mergedData).length);
|
||||
for (const timestamp in apiData) {
|
||||
mergedData[timestamp] = apiData[timestamp];
|
||||
}
|
||||
console.log(Object.keys(mergedData).length);
|
||||
for (const timestamp in webhookData) {
|
||||
mergedData[timestamp] = webhookData[timestamp];
|
||||
}
|
||||
console.log(Object.keys(mergedData).length);
|
||||
for (const timestamp in manualData) {
|
||||
mergedData[timestamp] = manualData[timestamp];
|
||||
}
|
||||
console.log(Object.keys(mergedData).filter((x) => !Object.keys(mergedData).includes(x)));
|
||||
|
||||
//update day data
|
||||
await updateDayData(mergedData, startOfMinute, monitor, githubConfig.incidentSince);
|
||||
await updateDayData(mergedData, startOfMinute, monitor);
|
||||
//update 90day data
|
||||
await update90DayData(monitor);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
// @ts-nocheck
|
||||
import axios from "axios";
|
||||
import {GetMinuteStartNowTimestampUTC} from "./tool.js";
|
||||
|
||||
const GH_TOKEN = process.env.GH_TOKEN;
|
||||
|
||||
const GetAllGHLabels = async function (owner, repo) {
|
||||
const options = {
|
||||
method: "GET",
|
||||
url: `https://api.github.com/repos/${owner}/${repo}/labels`,
|
||||
headers: {
|
||||
Accept: "application/vnd.github+json",
|
||||
Authorization: "Bearer " + GH_TOKEN,
|
||||
"X-GitHub-Api-Version": "2022-11-28",
|
||||
},
|
||||
};
|
||||
|
||||
let labels = [];
|
||||
try {
|
||||
const response = await axios.request(options);
|
||||
labels = response.data.map((label) => label.name);
|
||||
} catch (error) {
|
||||
console.log(error.response.data);
|
||||
return [];
|
||||
}
|
||||
return labels;
|
||||
};
|
||||
const CreateGHLabel = async function (owner, repo, label, description) {
|
||||
const options = {
|
||||
method: "POST",
|
||||
url: `https://api.github.com/repos/${owner}/${repo}/labels`,
|
||||
headers: {
|
||||
Accept: "application/vnd.github+json",
|
||||
Authorization: "Bearer " + GH_TOKEN,
|
||||
"X-GitHub-Api-Version": "2022-11-28",
|
||||
},
|
||||
data: {
|
||||
name: label,
|
||||
color: generateRandomColor(),
|
||||
description: description,
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await axios.request(options);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.log(error.response.data);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
const GetStartTimeFromBody = function (text) {
|
||||
const pattern = /\[start_datetime:(\d+)\]/;
|
||||
|
||||
const matches = pattern.exec(text);
|
||||
|
||||
if (matches) {
|
||||
const timestamp = matches[1];
|
||||
return parseInt(timestamp);
|
||||
}
|
||||
return null;
|
||||
};
|
||||
const GetEndTimeFromBody = function (text) {
|
||||
const pattern = /\[end_datetime:(\d+)\]/;
|
||||
|
||||
const matches = pattern.exec(text);
|
||||
|
||||
if (matches) {
|
||||
const timestamp = matches[1];
|
||||
return parseInt(timestamp);
|
||||
}
|
||||
return null;
|
||||
};
|
||||
const GetIncidentsOpen = async function (tagName, githubConfig) {
|
||||
if (tagName === undefined) {
|
||||
return [];
|
||||
}
|
||||
if (githubConfig === undefined) {
|
||||
return [];
|
||||
}
|
||||
const since = GetMinuteStartNowTimestampUTC() - githubConfig.incidentSince * 60 * 60;
|
||||
const sinceISO = new Date(since * 1000).toISOString();
|
||||
const options = {
|
||||
method: "GET",
|
||||
url: `https://api.github.com/repos/${githubConfig.owner}/${githubConfig.repo}/issues?labels=${tagName},incident&state=open&sort=created&direction=desc&since=${sinceISO}`,
|
||||
headers: {
|
||||
Accept: "application/vnd.github+json",
|
||||
Authorization: "Bearer " + GH_TOKEN,
|
||||
"X-GitHub-Api-Version": "2022-11-28",
|
||||
},
|
||||
};
|
||||
try {
|
||||
const response = await axios.request(options);
|
||||
let issues = response.data;
|
||||
//issues.createAt should be after sinceISO
|
||||
issues = issues.filter((issue) => {
|
||||
return new Date(issue.created_at) >= new Date(sinceISO);
|
||||
});
|
||||
return issues;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return [];
|
||||
}
|
||||
};
|
||||
export {
|
||||
GetAllGHLabels,
|
||||
CreateGHLabel,
|
||||
GetIncidentsOpen,
|
||||
GetStartTimeFromBody,
|
||||
GetEndTimeFromBody,
|
||||
};
|
||||
+16
-15
@@ -9,7 +9,8 @@ import fs from "fs-extra";
|
||||
import yaml from "js-yaml";
|
||||
import { Cron } from "croner";
|
||||
import { FOLDER, FOLDER_MONITOR, FOLDER_SITE, API_TIMEOUT } from "./constants.js";
|
||||
import { IsValidURL, IsValidHTTPMethod, LoadMonitorsPath, LoadSitePath, GetAllGHLabels, CreateGHLabel } from "./tool.js";
|
||||
import { IsValidURL, IsValidHTTPMethod, LoadMonitorsPath, LoadSitePath } from "./tool.js";
|
||||
import { GetAllGHLabels, CreateGHLabel } from "./github.js";
|
||||
import { Minuter } from "./cron-minute.js";
|
||||
let monitors = [];
|
||||
let site = {};
|
||||
@@ -120,8 +121,8 @@ const Startup = async () => {
|
||||
monitors[i].timeout = API_TIMEOUT;
|
||||
}
|
||||
|
||||
monitors[i].path0Day = `${FOLDER}/${folderName}-day-json.json`;
|
||||
monitors[i].path90Day = `${FOLDER}/${folderName}.90day.json`;
|
||||
monitors[i].path0Day = `${FOLDER}/${folderName}.0day.utc.json`;
|
||||
monitors[i].path90Day = `${FOLDER}/${folderName}.90day.utc.json`;
|
||||
monitors[i].hasAPI = hasAPI;
|
||||
|
||||
//secrets can be in url/body/headers
|
||||
@@ -145,7 +146,7 @@ const Startup = async () => {
|
||||
}
|
||||
if(site.github.incidentSince === undefined || site.github.incidentSince === null){
|
||||
site.github = {
|
||||
incidentSince: 24
|
||||
incidentSince: 48
|
||||
};
|
||||
}
|
||||
if (checkIfDuplicateExists(monitors.map((monitor) => monitor.folderName)) === true) {
|
||||
@@ -218,18 +219,18 @@ const Startup = async () => {
|
||||
|
||||
//trigger minute cron
|
||||
|
||||
// for (let i = 0; i < monitors.length; i++) {
|
||||
// const monitor = monitors[i];
|
||||
for (let i = 0; i < monitors.length; i++) {
|
||||
const monitor = monitors[i];
|
||||
|
||||
// let cronExpession = "* * * * *";
|
||||
// if (monitor.cron !== undefined && monitor.cron !== null) {
|
||||
// cronExpession = monitor.cron;
|
||||
// }
|
||||
// console.log("Staring " + cronExpession + " Cron for ", monitor.name);
|
||||
// Cron(cronExpession, async () => {
|
||||
// await Minuter(envSecrets, monitor, site.github);
|
||||
// });
|
||||
// }
|
||||
let cronExpession = "* * * * *";
|
||||
if (monitor.cron !== undefined && monitor.cron !== null) {
|
||||
cronExpession = monitor.cron;
|
||||
}
|
||||
console.log("Staring " + cronExpession + " Cron for ", monitor.name);
|
||||
Cron(cronExpession, async () => {
|
||||
await Minuter(envSecrets, monitor, site.github);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export { Startup };
|
||||
@@ -0,0 +1,10 @@
|
||||
import { GetNowTimestampUTC, GetMinuteStartTimestampUTC, GetMinuteStartNowTimestampUTC, GetDayStartTimestampUTC } from "./tool.js";
|
||||
|
||||
let ts = GetNowTimestampUTC();
|
||||
console.log("GetNowTimestampUTC: " + ts);
|
||||
let tm = GetMinuteStartTimestampUTC(ts);
|
||||
console.log("GetMinuteStartTimestampUTC: " + tm);
|
||||
let tms = GetMinuteStartNowTimestampUTC();
|
||||
console.log("GetMinuteStartNowTimestampUTC: " + tms);
|
||||
let td = GetDayStartTimestampUTC(ts);
|
||||
console.log("GetDayStartTimestampUTC: " + td);
|
||||
@@ -47,99 +47,6 @@ const LoadSitePath = function () {
|
||||
|
||||
return SITE;
|
||||
};
|
||||
const GetAllGHLabels = async function (owner, repo) {
|
||||
const options = {
|
||||
method: "GET",
|
||||
url: `https://api.github.com/repos/${owner}/${repo}/labels`,
|
||||
headers: {
|
||||
Accept: "application/vnd.github+json",
|
||||
Authorization: "Bearer " + GH_TOKEN,
|
||||
"X-GitHub-Api-Version": "2022-11-28",
|
||||
},
|
||||
};
|
||||
|
||||
let labels = [];
|
||||
try {
|
||||
const response = await axios.request(options);
|
||||
labels = response.data.map((label) => label.name);
|
||||
} catch (error) {
|
||||
console.log(error.response.data);
|
||||
return [];
|
||||
}
|
||||
return labels;
|
||||
};
|
||||
const CreateGHLabel = async function (owner, repo, label, description) {
|
||||
const options = {
|
||||
method: "POST",
|
||||
url: `https://api.github.com/repos/${owner}/${repo}/labels`,
|
||||
headers: {
|
||||
Accept: "application/vnd.github+json",
|
||||
Authorization: "Bearer " + GH_TOKEN,
|
||||
"X-GitHub-Api-Version": "2022-11-28",
|
||||
},
|
||||
data: {
|
||||
name: label,
|
||||
color: generateRandomColor(),
|
||||
description: description,
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await axios.request(options);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.log(error.response.data);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
const GetStartTimeFromBody = function (text) {
|
||||
const pattern = /\[start_datetime:(\d+)\]/;
|
||||
|
||||
const matches = pattern.exec(text);
|
||||
|
||||
if (matches) {
|
||||
const timestamp = matches[1];
|
||||
return parseInt(timestamp);
|
||||
}
|
||||
return null;
|
||||
};
|
||||
const GetEndTimeFromBody = function (text) {
|
||||
const pattern = /\[end_datetime:(\d+)\]/;
|
||||
|
||||
const matches = pattern.exec(text);
|
||||
|
||||
if (matches) {
|
||||
const timestamp = matches[1];
|
||||
return parseInt(timestamp);
|
||||
}
|
||||
return null;
|
||||
};
|
||||
const GetIncidentsOpen = async function (tagName, githubConfig) {
|
||||
if (tagName === undefined) {
|
||||
return [];
|
||||
}
|
||||
if (githubConfig === undefined) {
|
||||
return [];
|
||||
}
|
||||
const sinceHours = githubConfig.incidentSince || 24;
|
||||
const since = moment().subtract(sinceHours, "hours").toISOString();
|
||||
const options = {
|
||||
method: "GET",
|
||||
url: `https://api.github.com/repos/${githubConfig.owner}/${githubConfig.repo}/issues?labels=${tagName},incident&state=open&sort=created&direction=desc&since=${since}`,
|
||||
headers: {
|
||||
Accept: "application/vnd.github+json",
|
||||
Authorization: "Bearer " + GH_TOKEN,
|
||||
"X-GitHub-Api-Version": "2022-11-28",
|
||||
},
|
||||
};
|
||||
try {
|
||||
const response = await axios.request(options);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return [];
|
||||
}
|
||||
};
|
||||
//return given timestamp in UTC
|
||||
const GetNowTimestampUTC = function () {
|
||||
//use js date instead of moment
|
||||
@@ -179,11 +86,6 @@ export {
|
||||
IsValidHTTPMethod,
|
||||
LoadMonitorsPath,
|
||||
LoadSitePath,
|
||||
GetAllGHLabels,
|
||||
CreateGHLabel,
|
||||
GetIncidentsOpen,
|
||||
GetStartTimeFromBody,
|
||||
GetEndTimeFromBody,
|
||||
GetMinuteStartTimestampUTC,
|
||||
GetNowTimestampUTC,
|
||||
GetDayStartTimestampUTC,
|
||||
|
||||
Reference in New Issue
Block a user