Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] JPA data saving logic problem #1862

Closed
1 task done
zhangshenghang opened this issue Apr 27, 2024 · 1 comment · Fixed by #1863
Closed
1 task done

[BUG] JPA data saving logic problem #1862

zhangshenghang opened this issue Apr 27, 2024 · 1 comment · Fixed by #1863
Labels
bug Something isn't working

Comments

@zhangshenghang
Copy link
Member

zhangshenghang commented Apr 27, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

Problem Description

When using JPA as the data storage, in the historical chart monitoring page, a monitoring indicator name is not displayed, and the data displayed on the chart is also incorrect, as shown in the image below:
image

The CPU usage of pid=2080064 is 0.1, but it shows 0 in the image.

This issue can be assigned to me and I have fixed it。

Problem Cause

The timing of assigning values to the instance is incorrect: historyBuilder.instance(JsonUtil.toJson(labels));

The following code snippet is problematic. It causes the initial iteration to not assign a value, and the value used in the second iteration is from the first one.

Class Name: HistoryJpaDatabaseDataStorage
Method:

void saveData(CollectRep.MetricsData metricsData) {
        if (metricsData.getCode() != CollectRep.Code.SUCCESS) {
            return;
        }
        if (metricsData.getValuesList().isEmpty()) {
            log.info("[warehouse jpa] flush metrics data {} is null, ignore.", metricsData.getId());
            return;
        }
        String monitorType = metricsData.getApp();
        String metrics = metricsData.getMetrics();
        List<CollectRep.Field> fieldsList = metricsData.getFieldsList();
        try {
            List<History> historyList = new LinkedList<>();
            History.HistoryBuilder historyBuilder = History.builder()
                    .monitorId(metricsData.getId())
                    .app(monitorType)
                    .metrics(metrics)
                    .time(metricsData.getTime());
            for (CollectRep.ValueRow valueRow : metricsData.getValuesList()) {
                Map<String, String> labels = new HashMap<>(8);
                for (int i = 0; i < fieldsList.size(); i++) {
                    final CollectRep.Field field = fieldsList.get(i);
                    final int fieldType = field.getType();
                    final String fieldName = field.getName();
                    final String columnValue = valueRow.getColumns(i);

                    historyBuilder.metric(fieldName);

                    if (CommonConstants.NULL_VALUE.equals(columnValue)) {
                        switch (fieldType) {
                            case CommonConstants.TYPE_NUMBER -> {
                                historyBuilder.metricType(CommonConstants.TYPE_NUMBER)
                                        .dou(null);
                            }
                            case CommonConstants.TYPE_STRING -> {
                                historyBuilder.metricType(CommonConstants.TYPE_STRING)
                                        .str(null);
                            }
                            case CommonConstants.TYPE_TIME -> {
                                historyBuilder.metricType(CommonConstants.TYPE_TIME)
                                        .int32(null);
                            }
                            default -> historyBuilder.metricType(CommonConstants.TYPE_NUMBER);
                        }
                    } else {
                        switch (fieldType) {
                            case CommonConstants.TYPE_NUMBER -> {
                                historyBuilder.metricType(CommonConstants.TYPE_NUMBER)
                                        .dou(Double.parseDouble(columnValue));
                            }
                            case CommonConstants.TYPE_STRING -> {
                                historyBuilder.metricType(CommonConstants.TYPE_STRING)
                                        .str(formatStrValue(columnValue));
                            }
                            case CommonConstants.TYPE_TIME -> {
                                historyBuilder.metricType(CommonConstants.TYPE_TIME)
                                        .int32(Integer.parseInt(columnValue));
                            }
                            default -> historyBuilder.metricType(CommonConstants.TYPE_NUMBER)
                                    .dou(Double.parseDouble(columnValue));
                        }

                        if (field.getLabel()) {
                            labels.put(fieldName, columnValue);
                        }
                    }

                    historyList.add(historyBuilder.build());
                }
                historyBuilder.instance(JsonUtil.toJson(labels));
            }
            historyDao.saveAll(historyList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

Expected Behavior

image

Steps To Reproduce

No response

Environment

HertzBeat version(s):1.5

Debug logs

No response

Anything else?

No response

@tomsun28
Copy link
Contributor

hi this may cause by the pr #1801

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development

Successfully merging a pull request may close this issue.

2 participants