From 94d4555b4336b9d73a7a6ee370a6d71ad227a340 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E5=81=A5?= Date: Sun, 27 Apr 2025 12:01:33 +0800 Subject: [PATCH] =?UTF-8?q?data=E5=88=86=E5=8C=BA=E5=86=99=E5=85=A5?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E6=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 孙健 --- bundle.json | 4 + frameworks/fontmgr/fontmgr.gni | 1 + frameworks/fontmgr/hisysevent.yaml | 23 +++++ .../fontmgr/include/hisysevent_adapter.h | 40 +++++++++ frameworks/fontmgr/src/font_manager.cpp | 9 +- frameworks/fontmgr/src/hisysevent_adapter.cpp | 85 ++++++++++++++++++ frameworks/fontmgr/test/BUILD.gn | 36 +++++++- .../include/hisysevent_adapter_test.h | 41 +++++++++ .../unittest/src/hisysevent_adapter_test.cpp | 89 +++++++++++++++++++ service/BUILD.gn | 1 + 10 files changed, 325 insertions(+), 4 deletions(-) create mode 100644 frameworks/fontmgr/hisysevent.yaml create mode 100644 frameworks/fontmgr/include/hisysevent_adapter.h create mode 100644 frameworks/fontmgr/src/hisysevent_adapter.cpp create mode 100644 frameworks/fontmgr/test/unittest/include/hisysevent_adapter_test.h create mode 100644 frameworks/fontmgr/test/unittest/src/hisysevent_adapter_test.cpp diff --git a/bundle.json b/bundle.json index 662e9d1..bfe2cd9 100644 --- a/bundle.json +++ b/bundle.json @@ -44,6 +44,9 @@ ], "rom": "", "ram": "", + "hisysevent_config": [ + "//base/global/font_manager/frameworks/fontmgr/hisysevent.yaml" + ], "deps": { "components": [ "ability_base", @@ -54,6 +57,7 @@ "cJSON", "eventhandler", "hilog", + "hisysevent", "hitrace", "ipc", "napi", diff --git a/frameworks/fontmgr/fontmgr.gni b/frameworks/fontmgr/fontmgr.gni index 25194f6..3bad24b 100644 --- a/frameworks/fontmgr/fontmgr.gni +++ b/frameworks/fontmgr/fontmgr.gni @@ -24,6 +24,7 @@ fontmgr_src = [ "$root_path/src/font_config.cpp", "$root_path/src/font_manager.cpp", "$root_path/src/font_event_publish.cpp", + "$root_path/src/hisysevent_adapter.cpp" ] fontmgr_external_deps = [ diff --git a/frameworks/fontmgr/hisysevent.yaml b/frameworks/fontmgr/hisysevent.yaml new file mode 100644 index 0000000..b1f9ac9 --- /dev/null +++ b/frameworks/fontmgr/hisysevent.yaml @@ -0,0 +1,23 @@ +# Copyright (c) 2025 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. + +dommain: FILEMANAGEMENT + +# user data size +USER_DATA_SIZE: + __BASE: {type: STATISTIC, level: CRITICAL, desc: user data size} + COMPONENT_NAME: {type: STRING, desc: component name} + PARTITION_NAME: {type: STRING, desc: partition name} + REMAIN_PARTITION_SIZE: {type: UINT64, desc: remain partition size} + FILE_OR_FOLDER_PATH: {type: STRING, desc: file or folder path} + FILE_OR_FOLDER_SIZE: {type: STRING, desc: file or folder size} \ No newline at end of file diff --git a/frameworks/fontmgr/include/hisysevent_adapter.h b/frameworks/fontmgr/include/hisysevent_adapter.h new file mode 100644 index 0000000..2bfd202 --- /dev/null +++ b/frameworks/fontmgr/include/hisysevent_adapter.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2025 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 GLOBAL_FONT_MANAGER_HISYSEVENT_ADAPTER_H +#define GLOBAL_FONT_MANAGER_HISYSEVENT_ADAPTER_H + +#include +#include + +#include "singleton.h" + +namespace OHOS { +namespace Global { +namespace FontManager { +class HisyseventAdapter : public DelayedSingleton { + DECLARE_DELAYED_SINGLETON(HisyseventAdapter); + +public: + int CollectUserDataSize(); + uint64_t GetDataPartitionRemainSize(); + std::vector GetFileOrFolderPath(); + std::vector GetFileOrFolderSize(); +}; +} // namespace FontManager +} // namespace Global +} // namespace OHOS + +#endif // GLOBAL_FONT_MANAGER_HISYSEVENT_ADAPTER_H \ No newline at end of file diff --git a/frameworks/fontmgr/src/font_manager.cpp b/frameworks/fontmgr/src/font_manager.cpp index b86f9e1..51034cf 100644 --- a/frameworks/fontmgr/src/font_manager.cpp +++ b/frameworks/fontmgr/src/font_manager.cpp @@ -19,6 +19,7 @@ #include "font_event_publish.h" #include "font_config.h" #include "file_utils.h" +#include "hisysevent_adapter.h" #include "SkFontMgr.h" namespace OHOS { @@ -63,7 +64,7 @@ int32_t FontManager::InstallFont(const int32_t &fd) if (!(CheckInstallPath() && CheckFontConfigPath())) { return ERR_FILE_NOT_EXISTS; } - + std::vector fullNameVector = GetFontFullName(fd); if (fullNameVector.size() == 0) { FONT_LOGE("get fontFullName failed, font file verified failed"); @@ -91,6 +92,7 @@ int32_t FontManager::InstallFont(const int32_t &fd) FONT_LOGE("copy file %{public}s error", sourcePath.c_str()); return ERR_COPY_FAIL; } + HisyseventAdapter::GetInstance()->CollectUserDataSize(); if (fontConfig.InsertFontRecord(destPath, fullNameVector)) { FontEventPublish::PublishFontUpdate(FontEventType::INSTALL, GetFormatFullName(fullNameVector)); return SUCCESS; @@ -118,13 +120,13 @@ std::vector FontManager::GetFontFullName(const int32_t &fd) FONT_LOGE("fontMgr is null"); return fullNameVector; } - + int ret = fontMgr->GetFontFullName(fd, fullname); if (ret != SUCCESS) { FONT_LOGE("GetFontFullName failed, err:%{public}d", ret); return fullNameVector; } - + for (const auto &name : fullname) { std::string fullnameStr; fullnameStr.assign((char *)name.strData.get(), name.strLen); @@ -172,6 +174,7 @@ int32_t FontManager::UninstallFont(const std::string &fontFullName) FONT_LOGE("Can't find fontFullName = %{public}s", fontFullName.c_str()); return ERR_UNINSTALL_FILE_NOT_EXISTS; } + HisyseventAdapter::GetInstance()->CollectUserDataSize(); if (!FileUtils::RemoveFile(path)) { return ERR_UNINSTALL_REMOVE_FAIL; } diff --git a/frameworks/fontmgr/src/hisysevent_adapter.cpp b/frameworks/fontmgr/src/hisysevent_adapter.cpp new file mode 100644 index 0000000..20ed4e8 --- /dev/null +++ b/frameworks/fontmgr/src/hisysevent_adapter.cpp @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2025 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 GLOBAL_FONT_MANAGER_HISYSEVENT_ADAPTER_CPP +#define GLOBAL_FONT_MANAGER_HISYSEVENT_ADAPTER_CPP + +#include "hisysevent_adapter.h" + +#include +#include + +#include "directory_ex.h" +#include "font_hilog.h" +#include "hisysevent.h" + +namespace OHOS { +namespace Global { +namespace FontManager { +using HiSysEventNameSpace = OHOS::HiviewDFX::HiSysEvent; + +static const std::string DATA_PARTITION_NAME = "/data"; +static const std::string INSTALL_PATH = "/data/service/el1/public/for-all-app/fonts/"; +static const std::string COMPONENT_NAME = "font_manager"; + +HisyseventAdapter::HisyseventAdapter() {} +HisyseventAdapter::~HisyseventAdapter() {} + +int HisyseventAdapter::CollectUserDataSize() +{ + std::string componentName = COMPONENT_NAME; + std::string partitionName = DATA_PARTITION_NAME; + std::uint64_t remainPartitionSize = this->GetDataPartitionRemainSize(); + std::vector fileOrFolderPath = this->GetFileOrFolderPath(); + std::vector fileOrFolderSize = this->GetFileOrFolderSize(); + int ret = HiSysEventWrite(HiSysEventNameSpace::Domain::FILEMANAGEMENT, "USER_DATA_SIZE", + HiSysEventNameSpace::EventType::STATISTIC, "COMPONENT_NAME", componentName, "PARTITION_NAME", partitionName, + "REMAIN_PARTITION_SIZE", remainPartitionSize, "FILE_OR_FOLDER_PATH", fileOrFolderPath, "FILE_OR_FOLDER_SIZE", + fileOrFolderSize); + return ret; +} + +std::uint64_t HisyseventAdapter::GetDataPartitionRemainSize() +{ + std::string partitionName = DATA_PARTITION_NAME; + struct statfs stat; + if (statfs(partitionName.c_str(), &stat) != 0) { + return -1; + } + std::uint64_t blockSize = stat.f_bsize; + std::uint64_t freeSize = stat.f_bfree * blockSize; + constexpr double units = 1024.0; + std::uint64_t freeSizeMb = freeSize / (units * units); + return freeSizeMb; +} + +std::vector HisyseventAdapter::GetFileOrFolderPath() +{ + std::vector vec; + vec.push_back(INSTALL_PATH); + return vec; +} + +std::vector HisyseventAdapter::GetFileOrFolderSize() +{ + std::vector vec; + vec.push_back(OHOS::GetFolderSize(INSTALL_PATH)); + return vec; +} +} // namespace FontManager +} // namespace Global +} // namespace OHOS + +#endif // GLOBAL_FONT_MANAGER_HISYSEVENT_ADAPTER_CPP \ No newline at end of file diff --git a/frameworks/fontmgr/test/BUILD.gn b/frameworks/fontmgr/test/BUILD.gn index 4735d43..4ba6393 100644 --- a/frameworks/fontmgr/test/BUILD.gn +++ b/frameworks/fontmgr/test/BUILD.gn @@ -59,8 +59,42 @@ ohos_unittest("font_manager_module_test") { include_dirs += fontmgr_include external_deps = [ + "c_utils:utils", "googletest:gtest", - "googletest:gtest_main" + "googletest:gtest_main", + "hisysevent:libhisysevent" + ] + + external_deps += fontmgr_external_deps + + deps = [ + "../../../service:font_service_ability", + ] + + defines = [ "SUPPORT_GRAPHICS" ] +} + +ohos_unittest("hisysevent_adapter_test") { + module_out_path = "font_manager/font_manager" + + resource_config_file = "ohos_test.xml" + + sources = [ + "unittest/src/hisysevent_adapter_test.cpp", + ] + + sources += fontmgr_src + include_dirs = [ + "./unittest/include", + ] + + include_dirs += fontmgr_include + + external_deps = [ + "c_utils:utils", + "googletest:gtest", + "googletest:gtest_main", + "hisysevent:libhisysevent" ] external_deps += fontmgr_external_deps diff --git a/frameworks/fontmgr/test/unittest/include/hisysevent_adapter_test.h b/frameworks/fontmgr/test/unittest/include/hisysevent_adapter_test.h new file mode 100644 index 0000000..eaf323d --- /dev/null +++ b/frameworks/fontmgr/test/unittest/include/hisysevent_adapter_test.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2025 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 OHOS_GLOBAL_FONT_MANAGER_HISYSEVENT_ADAPTER_TEST_H +#define OHOS_GLOBAL_FONT_MANAGER_HISYSEVENT_ADAPTER_TEST_H + +#include + +namespace OHOS { +namespace Global { +namespace FontManager { +class HisyseventAdapterTest : public testing::Test { + public: + HisyseventAdapterTest(){}; + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +int CollectUserDataSize(void); +int GetDataPartitionRemainSize(void); +int GetFileOrFolderPath(void); +int GetFileOrFolderSize(void); + +} // namespace FontManager +} // namespace Global +} // namespace OHOS + +#endif // OHOS_GLOBAL_FONT_MANAGER_HISYSEVENT_ADAPTER_TEST_H \ No newline at end of file diff --git a/frameworks/fontmgr/test/unittest/src/hisysevent_adapter_test.cpp b/frameworks/fontmgr/test/unittest/src/hisysevent_adapter_test.cpp new file mode 100644 index 0000000..e7f8dc7 --- /dev/null +++ b/frameworks/fontmgr/test/unittest/src/hisysevent_adapter_test.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2025 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 OHOS_GLOBAL_FONT_MANAGER_HISYSEVENT_ADAPTER_TEST_CPP +#define OHOS_GLOBAL_FONT_MANAGER_HISYSEVENT_ADAPTER_TEST_CPP + +#include "hisysevent_adapter_test.h" + +#include + +#include "hisysevent_adapter.h" +#include "singleton.h" + +using testing::ext::TestSize; + +namespace OHOS { +namespace Global { +namespace FontManager { +static const std::string INSTALL_PATH = "/data/service/el1/public/for-all-app/fonts/"; +void HisyseventAdapterTest::SetUpTestCase(void) {} +void HisyseventAdapterTest::TearDownTestCase(void) {} +void HisyseventAdapterTest::SetUp(void) {} +void HisyseventAdapterTest::TearDown(void) {} + +/* * + * @tc.name: CollectUserDataSize + * @tc.desc: Normal + * @tc.type FUNC + */ +HWTEST_F(HisyseventAdapterTest, CollectUserDataSize, TestSize.Level1) +{ + std::shared_ptr adapter = HisyseventAdapter::GetInstance(); + EXPECT_EQ(adapter->CollectUserDataSize(), 0); +} + +/* * + * @tc.name: GetDataPartitionRemainSize + * @tc.desc: Test GetDataPartitionRemainSize + * @tc.type FUNC + */ +HWTEST_F(HisyseventAdapterTest, GetDataPartitionRemainSize, TestSize.Level1) +{ + std::shared_ptr adapter = HisyseventAdapter::GetInstance(); + std::uint64_t size = adapter->GetDataPartitionRemainSize(); + EXPECT_GE(size, 0); +} + +/* * + * @tc.name: GetFileOrFolderPath + * @tc.desc: Test GetFileOrFolderPath + * @tc.type FUNC + */ +HWTEST_F(HisyseventAdapterTest, GetFileOrFolderPath, TestSize.Level1) +{ + std::shared_ptr adapter = HisyseventAdapter::GetInstance(); + std::vector result = adapter->GetFileOrFolderPath(); + EXPECT_EQ(result.size(), 1); + EXPECT_EQ(result[0], INSTALL_PATH); +} + +/* * + * @tc.name: GetFileOrFolderSize + * @tc.desc: Test GetFileOrFolderSize + * @tc.type FUNC + */ +HWTEST_F(HisyseventAdapterTest, GetFileOrFolderSize, TestSize.Level1) +{ + std::shared_ptr adapter = HisyseventAdapter::GetInstance(); + std::vector result = adapter->GetFileOrFolderSize(); + EXPECT_EQ(result.size(), 1); + EXPECT_GE(result[0], 0); +} +} // namespace FontManager +} // namespace Global +} // namespace OHOS + +#endif \ No newline at end of file diff --git a/service/BUILD.gn b/service/BUILD.gn index 09e8d03..aecdae8 100644 --- a/service/BUILD.gn +++ b/service/BUILD.gn @@ -100,6 +100,7 @@ ohos_shared_library("font_manager_server") { "c_utils:utils", "eventhandler:libeventhandler", "hilog:libhilog", + "hisysevent:libhisysevent", "ipc:ipc_core", "safwk:system_ability_fwk", "samgr:samgr_proxy", -- Gitee