Skip to content

Commit

Permalink
all acitivities changed to pings
Browse files Browse the repository at this point in the history
  • Loading branch information
thevahidal committed Dec 12, 2021
1 parent f46e51a commit f8b7193
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 29 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@ nano .env

That's it, your status page is ready!

## Deployment
To deploy the project to a production environment:

```bash
yarn
yarn build
yarn start
```

## Development
First install dependencies and then run the development server:

```bash
npm run dev
# or
yarn dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.


## Deploy on Vercel

The easiest way to deploy your status page is to use the [Vercel Platform](https://vercel.com/new).
Expand Down
8 changes: 4 additions & 4 deletions components/services/ResponseTime.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const ResponseTime = ({ monitor }) => {
const theme = useTheme()

const calculateResponseTime = () => {
const length = Array.from(monitor.activities).filter(a => a.status).length;
return Array.from(monitor.activities)
const length = Array.from(monitor.pings).filter(a => a.status).length;
return Array.from(monitor.pings)
.filter(a => a.status)
.reduce((acc, curr) => {
return acc + (curr.duration / length);
Expand All @@ -45,15 +45,15 @@ const ResponseTime = ({ monitor }) => {
color={`url(#fade-gradient)`}
curve={'curveMonotoneX'}
style={{ strokeLinejoin: "round" }}
data={monitor.activities.map((activity, index) => ({
data={monitor.pings.map((activity, index) => ({
x: parseFloat(activity.stamp) * 1000, y: activity.duration * 1000
}))} />
<LineSeries
color={theme.colors.green}
style={{ strokeLinejoin: "round", background: 'transparent' }}
strokeWidth={2}
curve={'curveMonotoneX'}
data={monitor.activities.map((activity, index) => ({
data={monitor.pings.map((activity, index) => ({
x: parseFloat(activity.stamp) * 1000, y: activity.duration * 1000
}))} />
<XAxis tickTotal={5} tickFormat={v => new Date(v).toLocaleTimeString('en', {
Expand Down
2 changes: 1 addition & 1 deletion components/services/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const Service = ({ monitor }) => {
<StatusIndicator size='1x' up={monitor.latest_event.event === 'req-ok'} />
<Heading variant={5} className='mx-2'>{monitor.name}</Heading>
</Card.Header>
{monitor.activities && <StyledCardBody>
{monitor.pings && <StyledCardBody>
<Uptime monitor={monitor} />
<ResponseTime monitor={monitor} />
</StyledCardBody>}
Expand Down
6 changes: 3 additions & 3 deletions components/services/Uptime.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import StatusIndicator from '../services/StatusIndicator';

const Uptime = ({ monitor }) => {
const calculateUptime = () => {
const length = Array.from(monitor.activities).filter(a => a.status).length;
return Array.from(monitor.activities)
const length = Array.from(monitor.pings).filter(a => a.status).length;
return Array.from(monitor.pings)
.filter(a => a.status)
.reduce((acc, curr) => {
return curr.status === 'ok' ? acc + (1 / length) : acc
Expand All @@ -20,7 +20,7 @@ const Uptime = ({ monitor }) => {
</Heading>
<Text muted className='pb-2'>{calculateUptime().toFixed(3)}% uptime</Text>
<Pings>
{monitor.activities.map((activity, index) => (
{monitor.pings.map((activity, index) => (
<Ping key={index} status={activity.status} />
))}
</Pings>
Expand Down
2 changes: 1 addition & 1 deletion components/services/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Service from './Service';
const Services = ({ monitors }) => {
const getMonitorsStatus = () => {
return monitors.reduce((acc, curr) => {
return (curr.latest_event.event === 'req-ok') && acc
return (curr.latest_event?.event === 'req-ok') && acc
}, true)
}

Expand Down
7 changes: 0 additions & 7 deletions pages/api/monitors/[key]/activities.js

This file was deleted.

12 changes: 12 additions & 0 deletions pages/api/monitors/[key]/pings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { listMonitorPings } from '../../../../api/monitors'

export default async function handler(req, res) {
const { key } = req.query
try {
const { data, status } = await listMonitorPings(key)
res.status(status).json(data)
} catch (error) {
const { data, status } = error.response
res.status(status).json(data)
}
}
10 changes: 7 additions & 3 deletions pages/api/monitors/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { listMonitors } from '../../../api/monitors'

export default async function handler(req, res) {
const { data } = await listMonitors()
res.status(200).json(data)
try {
const { data, status } = await listMonitors()
res.status(status).json(data)
} catch (error) {
const { data, status } = error.response
res.status(status).json(data)
}
}
21 changes: 11 additions & 10 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import axios from 'axios'
import Head from 'next/head'

import Services from '../components/services'
import { listMonitorActivities, listMonitors } from '../api/monitors'
import { listMonitorPings, listMonitors } from '../api/monitors'
import configs from '../configs'

let liveStatsInterval = null;
Expand All @@ -14,7 +14,7 @@ const Landing = (props) => {
useEffect(() => {
liveStatsInterval = setInterval(() => {
fetchMonitorsData()
fetchMonitorsActivitiesDate()
fetchMonitorsPingsData()
}, 60000)

return () => {
Expand All @@ -37,16 +37,18 @@ const Landing = (props) => {
})
}

const fetchMonitorsActivitiesDate = () => {
const fetchMonitorsPingsData = () => {
monitors.forEach(monitor => {

axios.get(`api/monitors/${monitor.key}/activities`).then(res => {
axios.get(`api/monitors/${monitor.key}/pings`).then(res => {
if (res.status !== 200) return
setMonitors(monitors => monitors.map(m => {

if (m.key === monitor.key) {
const data = res.data[monitor.key]
m = {
...m,
activities: res.data
pings: data.concat([...new Array(50 - data.length).fill({})])
}
}

Expand All @@ -71,17 +73,16 @@ const Landing = (props) => {
export const getStaticProps = async () => {
const results = await listMonitors()

const activities = await Promise.all(results.data.monitors.map(monitor => {
return listMonitorActivities(monitor.key)
const pings = await Promise.all(results.data.monitors.map(monitor => {
return listMonitorPings(monitor.key)
}))


const monitors = results.data.monitors.map((monitor, index) => {
const activitiesData = activities[index].data
const pingsData = pings[index].data[monitor.key]

return {
...monitor,
activities: activitiesData.concat([...new Array(50 - activitiesData.length).fill({})]),
pings: pingsData.concat([...new Array(50 - pingsData.length).fill({})]),
}
})

Expand Down

0 comments on commit f8b7193

Please sign in to comment.