添加大数据打点能力

Signed-off-by: wangfeifan <wangfeifan2@huawei.com>
This commit is contained in:
wangfeifan 2023-06-17 10:09:31 +08:00
parent d7c0e99786
commit 0aadd86afe
6 changed files with 175 additions and 3 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.idea
.vscode/

View File

@ -25,6 +25,7 @@ config("print_service_config") {
ohos_shared_library("print_service") {
sources = [
"src/hisys_event_util.cpp",
"src/print_bms_death_recipient.cpp",
"src/print_bms_helper.cpp",
"src/print_callback_proxy.cpp",

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef HISYS_EVENT_UTIL_H
#define HISYS_EVENT_UTIL_H
#include <string>
namespace OHOS::Print {
class HisysEventUtil {
public:
static void reportPrintSuccess(const std::string &msg);
static void faultPrint(std::string scene, std::string msg);
private:
static constexpr char HW_PRINT_SPOOLER[] = "HW_PRINT_SPOOLER";
static const int PRINT_SUCCESS = 1003;
static const int FAULT_PRINT = 3002;
static void reportBehaviorEvent(std::string name, int behaviorCode, const std::string &msg);
static void reportFaultEvent(std::string name, int faultCode, std::string scene, std::string msg);
};
} // namespace OHOS::Print
#endif // HISYS_EVENT_UTIL_H

View File

@ -96,6 +96,9 @@ private:
void DestroyExtension(const std::string &printerId);
void NotifyAppJobQueueChanged(const std::string &applyResult);
std::shared_ptr<PrinterInfo> getPrinterInfo(const std::string printerId);
bool isEprint(const std::string &printerId);
void ReportHisysEvent(const std::shared_ptr<PrintJob> &jobInfo, const std::string &printerId, uint32_t subState);
void ReportCompletedPrint(const std::string & printerId);
#ifndef TDD_ENABLE
private:
@ -105,6 +108,8 @@ private:
static std::mutex instanceLock_;
static sptr<PrintServiceAbility> instance_;
static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
static std::chrono::time_point<std::chrono::high_resolution_clock> startPrintTime_;
static std::string ingressPackage;
std::recursive_mutex apiMutex_;
std::map<std::string, sptr<IPrintCallback>> registeredListeners_;

View File

@ -0,0 +1,57 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hisysevent.h"
#include "print_log.h"
#include "hisys_event_util.h"
namespace OHOS::Print {
using namespace OHOS::HiviewDFX;
void HisysEventUtil::reportBehaviorEvent(std::string name, int behaviorCode, const std::string &msg)
{
HiSysEventWrite(
HW_PRINT_SPOOLER,
name,
HiSysEvent::EventType::BEHAVIOR,
"PACKAGE_NAME", "com.huawei.hmos.hwprintspooler",
"PROCESS_NAME", "hwPrintSpooler",
"BEHAVIOR_CODE", behaviorCode,
"MSG", msg);
}
void HisysEventUtil::reportFaultEvent(std::string name, int faultCode, std::string scene, std::string msg)
{
HiSysEventWrite(
HW_PRINT_SPOOLER,
name,
HiSysEvent::EventType::FAULT,
"PACKAGE_NAME", "com.huawei.hmos.hwprintspooler",
"PROCESS_NAME", "hwPrintSpooler",
"FAULT_CODE", faultCode,
"SCENE", scene,
"MSG", msg);
}
void HisysEventUtil::reportPrintSuccess(const std::string &msg)
{
reportBehaviorEvent("PRINT_SUCCESS", PRINT_SUCCESS, msg);
}
void HisysEventUtil::faultPrint(std::string scene, std::string msg)
{
reportFaultEvent("PRINT_EXCEPTION", FAULT_PRINT, scene, msg);
}
}

View File

@ -40,11 +40,14 @@
#include "common_event_manager.h"
#include "common_event_support.h"
#include "print_security_guard_manager.h"
#include "hisys_event_util.h"
#include "nlohmann/json.hpp"
namespace OHOS::Print {
using namespace std;
using namespace OHOS::HiviewDFX;
using namespace Security::AccessToken;
using json = nlohmann::json;
const uint32_t MAX_RETRY_TIMES = 10;
const uint32_t START_ABILITY_INTERVAL = 6;
@ -81,6 +84,8 @@ REGISTER_SYSTEM_ABILITY_BY_ID(PrintServiceAbility, PRINT_SERVICE_ID, true);
std::mutex PrintServiceAbility::instanceLock_;
sptr<PrintServiceAbility> PrintServiceAbility::instance_;
std::shared_ptr<AppExecFwk::EventHandler> PrintServiceAbility::serviceHandler_;
std::chrono::time_point<std::chrono::high_resolution_clock> PrintServiceAbility::startPrintTime_;
std::string PrintServiceAbility::ingressPackage;
PrintServiceAbility::PrintServiceAbility(int32_t systemAbilityId, bool runOnCreate)
: SystemAbility(systemAbilityId, runOnCreate), state_(ServiceRunningState::STATE_NOT_START),
@ -227,6 +232,7 @@ int32_t PrintServiceAbility::StartPrint(const std::vector<std::string> &fileList
BuildFDParam(fdList, want);
int32_t callerTokenId = static_cast<int32_t>(IPCSkeleton::GetCallingTokenID());
std::string callerPkg = DelayedSingleton<PrintBMSHelper>::GetInstance()->QueryCallerBundleName();
ingressPackage = callerPkg;
int32_t callerUid = IPCSkeleton::GetCallingUid();
int32_t callerPid = IPCSkeleton::GetCallingPid();
want.SetParam(AAFwk::Want::PARAM_RESV_CALLER_TOKEN, callerTokenId);
@ -504,6 +510,7 @@ int32_t PrintServiceAbility::QueryPrintJobById(std::string &printJobId, PrintJob
int32_t PrintServiceAbility::StartPrintJob(const PrintJob &jobInfo)
{
startPrintTime_ = std::chrono::high_resolution_clock::now();
ManualStart();
if (!CheckPermission(PERMISSION_NAME_PRINT_JOB)) {
PRINT_HILOGE("no permission to access print service");
@ -802,15 +809,16 @@ int32_t PrintServiceAbility::UpdatePrintJobState(const std::string &jobId, uint3
SendPrintJobEvent(*jobIt->second);
auto printerId = jobIt->second->GetPrinterId();
if (state == PRINT_JOB_BLOCKED) {
ReportHisysEvent(jobIt->second, printerId, subState);
}
if (state == PRINT_JOB_COMPLETED) {
if (jobInQueue) {
printerJobMap_[printerId].erase(jobId);
queuedJobList_.erase(jobIt);
}
if (printerJobMap_[printerId].empty()) {
NotifyAppJobQueueChanged(QUEUE_JOB_LIST_COMPLETED);
PRINT_HILOGD("no print job exists, destroy extension");
DestroyExtension(printerId);
ReportCompletedPrint(printerId);
}
SendQueuePrintJob(printerId);
}
@ -819,6 +827,55 @@ int32_t PrintServiceAbility::UpdatePrintJobState(const std::string &jobId, uint3
return E_PRINT_NONE;
}
void PrintServiceAbility::ReportCompletedPrint(const std::string &printerId)
{
NotifyAppJobQueueChanged(QUEUE_JOB_LIST_COMPLETED);
PRINT_HILOGD("no print job exists, destroy extension");
DestroyExtension(printerId);
json msg;
auto endPrintTime = std::chrono::high_resolution_clock::now();
auto printTime = std::chrono::duration_cast<std::chrono::milliseconds>(endPrintTime - startPrintTime_);
msg["PRINT_TIME"] = printTime.count();
msg["INGRESS_PACKAGE"] = ingressPackage;
msg["STATUS"] = 0;
HisysEventUtil::reportPrintSuccess(msg.dump());
}
void PrintServiceAbility::ReportHisysEvent(const std::shared_ptr<PrintJob> &jobInfo,
const std::string &printerId, uint32_t subState)
{
json msg;
auto endPrintTime = std::chrono::high_resolution_clock::now();
auto printTime = std::chrono::duration_cast<std::chrono::milliseconds>(endPrintTime - startPrintTime_);
msg["PRINT_TIME"] = printTime.count();
msg["INGRESS_PACKAGE"] = ingressPackage;
if (isEprint(printerId)) {
msg["PRINT_TYPE"] = 1;
} else {
msg["PRINT_TYPE"] = 0;
}
auto printInfo = printerInfoList_.find(printerId);
std::vector<uint32_t> fdList;
jobInfo->GetFdList(fdList);
msg["FILE_NUM"] = fdList.size();
msg["PAGE_NUM"] = fdList.size();
if (printInfo == printerInfoList_.end()) {
msg["MODEL"] = "";
} else {
msg["MODEL"] = printInfo->second->GetPrinterName();
}
msg["COPIES_SETTING"] = jobInfo->GetCopyNumber();
std::string option = jobInfo->GetOption();
PRINT_HILOGI("test option:%{public}s", option.c_str());
json optionJson = json::parse(option);
PRINT_HILOGI("test optionJson: %{public}s", optionJson.dump().c_str());
PRINT_HILOGI("test jobDescription: %{public}s", optionJson["jobDescription"].get<std::string>().c_str());
msg["JOB_DESCRIPTION"] = optionJson["jobDescription"].get<std::string>();
msg["PRINT_STYLE_SETTING"] = jobInfo->GetDuplexMode();
msg["FAIL_REASON_CODE"] = subState;
HisysEventUtil::faultPrint("PRINT_JOB_BLOCKED", msg.dump());
}
void PrintServiceAbility::NotifyAppJobQueueChanged(const std::string &applyResult)
{
PRINT_HILOGD("NotifyAppJobQueueChanged started. %{public}s ", applyResult.c_str());
@ -830,6 +887,18 @@ void PrintServiceAbility::NotifyAppJobQueueChanged(const std::string &applyResul
PRINT_HILOGD("NotifyAppJobQueueChanged end.");
}
bool PrintServiceAbility::isEprint(const std::string &printerId)
{
if (printerId.length() <= 0) {
return false;
}
std::string ePrintID = "ePrintID";
if (printerId.length() < ePrintID.length()) {
return false;
}
return std::equal(ePrintID.rbegin(), ePrintID.rend(), printerId.rbegin());
}
void PrintServiceAbility::DestroyExtension(const std::string &printerId)
{
std::lock_guard<std::recursive_mutex> lock(apiMutex_);