• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apache / rocketmq-dashboard / 211

pending completion
211

Pull #141

travis-ci-com

web-flow
Merge cc612230f into 86bdb0636
Pull Request #141: [ISSUE #140]Fix DashboardCollectServiceImpl#jsonDataFile2map throw NullPointerException

4 of 4 new or added lines in 1 file covered. (100.0%)

2375 of 2871 relevant lines covered (82.72%)

1.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.75
/src/main/java/org/apache/rocketmq/dashboard/service/impl/DashboardCollectServiceImpl.java
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
package org.apache.rocketmq.dashboard.service.impl;
18

19
import com.alibaba.fastjson.JSONArray;
20
import com.alibaba.fastjson.JSONObject;
21
import com.google.common.base.Charsets;
22
import com.google.common.base.Throwables;
23
import com.google.common.base.Ticker;
24
import com.google.common.cache.CacheBuilder;
25
import com.google.common.cache.CacheLoader;
26
import com.google.common.cache.LoadingCache;
27
import com.google.common.cache.RemovalListener;
28
import com.google.common.cache.RemovalNotification;
29
import com.google.common.collect.Lists;
30
import com.google.common.collect.Maps;
31
import com.google.common.io.Files;
32
import java.io.File;
33
import java.io.IOException;
34
import java.util.List;
35
import java.util.Map;
36
import java.util.Set;
37
import javax.annotation.Resource;
38
import org.apache.rocketmq.dashboard.config.RMQConfigure;
39
import org.apache.rocketmq.dashboard.service.DashboardCollectService;
40
import org.slf4j.Logger;
41
import org.slf4j.LoggerFactory;
42
import org.springframework.stereotype.Service;
43

44
@Service
45
public class DashboardCollectServiceImpl implements DashboardCollectService {
2✔
46

47
    @Resource
48
    private RMQConfigure configure;
49

50
    private final static Logger log = LoggerFactory.getLogger(DashboardCollectServiceImpl.class);
2✔
51

52
    private LoadingCache<String, List<String>> brokerMap = CacheBuilder.newBuilder()
2✔
53
        .maximumSize(1000)
2✔
54
        .concurrencyLevel(10)
2✔
55
        .recordStats()
2✔
56
        .ticker(Ticker.systemTicker())
2✔
57
        .removalListener(new RemovalListener<Object, Object>() {
2✔
58
            @Override
59
            public void onRemoval(RemovalNotification<Object, Object> notification) {
60
                log.debug(notification.getKey() + " was removed, cause is " + notification.getCause());
2✔
61
            }
2✔
62
        })
63
        .build(
2✔
64
            new CacheLoader<String, List<String>>() {
2✔
65
                @Override
66
                public List<String> load(String key) {
67
                    List<String> list = Lists.newArrayList();
2✔
68
                    return list;
2✔
69
                }
70
            }
71
        );
72

73
    private LoadingCache<String, List<String>> topicMap = CacheBuilder.newBuilder()
2✔
74
        .maximumSize(1000)
2✔
75
        .concurrencyLevel(10)
2✔
76
        .recordStats()
2✔
77
        .ticker(Ticker.systemTicker())
2✔
78
        .removalListener(new RemovalListener<Object, Object>() {
2✔
79
            @Override
80
            public void onRemoval(RemovalNotification<Object, Object> notification) {
81
                log.debug(notification.getKey() + " was removed, cause is " + notification.getCause());
2✔
82
            }
2✔
83
        })
84
        .build(
2✔
85
            new CacheLoader<String, List<String>>() {
2✔
86
                @Override
87
                public List<String> load(String key) {
88
                    List<String> list = Lists.newArrayList();
2✔
89
                    return list;
2✔
90
                }
91
            }
92
        );
93

94
    @Override
95
    public LoadingCache<String, List<String>> getBrokerMap() {
96
        return brokerMap;
2✔
97
    }
98

99
    @Override
100
    public LoadingCache<String, List<String>> getTopicMap() {
101
        return topicMap;
2✔
102
    }
103

104
    @Override
105
    public Map<String, List<String>> jsonDataFile2map(File file) {
106
        List<String> strings;
107
        try {
108
            strings = Files.readLines(file, Charsets.UTF_8);
2✔
109
        } catch (IOException e) {
×
110
            throw Throwables.propagate(e);
×
111
        }
2✔
112
        StringBuffer sb = new StringBuffer();
2✔
113
        for (String string : strings) {
2✔
114
            sb.append(string);
2✔
115
        }
2✔
116
        Map<String, List<String>> map = Maps.newHashMap();
2✔
117
        JSONObject json = (JSONObject) JSONObject.parse(sb.toString());
2✔
118
        if (null == json) {
2✔
119
            return map;
×
120
        }
121
        Set<Map.Entry<String, Object>> entries = json.entrySet();
2✔
122
        for (Map.Entry<String, Object> entry : entries) {
2✔
123
            JSONArray tpsArray = (JSONArray) entry.getValue();
2✔
124
            if (tpsArray == null) {
2✔
125
                continue;
×
126
            }
127
            Object[] tpsStrArray = tpsArray.toArray();
2✔
128
            List<String> tpsList = Lists.newArrayList();
2✔
129
            for (Object tpsObj : tpsStrArray) {
2✔
130
                tpsList.add("" + tpsObj);
2✔
131
            }
132
            map.put(entry.getKey(), tpsList);
2✔
133
        }
2✔
134
        return map;
2✔
135
    }
136

137
    @Override
138
    public Map<String, List<String>> getBrokerCache(String date) {
139
        String dataLocationPath = configure.getDashboardCollectData();
2✔
140
        File file = new File(dataLocationPath + date + ".json");
2✔
141
        if (!file.exists()) {
2✔
142
            log.info(String.format("No dashboard data for broker cache data: %s", date));
2✔
143
            return Maps.newHashMap();
2✔
144
        }
145
        return jsonDataFile2map(file);
2✔
146
    }
147

148
    @Override
149
    public Map<String, List<String>> getTopicCache(String date) {
150
        String dataLocationPath = configure.getDashboardCollectData();
2✔
151
        File file = new File(dataLocationPath + date + "_topic" + ".json");
2✔
152
        if (!file.exists()) {
2✔
153
            log.info(String.format("No dashboard data for data: %s", date));
2✔
154
            //throw Throwables.propagate(new ServiceException(1, "This date have't data!"));
155
            return Maps.newHashMap();
2✔
156
        }
157
        return jsonDataFile2map(file);
2✔
158
    }
159

160
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc