shareLog.js 2.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/**
 * @class ShareLog 分享日志模型
 */
const BaseMod = require('./base')
const Platform = require('./platform')
const Channel = require('./channel')
const SessionLog = require('./sessionLog')
const {
	DateTime
} = require('../lib')
module.exports = class ShareLog extends BaseMod {
	constructor() {
		super()
		this.tableName = 'share-logs'
	}

	/**
	 * 分析日志填充
	 * @param {Object} reportParams 上报参数
	 * @param {Object} sessionLogData 会话日志数据,此参数传递可减少数据库查询
	 */
	async fill(reportParams, sessionLogData) {
		let params, sessionLogInfo, sessionKey;
		const fillParams = []
		const sessionLog = new SessionLog()
		const platform = new Platform()
		const dateTime = new DateTime()
		const channel = new Channel()
		for (const rk in reportParams) {
			params = reportParams[rk]
			
			//暂存下会话数据,减少读库
			sessionKey = params.ak + params.did + params.p
			if (!sessionLogData[sessionKey]) {
				// 会话日志
				sessionLogInfo = await sessionLog.getSession(params)
				if (sessionLogInfo.code) {
					return sessionLogInfo
				}
				if (this.debug) {
					console.log('sessionLogInfo', JSON.stringify(sessionLogInfo))
				}
				sessionLogData[sessionKey] = sessionLogInfo
			} else {
				sessionLogInfo = sessionLogData[sessionKey]
			}

			// 填充数据
			fillParams.push({
				appid: params.ak,
				version: params.v ? params.v : '',
				platform: platform.getPlatformCode(params.ut, params.p),
				channel: channel.getChannelCode(params),
				device_id: params.did,
				uid: params.uid ? params.uid : '',
				session_id: sessionLogInfo.data.sessionLogId,
				page_id: sessionLogInfo.data.pageId,
				create_time: dateTime.getTime()
			})
		}

		if (fillParams.length === 0) {
			return {
				code: 200,
				msg: 'Invild param'
			}
		}

		const res = await this.insert(this.tableName, fillParams)
		if (res && res.inserted) {
			return {
				code: 0,
				msg: 'success'
			}
		} else {
			return {
				code: 500,
				msg: 'Filled error'
			}
		}
	}

	/**
	 * 分享日志清理
	 * @param {Number} days 保留天数
	 */
	async clean(days) {
		days = Math.max(parseInt(days), 1)
		console.log('clean share logs - day:', days)

		const dateTime = new DateTime()

		const res = await this.delete(this.tableName, {
			create_time: {
				$lt: dateTime.getTimeBySetDays(0 - days)
			}
		})

		if (!res.code) {
			console.log('clean share log:', res)
		}
		return res
	}
}