Skip to content
This repository has been archived by the owner on Feb 11, 2023. It is now read-only.

[WIP] Adicionando a API status #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
.dccache
package-lock.json
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Licença MIT

Direitos autorais (c) 2019 Discloudbot.com.
Direitos autorais (c) 2019-2021 Discloudbot.com.

É concedida permissão, gratuitamente, a qualquer pessoa que obtenha uma cópia
deste software e arquivos de documentação associados (o "Software"), para lidar
Expand Down
76 changes: 66 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 Discloud
Copyright 2019-2021 Discloud
Todos os direitos reservados.

AVISO: A Discloud permite que você use, modifique e distribua esse arquivo em
Expand All @@ -15,44 +15,100 @@ function ram() {
try {
let usoBytes = fs.readFileSync(`/sys/fs/cgroup/memory/memory.max_usage_in_bytes`).toString();
let totalBytes = fs.readFileSync(`/sys/fs/cgroup/memory/memory.limit_in_bytes`).toString();
return formatoMb(usoBytes)+'/'+formatoMb(totalBytes)+'MB'
return formatoMb(usoBytes) + '/' + formatoMb(totalBytes) + 'MB'
} catch (err) {
throw new Error("Dados não encontrados")
throw new Error("Dados não encontrados")
}
}
function usoRam() {
try {
let usoBytes = fs.readFileSync(`/sys/fs/cgroup/memory/memory.max_usage_in_bytes`).toString();//588132352
return converter(usoBytes)
} catch (err) {
throw new Error("Dados não encontrados")
throw new Error("Dados não encontrados")
}
}
function totalRam() {
try {
let totalBytes = fs.readFileSync(`/sys/fs/cgroup/memory/memory.limit_in_bytes`).toString(); //1400897536
return converter(totalBytes)
} catch (err) {
throw new Error("Dados não encontrados")
throw new Error("Dados não encontrados")
}
}

function converter(bytes) {
let formatos = ['B', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return '0B';
let i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
if(i <= 2) return Math.round(bytes / Math.pow(1024, i), 2) + formatos[i];
if((bytes / Math.pow(1024, i)).toFixed(3).includes(".00")) return Math.round(bytes / Math.pow(1024, i), 2) + formatos[i];
if((bytes / Math.pow(1024, i)).toFixed(3).includes(".0")) return ((bytes / Math.pow(1024, i)).toFixed(3) + formatos[i]).replace("0","")
if (i <= 2) return Math.round(bytes / Math.pow(1024, i), 2) + formatos[i];
if ((bytes / Math.pow(1024, i)).toFixed(3).includes(".00")) return Math.round(bytes / Math.pow(1024, i), 2) + formatos[i];
if ((bytes / Math.pow(1024, i)).toFixed(3).includes(".0")) return ((bytes / Math.pow(1024, i)).toFixed(3) + formatos[i]).replace("0", "")
return (bytes / Math.pow(1024, i)).toFixed(3) + formatos[i];
}

function formatoMb(bytes) {
return Math.round(bytes / Math.pow(1024, 2), 2);
}

const axios = require('axios');
const FormData = require('form-data')

class APIstatus {
constructor(token) {
this.token = token;
this.instance = axios.create({
baseURL: 'https://discloud.app/status',
timeout: 5000,
headers: { 'api-token': this.token }
});
}
getUser() {
return new Promise((res, rej) => {
this.instance.get("/user").then(x => res(x.data))
.catch(err => rej(err))
})
}
getBot(id) {
return new Promise((res, rej) => {
if (id == undefined) {return rej("Need bot id")}
this.instance.get(`/bot/${id}`).then(x => res(x.data))
.catch(err => rej(err))
})
}
getBotLogs(id) {
return new Promise((res, rej) => {
if (id == undefined) {return rej("Need bot id")}
this.instance.get(`/bot/${id}/logs`).then(x => res(x.data))
.catch(err => rej(err))
})
}
postBotRestart(id = undefined) {
return new Promise((res, rej) => {
if (id == undefined) {return rej("Need bot id")}
this.instance.post(`/bot/${id}/restart`).then(x => res(x.data))
.catch(err => rej(err))
})
}
postBotCommit(id, pathZip, restartBot = false) {
return new Promise((res, rej) => {
if (id == undefined) {return rej("Need bot id")}
if (pathZip == undefined) {return rej("Need path zip")}
const fileZip = fs.createReadStream(pathZip).catch(err => rej(err))
let form = new FormData();
form.append('file', fileZip);
this.instance.post(`/bot/${id}/commit?restart=${restartBot}`, form, {
headers: form.getHeaders({ 'api-token': this.token })
}).then(x => res(x.data))
.catch(err => rej(err))
})
}

}

module.exports = {
usoRam,
totalRam,
ram
}
ram,
APIstatus
}
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "discloud-status",
"version": "0.1.6",
"version": "0.1.7",
"description": "Esse módulo Nodejs funciona apenas no linux, e foi criado exclusivamente para atender os usuários da discloudbot.com",
"main": "index.js",
"scripts": {
Expand All @@ -12,5 +12,9 @@
},
"keywords": [],
"author": "Pedro Ricardo",
"license": "MIT"
"license": "MIT",
"dependencies": {
"axios": "^0.25.0",
"form-data": "^4.0.0"
}
}