stat.js 10.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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
/**
 * @class UniStatDataStat uni统计-数据统计调度处理模块
 * @function cron 数据统计定时任务处理函数
 * @function stat 数据统计调度处理函数
 * @function cleanLog 日志清理调度处理函数
 */
const {
	DateTime
} = require('./lib')

const {
	sleep
} = require('../shared')

const {
	BaseMod,
	SessionLog,
	PageLog,
	EventLog,
	ShareLog,
	ErrorLog,
	StatResult,
	ActiveDevices,
	ActiveUsers,
	PageResult,
	EventResult,
	ErrorResult,
	Loyalty,
	RunErrors,
	UserSessionLog,
	uniPay,
	Setting
} = require('./mod')
class UniStatDataStat {
	/**
	 * 数据统计定时任务处理函数
	 * @param {Object} context 服务器请求上下文参数
	 */
	async cron(context) {
		const baseMod = new BaseMod()
		const dateTime = new DateTime()
		console.log('Cron start time: ', dateTime.getDate('Y-m-d H:i:s'))

		// const setting = new Setting();
		// let settingValue = await setting.getSetting()
		// if (settingValue.mode === "close") {
		// 	// 如果关闭了统计任务,则任务直接结束
		// 	return {
		// 		code: 0,
		// 		msg: 'Task is close',
		// 	}
		// } else if (settingValue.mode === "auto") {
		// 	// 如果开启了节能模式,则判断N天内是否有设备访问记录
		// 	let runKey = await setting.checkAutoRun(settingValue);
		// 	if (!runKey) {
		// 		return {
		// 			code: 0,
		// 			msg: 'Task is auto close',
		// 		}
		// 	}
		// }

		//获取运行参数
		const timeInfo = dateTime.getTimeInfo(null, false)
		const cronConfig = baseMod.getConfig('cron')
		const cronMin = baseMod.getConfig('cronMin')
		const realtimeStat = baseMod.getConfig('realtimeStat')
		// 数据跑批
		let res = null
		if (cronConfig && cronConfig.length > 0) {
			for (var mi in cronConfig) {
				const currCronConfig = cronConfig[mi]
				const cronType = currCronConfig.type
				const cronTime = currCronConfig.time.split(' ')
				const cronDimension = currCronConfig.dimension

				//未开启分钟级定时任务,则设置为小时级定时任务
				if (cronTime.length === 4 && !cronMin) {
					cronTime.splice(3, 1)
				}

				if (baseMod.debug) {
					console.log('cronTime', cronTime)
				}
				//精度为分钟级的定时任务
				if (cronTime.length === 4) {
					if (cronTime[0] !== '*') {
						//周统计任务
						if (timeInfo.nWeek == cronTime[0] && timeInfo.nHour == cronTime[2] && timeInfo.nMinutes ==
							cronTime[3]) {
							let dimension = cronDimension || 'week';
							console.log(cronType + `--${dimension} run`)
							res = await this.stat({
								type: cronType,
								dimension: cronDimension,
								config: currCronConfig
							})
						}
					} else if (cronTime[1] !== '*') {
						//月统计任务(包含季度统计任务和年统计任务)
						if (timeInfo.nDay == cronTime[1] && timeInfo.nHour == cronTime[2] && timeInfo.nMinutes ==
							cronTime[3]) {
							let dimension = cronDimension || 'month';
							console.log(cronType + `--${dimension} run`)
							res = await this.stat({
								type: cronType,
								dimension: dimension,
								config: currCronConfig
							})
						}
					} else if (cronTime[2] !== '*') {
						//日统计任务
						if (timeInfo.nHour == cronTime[2] && timeInfo.nMinutes == cronTime[3]) {
							let dimension = cronDimension || 'day';
							console.log(cronType + `--${dimension} run`)
							res = await this.stat({
								type: cronType,
								dimension: dimension,
								config: currCronConfig
							})
						}
					} else if (cronTime[3] !== '*') {
						//实时统计任务
						if (timeInfo.nMinutes == cronTime[3] && realtimeStat) {
							let dimension = cronDimension || 'hour';
							console.log(cronType + `--${dimension} run`)
							res = await this.stat({
								type: cronType,
								dimension: dimension,
								config: currCronConfig
							})
						}
					}
				}
				//精度为小时级的定时任务
				else if (cronTime.length === 3) {
					if (cronTime[0] !== '*') {
						//周统计任务
						if (timeInfo.nWeek == cronTime[0] && timeInfo.nHour == cronTime[2]) {
							let dimension = cronDimension || 'week';
							console.log(cronType + `--${dimension} run`)
							res = await this.stat({
								type: cronType,
								dimension: dimension,
								config: currCronConfig
							})
						}
					} else if (cronTime[1] !== '*') {
						//月统计任务(包含季度统计任务和年统计任务)
						if (timeInfo.nDay == cronTime[1] && timeInfo.nHour == cronTime[2]) {
							let dimension = cronDimension || 'month';
							console.log(cronType + `--${dimension} run`)
							res = await this.stat({
								type: cronType,
								dimension: dimension,
								config: currCronConfig
							})
						}
					} else if (cronTime[2] !== '*') {
						//日统计任务
						if (timeInfo.nHour == cronTime[2]) {
							let dimension = cronDimension || 'day';
							console.log(cronType + `--${dimension} run`)
							res = await this.stat({
								type: cronType,
								dimension: dimension,
								config: currCronConfig
							})
						}
					} else {
						//实时统计任务
						if (realtimeStat) {
							let dimension = cronDimension || 'hour';
							console.log(cronType + `--${dimension} run`)
							res = await this.stat({
								type: cronType,
								dimension: dimension,
								config: currCronConfig
							})
						}
					}
				} else {
					console.error('Cron configuration error')
				}
			}
		}
		console.log('Cron end time: ', dateTime.getDate('Y-m-d H:i:s'))
		return {
			code: 0,
			msg: 'Task have done',
			lastCronResult: res
		}
	}

