const { LogType } = require("loguix")
const fs = require("fs")
const path = require("path")
const { __glob } = require("./global-variables")
const clog = new LogType("Serveur Metrics")
const osutils = require("os-utils")
const os = require("os")
const { statfs } = require("fs")
const config = require("./config")



module.exports.getMetrics = async function(server) {

    return new Promise((resolve, reject) => {
        if(server == "Alpha") {
            var resp = "NULL"
            const space = statfs("/", (err, stats) => {
                if (err) {
                  throw err
                }

            
               
                osutils.cpuUsage(function(cpuUsage) {   
                    resp = {
                        cpu: Math.round(cpuUsage * 1000),
                        usedram: osutils.totalmem() - osutils.freemem(),
                        totalram: osutils.totalmem(),
                        usedisk: stats.blocks - stats.bfree, 
                        totaldisk: stats.blocks,
                        name: server
                    }
    
                resolve(resp)
                
                   
                })
                
            })
            
            
        } else if(server == "Omega") {
      
            const key = config.getFile().OMEGA_KEY;
       
            fetch("http://omega.raphix.fr:4000/metrics?key="+key, {
                method: "GET",
                headers: {
                    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
                    "Accept-Encoding": "gzip, deflate",
                    "Accept-Language": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7",
                    "Cache-Control": "no-cache",
                    "Connection": "keep-alive"
                }
            })
                .then(response => response.json())
                .then(raw => {
                    raw.name = server;
                    resolve(raw);
                })
                .catch(error => {
                    
                    reject(error);
                });

    

        }
    
    })


}