/* * 更新redis缓存 * * @param {*} appId * @memberof SystemService */ async updateSystemCache(appId) { const system = await this.getSystemForDb(appId); await this.app.redis.set(appId, JSON.stringify(system)); }
// top浏览器排行 async getRealTimeTopBrowser(appId, beginTime, endTime) { let result = await this.app.redis.get(`${appId}_top_browser_realtime`); result = result ? JSON.parse(result) : await this.getRealTimeTopBrowserForDb(appId, beginTime, endTime); return result; }
/* 删除用户 * @param {*} id * @param {*} usertoken * @returns * @memberof UserService */ async delete(id, usertoken) { // 删除 const result = await this.ctx.model.User.findOneAndRemove({ _id: id }).exec(); // 清空登录态 if (usertoken) this.app.redis.set(`${usertoken}_user_login`, ''); return result; }
// 通过redis 消息队列消费数据 async saveWebReportDataForRedis(query) { try { if (this.app.config.redis_consumption.total_limit_web){ // 限流 const length = await this.app.redis.llen('web_repore_datas'); if (length >= this.app.config.redis_consumption.total_limit_web) return; } // 生产者 this.app.redis.lpush('web_repore_datas', JSON.stringify(query)); } catch (e) { console.log(e); } }
/* 登出 * @param {*} usertoken * @returns * @memberof UserService */ logout(usertoken) { this.ctx.cookies.set('usertoken', ''); this.app.redis.set(`${usertoken}_user_login`, ''); return {}; }
/* * 获得某个系统信息(redis) * * @param {*} appId * @return * @memberof SystemService */ async getSystemForAppId(appId) { if (!appId) throw new Error('查询某个系统信:appId不能为空'); const result = await this.app.redis.get(appId) || '{}'; return JSON.parse(result); }
/* * 获得列表信息 * * @returns * @memberof ErrorsService */ async getErrorList() { const result = await this.app.redis.lrange('db_servers_error_list', 0, -1); return result ? result : []; }
// 省份流量统计 async getProvinceAvgCount(appId, beginTime, endTime, type) { if (type) type = type * 1; if (type === 1) { let res = await this.app.redis.get(`${appId}_top_province_realtime`); res = res ? JSON.parse(res) : await this.getRealTimeTopProvinceForDb(appId, beginTime, endTime); return { provinces: res }; } else if (type === 2) { return await this.getDbTopAnalysis(appId, beginTime, endTime); } }
// 通过redis 消费者模式存储数据 async saveWxReportDataForRedis(query) { try { if (this.app.config.redis_consumption.total_limit_wx) { // 限流 const length = await this.app.redis.llen('wx_repore_datas'); if (length >= this.app.config.redis_consumption.total_limit_wx) return; } this.app.redis.lpush('wx_repore_datas', JSON.stringify(query)); } catch (e) { console.log(e); } }
// top跳出率 async getRealTimeTopJumpOut(appId, beginTime, endTime) { let result = await this.app.redis.get(`${appId}_top_jump_out_realtime`); result = result ? JSON.parse(result) : await this.getRealTimeTopJumpOutForDb(appId, beginTime, endTime); return result; }
/* 通过redis登录key获取用户信息 * @param {*} usertoken * @returns * @memberof UserService */ async getUserInfoForUsertoken(usertoken) { return this.app.redis.get(`${usertoken}_user_login`) || {}; }
// top 页面 async getRealTimeTopPages(appId, beginTime, endTime) { let result = await this.app.redis.get(`${appId}_top_pages_realtime`); result = result ? JSON.parse(result) : await this.getRealTimeTopPagesForDb(appId, beginTime, endTime); return result; }
// top省市排行榜 async getRealTimeTopProvince(appId, beginTime, endTime, type = 1) { let result = await this.app.redis.get(`${appId}_top_province_realtime`); result = (result && type === 1) ? JSON.parse(result) : await this.getRealTimeTopProvinceForDb(appId, beginTime, endTime, type); return result; }
// top跳出率 async getRealTimeTopJumpOut(appId, beginTime, endTime) { let result = await this.app.redis.get(`${appId}_top_jump_out_realtime`); result = result ? JSON.parse(result) : await this.getRealTimeTopJumpOutForDb(appId, beginTime, endTime); return result; }
// 省份流量统计 async getProvinceAvgCount(appId, beginTime, endTime, type) { if (type) type = type * 1; if (type === 1) { let res = await this.app.redis.get(`${appId}_top_province_realtime`); res = res ? JSON.parse(res) : await this.getRealTimeTopProvinceForDb(appId, beginTime, endTime); return { provinces: res }; } else if (type === 2) { return await this.getDbTopAnalysis(appId, beginTime, endTime); } }