	/**
	 * 数据统计调度处理函数
	 * @param {Object} params 统计参数
	 */
	async stat(params) {
		const {
			type,
			dimension,
			date,
			reset,
			config
		} = params
		let res = {
			code: 0,
			msg: 'success'
		}

		try {
			switch (type) {
				// 基础统计
				case 'stat': {
					const resultStat = new StatResult()
					res = await resultStat.stat(dimension, date, reset)
					break
				}
				// 活跃设备统计归集
				case 'active-device': {
					const activeDevices = new ActiveDevices()
					res = await activeDevices.stat(date, reset)
					break
				}
				// 活跃用户统计归集
				case 'active-user': {
					const activeUsers = new ActiveUsers()
					res = await activeUsers.stat(date, reset)
					break
				}
				// 设备留存统计
				case 'retention-device': {
					const retentionStat = new StatResult()
					res = await retentionStat.retentionStat(dimension, date)
					break
				}
				// 用户留存统计
				case 'retention-user': {
					const retentionStat = new StatResult()
					res = await retentionStat.retentionStat(dimension, date, 'user')
					break
				}
				// 页面统计
				case 'page': {
					const pageStat = new PageResult()
					res = await pageStat.stat(dimension, date, reset)
					break
				}
				// 事件统计
				case 'event': {
					const eventStat = new EventResult()
					res = await eventStat.stat(dimension, date, reset)
					break
				}
				// 错误统计
				case 'error': {
					const errorStat = new ErrorResult()
					res = await errorStat.stat(dimension, date, reset)
					break
				}
				// 设备忠诚度统计
				case 'loyalty': {
					const loyaltyStat = new Loyalty()
					res = await loyaltyStat.stat(dimension, date, reset)
					break
				}
				// 日志清理
				case 'clean': {
					res = await this.cleanLog()
				}
				// 支付统计
				case 'pay-result': {
					const paymentResult = new uniPay.PayResult()
					res = await paymentResult.stat(dimension, date, reset, config)
					break
				}
			}
		} catch (e) {
			const maxTryTimes = 2
			if (!this.tryTimes) {
				this.tryTimes = 1
			} else {
				this.tryTimes++
			}

			//报错则重新尝试2次, 解决部分云服务器偶现连接超时问题
			if (this.tryTimes <= maxTryTimes) {
				//休眠1秒后重新调用
				await sleep(1000)
				params.reset = true
				res = await this.stat(params)
			} else {
				// 2次尝试失败后记录错误
				console.error('server error: ' + e)
				const runError = new RunErrors()
				runError.create({
					mod: 'stat',
					params: params,
					error: e,
					create_time: new DateTime().getTime()
				})

				res = {
					code: 500,
					msg: 'server error' + e
				}
			}
		}
		return res
	}

	/**
	 * 日志清理调度处理函数
	 */
	async cleanLog() {
		const baseMod = new BaseMod()
		const cleanLog = baseMod.getConfig('cleanLog')
		if (!cleanLog || !cleanLog.open) {
			return {
				code: 100,
				msg: 'The log cleanup service has not been turned on'
			}
		}

		const res = {
			code: 0,
			msg: 'success',
			data: {}
		}

		// 会话日志
		if (cleanLog.reserveDays.sessionLog > 0) {
			const sessionLog = new SessionLog()
			res.data.sessionLog = await sessionLog.clean(cleanLog.reserveDays.sessionLog)
		}

		// 用户会话日志
		if (cleanLog.reserveDays.userSessionLog > 0) {
			const userSessionLog = new UserSessionLog()
			res.data.userSessionLog = await userSessionLog.clean(cleanLog.reserveDays.userSessionLog)
		}

		// 页面日志
		if (cleanLog.reserveDays.pageLog > 0) {
			const pageLog = new PageLog()
			res.data.pageLog = await pageLog.clean(cleanLog.reserveDays.pageLog)
		}

		// 事件日志
		if (cleanLog.reserveDays.eventLog > 0) {
			const eventLog = new EventLog()
			res.data.eventLog = await eventLog.clean(cleanLog.reserveDays.eventLog)
		}

		// 分享日志
		if (cleanLog.reserveDays.shareLog > 0) {
			const shareLog = new ShareLog()
			res.data.shareLog = await shareLog.clean(cleanLog.reserveDays.shareLog)
		}

		// 错误日志
		if (cleanLog.reserveDays.errorLog > 0) {
			const errorLog = new ErrorLog()
			res.data.errorLog = await errorLog.clean(cleanLog.reserveDays.errorLog)
		}

		// 活跃设备日志
		const activeDevicesLog = new ActiveDevices()
		res.data.activeDevicesLog = await activeDevicesLog.clean()

		// 活跃用户日志
		const activeUsersLog = new ActiveUsers()
		res.data.activeUsersLog = await activeUsersLog.clean()

		// 实时统计日志
		const resultHourLog = new StatResult()
		res.data.resultHourLog = await resultHourLog.cleanHourLog()

		//原生应用崩溃日志
		const appCrashLogs = new AppCrashLogs()
		res.data.appCrashLogs = await appCrashLogs.clean()

		return res
	}
}

module.exports = UniStatDataStat