Skip to content

Commit

Permalink
workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
mahatab committed Aug 7, 2022
1 parent 308bbe7 commit d3e0157
Show file tree
Hide file tree
Showing 12 changed files with 2,109 additions and 1,988 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/health-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Scheduled Health Check

# Controls when the action will run.
on:
schedule:
- cron: "5 * * * *"

jobs:
health_check_job:
runs-on: ubuntu-latest
name: Check all sites
steps:
- uses: actions/checkout@v2
- name: Run Shell Script
id: shell_script_run
run: bash ./scripts/health-check.sh
27 changes: 27 additions & 0 deletions common/hooks/useRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useState, useEffect } from "react";

function useRequest(url: string) {
const [data, setData] = useState([]);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState();

useEffect(() => {
const loadData = async () => {
setIsLoading(true);
try {
let response = await fetch(url);
let data = await response.json();
setData(data);
} catch (e: any) {
setError(e);
} finally {
setIsLoading(false);
}
};
loadData();
}, [url]);

return [data, isLoading, error];
}

export default useRequest;
1 change: 1 addition & 0 deletions config/urls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google: https://google.com
28 changes: 28 additions & 0 deletions page-components/incidents/hooks/useIncidents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useState, useEffect } from "react";
import Incidents from '../types/Incidents';

function useIncidents(url: string) {
const [data, setData] = useState<Incidents[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState();

useEffect(() => {
const loadData = async () => {
setIsLoading(true);
try {
let response = await fetch(url);
let data = await response.json();
setData(data as Incidents[]);
} catch (e: any) {
setError(e);
} finally {
setIsLoading(false);
}
};
loadData();
}, [url]);

return [data, isLoading, error];
}

export default useIncidents;
31 changes: 31 additions & 0 deletions page-components/incidents/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import useIncidents from './hooks/useIncidents'
import type { NextPage } from 'next'
import Incidents from './types/Incidents';

const IncidentsSection: NextPage = () => {
const [data, isPostsLoading] = useIncidents(
"https://api.github.com/repos/aws/aws-cdk/issues"
);
return (
<div>
<h1>Incidents </h1>
{
isPostsLoading ? (
<p>Loading...</p>
) : (
<ul>
{
(data as Incidents[]).map(incident => (
<li key={incident.id}>
<h2>{incident.title}</h2>
</li>
))
}
</ul>
)
}
</div>
)
}

export default IncidentsSection
11 changes: 11 additions & 0 deletions page-components/incidents/types/Incidents.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
interface Incidents {
id: number;
title: string;
state: string;
created_at: string;
updated_at: string;
closed_at: string;
body: string;
}

export default Incidents;
13 changes: 13 additions & 0 deletions pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Html, Head, Main, NextScript } from 'next/document'

export default function Document() {
return (
<Html className="light">
<Head />
<body className="dark:bg-gray-800">
<Main />
<NextScript />
</body>
</Html>
)
}
16 changes: 9 additions & 7 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type { NextPage } from 'next'
import Head from 'next/head'
import Image from 'next/image'
import styles from '../styles/Home.module.css'
import useRequest from '../common/hooks/useRequest'
import IncidentsSection from "../page-components/incidents"

const Home: NextPage = () => {
const [data, isPostsLoading, error] = useRequest(
"https://api.github.com/repos/aws/aws-cdk/issues"
);
return (
<h1 className="text-3xl font-bold underline">
Hello world!
</h1>
<div className="bg-white dark:bg-slate-800 rounded-lg px-6 py-8 ring-1 ring-slate-900/5 shadow-xl">
<IncidentsSection />
</div>
)
}

export default Home
export default Home;
67 changes: 67 additions & 0 deletions scripts/health-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# In the original repository we'll just print the result of status checks,
# without committing. This avoids generating several commits that would make
# later upstream merges messy for anyone who forked us.
commit=true
origin=$(git remote get-url origin)
if [[ $origin == *statsig-io/statuspage* ]]
then
commit=false
fi

KEYSARRAY=()
URLSARRAY=()

urlsConfig="./urls.yml"
echo "Reading $urlsConfig"
while read -r line
do
echo " $line"
IFS='=' read -ra TOKENS <<< "$line"
KEYSARRAY+=(${TOKENS[0]})
URLSARRAY+=(${TOKENS[1]})
done < "$urlsConfig"

echo "***********************"
echo "Starting health checks with ${#KEYSARRAY[@]} configs:"

mkdir -p logs

for (( index=0; index < ${#KEYSARRAY[@]}; index++))
do
key="${KEYSARRAY[index]}"
url="${URLSARRAY[index]}"
echo " $key=$url"

for i in 1 2 3 4;
do
response=$(curl --write-out '%{http_code}' --silent --output /dev/null $url)
if [ "$response" -eq 200 ] || [ "$response" -eq 202 ] || [ "$response" -eq 301 ] || [ "$response" -eq 302 ] || [ "$response" -eq 307 ]; then
result="success"
else
result="failed"
fi
if [ "$result" = "success" ]; then
break
fi
sleep 5
done
dateTime=$(date +'%Y-%m-%d %H:%M')
if [[ $commit == true ]]
then
echo $dateTime, $result >> "status/${key}_report.log"
# By default we keep 2000 last log entries. Feel free to modify this to meet your needs.
echo "$(tail -2000 status/${key}_report.log)" > "status/${key}_report.log"
else
echo " $dateTime, $result"
fi
done

if [[ $commit == true ]]
then
# Let's make Vijaye the most productive person on GitHub.
git config --global user.name 'mehatab'
git config --global user.email '[email protected]'
git add -A --force status/
git commit -am '[Automated] Update Health Check Logs'
git push
fi
129 changes: 0 additions & 129 deletions styles/Home.module.css

This file was deleted.

1 change: 1 addition & 0 deletions tailwind.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
darkMode: 'class',
content: [
"./pages/**/*.{js,ts,jsx,tsx}"
],
Expand Down
Loading

0 comments on commit d3e0157

Please sign in to comment.