pageLog.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * @class PageLog 页面日志模型
  3. */
  4. const BaseMod = require('./base')
  5. const Page = require('./page')
  6. const Platform = require('./platform')
  7. const Channel = require('./channel')
  8. const SessionLog = require('./sessionLog')
  9. const {
  10. DateTime
  11. } = require('../lib')
  12. const {
  13. parseUrl
  14. } = require('../../shared')
  15. module.exports = class PageLog extends BaseMod {
  16. constructor() {
  17. super()
  18. this.tableName = 'page-logs'
  19. this.sessionLogInfo = []
  20. }
  21. /**
  22. * 页面日志数据填充
  23. * @param {Object} reportParams 上报参数
  24. */
  25. async fill(reportParams) {
  26. let params;
  27. let sessionKey
  28. let sessionLogKey
  29. let sessionLogInfo
  30. let pageKey
  31. let pageInfo
  32. let referPageInfo
  33. const sessionData = []
  34. const pageData = []
  35. const fillParams = []
  36. const sessionLog = new SessionLog()
  37. const page = new Page()
  38. const platform = new Platform()
  39. const dateTime = new DateTime()
  40. const channel = new Channel()
  41. for (const pk in reportParams) {
  42. params = reportParams[pk]
  43. if (['3', '4'].includes(params.lt) && !params.url && params.urlref) {
  44. params.url = params.urlref
  45. }
  46. // 页面信息
  47. pageKey = params.ak + params.url
  48. if (pageData[pageKey]) {
  49. pageInfo = pageData[pageKey]
  50. } else {
  51. pageInfo = await page.getPageAndCreate(params.ak, params.url, params.ttpj)
  52. if (!pageInfo || pageInfo.length === 0) {
  53. console.log('Not found this page by param:', JSON.stringify(params))
  54. continue
  55. }
  56. pageData[pageKey] = pageInfo
  57. }
  58. // 会话日志,暂存下会话数据,减少读库
  59. sessionKey = params.ak + params.did + params.p
  60. if (!this.sessionLogInfo[sessionKey]) {
  61. sessionLogInfo = await sessionLog.getSession(params)
  62. if (sessionLogInfo.code) {
  63. return sessionLogInfo
  64. }
  65. if (this.debug) {
  66. console.log('sessionLogInfo', JSON.stringify(sessionLogInfo))
  67. }
  68. this.sessionLogInfo[sessionKey] = sessionLogInfo
  69. } else {
  70. sessionLogInfo = this.sessionLogInfo[sessionKey]
  71. }
  72. // 会话数据
  73. sessionLogKey = sessionLogInfo.data.sessionLogId.toString()
  74. if (!sessionData[sessionLogKey]) {
  75. //临时存储减少查询次数
  76. sessionData[sessionLogKey] = {
  77. pageCount: sessionLogInfo.data.pageCount + 1,
  78. addPageCount: 1,
  79. createTime: sessionLogInfo.data.createTime,
  80. pageId: pageInfo._id,
  81. uid: sessionLogInfo.data.uid
  82. }
  83. if (this.debug) {
  84. console.log('add sessionData - ' + sessionLogKey, sessionData)
  85. }
  86. } else {
  87. sessionData[sessionLogKey].pageCount += 1
  88. sessionData[sessionLogKey].addPageCount += 1
  89. sessionData[sessionLogKey].pageId = pageInfo._id
  90. if (this.debug) {
  91. console.log('update sessionData - ' + sessionLogKey, sessionData)
  92. }
  93. }
  94. // 上级页面信息
  95. pageKey = params.ak + params.urlref
  96. if (pageData[pageKey]) {
  97. referPageInfo = pageData[pageKey]
  98. } else {
  99. referPageInfo = await page.getPageAndCreate(params.ak, params.urlref, params.ttpj)
  100. if (!referPageInfo || referPageInfo.length === 0) {
  101. referPageInfo = {_id:''}
  102. }
  103. pageData[pageKey] = referPageInfo
  104. }
  105. //当前页面url信息
  106. const urlInfo = parseUrl(params.url)
  107. // 填充数据
  108. fillParams.push({
  109. appid: params.ak,
  110. version: params.v ? params.v : '',
  111. platform: platform.getPlatformCode(params.ut, params.p),
  112. channel: channel.getChannelCode(params),
  113. device_id: params.did,
  114. uid: params.uid ? params.uid : '',
  115. session_id: sessionLogInfo.data.sessionLogId,
  116. page_id: pageInfo._id,
  117. query_string: urlInfo.query,
  118. //上级页面相关
  119. previous_page_id: referPageInfo._id,
  120. previous_page_duration: params.urlref_ts ? parseInt(params.urlref_ts) : 0,
  121. previous_page_is_entry: referPageInfo._id === sessionLogInfo.data.entryPageId ? 1 : 0,
  122. create_time: dateTime.getTime()
  123. })
  124. }
  125. if (fillParams.length === 0) {
  126. console.log('No page params')
  127. return {
  128. code: 200,
  129. msg: 'Invild param'
  130. }
  131. }
  132. //日志数据入库
  133. const res = await this.insert(this.tableName, fillParams)
  134. if (res && res.inserted) {
  135. // 更新会话数据
  136. const nowTime = dateTime.getTime()
  137. for (const sid in sessionData) {
  138. await sessionLog.updateSession(sid, sessionData[sid])
  139. }
  140. return {
  141. code: 0,
  142. msg: 'success'
  143. }
  144. } else {
  145. return {
  146. code: 500,
  147. msg: 'Filled error'
  148. }
  149. }
  150. }
  151. /**
  152. * 页面日志清理
  153. * @param {Number} days 页面日志保留天数
  154. */
  155. async clean(days) {
  156. days = Math.max(parseInt(days), 1)
  157. console.log('clean page logs - day:', days)
  158. const dateTime = new DateTime()
  159. const res = await this.delete(this.tableName, {
  160. create_time: {
  161. $lt: dateTime.getTimeBySetDays(0 - days)
  162. }
  163. })
  164. if (!res.code) {
  165. console.log('clean page log:', res)
  166. }
  167. return res
  168. }
  169. }