From 0d7088e40bf7c6af43ec38f1e137f12a98f4e707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BF=9E=E6=BC=AA?= Date: Sun, 27 Apr 2025 21:20:07 +0800 Subject: [PATCH] =?UTF-8?q?asweb=E6=96=B0=E5=A2=9E=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E6=89=93=E7=82=B9=E4=B8=8Eapi=E6=89=93=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 连漪 --- .../interfaces/atomicserviceweb.js | 165 ++++++++++++++++- atomicserviceweb/source/atomicserviceweb.ets | 168 ++++++++++++++++++ 2 files changed, 332 insertions(+), 1 deletion(-) diff --git a/atomicserviceweb/interfaces/atomicserviceweb.js b/atomicserviceweb/interfaces/atomicserviceweb.js index 01aea88..c28fd51 100644 --- a/atomicserviceweb/interfaces/atomicserviceweb.js +++ b/atomicserviceweb/interfaces/atomicserviceweb.js @@ -51,6 +51,7 @@ const call = requireNapi('telephony.call'); const authentication = globalThis.requireNapi('core.authentication', false, '', 'hms'); const paymentService = globalThis.requireNapi('core.payment.paymentService', false, '', 'hms'); const atomicServiceWebNapi = requireInternal('atomicservice.AtomicServiceWeb'); +const hiAppEvent = globalThis.requireNapi('hiviewdfx.hiAppEvent'); let atomicBasicEngine = null; @@ -309,6 +310,7 @@ export class AtomicServiceWeb extends ViewPU { console.error(`AtomicServiceWeb set Web Debug Mode failed, code is ${d2.code}, message is ${d2.message}`); } this.initDomainCheckLog(); + HiAnalyticsUtil.reportComponentEvent(); } aboutToDisappear() { @@ -1193,18 +1195,22 @@ class AtomicServiceApi extends AtomicService { } pickCamera(c4) { + HiAnalyticsUtil.reportApiEvent('cameraPicker.pick()', ActionState.BEGIN); picker.pick(this.context, c4.mediaTypes, { cameraPosition: c4.cameraPosition, saveUri: c4.saveUri, videoDuration: c4.videoDuration }).then((g4) => { this.success(new PickCameraResult(g4.resultCode, g4.resultUri, g4.mediaType), c4); + HiAnalyticsUtil.reportApiEvent('cameraPicker.pick()', ActionState.SUCCESS); }).catch((f4) => { this.error(f4, c4); + HiAnalyticsUtil.reportApiEvent('selectPhoto', ActionState.FAIL, err.code); }); } selectPhoto(w3) { + HiAnalyticsUtil.reportApiEvent('photoViewPicker.select()', ActionState.BEGIN); let x3 = new photoAccessHelper.PhotoViewPicker(); x3.select({ MIMEType: w3.mimeType, @@ -1218,12 +1224,15 @@ class AtomicServiceApi extends AtomicService { preselectedUris: w3.preselectedUris }).then((b4) => { this.success(new SelectPhotoResult(b4.photoUris, b4.isOriginalPhoto), w3); + HiAnalyticsUtil.reportApiEvent('photoViewPicker.select()', ActionState.SUCCESS); }).catch((a4) => { this.error(a4, w3); + HiAnalyticsUtil.reportApiEvent('photoViewPicker.select()', ActionState.FAIL, err.code); }); } openPreview(n3) { + HiAnalyticsUtil.reportApiEvent('filePreview.openPreview()', ActionState.BEGIN); let o3 = this.getUri(n3.uri); filePreview.openPreview(this.context, { uri: o3, @@ -1231,14 +1240,18 @@ class AtomicServiceApi extends AtomicService { title: n3.title }).then(() => { this.success(new OpenPreviewResult(), n3); + HiAnalyticsUtil.reportApiEvent('filePreview.openPreview()', ActionState.SUCCESS); }).catch((v3) => { this.error(v3, n3); + HiAnalyticsUtil.reportApiEvent('filePreview.openPreview()', ActionState.FAIL, err.code); }); } uploadFile(z2) { + HiAnalyticsUtil.reportApiEvent('request.uploadFile()', ActionState.BEGIN); this.checkUploadFile(z2).then(d3 => { if (!d3.checkResult) { + HiAnalyticsUtil.reportApiEvent('request.uploadFile()', ActionState.FAIL); return; } let e3 = { @@ -1251,19 +1264,24 @@ class AtomicServiceApi extends AtomicService { request.uploadFile(this.context, e3).then((i3) => { i3.on('complete', (m3) => { this.handleUploadFileResult(m3, d3.uriMap, z2); + HiAnalyticsUtil.reportApiEvent('request.uploadFile()', ActionState.SUCCESS); }); i3.on('fail', (l3) => { this.handleUploadFileResult(l3, d3.uriMap, z2); + HiAnalyticsUtil.reportApiEvent('request.uploadFile()', ActionState.FAIL); }); }).catch((h3) => { this.error(h3, z2); + HiAnalyticsUtil.reportApiEvent('request.uploadFile()', ActionState.FAIL, err.code); }); }).catch((c3) => { this.error(c3, z2); + HiAnalyticsUtil.reportApiEvent('request.uploadFile()', ActionState.FAIL, err.code); }); } downloadFile(m2) { + HiAnalyticsUtil.reportApiEvent('request.downloadFile()', ActionState.BEGIN); let o2 = `${util.generateRandomUUID().replaceAll('-', '')}`; let p2 = `${this.context.cacheDir}/${o2}`; request.downloadFile(this.context, { @@ -1277,37 +1295,47 @@ class AtomicServiceApi extends AtomicService { }).then((t2) => { t2.on('complete', () => { this.success(new DownloadFileResult(p2), m2); + HiAnalyticsUtil.reportApiEvent('request.downloadFile()', ActionState.SUCCESS); }); t2.on('fail', w2 => { this.errorWithCodeAndMsg(new AsError(w2, 'File download fail.'), m2); + HiAnalyticsUtil.reportApiEvent('request.downloadFile()', ActionState.FAIL, errCode); }); }).catch((s2) => { this.error(s2, m2); + HiAnalyticsUtil.reportApiEvent('request.downloadFile()', ActionState.FAIL, err.code); }); } getNetworkType(c2) { + HiAnalyticsUtil.reportApiEvent('connection.getNetworkType()', ActionState.BEGIN); connection.getDefaultNet().then(g2 => { if (!g2 || g2.netId === 0) { this.errorWithCodeAndMsg(NETWORK_NO_ACTIVE_ERROR, c2); + HiAnalyticsUtil.reportApiEvent('request.downloadFile()', ActionState.FAIL, NETWORK_NO_ACTIVE_ERROR.code); return; } connection.getNetCapabilities(g2).then(k2 => { let l2 = new GetNetworkTypeResult(k2.bearerTypes, k2.networkCap, k2.linkUpBandwidthKbps, k2.linkDownBandwidthKbps); this.success(l2, c2); + HiAnalyticsUtil.reportApiEvent('connection.getNetworkType()', ActionState.SUCCESS); }).catch((j2) => { this.error(j2, c2); + HiAnalyticsUtil.reportApiEvent('request.downloadFile()', ActionState.FAIL, err.code); }); }).catch((f2) => { this.error(f2, c2); + HiAnalyticsUtil.reportApiEvent('request.downloadFile()', ActionState.FAIL, err.code); }); } getLocation(u1) { + HiAnalyticsUtil.reportApiEvent('location.getLocation()', ActionState.BEGIN); this.checkPermissions(PERMISSION_APPROXIMATELY_LOCATION, w1 => { if (w1) { this.error(w1, u1); + HiAnalyticsUtil.reportApiEvent('location.getLocation()', ActionState.FAIL, err.code); return; } geoLocationManager.getCurrentLocation({ @@ -1320,8 +1348,10 @@ class AtomicServiceApi extends AtomicService { new GetLocationResult(a2.latitude, a2.longitude, a2.altitude, a2.accuracy, a2.speed, a2.timeStamp, a2.direction, a2.timeSinceBoot, a2.additions, a2.additionSize); this.success(b2, u1); + HiAnalyticsUtil.reportApiEvent('location.getLocation()', ActionState.SUCCESS); }).catch((z1) => { this.error(z1, u1); + HiAnalyticsUtil.reportApiEvent('location.getLocation()', ActionState.FAIL, err.code); }); }); } @@ -1610,7 +1640,140 @@ class RequestPaymentOptions extends BaseOptions { class RequestPaymentResult { } - +class HiAnalyticsUtil { + /** + * 打点数据上报 + * + * @param time 时间戳 + * @param content 打点参数json字符串 + */ + static writeEndEvent(time, content) { + console.info(`writeEndEvent -> time: ${time} ,content: ${content} `); + let event = { + domain: 'api_diagnostic', + name: 'api_exec_end', + params: { + api_name: 'ascf', + sdk_name: 'atomicservice_web', + begin_time: time, + // 调用次数 + call_times: 3, + // 调用成功次数 + success_times: 1, + contents: content + }, + eventType: hiAppEvent.EventType.BEHAVIOR + }; + hiAppEvent.write(event); + } + /** + * 应用的唯一标识 + */ + static initAnalytics() { + if (!HiAnalyticsUtil.processorId) { + HiAnalyticsUtil.addEventProcessor(); + } + if (HiAnalyticsUtil.appIdentifier && HiAnalyticsUtil.runningMode) { + return; + } + let bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_SIGNATURE_INFO | + bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION); + HiAnalyticsUtil.appIdentifier = bundleInfo.signatureInfo.appIdentifier; + HiAnalyticsUtil.runningMode = bundleInfo.appInfo.appProvisionType; + } + /** + * 添加处理者 + */ + static addEventProcessor() { + let processor = { + name: 'ha_app_event', + appId: 'com_huawei_hmos_sdk_ocg', + routeInfo: 'https://cloudbackdown.hwcloudtest.cn', + eventConfigs: [ + { + domain: 'api_diagnostic', + name: 'api_exec_end', + isRealTime: false + }, + { + domain: 'api_diagnostic', + name: 'api_called_stat', + isRealTime: true + }, + { + domain: 'api_diagnostic', + name: 'api_called_stat_cnt', + isRealTime: true + }, + ], + periodReport: 30, + batchReport: 30 + }; + return hiAppEvent.addProcessor(processor); + } + /** + * asweb jssdk API数据打点业务处理 + * + * @param apiName 接口名 + * @param actionState 接口调用状态 + * @param errorNumber 错误码 + */ + static reportApiEvent(apiName, actionState, errorNumber) { + try { + if (!apiName || !actionState) { + return; + } + HiAnalyticsUtil.initAnalytics(); + const ascfAction = 'APICaller_' + apiName; + const content = JSON.stringify({ + appIdentify: HiAnalyticsUtil.appIdentifier, + ascfVersionName: deviceInfo.displayVersion, + runningMode: HiAnalyticsUtil.runningMode, + ascfAction, + caller: 'ASWeb', + actionState, + errorNumber + }); + HiAnalyticsUtil.writeEndEvent(new Date().getTime(), content); + } + catch (err) { + console.error(`reportApiEvent -> reportApiEvent error, message: ${err.message}`); + } + } + /** + * asweb 组件创建打点 + * + * @param apiName 接口名 + * @param actionState 接口调用状态 + * @param errorNumber 错误码 + */ + static reportComponentEvent() { + try { + HiAnalyticsUtil.initAnalytics(); + const content = JSON.stringify({ + appIdentify: HiAnalyticsUtil.appIdentifier, + ascfVersionName: deviceInfo.displayVersion, + runningMode: HiAnalyticsUtil.runningMode, + caller: 'ASWeb', + }); + HiAnalyticsUtil.writeEndEvent(new Date().getTime(), content); + } + catch (err) { + console.error(`reportApiEvent -> reportApiEvent error, message: ${err.message}`); + } + } +} +HiAnalyticsUtil.processorId = undefined; +/** + * 数据打点的接口状态 + */ +var ActionState; +(function (ActionState) { + ActionState["BEGIN"] = "begin"; + ActionState["SUCCESS"] = "success"; + ActionState["FAIL"] = "fail"; +})(ActionState || (ActionState = {})); +//# sourceMappingURL=atomicserviceweb.js.map export default { AtomicServiceWeb, AtomicServiceWebController diff --git a/atomicserviceweb/source/atomicserviceweb.ets b/atomicserviceweb/source/atomicserviceweb.ets index 83a2ba3..d1fb0f8 100644 --- a/atomicserviceweb/source/atomicserviceweb.ets +++ b/atomicserviceweb/source/atomicserviceweb.ets @@ -33,6 +33,7 @@ import { BusinessError } from '@ohos.base'; import { call } from '@kit.TelephonyKit'; import { authentication } from '@kit.AccountKit'; import { paymentService } from '@kit.PaymentKit'; +import { hiAppEvent } from '@kit.PerformanceAnalysisKit'; let atomicBasicEngine: ESObject | null = null; @@ -190,6 +191,7 @@ export struct AtomicServiceWeb { } this.initDomainCheckLog(); + HiAnalyticsUtil.reportComponentEvent(); } aboutToDisappear(): void { @@ -1119,6 +1121,7 @@ class AtomicServiceApi extends AtomicService { } pickCamera(options: PickCameraOptions): void { + HiAnalyticsUtil.reportApiEvent('cameraPicker.pick()', ActionState.BEGIN); picker.pick(this.context, options.mediaTypes as Array, { cameraPosition: options.cameraPosition, saveUri: options.saveUri, @@ -1126,12 +1129,15 @@ class AtomicServiceApi extends AtomicService { }).then((pickerResult: picker.PickerResult) => { this.success(new PickCameraResult(pickerResult.resultCode, pickerResult.resultUri, pickerResult.mediaType), options); + HiAnalyticsUtil.reportApiEvent('cameraPicker.pick()', ActionState.SUCCESS); }).catch((err: BusinessError) => { this.error(err, options); + HiAnalyticsUtil.reportApiEvent('selectPhoto', ActionState.FAIL, err.code); }); } selectPhoto(options: SelectPhotoOptions): void { + HiAnalyticsUtil.reportApiEvent('photoViewPicker.select()', ActionState.BEGIN); let photoViewPicker = new photoAccessHelper.PhotoViewPicker(); photoViewPicker.select({ MIMEType: options.mimeType as photoAccessHelper.PhotoViewMIMETypes, @@ -1145,12 +1151,15 @@ class AtomicServiceApi extends AtomicService { preselectedUris: options.preselectedUris }).then((selectResult: photoAccessHelper.PhotoSelectResult) => { this.success(new SelectPhotoResult(selectResult.photoUris, selectResult.isOriginalPhoto), options); + HiAnalyticsUtil.reportApiEvent('photoViewPicker.select()', ActionState.SUCCESS); }).catch((err: BusinessError) => { this.error(err, options); + HiAnalyticsUtil.reportApiEvent('photoViewPicker.select()', ActionState.FAIL, err.code); }); } openPreview(options: OpenPreviewOptions): void { + HiAnalyticsUtil.reportApiEvent('filePreview.openPreview()', ActionState.BEGIN); let uri: string = this.getUri(options.uri as string); filePreview.openPreview(this.context, { uri: uri, @@ -1158,14 +1167,18 @@ class AtomicServiceApi extends AtomicService { title: options.title }).then(() => { this.success(new OpenPreviewResult(), options); + HiAnalyticsUtil.reportApiEvent('filePreview.openPreview()', ActionState.SUCCESS); }).catch((err: BusinessError) => { this.error(err, options); + HiAnalyticsUtil.reportApiEvent('filePreview.openPreview()', ActionState.FAIL, err.code); }); } uploadFile(options: UploadFileOptions): void { + HiAnalyticsUtil.reportApiEvent('request.uploadFile()', ActionState.BEGIN); this.checkUploadFile(options).then(res => { if (!res.checkResult) { + HiAnalyticsUtil.reportApiEvent('request.uploadFile()', ActionState.FAIL); return; } let uploadConfig: request.UploadConfig = { @@ -1178,19 +1191,24 @@ class AtomicServiceApi extends AtomicService { request.uploadFile(this.context, uploadConfig).then((uploadTask: request.UploadTask) => { uploadTask.on('complete', (taskStateArray: Array) => { this.handleUploadFileResult(taskStateArray, res.uriMap as Map, options); + HiAnalyticsUtil.reportApiEvent('request.uploadFile()', ActionState.SUCCESS); }); uploadTask.on('fail', (taskStateArray: Array) => { this.handleUploadFileResult(taskStateArray, res.uriMap as Map, options); + HiAnalyticsUtil.reportApiEvent('request.uploadFile()', ActionState.FAIL); }); }).catch((err: BusinessError) => { this.error(err, options); + HiAnalyticsUtil.reportApiEvent('request.uploadFile()', ActionState.FAIL, err.code); }); }).catch((err: BusinessError) => { this.error(err, options); + HiAnalyticsUtil.reportApiEvent('request.uploadFile()', ActionState.FAIL, err.code); }); } downloadFile(options: DownloadFileOptions): void { + HiAnalyticsUtil.reportApiEvent('request.downloadFile()', ActionState.BEGIN); let cacheFileName: string = `${util.generateRandomUUID().replaceAll('-', '')}`; let filePath: string = `${this.context.cacheDir}/${cacheFileName}`; request.downloadFile(this.context, { @@ -1204,37 +1222,47 @@ class AtomicServiceApi extends AtomicService { }).then((downloadTask: request.DownloadTask) => { downloadTask.on('complete', () => { this.success(new DownloadFileResult(filePath), options); + HiAnalyticsUtil.reportApiEvent('request.downloadFile()', ActionState.SUCCESS); }); downloadTask.on('fail', errCode => { this.errorWithCodeAndMsg(new AsError(errCode, 'File download fail.'), options); + HiAnalyticsUtil.reportApiEvent('request.downloadFile()', ActionState.FAIL, errCode); }); }).catch((err: BusinessError) => { this.error(err, options); + HiAnalyticsUtil.reportApiEvent('request.downloadFile()', ActionState.FAIL, err.code); }); } getNetworkType(options: GetNetworkTypeOptions): void { + HiAnalyticsUtil.reportApiEvent('connection.getNetworkType()', ActionState.BEGIN); connection.getDefaultNet().then(netHandle => { if (!netHandle || netHandle.netId === 0) { this.errorWithCodeAndMsg(NETWORK_NO_ACTIVE_ERROR, options); + HiAnalyticsUtil.reportApiEvent('request.downloadFile()', ActionState.FAIL, NETWORK_NO_ACTIVE_ERROR.code); return; } connection.getNetCapabilities(netHandle).then(netCapabilities => { let res: GetNetworkTypeResult = new GetNetworkTypeResult(netCapabilities.bearerTypes, netCapabilities.networkCap, netCapabilities.linkUpBandwidthKbps, netCapabilities.linkDownBandwidthKbps); this.success(res, options); + HiAnalyticsUtil.reportApiEvent('connection.getNetworkType()', ActionState.SUCCESS); }).catch((err: BusinessError) => { this.error(err, options); + HiAnalyticsUtil.reportApiEvent('request.downloadFile()', ActionState.FAIL, err.code); }); }).catch((err: BusinessError) => { this.error(err, options); + HiAnalyticsUtil.reportApiEvent('request.downloadFile()', ActionState.FAIL, err.code); }); } getLocation(options: GetLocationOptions): void { + HiAnalyticsUtil.reportApiEvent('location.getLocation()', ActionState.BEGIN); this.checkPermissions(PERMISSION_APPROXIMATELY_LOCATION, err => { if (err) { this.error(err, options); + HiAnalyticsUtil.reportApiEvent('location.getLocation()', ActionState.FAIL, err.code); return; } geoLocationManager.getCurrentLocation({ @@ -1247,8 +1275,10 @@ class AtomicServiceApi extends AtomicService { location.accuracy, location.speed, location.timeStamp, location.direction, location.timeSinceBoot, location.additions, location.additionSize); this.success(res, options); + HiAnalyticsUtil.reportApiEvent('location.getLocation()', ActionState.SUCCESS); }).catch((err: BusinessError) => { this.error(err, options); + HiAnalyticsUtil.reportApiEvent('location.getLocation()', ActionState.FAIL, err.code); }); }); } @@ -1698,3 +1728,141 @@ class RequestPaymentOptions extends BaseOptions { */ class RequestPaymentResult { } +class HiAnalyticsUtil { + private static appIdentifier?: string; + private static runningMode?: string; + private static processorId: number | undefined = undefined; + /** + * 打点数据上报 + * + * @param time 时间戳 + * @param content 打点参数json字符串 + */ + private static writeEndEvent(time: number, content: string): void { + console.info(`writeEndEvent -> time: ${time} ,content: ${content} `); + let event: hiAppEvent.AppEventInfo = { + domain: 'api_diagnostic', + name: 'api_exec_end', + params: { + api_name: 'ascf', + sdk_name: 'atomicservice_web', + begin_time: time, + // 调用次数 + call_times: 3, + // 调用成功次数 + success_times: 1, + contents: content + }, + eventType: hiAppEvent.EventType.BEHAVIOR + }; + hiAppEvent.write(event); + } + + /** + * 应用的唯一标识 + */ + private static initAnalytics(): void { + if (!HiAnalyticsUtil.processorId) { + HiAnalyticsUtil.addEventProcessor(); + } + if (HiAnalyticsUtil.appIdentifier && HiAnalyticsUtil.runningMode) { + return; + } + let bundleInfo: bundleManager.BundleInfo = + bundleManager.getBundleInfoForSelfSync(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_SIGNATURE_INFO | + bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION); + HiAnalyticsUtil.appIdentifier = bundleInfo.signatureInfo.appIdentifier; + HiAnalyticsUtil.runningMode = bundleInfo.appInfo.appProvisionType; + } + + + /** + * 添加处理者 + */ + private static addEventProcessor(): number { + let processor: hiAppEvent.Processor = { + name: 'ha_app_event', + appId: 'com_huawei_hmos_sdk_ocg', + routeInfo: 'https://cloudbackdown.hwcloudtest.cn', + eventConfigs: [ + { + domain: 'api_diagnostic', + name: 'api_exec_end', + isRealTime: false + }, + { + domain: 'api_diagnostic', + name: 'api_called_stat', + isRealTime: true + }, + { + domain: 'api_diagnostic', + name: 'api_called_stat_cnt', + isRealTime: true + }, + ], + periodReport: 30, // 一般是90 + batchReport: 30 + }; + return hiAppEvent.addProcessor(processor); + } + + /** + * asweb jssdk API数据打点业务处理 + * + * @param apiName 接口名 + * @param actionState 接口调用状态 + * @param errorNumber 错误码 + */ + public static reportApiEvent(apiName: string, actionState: string, errorNumber?: number): void { + try { + if (!apiName || !actionState) { + return; + } + HiAnalyticsUtil.initAnalytics(); + const ascfAction: string = 'APICaller_' + apiName; + const content: string = JSON.stringify({ + appIdentify: HiAnalyticsUtil.appIdentifier, // 获取元服务appid + ascfVersionName: deviceInfo.displayVersion, // rom版本号 + runningMode: HiAnalyticsUtil.runningMode, + ascfAction, + caller: 'ASWeb', + actionState, + errorNumber + }); + HiAnalyticsUtil.writeEndEvent(new Date().getTime(), content); + } catch (err) { + console.error(`reportApiEvent -> reportApiEvent error, message: ${err.message}`); + } + } + /** + * asweb 组件创建打点 + * + * @param apiName 接口名 + * @param actionState 接口调用状态 + * @param errorNumber 错误码 + */ + public static reportComponentEvent(): void { + try { + HiAnalyticsUtil.initAnalytics(); + const content: string = JSON.stringify({ + appIdentify: HiAnalyticsUtil.appIdentifier, // 获取元服务appid + ascfVersionName: deviceInfo.displayVersion, // rom版本号 + runningMode: HiAnalyticsUtil.runningMode, + caller: 'ASWeb', + }); + HiAnalyticsUtil.writeEndEvent(new Date().getTime(), content); + } catch (err) { + console.error(`reportApiEvent -> reportApiEvent error, message: ${err.message}`); + } + } +} + +/** + * 数据打点的接口状态 + */ +enum ActionState { + BEGIN = 'begin', + SUCCESS = 'success', + FAIL = 'fail' +} -- Gitee