activeUsers.js 8.3 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 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
/**
 * @class ActiveUsers 活跃用户模型 - 每日跑批合并,仅添加本周/本月首次访问的用户。
 */
const BaseMod = require('./base')
const Platform = require('./platform')
const Channel = require('./channel')
const Version = require('./version')
const UserSessionLog = require('./userSessionLog')
const {
	DateTime,
	UniCrypto
} = require('../lib')
module.exports = class ActiveUsers extends BaseMod {
	constructor() {
		super()
		this.tableName = 'active-users'
		this.platforms = []
		this.channels = []
		this.versions = []
	}

	async stat(date, reset) {
		const dateTime = new DateTime()
		const dateDimension = dateTime.getTimeDimensionByType('day', -1, date)
		this.startTime = dateDimension.startTime
		// 查看当前时间段数据是否已存在,防止重复生成
		if (!reset) {
			const checkRes = await this.getCollection(this.tableName).where({
				create_time: {
					$gte: dateDimension.startTime,
					$lte: dateDimension.endTime
				}
			}).get()
			if (checkRes.data.length > 0) {
				console.log('data have exists')
				return {
					code: 1003,
					msg: 'Users data in this time have already existed'
				}
			}
		} else {
			const delRes = await this.delete(this.tableName, {
				create_time: {
					$gte: dateDimension.startTime,
					$lte: dateDimension.endTime
				}
			})
			console.log('Delete old data result:', JSON.stringify(delRes))
		}

		const userSessionLog = new UserSessionLog()
		const statRes = await this.aggregate(userSessionLog.tableName, {
			project: {
				appid: 1,
				version: 1,
				platform: 1,
				channel: 1,
				create_time: 1,
				uid: 1
			},
			match: {
				create_time: {
					$gte: dateDimension.startTime,
					$lte: dateDimension.endTime
				}
			},
			group: {
				_id: {
					appid: '$appid',
					version: '$version',
					platform: '$platform',
					channel: '$channel',
					uid: '$uid'
				},
				create_time: {
					$min: '$create_time'
				}
			},
			sort: {
				create_time: 1
			},
			getAll: true
		})

		let res = {
			code: 0,
			msg: 'success'
		}
		// if (this.debug) {
		//   console.log('statRes', JSON.stringify(statRes))
		// }
		if (statRes.data.length > 0) {
			const uniCrypto = new UniCrypto()
			// 同应用、平台、渠道、版本的数据合并
			const statData = [];
			let statKey;
			let data

			for (const sti in statRes.data) {
				data = statRes.data[sti]
				statKey = uniCrypto.md5(data._id.appid + data._id.platform + data._id.version + data._id
					.channel)
				if (!statData[statKey]) {
					statData[statKey] = {
						appid: data._id.appid,
						platform: data._id.platform,
						version: data._id.version,
						channel: data._id.channel,
						uids: [],
						info: []
					}
					statData[statKey].uids.push(data._id.uid)
					statData[statKey].info[data._id.uid] = {
						create_time: data.create_time
					}
				} else {
					statData[statKey].uids.push(data._id.uid)
					statData[statKey].info[data._id.uid] = {
						create_time: data.create_time
					}
				}
			}

			this.fillData = []
			for (const sk in statData) {
				await this.getFillData(statData[sk])
			}

			if (this.fillData.length > 0) {
				res = await this.batchInsert(this.tableName, this.fillData)
			}
		}
		return res
	}

	async getFillData(data) {
		// 平台信息
		let platformInfo = null
		if (this.platforms && this.platforms[data.platform]) {
			platformInfo = this.platforms[data.platform]
		} else {
			const platform = new Platform()
			platformInfo = await platform.getPlatformAndCreate(data.platform, null)
			if (!platformInfo || platformInfo.length === 0) {
				platformInfo._id = ''
			}
			this.platforms[data.platform] = platformInfo
			if (this.debug) {
				console.log('platformInfo', JSON.stringify(platformInfo))
			}
		}

		// 渠道信息
		let channelInfo = null
		const channelKey = data.appid + '_' + platformInfo._id + '_' + data.channel
		if (this.channels && this.channels[channelKey]) {
			channelInfo = this.channels[channelKey]
		} else {
			const channel = new Channel()
			channelInfo = await channel.getChannelAndCreate(data.appid, platformInfo._id, data.channel)
			if (!channelInfo || channelInfo.length === 0) {
				channelInfo._id = ''
			}
			this.channels[channelKey] = channelInfo
			if (this.debug) {
				console.log('channelInfo', JSON.stringify(channelInfo))
			}
		}

		// 版本信息
		let versionInfo = null
		const versionKey = data.appid + '_' + data.platform + '_' + data.version
		if (this.versions && this.versions[versionKey]) {
			versionInfo = this.versions[versionKey]
		} else {
			const version = new Version()
			versionInfo = await version.getVersionAndCreate(data.appid, data.platform, data.version)
			if (!versionInfo || versionInfo.length === 0) {
				versionInfo._id = ''
			}
			this.versions[versionKey] = versionInfo
			if (this.debug) {
				console.log('versionInfo', JSON.stringify(versionInfo))
			}
		}

		// 是否在本周内已存在
		const datetime = new DateTime()
		const dateDimension = datetime.getTimeDimensionByType('week', 0, this.startTime)

		// 取出本周已经存储的uid
		const weekHaveUserList = []
		const haveWeekList = await this.selectAll(this.tableName, {
			appid: data.appid,
			version_id: versionInfo._id,
			platform_id: platformInfo._id,
			channel_id: channelInfo._id,
			uid: {
				$in: data.uids
			},
			dimension: 'week',
			create_time: {
				$gte: dateDimension.startTime,
				$lte: dateDimension.endTime
			}
		}, {
			uid: 1
		})

		if (this.debug) {
			console.log('haveWeekList', JSON.stringify(haveWeekList))
		}

		if (haveWeekList.data.length > 0) {
			for (const hui in haveWeekList.data) {
				weekHaveUserList.push(haveWeekList.data[hui].uid)
			}
		}

		// 取出本月已经存储的uid
		const dateMonthDimension = datetime.getTimeDimensionByType('month', 0, this.startTime)
		const monthHaveUserList = []
		const haveMonthList = await this.selectAll(this.tableName, {
			appid: data.appid,
			version_id: versionInfo._id,
			platform_id: platformInfo._id,
			channel_id: channelInfo._id,
			uid: {
				$in: data.uids
			},
			dimension: 'month',
			create_time: {
				$gte: dateMonthDimension.startTime,
				$lte: dateMonthDimension.endTime
			}
		}, {
			uid: 1
		})

		if (this.debug) {
			console.log('haveMonthList', JSON.stringify(haveMonthList))
		}

		if (haveMonthList.data.length > 0) {
			for (const hui in haveMonthList.data) {
				monthHaveUserList.push(haveMonthList.data[hui].uid)
			}
		}

		for (const ui in data.uids) {
			if (!weekHaveUserList.includes(data.uids[ui])) {
				this.fillData.push({
					appid: data.appid,
					platform_id: platformInfo._id,
					channel_id: channelInfo._id,
					version_id: versionInfo._id,
					uid: data.uids[ui],
					dimension: 'week',
					create_time: data.info[data.uids[ui]].create_time
				})
			}

			if (!monthHaveUserList.includes(data.uids[ui])) {
				this.fillData.push({
					appid: data.appid,
					platform_id: platformInfo._id,
					channel_id: channelInfo._id,
					version_id: versionInfo._id,
					uid: data.uids[ui],
					dimension: 'month',
					create_time: data.info[data.uids[ui]].create_time
				})
			}
		}

		return true
	}
	
	/**
	 * 日志清理,此处日志为临时数据并不需要自定义清理,默认为固定值即可
	 */
	async clean() {
		// 清除周数据,周留存统计最高需要10周数据,多余的为无用数据
		const weeks = 10
		console.log('Clean user\'s weekly logs - week:', weeks)

		const dateTime = new DateTime()

		const res = await this.delete(this.tableName, {
			dimension: 'week',
			create_time: {
				$lt: dateTime.getTimeBySetWeek(0 - weeks)
			}
		})

		if (!res.code) {
			console.log('Clean user\'s weekly logs - res:', res)
		}

		// 清除月数据,月留存统计最高需要10个月数据,多余的为无用数据
		const monthes = 10
		console.log('Clean user\'s monthly logs - month:', monthes)
		const monthRes = await this.delete(this.tableName, {
			dimension: 'month',
			create_time: {
				$lt: dateTime.getTimeBySetMonth(0 - monthes)
			}
		})
		if (!monthRes.code) {
			console.log('Clean user\'s monthly logs - res:', res)
		}
		return monthRes
	}
}