All files / src report-log.ts

97.56% Statements 40/41
85.71% Branches 24/28
100% Functions 7/7
97.22% Lines 35/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1031x 1x 1x         1x 1x 1x             3x 3x 3x                       3x             1x     6x     3x 1x       3x       3x     3x       3x               3x 3x 3x   3x 7x 4x   4x 5x   3x   3x     2x 2x 1x     1x   2x           1x     3x      
import { ReportConfig, ResultMsg, ReportResult } from './interface';
import LoganDB from './lib/logan-db';
import {
    LoganLogDayItem,
    FormattedLogReportName,
    LOG_DAY_TABLE_PRIMARY_KEY
} from './lib/logan-db';
import Config from './global';
import { ajaxPost } from './lib/ajax';
import { dayFormat2Date, ONE_DAY_TIME_SPAN, dateFormat2Day } from './lib/utils';
let LoganDBInstance: LoganDB;
interface ReportResponse {
    code: number;
}
 
async function getLogAndSend (reportName: string, reportConfig: ReportConfig): Promise<ReportResponse> {
    const logItems = await LoganDBInstance.getLogsByReportName(reportName);
    const logReportOb = LoganDBInstance.logReportNameParser(reportName);
    return await ajaxPost(
        reportConfig.reportUrl || (Config.get('reportUrl') as string),
        {
            client: 'Web',
            webSource: `${reportConfig.webSource || ''}`,
            deviceId: reportConfig.deviceId,
            environment: `${reportConfig.environment || ''}`,
            customInfo: `${reportConfig.customInfo || ''}`,
            logPageNo: logReportOb.pageIndex + 1, // pageNo start from 1,
            fileDate: logReportOb.logDay,
            logArray: logItems
                .map(logItem => {
                    return encodeURIComponent(logItem.logString);
                })
                .toString()
        }
    ) as Promise<ReportResponse>;
}
 
export default async function reportLog (
    reportConfig: ReportConfig
): Promise<ReportResult> {
    if (E!LoganDB.idbIsSupported()) {
        throw new Error(ResultMsg.DB_NOT_SUPPORT);
    } else {
        if (!LoganDBInstance) {
            LoganDBInstance = new LoganDB(Config.get('dbName') as
                | string
                | undefined);
        }
        const logDaysInfoList: LoganLogDayItem[] = await LoganDBInstance.getLogDaysInfo(
            reportConfig.fromDayString,
            reportConfig.toDayString
        );
        const logReportMap: {
            [key: string]: FormattedLogReportName[];
        } = logDaysInfoList.reduce((acc, logDayInfo: LoganLogDayItem) => {
            return {
                [logDayInfo[
                    LOG_DAY_TABLE_PRIMARY_KEY
                ]]: logDayInfo.reportPagesInfo ? logDayInfo.reportPagesInfo.pageSizes.map((i, pageIndex) => {
                    return LoganDBInstance.logReportNameFormatter(
                        logDayInfo[LOG_DAY_TABLE_PRIMARY_KEY],
                        pageIndex
                    );
                }) : [],
                ...acc
            };
        }, {});
        const reportResult: ReportResult = {};
        const startDate = dayFormat2Date(reportConfig.fromDayString);
        const endDate = dayFormat2Date(reportConfig.toDayString);
        for (
            let logTime = +startDate;
            logTime <= +endDate;
            logTime += ONE_DAY_TIME_SPAN
        ) {
            const logDay = dateFormat2Day(new Date(logTime));
            if (logReportMap[logDay] && logReportMap[logDay].length > 0) {
                try {
                    const results = (await Promise.all(
                        logReportMap[logDay].map(reportName => {
                            return getLogAndSend(reportName, reportConfig);
                        })
                    ));
                    results.forEach(result => {
                        if (result.code !== 200) {
                            throw new Error(`Server error: ${result.code}`);
                        }
                    });
                    reportResult[logDay] = { msg: ResultMsg.REPORT_LOG_SUCC };
                } catch (e) {
                    reportResult[logDay] = {
                        msg: ResultMsg.REPORT_LOG_FAIL,
                        desc: e.message || e.stack || JSON.stringify(e)
                    };
                }
            } else {
                reportResult[logDay] = { msg: ResultMsg.NO_LOG };
            }
        }
        return reportResult;
    }
}