h_monitor.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. "time"
  10. )
  11. type DeviceMonitor struct {
  12. Id int `db:"id" json:"id"` // 自增id
  13. TotalCpuUsage string `db:"total_cpu_usage" json:"totalCpuUsage"` // cpu总占用
  14. TotalMemoryUsage string `db:"total_memory_usage" json:"totalMemoryUsage"` // 内存总占用
  15. Top10Process string `db:"top10_process" json:"top10Process"` // cpu占用前十的进程信息
  16. DeviceNumber string `db:"device_number" json:"deviceNumber"` // 设备编号
  17. SocIp string `db:"soc_ip" json:"socIp"` // 芯片IP
  18. ReportTime time.Time `db:"report_time" json:"reportTime"` // 数据上报时间
  19. }
  20. // 保存实车上传的监控信息
  21. func SaveDeviceMonitor(c *gin.Context) {
  22. param := new(DeviceMonitor)
  23. // 映射到结构体
  24. if err := c.ShouldBindJSON(&param); err != nil {
  25. c_log.GlobalLogger.Error("项目启动接收请求参数报错:", err)
  26. c.JSON(http.StatusBadRequest, commonEntity.Response{
  27. Code: 500,
  28. Msg: "请求体解析失败。",
  29. })
  30. return
  31. }
  32. // 插入到数据库
  33. sqlTemplate, _ := util.ReadFile(c_db.SqlFilesMap["insert_device_monitor.sql"])
  34. if err := c_db.DoTx(sqlTemplate, []any{
  35. param.TotalCpuUsage,
  36. param.TotalMemoryUsage,
  37. param.Top10Process,
  38. param.DeviceNumber,
  39. param.SocIp,
  40. time.Now(),
  41. }); err != nil {
  42. c_log.GlobalLogger.Error("插入数据报错:", err)
  43. c.JSON(http.StatusBadRequest, commonEntity.Response{
  44. Code: 500,
  45. Msg: "插入数据报错。",
  46. })
  47. return
  48. }
  49. c.JSON(http.StatusOK, commonEntity.Response{
  50. Code: 200,
  51. Msg: "插入数据成功。",
  52. })
  53. }