eventLog.js 4.1 KB
Newer Older
study夏羽's avatar
study夏羽 已提交
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
/**
 * @class EventLog 事件日志模型
 */
const BaseMod = require('./base')
const Platform = require('./platform')
const Channel = require('./channel')
const StatEvent = require('./event')
const SessionLog = require('./sessionLog')
const ShareLog = require('./shareLog')
const {
	DateTime
} = require('../lib')
module.exports = class EventLog extends BaseMod {
	constructor() {
		super()
		this.tableName = 'event-logs'
		this.sessionLogInfo = []
	}

	/**
	 * 事件日志填充
	 * @param {Object} reportParams 上报参数
	 */
	async fill(reportParams) {
		let params;
		let sessionKey, sessionLogKey;
		let sessionLogInfo;
		const sessionData = []
		const fillParams = []
		const shareParams = []
		const sessionLog = new SessionLog()
		const event = new StatEvent()
		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 (!this.sessionLogInfo[sessionKey]) {
				// 会话日志
				sessionLogInfo = await sessionLog.getSession(params)
				if (sessionLogInfo.code) {
					return sessionLogInfo
				}
				if (this.debug) {
					console.log('sessionLogInfo', JSON.stringify(sessionLogInfo))
				}
				this.sessionLogInfo[sessionKey] = sessionLogInfo
			} else {
				sessionLogInfo = this.sessionLogInfo[sessionKey]
			}

			// 会话数据
			sessionLogKey = sessionLogInfo.data.sessionLogId.toString()
			if (!sessionData[sessionLogKey]) {
				sessionData[sessionLogKey] = {
					eventCount: sessionLogInfo.data.eventCount + 1,
					addEventCount: 1,
					uid: sessionLogInfo.data.uid,
					createTime: sessionLogInfo.data.createTime
				}
			} else {
				sessionData[sessionLogKey].eventCount++
				sessionData[sessionLogKey].addEventCount++
			}

			// 事件
			const eventInfo = await event.getEventAndCreate(params.ak, params.e_n)

			// 填充数据
			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,
				event_key: eventInfo.event_key,
				param: params.e_v ? params.e_v : '',
				// 版本
				sdk_version: params.mpsdk ? params.mpsdk : '',
				platform_version: params.mpv ? params.mpv : '',
				// 设备相关
				device_os_name: params.on ? params.on : platform.getOsName(params.p),
				device_os_version: params.sv ? params.sv : '',
				device_vendor: params.brand ? params.brand : '',
				device_model: params.md ? params.md : '',
				device_language: params.lang ? params.lang : '',
				device_pixel_ratio: params.pr ? params.pr : '',
				device_window_width: params.ww ? params.ww : '',
				device_window_height: params.wh ? params.wh : '',
				device_screen_width: params.sw ? params.sw : '',
				device_screen_height: params.sh ? params.sh : '',
				create_time: dateTime.getTime()
			})
			// 分享数据
			if (eventInfo.event_key === 'share') {
				shareParams.push(params)
			}
		}

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

		if (shareParams.length > 0) {
			const shareLog = new ShareLog()
			await shareLog.fill(shareParams, this.sessionLogInfo)
		}

		const res = await this.insert(this.tableName, fillParams)
		if (res && res.inserted) {
			for (const sid in sessionData) {
				await sessionLog.updateSession(sid, sessionData[sid])
			}
			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 event 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 event log:', res)
		}
		return res
	}
}