Skip to content

Commit

Permalink
Merge pull request #29 from AlAnNuB/status-page
Browse files Browse the repository at this point in the history
Adiciona versão inicial da página `/status`
  • Loading branch information
AlAnNuB authored Nov 25, 2024
2 parents fa3f14c + d5ef787 commit cebc927
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
23 changes: 22 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"node-pg-migrate": "7.6.1",
"pg": "8.12.0",
"react": "18.3.1",
"react-dom": "18.3.1"
"react-dom": "18.3.1",
"swr": "2.2.5"
},
"devDependencies": {
"@commitlint/cli": "19.4.0",
Expand Down
64 changes: 64 additions & 0 deletions pages/status/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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 (
<>
<h1>Status</h1>
<UpdatedAt />
<DatabaseStatus />
</>
);
}

function UpdatedAt() {
const { isLoading, data } = useSWR("/api/v1/status", fetchAPI, {
//Recarregar de 2 em 2 segundos
refreshInterval: 2000,
//Guarda a resposta no cache por padrão de 2 segundos não executando nova chamada nesse tempo
dedupingInterval: 2000,
});

let updatedAtText = "Carregando...";

if (!isLoading && data) {
updatedAtText = new Date(data.updated_at).toLocaleString("pt-BR");
}

return <div>Última atualização: {updatedAtText}</div>;
}

function DatabaseStatus() {
const { isLoading, data } = useSWR("/api/v1/status", fetchAPI, {
refreshInterval: 2000,
});

let databaseStatusInformation = "Carregando...";

if (!isLoading && data) {
databaseStatusInformation = (
<>
<div>Versão: {data.dependencies.database.version}</div>
<div>
Conexões abertas: {data.dependencies.database.opened_connections}
</div>
<div>
Conexões máximas: {data.dependencies.database.max_connections}
</div>
</>
);

return (
<>
<h2>Database</h2>
<div>{databaseStatusInformation}</div>
</>
);
}
}

0 comments on commit cebc927

Please sign in to comment.