shareLog.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @class ShareLog 分享日志模型
  3. */
  4. const BaseMod = require('./base')
  5. const Platform = require('./platform')
  6. const Channel = require('./channel')
  7. const SessionLog = require('./sessionLog')
  8. const {
  9. DateTime
  10. } = require('../lib')
  11. module.exports = class ShareLog extends BaseMod {
  12. constructor() {
  13. super()
  14. this.tableName = 'share-logs'
  15. }
  16. /**
  17. * 分析日志填充
  18. * @param {Object} reportParams 上报参数
  19. * @param {Object} sessionLogData 会话日志数据,此参数传递可减少数据库查询
  20. */
  21. async fill(reportParams, sessionLogData) {
  22. let params, sessionLogInfo, sessionKey;
  23. const fillParams = []
  24. const sessionLog = new SessionLog()
  25. const platform = new Platform()
  26. const dateTime = new DateTime()
  27. const channel = new Channel()
  28. for (const rk in reportParams) {
  29. params = reportParams[rk]
  30. //暂存下会话数据,减少读库
  31. sessionKey = params.ak + params.did + params.p
  32. if (!sessionLogData[sessionKey]) {
  33. // 会话日志
  34. sessionLogInfo = await sessionLog.getSession(params)
  35. if (sessionLogInfo.code) {
  36. return sessionLogInfo
  37. }
  38. if (this.debug) {
  39. console.log('sessionLogInfo', JSON.stringify(sessionLogInfo))
  40. }
  41. sessionLogData[sessionKey] = sessionLogInfo
  42. } else {
  43. sessionLogInfo = sessionLogData[sessionKey]
  44. }
  45. // 填充数据
  46. fillParams.push({
  47. appid: params.ak,
  48. version: params.v ? params.v : '',
  49. platform: platform.getPlatformCode(params.ut, params.p),
  50. channel: channel.getChannelCode(params),
  51. device_id: params.did,
  52. uid: params.uid ? params.uid : '',
  53. session_id: sessionLogInfo.data.sessionLogId,
  54. page_id: sessionLogInfo.data.pageId,
  55. create_time: dateTime.getTime()
  56. })
  57. }
  58. if (fillParams.length === 0) {
  59. return {
  60. code: 200,
  61. msg: 'Invild param'
  62. }
  63. }
  64. const res = await this.insert(this.tableName, fillParams)
  65. if (res && res.inserted) {
  66. return {
  67. code: 0,
  68. msg: 'success'
  69. }
  70. } else {
  71. return {
  72. code: 500,
  73. msg: 'Filled error'
  74. }
  75. }
  76. }
  77. /**
  78. * 分享日志清理
  79. * @param {Number} days 保留天数
  80. */
  81. async clean(days) {
  82. days = Math.max(parseInt(days), 1)
  83. console.log('clean share logs - day:', days)
  84. const dateTime = new DateTime()
  85. const res = await this.delete(this.tableName, {
  86. create_time: {
  87. $lt: dateTime.getTimeBySetDays(0 - days)
  88. }
  89. })
  90. if (!res.code) {
  91. console.log('clean share log:', res)
  92. }
  93. return res
  94. }
  95. }