1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "json_parser.h"
16
17 #include "usage_event_common.h"
18
19 namespace OHOS {
20 namespace HiviewDFX {
21 namespace {
22 const std::vector<std::string> PLUGIN_STATS_FIELDS = {
23 PluginStatsEventSpace::KEY_OF_PLUGIN_NAME,
24 PluginStatsEventSpace::KEY_OF_AVG_TIME,
25 PluginStatsEventSpace::KEY_OF_TOP_K_TIME,
26 PluginStatsEventSpace::KEY_OF_TOP_K_EVENT,
27 PluginStatsEventSpace::KEY_OF_TOTAL,
28 PluginStatsEventSpace::KEY_OF_PROC_NAME,
29 PluginStatsEventSpace::KEY_OF_PROC_TIME,
30 PluginStatsEventSpace::KEY_OF_TOTAL_TIME
31 };
32 const std::vector<std::string> SYS_USAGE_FIELDS = {
33 SysUsageEventSpace::KEY_OF_START,
34 SysUsageEventSpace::KEY_OF_END,
35 SysUsageEventSpace::KEY_OF_POWER,
36 SysUsageEventSpace::KEY_OF_SCREEN,
37 SysUsageEventSpace::KEY_OF_RUNNING
38 };
39 }
ParseJsonString(Json::Value & eventJson,const std::string & jsonStr)40 bool JsonParser::ParseJsonString(Json::Value& eventJson, const std::string& jsonStr)
41 {
42 Json::CharReaderBuilder jsonRBuilder;
43 Json::CharReaderBuilder::strictMode(&jsonRBuilder.settings_);
44 std::unique_ptr<Json::CharReader> const reader(jsonRBuilder.newCharReader());
45 JSONCPP_STRING err;
46 return reader->parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), &eventJson, &err);
47 }
48
CheckJsonValue(const Json::Value & eventJson,const std::vector<std::string> & fields)49 bool JsonParser::CheckJsonValue(const Json::Value& eventJson, const std::vector<std::string>& fields)
50 {
51 for (auto field : fields) {
52 if (!eventJson.isMember(field)) {
53 return false;
54 }
55 }
56 return true;
57 }
58
ParseUInt32(const Json::Value & value)59 uint32_t JsonParser::ParseUInt32(const Json::Value& value)
60 {
61 return value.isUInt() ? value.asUInt() : 0;
62 }
63
ParseUInt64(const Json::Value & value)64 uint64_t JsonParser::ParseUInt64(const Json::Value& value)
65 {
66 return value.isUInt64() ? value.asUInt64() : 0;
67 }
68
ParseString(const Json::Value & value)69 std::string JsonParser::ParseString(const Json::Value& value)
70 {
71 return value.isString() ? value.asString() : "";
72 }
73
ParseUInt32Vec(const Json::Value & root,std::vector<uint32_t> & vec)74 void JsonParser::ParseUInt32Vec(const Json::Value& root, std::vector<uint32_t>& vec)
75 {
76 if (!root.isArray()) {
77 return;
78 }
79 for (size_t i = 0; i < root.size(); ++i) {
80 vec.push_back(ParseUInt32(root[static_cast<int>(i)]));
81 }
82 }
83
ParseStringVec(const Json::Value & root,std::vector<std::string> & vec)84 void JsonParser::ParseStringVec(const Json::Value& root, std::vector<std::string>& vec)
85 {
86 if (!root.isArray()) {
87 return;
88 }
89 for (size_t i = 0; i < root.size(); ++i) {
90 vec.push_back(ParseString(root[static_cast<int>(i)]));
91 }
92 }
93
ParsePluginStatsEvent(std::shared_ptr<LoggerEvent> & event,const std::string & jsonStr)94 bool JsonParser::ParsePluginStatsEvent(std::shared_ptr<LoggerEvent>& event, const std::string& jsonStr)
95 {
96 using namespace PluginStatsEventSpace;
97 Json::Value root;
98 if (!ParseJsonString(root, jsonStr) || !CheckJsonValue(root, PLUGIN_STATS_FIELDS)) {
99 return false;
100 }
101
102 std::vector<uint32_t> topKTimes;
103 ParseUInt32Vec(root[KEY_OF_TOP_K_TIME], topKTimes);
104 event->Update(KEY_OF_TOP_K_TIME, topKTimes);
105
106 std::vector<std::string> topKEvents;
107 ParseStringVec(root[KEY_OF_TOP_K_EVENT], topKEvents);
108 event->Update(KEY_OF_TOP_K_EVENT, topKEvents);
109
110 event->Update(KEY_OF_PLUGIN_NAME, ParseString(root[KEY_OF_PLUGIN_NAME]));
111 event->Update(KEY_OF_TOTAL, ParseUInt32(root[KEY_OF_TOTAL]));
112 event->Update(KEY_OF_PROC_NAME, ParseString(root[KEY_OF_PROC_NAME]));
113 event->Update(KEY_OF_PROC_TIME, ParseUInt32(root[KEY_OF_PROC_TIME]));
114 event->Update(KEY_OF_TOTAL_TIME, ParseUInt64(root[KEY_OF_TOTAL_TIME]));
115 return true;
116 }
117
ParseSysUsageEvent(std::shared_ptr<LoggerEvent> & event,const std::string & jsonStr)118 bool JsonParser::ParseSysUsageEvent(std::shared_ptr<LoggerEvent>& event, const std::string& jsonStr)
119 {
120 using namespace SysUsageEventSpace;
121 Json::Value root;
122 if (!ParseJsonString(root, jsonStr) || !CheckJsonValue(root, SYS_USAGE_FIELDS)) {
123 return false;
124 }
125 event->Update(KEY_OF_START, ParseUInt64(root[KEY_OF_START]));
126 event->Update(KEY_OF_END, ParseUInt64(root[KEY_OF_END]));
127 event->Update(KEY_OF_POWER, ParseUInt64(root[KEY_OF_POWER]));
128 event->Update(KEY_OF_SCREEN, ParseUInt64(root[KEY_OF_SCREEN]));
129 event->Update(KEY_OF_RUNNING, ParseUInt64(root[KEY_OF_RUNNING]));
130 return true;
131 }
132 } // namespace HiviewDFX
133 } // namespace OHOS
134