Skip to content

Commit

Permalink
✨ Add support for degraded performance check
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 22, 2020
1 parent 643f3cf commit ee91b1e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export interface UpptimeConfig {

export interface SiteHistory {
url: string;
status: "up" | "down";
status: "up" | "down" | "degraded";
code: number;
responseTime: number;
lastUpdated?: string;
Expand All @@ -76,7 +76,7 @@ export interface SiteStatus {
/** Full URL of the site */
url: string;
/** Current status, up or down */
status: "up" | "down";
status: "up" | "down" | "degraded";
/** Current response time (ms) */
time: number;
/** Total uptime percentage */
Expand Down
39 changes: 28 additions & 11 deletions src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const update = async (shouldCommit = false) => {
totalTime: number;
};
responseTime: string;
status: "up" | "down";
status: "up" | "down" | "degraded";
}> => {
const result = await curl(site);
console.log("Result from test", result);
Expand Down Expand Up @@ -77,9 +77,10 @@ export const update = async (shouldCommit = false) => {
308,
]
).map(Number);
const status: "up" | "down" = expectedStatusCodes.includes(Number(result.httpCode))
let status: "up" | "down" | "degraded" = expectedStatusCodes.includes(Number(result.httpCode))
? "up"
: "down";
if (parseInt(responseTime) > (site.maxResponseTime || 60000)) status = "degraded";
return { result, responseTime, status };
};

Expand All @@ -88,7 +89,7 @@ export const update = async (shouldCommit = false) => {
* If the site is down, we perform the test 2 more times to make
* sure that it's not a false alarm
*/
if (status === "down") {
if (status === "down" || status === "degraded") {
wait(1000);
const secondTry = await performTestOnce();
if (secondTry.status === "up") {
Expand Down Expand Up @@ -124,7 +125,7 @@ generator: Upptime <https://github.com/upptime/upptime>
(config.commitMessages || {}).statusChange ||
"$EMOJI $SITE_NAME is $STATUS ($RESPONSE_CODE in $RESPONSE_TIME ms) [skip ci] [upptime]"
)
.replace("$EMOJI", status === "up" ? "🟩" : "πŸŸ₯")
.replace("$EMOJI", status === "up" ? "🟩" : status === "degraded" ? "🟨" : "πŸŸ₯")
.replace("$SITE_NAME", site.name)
.replace("$SITE_URL", site.url)
.replace("$SITE_METHOD", site.method || "GET")
Expand Down Expand Up @@ -152,19 +153,22 @@ generator: Upptime <https://github.com/upptime/upptime>
});
console.log(`Found ${issues.data.length} issues`);

// If the site was just recorded as down, open an issue
if (status === "down") {
// If the site was just recorded as down or degraded, open an issue
if (status === "down" || status === "degraded") {
if (!issues.data.length) {
const newIssue = await octokit.issues.create({
owner,
repo,
title: `πŸ›‘ ${site.name} is down`,
title:
status === "down"
? `πŸ›‘ ${site.name} is down`
: `🟨 ${site.name} has degraded performance`,
body: `In [\`${lastCommitSha.substr(
0,
7
)}\`](https://github.com/${owner}/${repo}/commit/${lastCommitSha}), ${site.name} (${
site.url
}) was **down**:
}) ${status === "down" ? "was **down**" : "experienced **degraded performance**"}:
- HTTP code: ${result.httpCode}
- Response time: ${responseTime} ms
`,
Expand All @@ -179,7 +183,9 @@ generator: Upptime <https://github.com/upptime/upptime>
console.log("Opened and locked a new issue");
await sendNotification(
config,
`πŸŸ₯ ${site.name} (${site.url}) is **down**: ${newIssue.data.html_url}`
status === "down"
? `πŸŸ₯ ${site.name} (${site.url}) is **down**: ${newIssue.data.html_url}`
: `🟨 ${site.name} (${site.url}) is experiencing **degraded performance**: ${newIssue.data.html_url}`
);
} else {
console.log("An issue is already open for this");
Expand All @@ -190,7 +196,11 @@ generator: Upptime <https://github.com/upptime/upptime>
owner,
repo,
issue_number: issues.data[0].number,
body: `**Resolved:** ${site.name} is back up in [\`${lastCommitSha.substr(
body: `**Resolved:** ${site.name} ${
issues.data[0].title.includes("degraded")
? "performance has improved"
: "is back up"
} in [\`${lastCommitSha.substr(
0,
7
)}\`](https://github.com/${owner}/${repo}/commit/${lastCommitSha}).`,
Expand All @@ -203,7 +213,14 @@ generator: Upptime <https://github.com/upptime/upptime>
state: "closed",
});
console.log("Closed issue");
await sendNotification(config, `🟩 ${site.name} (${site.url}) is back up.`);
await sendNotification(
config,
`🟩 ${site.name} (${site.url}) ${
issues.data[0].title.includes("degraded")
? "performance has improved"
: "is back up"
}.`
);
} else {
console.log("Could not find a relevant issue", issues.data);
}
Expand Down

0 comments on commit ee91b1e

Please sign in to comment.