errorLog.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @class ErrorLog 错误日志模型
  3. */
  4. const BaseMod = require('./base')
  5. const Platform = require('./platform')
  6. const Channel = require('./channel')
  7. const {
  8. DateTime,
  9. UniCrypto
  10. } = require('../lib')
  11. module.exports = class ErrorLog extends BaseMod {
  12. constructor() {
  13. super()
  14. this.tableName = 'error-logs'
  15. }
  16. /**
  17. * 错误日志数据填充
  18. * @param {Object} reportParams 上报参数
  19. */
  20. async fill(reportParams) {
  21. let params, errorHash, errorCount, cacheKey;
  22. const fillParams = []
  23. const platform = new Platform()
  24. const dateTime = new DateTime()
  25. const uniCrypto = new UniCrypto()
  26. const channel = new Channel()
  27. const {
  28. needCheck,
  29. checkTime
  30. } = this.getConfig('errorCheck')
  31. const errorCheckTime = Math.max(checkTime, 1)
  32. let spaceId
  33. let spaceProvider
  34. for (const rk in reportParams) {
  35. params = reportParams[rk]
  36. errorHash = uniCrypto.md5(params.em)
  37. cacheKey = 'error-count-' + errorHash
  38. // 校验在指定时间段内是否已存在相同的错误项
  39. if (needCheck) {
  40. errorCount = await this.getCache(cacheKey)
  41. if (!errorCount) {
  42. errorCount = await this.getCollection(this.tableName).where({
  43. error_hash: errorHash,
  44. create_time: {
  45. $gte: dateTime.getTime() - errorCheckTime * 60000
  46. }
  47. }).count()
  48. if (errorCount && errorCount.total > 0) {
  49. await this.setCache(cacheKey, errorCount, errorCheckTime * 60)
  50. }
  51. }
  52. if (errorCount && errorCount.total > 0) {
  53. if (this.debug) {
  54. console.log('This error have already existsed: ' + params.em)
  55. }
  56. continue
  57. }
  58. }
  59. //获取云端信息
  60. spaceId = null
  61. spaceProvider = null
  62. if (params.spi) {
  63. //云函数调用参数
  64. spaceId = params.spi.spaceId
  65. spaceProvider = params.spi.provider
  66. } else {
  67. //云对象调用参数
  68. if (params.spid) {
  69. spaceId = params.spid
  70. }
  71. if (params.sppd) {
  72. spaceProvider = params.sppd
  73. }
  74. }
  75. // 填充数据
  76. fillParams.push({
  77. appid: params.ak,
  78. version: params.v ? params.v : '',
  79. platform: platform.getPlatformCode(params.ut, params.p),
  80. channel: channel.getChannelCode(params),
  81. device_id: params.did,
  82. uid: params.uid ? params.uid : '',
  83. os: params.on ? params.on : platform.getOsName(params.p),
  84. ua: params.ua ? params.ua : '',
  85. page_url: params.url ? params.url : '',
  86. space_id: spaceId ? spaceId : '',
  87. space_provider: spaceProvider ? spaceProvider : '',
  88. platform_version: params.mpv ? params.mpv : '',
  89. error_msg: params.em ? params.em : '',
  90. error_hash: errorHash,
  91. create_time: dateTime.getTime()
  92. })
  93. }
  94. if (fillParams.length === 0) {
  95. return {
  96. code: 200,
  97. msg: 'Invild param'
  98. }
  99. }
  100. const res = await this.insert(this.tableName, fillParams)
  101. if (res && res.inserted) {
  102. return {
  103. code: 0,
  104. msg: 'success'
  105. }
  106. } else {
  107. return {
  108. code: 500,
  109. msg: 'Filled error'
  110. }
  111. }
  112. }
  113. /**
  114. * 错误日志清理
  115. * @param {Number} days 日志保留天数
  116. */
  117. async clean(days) {
  118. days = Math.max(parseInt(days), 1)
  119. console.log('clean error logs - day:', days)
  120. const dateTime = new DateTime()
  121. const res = await this.delete(this.tableName, {
  122. create_time: {
  123. $lt: dateTime.getTimeBySetDays(0 - days)
  124. }
  125. })
  126. if (!res.code) {
  127. console.log('clean error log:', res)
  128. }
  129. return res
  130. }
  131. }