Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.1.1

### Important Notes

- Update stats formatting

## 0.0.1 (Example)

### Important Notes
Expand Down
34 changes: 31 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sama-push-queue-board",
"version": "0.1.0",
"version": "0.1.1",
"description": "Push queue dashboard",
"main": "index.js",
"scripts": {
Expand All @@ -19,6 +19,7 @@
"@fastify/view": "7.4.1",
"bullmq": "^3.14.2",
"dotenv": "^16.1.1",
"fastify": "^4.17.0"
"fastify": "^4.17.0",
"pretty-ms": "9.3.0"
}
}
56 changes: 55 additions & 1 deletion sama-stats/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const path = require('node:path')
const Redis = require('ioredis')
const { default: prettyMs } = require('pretty-ms')
const pointOfView = require('@fastify/view')

const PING_TIMEOUT_MS = 3_000

let redisConnection = void 0
let lastFetchDate = void 0
let lastServerStats = {}
Expand Down Expand Up @@ -63,6 +66,48 @@ const updateClusterStats = async () => {

const startFetchingServerStats = () => setInterval(updateClusterStats, process.env.SERVER_UPDATE_INTERVAL ?? 30_000)

const checkHealth = async (req, reply) => {
const redisResult = await pingRedis()
const dependencies = [
redisResult,
...(Object.values(lastServerStats) ?? [])
]

const isOk = dependencies.every(item => item?.status === 'ok')

const status = {
status: isOk ? 'ok' : 'fail',
uptime_seconds: Math.floor(process.uptime()),
dependencies
}

return status
}

const pingRedis = async () => {
try {
await Promise.race([
redisConnection.ping(),
new Promise((resolve, reject) => setTimeout(() => reject(new Error(`ping timed out after ${PING_TIMEOUT_MS}ms`)), PING_TIMEOUT_MS))
])
return { name: 'redis', status: 'ok' }
} catch (error) {
return { name: 'redis', status: 'fail', error: error.message }
}
}

const formatStats = (stats) => {
if (!Object.entries(stats).length || !stats) return stats

stats = JSON.parse(JSON.stringify(stats))

Object.entries(stats).forEach(([endpoint, item]) => {
item.uptime = prettyMs(item.uptime_seconds * 1000)
})

return stats
}

module.exports = (fastifyApp, redisOptions) => {
redisConnection = new Redis(redisOptions)

Expand All @@ -82,12 +127,21 @@ module.exports = (fastifyApp, redisOptions) => {
});
},
});

fastifyApp.route({
method: 'GET',
url: '/stats/health',
handler: async (req, reply) => {
const stats = await checkHealth()
reply.send(stats)
},
});

fastifyApp.route({
method: 'GET',
url: '/stats/data/sama-server',
handler: async (req, reply) => {
reply.send({ fetchDate: lastFetchDate, stats: lastServerStats })
reply.send({ fetchDate: lastFetchDate, stats: formatStats(lastServerStats) })
},
});

Expand Down