1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15import { HiLog } from './HiLog'; 16import { getDLPInfo } from './FileUtils/utils'; 17import Constants from './constant'; 18import { ObjectUtil } from './ObjectUtil'; 19import hiSysEvent from '@ohos.hiSysEvent'; 20 21const TAG: string = 'ReportToBigDataUtil'; 22 23export declare type BigDataMsg = Record<string, number | string | boolean | undefined>; 24 25interface DlpBundleInfo { 26 PNAMEID: string; 27 PVERSIONID: string; 28} 29 30export class ReportToBigDataUtil { 31 private static readonly DLP_UE_DOMAIN: string = 'DLP_UE'; 32 private static readonly DLP_DOMAIN: string = 'DLP'; 33 private static currentBundleInfo: DlpBundleInfo | undefined = undefined; 34 35 private static async loadBundleInfo(): Promise<DlpBundleInfo> { 36 if (ReportToBigDataUtil.currentBundleInfo) { 37 return ReportToBigDataUtil.currentBundleInfo; 38 } 39 const getDLPInfoRet = await getDLPInfo(); 40 if (getDLPInfoRet.errcode !== Constants.ERR_CODE_SUCCESS) { 41 HiLog.error(TAG, 'get DLP bundle info failed'); 42 return { PNAMEID: '', PVERSIONID: '' }; 43 } 44 ReportToBigDataUtil.currentBundleInfo = { 45 PNAMEID: getDLPInfoRet.result?.name ?? '', 46 PVERSIONID: getDLPInfoRet.result?.versionCode ?? '' 47 }; 48 return ReportToBigDataUtil.currentBundleInfo; 49 } 50 51 private static async constructEventMsg(eventMsg?: object | BigDataMsg): Promise<object | BigDataMsg> { 52 let dlpBundleInfo = await ReportToBigDataUtil.loadBundleInfo(); 53 let msg = eventMsg ? ObjectUtil.AssignCopyAll({}, dlpBundleInfo, eventMsg) : dlpBundleInfo; 54 return msg; 55 } 56 57 public static report(eventId: string, eventMsg?: object | BigDataMsg): void { 58 ReportToBigDataUtil.constructEventMsg(eventMsg).then((msg: object): void => { 59 ReportToBigDataUtil.reportByEventType(hiSysEvent.EventType.BEHAVIOR, eventId, msg); 60 }) 61 } 62 63 public static reportErrEvent(eventId: string, eventMsg?: object | BigDataMsg): void { 64 ReportToBigDataUtil.constructEventMsg(eventMsg).then((msg: object): void => { 65 ReportToBigDataUtil.reportByEventType(hiSysEvent.EventType.FAULT, eventId, msg); 66 }) 67 } 68 69 private static reportByEventType(eventType: hiSysEvent.EventType, eventId: string, eventMsg?: object | BigDataMsg) { 70 let info: hiSysEvent.SysEventInfo = { 71 domain: ReportToBigDataUtil.DLP_UE_DOMAIN, 72 name: eventId, 73 eventType: eventType, 74 params: eventMsg 75 } 76 hiSysEvent.write(info, (err, val) => { 77 if (err) { 78 HiLog.error(TAG, `report failed, err: ${JSON.stringify(err)}, val: ${JSON.stringify(val)}`); 79 } else { 80 let msg: string[] = []; 81 msg.push(`domain: ${info.domain}`); 82 msg.push(`eventId: ${info.name}`); 83 msg.push(`eventType: ${info.eventType}`); 84 msg.push(eventMsg ? `eventMsg: ${JSON.stringify(info.params)}` : ''); 85 HiLog.info(TAG, msg.join(' ')); 86 } 87 }); 88 } 89} 90 91export class BigDataConstants { 92 public static readonly DLP_FILE_OPEN: string = 'DLP_FILE_OPEN'; 93 public static readonly DLP_FILE_OPEN_EVENT: string = 'DLP_FILE_OPEN_EVENT'; 94 public static readonly DLP_MANAGER_ACCOUNT_LOGIN: string = 'DLP_MANAGER_ACCOUNT_LOGIN'; 95 public static readonly DLP_MANAGER_FILE_CONFIGURATION: string = 'DLP_MANAGER_FILE_CONFIGURATION'; 96 public static readonly DLP_FILE_CREATE_EVENT: string = 'DLP_FILE_CREATE_EVENT'; 97 public static readonly DLP_FILE_CREATE: string = 'DLP_FILE_CREATE'; 98}