1/*
2 * Copyright (c) 2021 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#ifndef EXTRACT_RULE_H
16#define EXTRACT_RULE_H
17#include <list>
18#include <map>
19#include <string>
20#include <vector>
21
22#include <json/json.h>
23
24#include "syntax_rules.h"
25namespace OHOS {
26namespace HiviewDFX {
27class ExtractRule {
28public:
29    ExtractRule() {};
30    ~ExtractRule() {};
31    void ParseExtractRule(const std::string& eventType, const std::string& config, const std::string& fullPath);
32    std::vector<std::string> GetFeatureId();
33    std::map<std::string, FeatureSet> GetExtractRule() { return featureSets_; };
34    std::map<std::string, std::vector<std::string>> GetSegStatusCfg() { return segStatusCfgMap_; };
35
36private:
37    static constexpr int JSON_ARRAY_THRESHOLD = 50;
38
39    void ParseSegStatusCfg(const Json::Value& json);
40    void ParseRule(const std::string& eventType, const Json::Value& json, const std::string& fullPath);
41    bool IsMatchId(const std::string& eventType, const std::string& featureId) const;
42    bool IsMatchPath(const std::string& sourceFile, const std::string& name, const std::string& pattern,
43        std::string& desPath) const;
44    std::vector<std::string> SplitFeatureId(const Json::Value& oject) const;
45    void ParseRule(const Json::Value& oject, std::list<FeatureRule>& features) const;
46    void ParseRuleParam(const Json::Value& oject, std::list<FeatureRule>& features, const std::string& type) const;
47    void GetExtractParam(const Json::Value& rules,
48        std::map<std::string, std::string>& param, const std::string& preKey) const;
49    std::vector<std::string> GetJsonArray(const Json::Value& json, const std::string& param);
50
51private:
52    std::map<std::string, FeatureSet> featureSets_;
53    std::map<std::string, std::string> lockInfoMap_;
54    std::vector<std::string> featureIds_;
55    std::map<std::string, std::vector<std::string>> segStatusCfgMap_; // key: name  value: [matchKey, desc]
56};
57} // namespace HiviewDFX
58} // namespace OHOS
59#endif /* EXTRACT_RULE_H */