h_monitor.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package handler
  2. import (
  3. "cicv-data-closedloop/common/config/c_db"
  4. "cicv-data-closedloop/common/config/c_log"
  5. commonEntity "cicv-data-closedloop/common/entity"
  6. "cicv-data-closedloop/common/util"
  7. "github.com/gin-gonic/gin"
  8. "net/http"
  9. )
  10. type DeviceMonitor struct {
  11. Id int `db:"id" json:"id"` // 自增id
  12. TotalCpuUsage string `db:"total_cpu_usage" json:"totalCpuUsage"` // cpu总占用
  13. TotalMemoryUsage string `db:"total_memory_usage" json:"totalMemoryUsage"` // 内存总占用
  14. Top10Process string `db:"top10_process" json:"top10Process"` // cpu占用前十的进程信息
  15. DeviceNumber string `db:"device_number" json:"deviceNumber"` // cpu占用前十的进程信息
  16. SocIp string `db:"soc_ip" json:"socIp"` // cpu占用前十的进程信息
  17. }
  18. // 保存实车上传的监控信息
  19. func SaveDeviceMonitor(c *gin.Context) {
  20. param := new(DeviceMonitor)
  21. // 映射到结构体
  22. if err := c.ShouldBindJSON(&param); err != nil {
  23. c_log.GlobalLogger.Error("项目启动接收请求参数报错:", err)
  24. c.JSON(http.StatusBadRequest, commonEntity.Response{
  25. Code: 500,
  26. Msg: "请求体解析失败。",
  27. })
  28. return
  29. }
  30. // 插入到数据库
  31. sqlTemplate, _ := util.ReadFile(c_db.SqlFilesMap["insert_device_monitor.sql"])
  32. if err := c_db.DoTx(sqlTemplate, []any{
  33. param.TotalCpuUsage,
  34. param.TotalMemoryUsage,
  35. param.Top10Process,
  36. param.DeviceNumber,
  37. param.SocIp,
  38. }); err != nil {
  39. c_log.GlobalLogger.Error("插入数据报错:", err)
  40. c.JSON(http.StatusBadRequest, commonEntity.Response{
  41. Code: 500,
  42. Msg: "插入数据报错。",
  43. })
  44. return
  45. }
  46. c.JSON(http.StatusOK, commonEntity.Response{
  47. Code: 200,
  48. Msg: "插入数据成功。",
  49. })
  50. }