eventLog.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * @class EventLog 事件日志模型
  3. */
  4. const BaseMod = require('./base')
  5. const Platform = require('./platform')
  6. const Channel = require('./channel')
  7. const StatEvent = require('./event')
  8. const SessionLog = require('./sessionLog')
  9. const ShareLog = require('./shareLog')
  10. const {
  11. DateTime
  12. } = require('../lib')
  13. module.exports = class EventLog extends BaseMod {
  14. constructor() {
  15. super()
  16. this.tableName = 'event-logs'
  17. this.sessionLogInfo = []
  18. }
  19. /**
  20. * 事件日志填充
  21. * @param {Object} reportParams 上报参数
  22. */
  23. async fill(reportParams) {
  24. let params;
  25. let sessionKey, sessionLogKey;
  26. let sessionLogInfo;
  27. const sessionData = []
  28. const fillParams = []
  29. const shareParams = []
  30. const sessionLog = new SessionLog()
  31. const event = new StatEvent()
  32. const platform = new Platform()
  33. const dateTime = new DateTime()
  34. const channel = new Channel()
  35. for (const rk in reportParams) {
  36. params = reportParams[rk]
  37. //暂存下会话数据,减少读库
  38. sessionKey = params.ak + params.did + params.p
  39. if (!this.sessionLogInfo[sessionKey]) {
  40. // 会话日志
  41. sessionLogInfo = await sessionLog.getSession(params)
  42. if (sessionLogInfo.code) {
  43. return sessionLogInfo
  44. }
  45. if (this.debug) {
  46. console.log('sessionLogInfo', JSON.stringify(sessionLogInfo))
  47. }
  48. this.sessionLogInfo[sessionKey] = sessionLogInfo
  49. } else {
  50. sessionLogInfo = this.sessionLogInfo[sessionKey]
  51. }
  52. // 会话数据
  53. sessionLogKey = sessionLogInfo.data.sessionLogId.toString()
  54. if (!sessionData[sessionLogKey]) {
  55. sessionData[sessionLogKey] = {
  56. eventCount: sessionLogInfo.data.eventCount + 1,
  57. addEventCount: 1,
  58. uid: sessionLogInfo.data.uid,
  59. createTime: sessionLogInfo.data.createTime
  60. }
  61. } else {
  62. sessionData[sessionLogKey].eventCount++
  63. sessionData[sessionLogKey].addEventCount++
  64. }
  65. // 事件
  66. const eventInfo = await event.getEventAndCreate(params.ak, params.e_n)
  67. // 填充数据
  68. fillParams.push({
  69. appid: params.ak,
  70. version: params.v ? params.v : '',
  71. platform: platform.getPlatformCode(params.ut, params.p),
  72. channel: channel.getChannelCode(params),
  73. device_id: params.did,
  74. uid: params.uid ? params.uid : '',
  75. session_id: sessionLogInfo.data.sessionLogId,
  76. page_id: sessionLogInfo.data.pageId,
  77. event_key: eventInfo.event_key,
  78. param: params.e_v ? params.e_v : '',
  79. // 版本
  80. sdk_version: params.mpsdk ? params.mpsdk : '',
  81. platform_version: params.mpv ? params.mpv : '',
  82. // 设备相关
  83. device_os_name: params.on ? params.on : platform.getOsName(params.p),
  84. device_os_version: params.sv ? params.sv : '',
  85. device_vendor: params.brand ? params.brand : '',
  86. device_model: params.md ? params.md : '',
  87. device_language: params.lang ? params.lang : '',
  88. device_pixel_ratio: params.pr ? params.pr : '',
  89. device_window_width: params.ww ? params.ww : '',
  90. device_window_height: params.wh ? params.wh : '',
  91. device_screen_width: params.sw ? params.sw : '',
  92. device_screen_height: params.sh ? params.sh : '',
  93. create_time: dateTime.getTime()
  94. })
  95. // 分享数据
  96. if (eventInfo.event_key === 'share') {
  97. shareParams.push(params)
  98. }
  99. }
  100. if (fillParams.length === 0) {
  101. return {
  102. code: 200,
  103. msg: 'Invild param'
  104. }
  105. }
  106. if (shareParams.length > 0) {
  107. const shareLog = new ShareLog()
  108. await shareLog.fill(shareParams, this.sessionLogInfo)
  109. }
  110. const res = await this.insert(this.tableName, fillParams)
  111. if (res && res.inserted) {
  112. for (const sid in sessionData) {
  113. await sessionLog.updateSession(sid, sessionData[sid])
  114. }
  115. return {
  116. code: 0,
  117. msg: 'success'
  118. }
  119. } else {
  120. return {
  121. code: 500,
  122. msg: 'Filled error'
  123. }
  124. }
  125. }
  126. /**
  127. * 事件日志清理
  128. * @param {Number} days 保留天数
  129. */
  130. async clean(days) {
  131. days = Math.max(parseInt(days), 1)
  132. console.log('clean event logs - day:', days)
  133. const dateTime = new DateTime()
  134. //删除过期数据
  135. const res = await this.delete(this.tableName, {
  136. create_time: {
  137. $lt: dateTime.getTimeBySetDays(0 - days)
  138. }
  139. })
  140. if (!res.code) {
  141. console.log('clean event log:', res)
  142. }
  143. return res
  144. }
  145. }