From bed6d3f68c22e81cf8f3dbd04ee6ea1d0679c2fa Mon Sep 17 00:00:00 2001 From: Bruno Henrique Date: Sun, 5 Jan 2025 20:11:55 -0300 Subject: [PATCH] feat: add `swr` and create `status` page --- package-lock.json | 25 +++++++++++++++++++++- package.json | 3 ++- pages/status/index.js | 49 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 pages/status/index.js diff --git a/package-lock.json b/package-lock.json index 146b988..05dbbb4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,8 @@ "node-pg-migrate": "6.2.2", "pg": "8.13.1", "react": "18.3.1", - "react-dom": "18.3.1" + "react-dom": "18.3.1", + "swr": "2.2.5" }, "devDependencies": { "@commitlint/cli": "19.6.1", @@ -10519,6 +10520,19 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/swr": { + "version": "2.2.5", + "resolved": "https://registry.npmjs.org/swr/-/swr-2.2.5.tgz", + "integrity": "sha512-QtxqyclFeAsxEUeZIYmsaQ0UjimSq1RZ9Un7I68/0ClKK/U3LoyQunwkQfJZr2fc22DfIXLNDc2wFyTEikCUpg==", + "license": "MIT", + "dependencies": { + "client-only": "^0.0.1", + "use-sync-external-store": "^1.2.0" + }, + "peerDependencies": { + "react": "^16.11.0 || ^17.0.0 || ^18.0.0" + } + }, "node_modules/tapable": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/tapable/-/tapable-2.2.1.tgz", @@ -10894,6 +10908,15 @@ "punycode": "^2.1.0" } }, + "node_modules/use-sync-external-store": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/use-sync-external-store/-/use-sync-external-store-1.4.0.tgz", + "integrity": "sha512-9WXSPC5fMv61vaupRkCKCxsPxBocVnwakBEkMIHHpkTTg6icbJtg6jzgtLDm4bl3cSHAca52rYWih0k4K3PfHw==", + "license": "MIT", + "peerDependencies": { + "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0" + } + }, "node_modules/util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", diff --git a/package.json b/package.json index a4ecfb4..3075e4c 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ "node-pg-migrate": "6.2.2", "pg": "8.13.1", "react": "18.3.1", - "react-dom": "18.3.1" + "react-dom": "18.3.1", + "swr": "2.2.5" }, "devDependencies": { "@commitlint/cli": "19.6.1", diff --git a/pages/status/index.js b/pages/status/index.js new file mode 100644 index 0000000..7c50fe8 --- /dev/null +++ b/pages/status/index.js @@ -0,0 +1,49 @@ +import useSWR from "swr"; + +async function fetchAPI(key) { + const response = await fetch(key); + const responseBody = await response.json(); + return responseBody; +} + +export default function StatusPage() { + return ( + <> +

Status

+ + + + ); +} + +function UpdatedAt() { + const { data, isLoading } = useSWR("/api/v1/status", fetchAPI, { + refreshInterval: 2000, + }); + + let updatedAtText = "Carregando..."; + + if (!isLoading && data) { + updatedAtText = new Date(data.updated_at).toLocaleString("pt-br"); + } + + return
Última atualização: {updatedAtText}
; +} + +function ShowAPIStatus() { + const { data, isLoading } = useSWR("/api/v1/status", fetchAPI, { + refreshInterval: 2000, + }); + + return isLoading ? ( +
Carregando...
+ ) : ( + <> +
Versão do PostgreSQL: {data.dependencies.database.version}
+
Conexões máximas: {data.dependencies.database.max_connections}
+
+ Conexões abertas: {data.dependencies.database.opened_connections} +
+ + ); +}