完善基础数据采集

Signed-off-by: m30030488 <maluyao3@huawei.com>
This commit is contained in:
m30030488 2022-07-27 16:26:13 +08:00
parent 28fdf4b716
commit 49ab5ae52c
6 changed files with 115 additions and 2 deletions

View File

@ -44,6 +44,7 @@ ohos_shared_library("distributed_device_profile") {
"src/contentsensor/content_collector.cpp",
"src/contentsensor/content_sensor_manager.cpp",
"src/contentsensor/device_info_collector.cpp",
"src/contentsensor/storage_info_collector.cpp",
"src/contentsensor/syscap_info_collector.cpp",
"src/contentsensor/system_info_collector.cpp",
"src/dbstorage/device_profile_storage.cpp",

View File

@ -0,0 +1,33 @@
/*
* Copyright (c) 2022 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_DEVICE_PROFILE_STORAGE_INFO_COLLECTOR_H
#define OHOS_DEVICE_PROFILE_STORAGE_INFO_COLLECTOR_H
#include "content_collector.h"
#include "service_characteristic_profile.h"
namespace OHOS {
namespace DeviceProfile {
class StorageInfoCollector : public ContentCollector {
public:
bool ConvertToProfileData(ServiceCharacteristicProfile& profile) override;
private:
int64_t GetTotalSize();
};
} // namespace DeviceProfile
} // namespace OHOS
#endif // OHOS_DEVICE_PROFILE_STORAGE_INFO_COLLECTOR_H

View File

@ -18,6 +18,7 @@
#include "device_info_collector.h"
#include "device_profile_log.h"
#include "hitrace_meter.h"
#include "storage_info_collector.h"
#include "syscap_info_collector.h"
#include "system_info_collector.h"
@ -48,6 +49,7 @@ bool ContentSensorManager::Collect()
taskList.push_back(std::make_shared<DeviceInfoCollector>());
taskList.push_back(std::make_shared<SystemInfoCollector>());
taskList.push_back(std::make_shared<SyscapInfoCollector>());
taskList.push_back(std::make_shared<StorageInfoCollector>());
HITRACE_METER_NAME(HITRACE_TAG_DEVICE_PROFILE, DP_CONTENT_SENSOR_TRACE);
for (auto& task : taskList) {
ServiceCharacteristicProfile profileData;

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2022 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 "storage_info_collector.h"
#include <sys/statvfs.h>
#include "device_profile_log.h"
#include "nlohmann/json.hpp"
namespace OHOS {
namespace DeviceProfile {
namespace {
const std::string TAG = "StorageInfoCollector";
const char* PATH_DATA = "/data";
const std::string SERVICE_ID = "storage";
const std::string SERVICE_TYPE = "storage";
const std::string CAPACITY = "capacity";
constexpr int64_t KILOBYTE = 1024;
}
bool StorageInfoCollector::ConvertToProfileData(ServiceCharacteristicProfile& profile)
{
profile.SetServiceId(SERVICE_ID);
profile.SetServiceType(SERVICE_TYPE);
nlohmann::json jsonData;
int64_t totalSize = GetTotalSize();
if (totalSize == 0) {
return false;
}
jsonData[CAPACITY] = totalSize;
profile.SetCharacteristicProfileJson(jsonData.dump());
return true;
}
int64_t StorageInfoCollector::GetTotalSize()
{
int64_t totalSize = 0;
struct statvfs diskInfo;
int ret = statvfs(PATH_DATA, &diskInfo);
if (ret != 0) {
HILOGE("GetTotalSize failed");
return totalSize;
}
totalSize = (int64_t)diskInfo.f_bsize * (int64_t)diskInfo.f_blocks / KILOBYTE;
return totalSize;
}
} // namespace DeviceProfile
} // OHOS

View File

@ -76,7 +76,7 @@ ohos_unittest("profile_authority_test") {
subsystem_name = "deviceprofile"
}
ohos_unittest("content_sersor_test") {
ohos_unittest("content_sensor_test") {
module_out_path = module_output_path
sources = [ "unittest/content_sensor_test.cpp" ]
@ -92,7 +92,7 @@ group("unittest") {
deps = [
# ":event_subscribe_test",
# ":profile_authority_test",
":content_sersor_test",
":content_sensor_test",
":profile_crud_test",
]
}

View File

@ -15,6 +15,8 @@
#include "gtest/gtest.h"
#include <sys/statvfs.h>
#include "utils.h"
#define private public
@ -123,5 +125,18 @@ HWTEST_F(ContentSensorTest, GetDeviceSerial_001, TestSize.Level2)
auto result = devInfo.GetDeviceSerial();
EXPECT_TRUE(result != "");
}
/**
* @tc.name: GetTotalSize_001
* @tc.desc: get total size
* @tc.type: FUNC
*/
HWTEST_F(ContentSensorTest, GetTotalSize_001, TestSize.Level2)
{
const char* PATH_DATA = "/data";
struct statvfs diskInfo;
int ret = statvfs(PATH_DATA, &diskInfo);
EXPECT_TRUE(ret == 0);
}
}
}