• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 package ohos.devtools.datasources.utils.monitorconfig.service;
17 
18 import com.alibaba.fastjson.JSONObject;
19 import ohos.devtools.datasources.utils.monitorconfig.dao.MonitorConfigDao;
20 import ohos.devtools.datasources.utils.monitorconfig.entity.MonitorInfo;
21 import ohos.devtools.datasources.utils.profilerlog.ProfilerLogManager;
22 import org.apache.logging.log4j.LogManager;
23 import org.apache.logging.log4j.Logger;
24 
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.LinkedList;
28 import java.util.Map;
29 import java.util.concurrent.ConcurrentHashMap;
30 
31 /**
32  * MonitorConfigManager
33  *
34  * @since 2021/5/19 16:39
35  */
36 public class MonitorConfigManager {
37     private static final Logger LOGGER = LogManager.getLogger(MonitorConfigManager.class);
38 
39     /**
40      * dataMap
41      */
42     public static final ConcurrentHashMap<Long, Map<String, LinkedList<String>>> DATA_MAP = new ConcurrentHashMap<>();
43 
44     private static volatile MonitorConfigManager singleton;
45 
MonitorConfigManager()46     private MonitorConfigManager() {
47     }
48 
49     /**
50      * getInstance
51      *
52      * @return MonitorConfigManager
53      */
getInstance()54     public static MonitorConfigManager getInstance() {
55         if (singleton == null) {
56             synchronized (MonitorConfigManager.class) {
57                 if (singleton == null) {
58                     singleton = new MonitorConfigManager();
59                 }
60             }
61         }
62         return singleton;
63     }
64 
65     /**
66      * analyzeCharTarget
67      *
68      * @param localSessionId localSessionId
69      * @param jsonMonitor    jsonMonitor
70      * @return Map<String, LinkedList < String>>
71      */
analyzeCharTarget(long localSessionId, JSONObject jsonMonitor)72     public Map<String, LinkedList<String>> analyzeCharTarget(long localSessionId, JSONObject jsonMonitor) {
73         if (ProfilerLogManager.isInfoEnabled()) {
74             LOGGER.info("analyzeCharTarget");
75         }
76         // 写表的实体对象传递
77         MonitorInfo monitorInfo = null;
78         LinkedList<MonitorInfo> monitorInfos = new LinkedList<>();
79 
80         // 传递给界面的具体指标项配置(True),通过Dao获取则丢弃这个集合对象
81         LinkedList<String> monitor = null;
82         Map<String, LinkedList<String>> monitors = new HashMap<>();
83 
84         // 迭代传递的JSON对象解析,并写表
85         Iterator<Map.Entry<String, Object>> rtn = jsonMonitor.entrySet().iterator();
86         while (rtn.hasNext()) {
87             Map.Entry<String, Object> unit = rtn.next();
88             monitor = new LinkedList<>();
89 
90             JSONObject jsonObject = jsonMonitor.getJSONObject(unit.getKey());
91             Iterator<Map.Entry<String, Object>> iterator = jsonObject.entrySet().iterator();
92 
93             while (iterator.hasNext()) {
94                 Map.Entry<String, Object> next = iterator.next();
95                 monitorInfo = MonitorInfo.builder().localSessionId(localSessionId).monitorType(unit.getKey())
96                     .parameter(next.getKey()).value(next.getValue().toString()).build();
97 
98                 if ("true".equals(next.getValue().toString())) {
99                     monitor.add(next.getKey());
100                 }
101                 monitorInfos.add(monitorInfo);
102             }
103 
104             if (monitor.size() > 0) {
105                 // 传递给二级界面的数据monitors
106                 monitors.put(unit.getKey(), monitor);
107             }
108         }
109 
110         DATA_MAP.put(localSessionId, monitors);
111         // 解析后的数据先写表
112         MonitorConfigDao.getInstance().insertMonitorInfos(monitorInfos);
113         if (ProfilerLogManager.isInfoEnabled()) {
114             LOGGER.info("analyze Chart Target success");
115         }
116         // 返回界面所有配置(true)的指标项
117         return monitors;
118     }
119 }
120