apache / hertzbeat

Apache HertzBeat(incubating) is a real-time monitoring system with agentless, performance cluster, prometheus-compatible, custom monitoring and status page building capabilities.
https://hertzbeat.apache.org/
Apache License 2.0
5.53k stars 961 forks source link

[Task] refactor nest block to improve readability by guard #2056

Open Thespica opened 4 months ago

Thespica commented 4 months ago

Description

There are some code with deep nest block, which are hard to read and understand. We can refactor nest block to improve readability by guard

For example, in CollectUtil.java:

            JsonObject jsonObject = jsonElement.getAsJsonObject();
            Iterator<Map.Entry<String, JsonElement>> iterator = jsonObject.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, JsonElement> entry = iterator.next();
                JsonElement element = entry.getValue();
                // Replace normal VALUE value
                if (element.isJsonPrimitive()) {
                    // Check if there are special characters Replace
                    String value = element.getAsString();
                    Matcher cryingMatcher = CRYING_PLACEHOLDER_REGEX_PATTERN.matcher(value);
                    if (cryingMatcher.find()) {
                        cryingMatcher.reset();
                        while (cryingMatcher.find()) {
                            String group = cryingMatcher.group();
                            String replaceField = group.replaceAll(CRYING_PLACEHOLDER_REX, "");
                            Configmap param = configmap.get(replaceField);
                            if (param != null) {
                                if (param.getValue() == null) {
                                    if (group.length() == value.length()) {
                                        value = null;
                                        break;
                                    } else {
                                        value = value.replace(group, "");
                                    }
                                } else {
                                    value = value.replace(group, (String) param.getValue());
                                }
                            }
                        }
                        jsonObject.addProperty(entry.getKey(), value);
                    }
                } else {
                    jsonObject.add(entry.getKey(), replaceCryPlaceholder(entry.getValue(), configmap));
                }
            }

can be replaced by:

            JsonObject jsonObject = jsonElement.getAsJsonObject();
            Iterator<Map.Entry<String, JsonElement>> iterator = jsonObject.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, JsonElement> entry = iterator.next();
                JsonElement element = entry.getValue();
                if (!element.isJsonPrimitive()) {
                    jsonObject.add(entry.getKey(), replaceCryPlaceholder(entry.getValue(), configmap));
                    continue;
                }
                // Replace normal VALUE value                
                // Check if there are special characters Replace
                String value = element.getAsString();
                Matcher cryingMatcher = CRYING_PLACEHOLDER_REGEX_PATTERN.matcher(value);
                if (!cryingMatcher.find()) {
                    continue;
                }
                cryingMatcher.reset();
                while (cryingMatcher.find()) {
                    String group = cryingMatcher.group();
                    String replaceField = group.replaceAll(CRYING_PLACEHOLDER_REX, "");
                    Configmap param = configmap.get(replaceField);
                    if (param == null) {
                        continue;
                    }
                    if (param.getValue() == null) {
                        if (group.length() == value.length()) {
                            value = null;
                            break;
                        } else {
                            value = value.replace(group, "");
                        }
                    } else {
                        value = value.replace(group, (String) param.getValue());
                    }
                }
                jsonObject.addProperty(entry.getKey(), value);

Task List

tomsun28 commented 4 months ago

hi, this look good. 👍 Welcome help us refactor submit pr.

ankurnotwarikoo commented 4 months ago

I would like to help. Can let me know how to get started ? That'd be great help.

LiuTianyou commented 3 months ago

I would like to help. Can let me know how to get started ? That'd be great help.

@ankurnotwarikoo Hello, you can refer to the following links to contribute code to Apache hertzbeat. We look forward to your participation. https://hertzbeat.apache.org/docs/community/contribution https://hertzbeat.apache.org/docs/community/document https://hertzbeat.apache.org/docs/community/submit_code https://hertzbeat.apache.org/docs/community/code_style_and_quality_guide