GAAS GFrame项目web后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.3 KiB

  1. package models
  2. import (
  3. "LAPP_GAAS_GFrame_BACKEND/conf"
  4. "LAPP_GAAS_GFrame_BACKEND/db"
  5. "LAPP_GAAS_GFrame_BACKEND/utils"
  6. "context"
  7. "fmt"
  8. "go.mongodb.org/mongo-driver/bson/primitive"
  9. "go.mongodb.org/mongo-driver/bson"
  10. "go.mongodb.org/mongo-driver/mongo/options"
  11. "log"
  12. "strings"
  13. "time"
  14. )
  15. type LeitServerLog struct {
  16. Level string `bson:"level" json:"leit_server_log-level"` //日志类别
  17. TimeStamp string `bson:"timestamp" json:"leit_server_log-timestamp"` //时间戳
  18. Message string `bson:"message" json:"leit_server_log-message"` //出错信息提示
  19. Function string `bson:"function" json:"leit_server_log-function"` //出错函数
  20. File string `bson:"file" json:"leit_server_log-file"` //出错文件
  21. Operator string `bson:"operator" json:"leit_server_log-operator"` //操作人员
  22. }
  23. type SerchData struct {
  24. Key string
  25. Val string
  26. Userid string
  27. }
  28. func (t *LeitServerLog) InsertRecord() (insertID primitive.ObjectID) {
  29. //1.初始化链接
  30. client := db.MgoDb()
  31. //2.选择数据库,数据表
  32. collect := client.Database(conf.DbConfig.Mongdbname).Collection("leit_server")
  33. insertRest, err := collect.InsertOne(context.TODO(), t)
  34. if err != nil {
  35. fmt.Println(err)
  36. return
  37. }
  38. insertID = insertRest.InsertedID.(primitive.ObjectID)
  39. return insertID
  40. }
  41. func (t *LeitServerLog) FindLog(logred SerchData, skip int64, limit int64) ([]*LeitServerLog, int64) {
  42. res := bson.M{}
  43. // 创建需要过滤的条件
  44. if logred.Key == "level" {
  45. res = bson.M{"level": primitive.Regex{Pattern: logred.Val}}
  46. }
  47. if logred.Key == "message" {
  48. res = bson.M{"message": primitive.Regex{Pattern: logred.Val}}
  49. }
  50. if logred.Key == "function" {
  51. res = bson.M{"function": primitive.Regex{Pattern: logred.Val}}
  52. }
  53. if logred.Key == "file" {
  54. res = bson.M{"file": primitive.Regex{Pattern: logred.Val}}
  55. }
  56. if logred.Key == "operator" {
  57. res = bson.M{"operator": primitive.Regex{Pattern: logred.Val}}
  58. }
  59. if logred.Key == "timestamp" {
  60. times := strings.Split(logred.Val, "-")
  61. stime := time.Unix(int64(utils.ValueToInt(times[0], 0)), 0)
  62. etime := time.Unix(int64(utils.ValueToInt(times[1], 0)), 0)
  63. stimestr := utils.TimeFormat(stime, "yyyyMMddHHmmss")
  64. etimestr := utils.TimeFormat(etime, "yyyyMMddHHmmss")
  65. res = bson.M{"timestamp": bson.M{"$lte": etimestr, "$gte": stimestr}}
  66. }
  67. if logred.Userid != "admin" {
  68. res = bson.M{"operator": logred.Userid}
  69. }
  70. //1.初始化链接
  71. client := db.MgoDb()
  72. //2.选择数据库,数据表
  73. collect := client.Database(conf.DbConfig.Mongdbname).Collection("leit_server")
  74. //var skip int64 = 0//从那个开始
  75. //var limit int64 = 2//炼制几个输出字段
  76. cursor, err := collect.Find(context.TODO(), res, &options.FindOptions{
  77. Skip: &skip,
  78. Limit: &limit,
  79. Sort: bson.D{{"timestamp", -1}},
  80. })
  81. if err != nil {
  82. fmt.Println(err)
  83. return nil, 0
  84. }
  85. defer cursor.Close(context.TODO())
  86. //创建需要反序列化成什么样子的结构体对象
  87. records := make([]*LeitServerLog, 0)
  88. for cursor.Next(context.TODO()) {
  89. record := &LeitServerLog{}
  90. //反序列化
  91. err = cursor.Decode(record)
  92. if err != nil {
  93. fmt.Println(err)
  94. return nil, 0
  95. }
  96. //追加
  97. records = append(records, record)
  98. }
  99. // 获取数据总数
  100. count, err := collect.CountDocuments(context.Background(), bson.D{})
  101. if err != nil {
  102. log.Fatal(count)
  103. }
  104. return records, count
  105. }