package config

import (
	"cicv-data-closedloop/common/config/c_log"
	"cicv-data-closedloop/common/util"
	"encoding/json"
	"time"
)

// SendResourceUsage 保存资源占用情况
func SendResourceUsage() {
	for {
		time.Sleep(time.Duration(1) * time.Second)
		top10Cpu, top10Mem := util.GetTop10CpuAndMem()
		top10CpuJson, _ := json.MarshalIndent(top10Cpu, "", "    ")
		top10MemJson, _ := json.MarshalIndent(top10Mem, "", "    ")
		responseString, err := util.HttpPostJsonWithHeaders(
			CloudConfig.Monitor.Url,
			map[string]string{"Authorization": "U9yKpD6kZZDDe4LFKK6myAxBUT1XRrDM"},
			map[string]string{
				"totalCpuUsage":    util.ToString(util.GetCpuPercent()),
				"totalMemoryUsage": util.ToString(util.GetMemoryPercent()),
				"top10Process":     string(top10CpuJson),
				"top10Cpu":         string(top10CpuJson),
				"top10Mem":         string(top10MemJson),
				"deviceNumber":     LocalConfig.EquipmentNo,
				"socIp":            LocalConfig.Node.Ip,
			},
		)
		if err != nil {
			c_log.GlobalLogger.Errorf("发送数据监控信息报错%v,响应信息为:%v", err, responseString)
		}
		//c_log.GlobalLogger.Infof("发送数据监控信息成功,响应信息为:%v", responseString)
	}
}