!1464 storage_service:存储空间不足高强度提醒

Merge pull request !1464 from lianwei/master
This commit is contained in:
openharmony_ci 2024-11-11 09:19:50 +00:00 committed by Gitee
commit c41617922e
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 39 additions and 7 deletions

View File

@ -26,6 +26,7 @@
namespace OHOS {
namespace StorageManager {
const int SMART_EVENT_INTERVAL = 24; // 24h
const int SMART_EVENT_INTERVAL_HIGH_FREQ = 5; // 5m
class StorageMonitorService : public NoCopyable {
DECLARE_DELAYED_SINGLETON(StorageMonitorService);
@ -38,7 +39,8 @@ private:
void CheckAndCleanBundleCache();
int64_t GetLowerThreshold(int64_t totalSize);
void CheckAndEventNotify(int64_t freeSize, int64_t totalSize);
void SendSmartNotificationEvent(const std::string &faultId);
void SendSmartNotificationEvent(const std::string &faultDesc, const std::string &faultSuggest, bool isHighFreq);
void EventNotifyHighFreqHandler();
std::mutex eventMutex_;
std::thread eventThread_;
@ -47,6 +49,9 @@ private:
std::chrono::steady_clock::time_point lastNotificationTime_ =
std::chrono::time_point_cast<std::chrono::steady_clock::duration>(
std::chrono::steady_clock::now()) - std::chrono::hours(SMART_EVENT_INTERVAL);
std::chrono::steady_clock::time_point lastNotificationTimeHighFreq_ =
std::chrono::time_point_cast<std::chrono::steady_clock::duration>(
std::chrono::steady_clock::now()) - std::chrono::minutes(SMART_EVENT_INTERVAL_HIGH_FREQ);
};
} // StorageManager
} // OHOS

View File

@ -41,14 +41,18 @@ constexpr int32_t WAIT_THREAD_TIMEOUT_MS = 5;
constexpr int32_t DEFAULT_CHECK_INTERVAL = 60 * 1000; // 60s
constexpr int32_t STORAGE_THRESHOLD_PERCENTAGE = 5; // 5%
constexpr int64_t STORAGE_THRESHOLD_MAX_BYTES = 500 * 1024 * 1024; // 500M
constexpr int64_t STORAGE_THRESHOLD_100M = 100 * 1024 * 1024; // 100M
constexpr int64_t STORAGE_THRESHOLD_1G = 1000 * 1024 * 1024; // 1G
constexpr int32_t STORAGE_LEFT_SIZE_THRESHOLD = 10; // 10%
constexpr int32_t SEND_EVENT_INTERVAL = 24; // 24H
constexpr int32_t SEND_EVENT_INTERVAL_HIGH_FREQ = 5; // 5m
const std::string PUBLISH_SYSTEM_COMMON_EVENT = "ohos.permission.PUBLISH_SYSTEM_COMMON_EVENT";
const std::string SMART_BUNDLE_NAME = "com.ohos.hmos.hiviewcare";
const std::string SMART_ACTION = "hicare.event.SMART_NOTIFICATION";
const std::string FAULT_ID_ONE = "845010021";
const std::string FAULT_ID_TWO = "845010022";
const std::string FAULT_ID_THREE = "845010023";
const std::string FAULT_SUGGEST_THREE = "545010023";
StorageMonitorService::StorageMonitorService() {}
@ -160,21 +164,27 @@ int64_t StorageMonitorService::GetLowerThreshold(int64_t totalSize)
void StorageMonitorService::CheckAndEventNotify(int64_t freeSize, int64_t totalSize)
{
LOGI("StorageMonitorService, start CheckAndEventNotify.");
if (freeSize < STORAGE_THRESHOLD_100M) {
EventNotifyHighFreqHandler();
return;
}
auto currentTime = std::chrono::steady_clock::now();
int32_t duration = static_cast<int32_t>(std::chrono::duration_cast<std::chrono::hours>
(currentTime - lastNotificationTime_).count());
LOGI("StorageMonitorService, duration is %{public}d", duration);
if (duration >= SEND_EVENT_INTERVAL) {
if (freeSize >= STORAGE_THRESHOLD_1G) {
SendSmartNotificationEvent(FAULT_ID_ONE);
SendSmartNotificationEvent(FAULT_ID_ONE, FAULT_ID_ONE, false);
} else {
SendSmartNotificationEvent(FAULT_ID_TWO);
SendSmartNotificationEvent(FAULT_ID_TWO, FAULT_ID_TWO, false);
}
lastNotificationTime_ = currentTime;
}
}
void StorageMonitorService::SendSmartNotificationEvent(const std::string &faultId)
void StorageMonitorService::SendSmartNotificationEvent(const std::string &faultDesc,
const std::string &faultSuggest,
bool isHighFreq)
{
LOGI("StorageMonitorService, start SendSmartNotificationEvent.");
EventFwk::CommonEventPublishInfo publishInfo;
@ -192,9 +202,14 @@ void StorageMonitorService::SendSmartNotificationEvent(const std::string &faultI
eventData.SetWant(want);
cJSON *root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "faultDescription", faultId.c_str());
cJSON_AddStringToObject(root, "faultSuggestion", faultId.c_str());
cJSON_AddStringToObject(root, "faultDescription", faultDesc.c_str());
cJSON_AddStringToObject(root, "faultSuggestion", faultSuggest.c_str());
if (isHighFreq) {
cJSON *faultSuggestionParam = cJSON_CreateString("100M");
cJSON *faultSuggestionArray = cJSON_CreateArray();
cJSON_AddItemToArray(faultSuggestionArray, faultSuggestionParam);
cJSON_AddItemToObject(root, "faultSuggestionParams", faultSuggestionArray);
}
char *json_string = cJSON_Print(root);
std::string eventDataStr(json_string);
eventDataStr.erase(remove(eventDataStr.begin(), eventDataStr.end(), '\n'), eventDataStr.end());
@ -205,5 +220,17 @@ void StorageMonitorService::SendSmartNotificationEvent(const std::string &faultI
cJSON_Delete(root);
EventFwk::CommonEventManager::PublishCommonEvent(eventData, publishInfo, nullptr);
}
void StorageMonitorService::EventNotifyHighFreqHandler()
{
auto currentTime = std::chrono::steady_clock::now();
int32_t duration = static_cast<int32_t>(std::chrono::duration_cast<std::chrono::minutes>
(currentTime - lastNotificationTimeHighFreq_).count());
LOGI("StorageMonitorService high frequency, duration is %{public}d", duration);
if (duration >= SEND_EVENT_INTERVAL_HIGH_FREQ) {
SendSmartNotificationEvent(FAULT_ID_THREE, FAULT_SUGGEST_THREE, true);
lastNotificationTimeHighFreq_ = currentTime;
}
}
} // StorageManager
} // OHOS