publish.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "go-common/app/admin/main/macross/model/publish"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _logSharding = 10
  11. // dashborad
  12. _inDashboradSQL = `INSERT INTO dashboard (name,label,commit_info,out_url,coverage_url,text_size_arm64,res_size,extra) VALUES(?,?,?,?,?,?,?,?)`
  13. _inDashboradLogsSQL = `INSERT INTO dashboard_log_%02d (dashboard_id,level,msg) VALUES`
  14. )
  15. func (d *Dao) hitLogs(id int64) int64 {
  16. return id % _logSharding
  17. }
  18. // Dashborad insert dashboard.
  19. func (d *Dao) Dashborad(c context.Context, dashboard *publish.Dashboard) (rows int64, err error) {
  20. res, err := d.db.Exec(c, _inDashboradSQL, dashboard.Name, dashboard.Label, dashboard.Commit, dashboard.OutURL, dashboard.CoverageURL, dashboard.TextSizeArm64, dashboard.ResSize, dashboard.Extra)
  21. if err != nil {
  22. log.Error("Dashborad() d.db.Exec() error(%v)", err)
  23. return
  24. }
  25. rows, err = res.LastInsertId()
  26. return
  27. }
  28. // DashboradLogs insert dashboard log.
  29. func (d *Dao) DashboradLogs(c context.Context, id int64, logs []*publish.Log) (rows int64, err error) {
  30. var (
  31. buffer bytes.Buffer
  32. insertTp string
  33. )
  34. insertTp = "(%d,'%s','%s'),"
  35. buffer.WriteString(fmt.Sprintf(_inDashboradLogsSQL, d.hitLogs(id)))
  36. for _, v := range logs {
  37. buffer.WriteString(fmt.Sprintf(insertTp, id, v.Level, v.Msg))
  38. }
  39. buffer.Truncate(buffer.Len() - 1)
  40. res, err := d.db.Exec(c, buffer.String())
  41. if err != nil {
  42. log.Error("DashboradLogs d.db.Exec() error(%v)", err)
  43. return
  44. }
  45. rows, err = res.RowsAffected()
  46. return
  47. }