list.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package operation_log
  4. import (
  5. "context"
  6. "github.com/jaryhe/gopkgs/database"
  7. "smart-log/errors"
  8. "smart-log/model"
  9. "smart-log/pb/v1"
  10. )
  11. func LogList(ctx context.Context, req *v1.LogListRequest) (reply *v1.LogListReply, err error) {
  12. p := &model.TLog{}
  13. p.Type = req.Type
  14. list, total, err := p.ListWithProject(database.DB(), req.Page, req.Start, req.End)
  15. if err != nil {
  16. return nil, errors.DataBaseError
  17. }
  18. reply = &v1.LogListReply{
  19. Total:total,
  20. Page:req.Page,
  21. PageSize:int32(model.PageSize),
  22. }
  23. reply.List = make([]*v1.LogInfo, len(list))
  24. for i, v := range list {
  25. item := &v1.LogInfo{
  26. Name:v.Name,
  27. Uid:v.Uid,
  28. Operation:v.Operation,
  29. Detail:v.Detail,
  30. Result:"成功",
  31. Time:v.CreatedAt.Format("2006-01-02 15:04:05"),
  32. ProjectId:v.ProjectId,
  33. ProjectNo:v.ProjectNo,
  34. ProjectName:v.ProjectName,
  35. }
  36. if v.Result == 2 {
  37. item.Result = "失败"
  38. }
  39. reply.List[i] = item
  40. }
  41. return reply, nil
  42. }
  43. /*
  44. func getLogListWhere(req *v1.LogListRequest) string {
  45. whereArray := []string{}
  46. if req.Result != 0 {
  47. whereArray = append(whereArray, fmt.Sprintf("result = %d", req.Result))
  48. }
  49. if req.Name != "" {
  50. whereArray = append(whereArray, fmt.Sprintf("user_name=~/%s/", req.Name))
  51. }
  52. if req.Start > 0 {
  53. str := time.Unix(req.Start, 0).Format("2006-01-02 15:04:05")
  54. whereArray = append(whereArray, fmt.Sprintf("time >= '%s'", str))
  55. }
  56. if req.End > 0 {
  57. str := time.Unix(req.End, 0).Format("2006-01-02 15:04:05")
  58. whereArray = append(whereArray, fmt.Sprintf("time < '%s'", str))
  59. }
  60. if req.Operation > 0 {
  61. whereArray = append(whereArray, fmt.Sprintf("operation = %d", req.Operation))
  62. }
  63. where := ""
  64. for _, v := range whereArray {
  65. if where == "" {
  66. where = fmt.Sprintf("where %s", v)
  67. } else {
  68. where = fmt.Sprintf(" %s and %s", where, v)
  69. }
  70. }
  71. return where
  72. }
  73. type LogInfo struct {
  74. Uid string `json:"uid"`
  75. UserName string `json:"user_name"`
  76. Operation int `json:"operation"`
  77. OperationName string `json:"operation_name"`
  78. Result int `json:"result"`
  79. Detail string `json:"detail"`
  80. Time string `json:"time"`
  81. }
  82. */
  83. /*
  84. func LogListFromInflux(ctx context.Context, req *v1.LogListRequest) (reply *v1.LogListReply, err error) {
  85. if req.Page == 0 {
  86. req.Page = 1
  87. }
  88. if req.ProjectId == 0 {
  89. return nil, errors.ParamsError
  90. }
  91. reply = &v1.LogListReply{
  92. Page:req.Page,
  93. PageSize:model.PageSize,
  94. }
  95. offset := (req.Page - 1) *model.PageSize
  96. // prepare sql
  97. where := getLogListWhere(req)
  98. sql := fmt.Sprintf("select * from \"%d\" %s limit %d offset %d",req.ProjectId, where, model.PageSize, offset)
  99. countSql := fmt.Sprintf("select count(operation) as count from \"%d\" %s",req.ProjectId, where)
  100. // get count
  101. type Result struct {
  102. Count int64
  103. }
  104. array := []Result{}
  105. _, err = model.QueryInfluxdb(countSql, "operation_log", &array)
  106. if err != nil {
  107. return nil, errors.DataBaseError
  108. }
  109. if len(array) == 0 {
  110. return reply, nil
  111. }
  112. reply.Total = array[0].Count
  113. // get list
  114. lArray := []LogInfo{}
  115. _, err = model.QueryInfluxdb(sql, "operation_log", &lArray)
  116. if err != nil {
  117. return nil, errors.DataBaseError
  118. }
  119. if len(lArray) == 0 {
  120. return reply, nil
  121. }
  122. reply.LogList = make([]*v1.LogInfo, len(lArray))
  123. for i, v := range lArray {
  124. item := &v1.LogInfo{}
  125. item.Result = "失败"
  126. if v.Result == 1 {
  127. item.Result = "成功"
  128. }
  129. item.Operation = v.OperationName
  130. item.Name = v.UserName
  131. item.Detail = v.Detail
  132. item.Time = v.Time
  133. item.Uid,_ = strconv.ParseInt(v.Uid, 10, 64)
  134. reply.LogList[i] = item
  135. }
  136. return reply, nil
  137. }
  138. */
  139. /*
  140. func LogList(ctx context.Context, req *v1.LogListRequest) (reply *v1.LogListReply, err error) {
  141. filter := model.FilterInfo{}
  142. if req.Page == 0 {
  143. req.Page = 1
  144. }
  145. filter.Page = (req.Page - 1) *model.PageSize
  146. filter.Start = req.Start
  147. filter.End = req.End
  148. filter.Name = req.Name
  149. filter.Operation = req.Operation
  150. filter.Result = req.Result
  151. reply = &v1.LogListReply{}
  152. p := &model.TLog{}
  153. list, total, err := p.List(database.DB(), filter)
  154. if err != nil {
  155. return nil, errors.DataBaseError
  156. }
  157. reply.PageSize = model.PageSize
  158. reply.Total = total
  159. reply.LogList = make([]*v1.LogInfo, len(list))
  160. for i, v := range list {
  161. item := &v1.LogInfo{}
  162. item.Result = "失败"
  163. if v.Result == 1 {
  164. item.Result = "成功"
  165. }
  166. item.Operation = v.OperationName
  167. item.Name = v.Name
  168. item.Detail = v.Detail
  169. item.Uid = v.Uid
  170. reply.LogList[i] = item
  171. }
  172. return reply, nil
  173. }
  174. */