import LogDB, { DBLogDayItem, DBLogItem } from "./log_db" import OperationQueue from "./opration_queue" import { dateFormat2Day } from './lib/utils' import LogReporter from './log_reporter' import { LogConfig, LogItem, NOOP, RET } from "./interface" import log_configer from "./log_configer" import LogQueue from "./log_queue" class LogManager { static instance_ = null private logDB: LogDB = null private operationQueue: OperationQueue // private logConfig: LogConfig = null private logReporter: LogReporter = null private logQueue: LogQueue = null private today: string = null private dailyRemoved = false//每日一次的大清除是否完成,默认未完成 constructor(DBReady?: () => void) { this.logReporter = new LogReporter() //所有log存取、删除操作相关的任务,都push到这个Queue中执行 this.operationQueue = new OperationQueue() this.logQueue = new LogQueue((logItems: Array) => { //从队列中取出一条日志后,对日志的操作 this.operationQueue.invokeInQueue(() => { const { ImLogs, logs } = logItems.reduce((obj, logItem) => { if (logItem.isImmediate === true) { obj.ImLogs.push(logItem) } else { obj.logs.push(logItem) } return obj }, { ImLogs: [], logs: [] }) // console.log('ImLogs', ImLogs, 'logs', logs) if (ImLogs.length > 0) { this.reportImmediately(ImLogs) } if (logs.length > 0) { this.logDB.addLogItems(logs).then(({ ret, data }) => { // if (ret === 1){} //存储成功 }) } }) }, (log_configer.get('isOnebyone')) as boolean) this.today = dateFormat2Day(new Date()) this.init(DBReady) } getDB() { return this.logDB } async init(DBReady: () => void) { this.logDB = new LogDB(log_configer.get('DBName'), async () => { //每次登陆后先进行清理 DBReady && DBReady() // const logDate = await this.logDB.getDayRemovedDayInfo() if (!this.dailyRemoved) { this.reportDaily().then(() => { // console.log('--today第一次清除---今日完成清除---') this.logQueue.startOperate() }) } else { console.log('开始init') this.logQueue.startOperate() } }) } static getInstance(DBReady?: () => void) { if (this.instance_ === null) { this.instance_ = new LogManager(DBReady); } return this.instance_ } //日志主动调用次方法 若logDB未初始化好,100ms后准备重试一次 // async regist(params: LogItem) { // if (!this.logDB.db) { // console.error('----regist logdb is unprepared-----') // return Promise.resolve(setTimeout(() => this.operationQueue.invokeInQueue(() => this.logDB.addLogItem(params)), 100)) // } else { // return await this.operationQueue.invokeInQueue(() => this.logDB.addLogItem(params)) // } // } async regist(log: LogItem) { console.log('--regist- && -invokePushQueue-') return this.logQueue.invokePushQueue(log) } /** * @description 未及时上报的imdiate_table的logs上报 */ async reportImmediateLogsUnReported() { const logs: Array = await this.logDB.getAllImediateLogs() if (logs.length > 0) { this.reportLogs(logs, () => { }) } else { return } } //每日一次的上传计划,实例化后最先完成的事 async reportDaily(): Promise { return await this.operationQueue.invokeInQueue(() => this.reportDaily_()) } async reportDaily_() { const days = await this.logDB.getAllLogDays() || [] const logs: Array = await this.logDB.getLogsByReportName(days.map(day => { return day.logDay })) || [] return this.reportLogs(logs, async () => { await this.logDB.incrementalDeleteLogs(logs, days) //删除当日清除的日志信息和日志日期信息 this.dailyRemoved = true this.logDB.updateDayRemovedInfo(this.today) console.info('Daily Report LOGs has success') }) } /** * @description 日志上报请求 * @param logs 待上传的日志 * @param callback */ async reportLogs(logs: Array, callback: Function) { if (logs.length === 0) return const data = await this.logReporter.report({ data: logs }) console.log('reportDaily callback data', data) if (data.ret === RET.SUCCESS) { callback && callback(data) } else { console.error(data.error) } } /** * @description 上报日志记录,立即上传的记录 * @param logItems */ async reportImmediately(logItems: Array) { if (logItems.length === 0) return this.logDB.addLogItemsImmediate(logItems).then(async ({ ret, data }) => { if (ret === RET.SUCCESS) { console.log('存储日志记录成功', data.ids) const dbLogs: Array = await this.logDB.getLogItemsImmediate(data.ids) this.reportLogs(dbLogs, async () => { await this.logDB.incrementDeleteLogsInImmediateTable(data.ids) }) } }) } //上传错误信息 errorHandler() { } } export default LogManager