processlog.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package model
  2. import (
  3. "database/sql"
  4. "errors"
  5. "fmt"
  6. "time"
  7. )
  8. type ProcessLog struct {
  9. Model
  10. }
  11. type ProcessLogRow struct {
  12. Id int64
  13. RedisId int64
  14. RedisAddress string
  15. RedisRemark string
  16. ProcessTime int64
  17. MaxMemoryBefore int64
  18. MaxMemoryAfter int64
  19. }
  20. func NewProcessLog(db *sql.DB) *ProcessLog {
  21. return &ProcessLog{
  22. Model{
  23. db: db,
  24. },
  25. }
  26. }
  27. func (m *ProcessLog) Add(row *ProcessLogRow) (int64, error) {
  28. if row.RedisId <= 0 {
  29. return 0, errors.New("Redis的ID必须是正整数")
  30. }
  31. if row.ProcessTime <= 0 {
  32. row.ProcessTime = time.Now().Unix()
  33. }
  34. ret, err := m.db.Exec("INSERT INTO processlog(redis_id,process_time,maxmemory_before,maxmemory_after) VALUES(?,?,?,?)", row.RedisId, row.ProcessTime, row.MaxMemoryBefore, row.MaxMemoryAfter)
  35. if err != nil {
  36. return 0, err
  37. } else {
  38. return ret.LastInsertId()
  39. }
  40. }
  41. func (m *ProcessLog) CleanUntil(until int64) (int64, error) {
  42. ret, err := m.db.Exec("DELETE FROM processlog WHERE process_time<?", until)
  43. if err != nil {
  44. return 0, err
  45. } else {
  46. return ret.RowsAffected()
  47. }
  48. }
  49. func (m *ProcessLog) Count(redisId int64) (int64, error) {
  50. var (
  51. row *sql.Row
  52. cnt int64
  53. )
  54. if redisId == 0 {
  55. row = m.db.QueryRow("SELECT COUNT(*) FROM processlog")
  56. } else {
  57. row = m.db.QueryRow("SELECT COUNT(*) FROM processlog WHERE redis_id=?", redisId)
  58. }
  59. if err := row.Scan(&cnt); err != nil {
  60. return 0, err
  61. } else {
  62. return cnt, nil
  63. }
  64. }
  65. func (m *ProcessLog) GetList(redisId int64, offset int64, limit int64, orderDesc bool) ([]*ProcessLogRow, error) {
  66. var (
  67. orderDir string
  68. ret *sql.Rows
  69. err error
  70. )
  71. if orderDesc {
  72. orderDir = "DESC"
  73. } else {
  74. orderDir = "ASC"
  75. }
  76. if redisId == 0 {
  77. if limit > 0 {
  78. sql := fmt.Sprintf("SELECT processlog.id,processlog.redis_id,processlog.process_time,processlog.maxmemory_before,processlog.maxmemory_after,rediscfg.address,rediscfg.remark FROM processlog INNER JOIN rediscfg ON processlog.redis_id=rediscfg.id ORDER BY processlog.id %s LIMIT ?,?", orderDir)
  79. ret, err = m.db.Query(sql, offset, limit)
  80. } else {
  81. sql := fmt.Sprintf("SELECT processlog.id,processlog.redis_id,processlog.process_time,processlog.maxmemory_before,processlog.maxmemory_after,rediscfg.address,rediscfg.remark FROM processlog INNER JOIN rediscfg ON processlog.redis_id=rediscfg.id ORDER BY processlog.id %s", orderDir)
  82. ret, err = m.db.Query(sql)
  83. }
  84. } else {
  85. if limit > 0 {
  86. sql := fmt.Sprintf("SELECT processlog.id,processlog.redis_id,processlog.process_time,processlog.maxmemory_before,processlog.maxmemory_after,rediscfg.address,rediscfg.remark FROM processlog INNER JOIN rediscfg ON processlog.redis_id=rediscfg.id WHERE processlog.redis_id=? ORDER BY processlog.id %s LIMIT ?,?", orderDir)
  87. ret, err = m.db.Query(sql, redisId, offset, limit)
  88. } else {
  89. sql := fmt.Sprintf("SELECT processlog.id,processlog.redis_id,processlog.process_time,processlog.maxmemory_before,processlog.maxmemory_after,rediscfg.address,rediscfg.remark FROM processlog INNER JOIN rediscfg ON processlog.redis_id=rediscfg.id WHERE processlog.redis_id=? ORDER BY processlog.id %s", orderDir)
  90. ret, err = m.db.Query(sql, redisId)
  91. }
  92. }
  93. if err != nil {
  94. return nil, err
  95. } else {
  96. rows := make([]*ProcessLogRow, 0)
  97. for ret.Next() {
  98. row := ProcessLogRow{}
  99. err = ret.Scan(&row.Id, &row.RedisId, &row.ProcessTime, &row.MaxMemoryBefore, &row.MaxMemoryAfter, &row.RedisAddress, &row.RedisRemark)
  100. if err != nil {
  101. continue
  102. }
  103. rows = append(rows, &row)
  104. }
  105. return rows, nil
  106. }
  107. }