errorLog.js 3.4 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 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
/**
 * @class ErrorLog 错误日志模型
 */
const BaseMod = require('./base')
const Platform = require('./platform')
const Channel = require('./channel')
const {
	DateTime,
	UniCrypto
} = require('../lib')
module.exports = class ErrorLog extends BaseMod {
	constructor() {
		super()
		this.tableName = 'error-logs'
	}

	/**
	 * 错误日志数据填充
	 * @param {Object} reportParams 上报参数
	 */
	async fill(reportParams) {
		let params, errorHash, errorCount, cacheKey;
		const fillParams = []
		const platform = new Platform()
		const dateTime = new DateTime()
		const uniCrypto = new UniCrypto()
		const channel = new Channel()
		const {
			needCheck,
			checkTime
		} = this.getConfig('errorCheck')
		const errorCheckTime = Math.max(checkTime, 1)
		let spaceId
		let spaceProvider
		for (const rk in reportParams) {
			params = reportParams[rk]
			errorHash = uniCrypto.md5(params.em)
			cacheKey = 'error-count-' + errorHash
			// 校验在指定时间段内是否已存在相同的错误项
			if (needCheck) {
				errorCount = await this.getCache(cacheKey)
				if (!errorCount) {
					errorCount = await this.getCollection(this.tableName).where({
						error_hash: errorHash,
						create_time: {
							$gte: dateTime.getTime() - errorCheckTime * 60000
						}
					}).count()
					if (errorCount && errorCount.total > 0) {
						await this.setCache(cacheKey, errorCount, errorCheckTime * 60)
					}
				}

				if (errorCount && errorCount.total > 0) {
					if (this.debug) {
						console.log('This error have already existsed: ' + params.em)
					}
					continue
				}
			}

			//获取云端信息
			spaceId = null
			spaceProvider = null
			if (params.spi) {
				//云函数调用参数
				spaceId = params.spi.spaceId
				spaceProvider = params.spi.provider
			} else {
				//云对象调用参数
				if (params.spid) {
					spaceId = params.spid
				}
				if (params.sppd) {
					spaceProvider = params.sppd
				}
			}

			// 填充数据
			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 : '',
				os: params.on ? params.on : platform.getOsName(params.p),
				ua: params.ua ? params.ua : '',
				page_url: params.url ? params.url : '',
				space_id: spaceId ? spaceId : '',
				space_provider: spaceProvider ? spaceProvider : '',
				platform_version: params.mpv ? params.mpv : '',
				error_msg: params.em ? params.em : '',
				error_hash: errorHash,
				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 error 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 error log:', res)
		}
		return res
	}
}