wu-sheng commented on a change in pull request #5202:
URL: https://github.com/apache/skywalking/pull/5202#discussion_r462363985



##########
File path: 
oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/MetricsQueryEsDAO.java
##########
@@ -138,42 +139,12 @@ public MetricsValues readMetricsValues(final 
MetricsCondition condition,
         });
 
         SearchResponse response = getClient().ids(condition.getName(), 
ids.toArray(new String[0]));
-        Map<String, Map<String, Object>> idMap = toMap(response);
-
-        Map<String, MetricsValues> labeledValues = new 
HashMap<>(labels.size());
-        labels.forEach(label -> {
-            MetricsValues labelValue = new MetricsValues();
-            labelValue.setLabel(label);
-
-            labeledValues.put(label, labelValue);
-        });
-
-        final int defaultValue = 
ValueColumnMetadata.INSTANCE.getDefaultValue(condition.getName());
-        for (String id : ids) {
-            if (idMap.containsKey(id)) {
-                Map<String, Object> source = idMap.get(id);
-                DataTable multipleValues = new DataTable((String) 
source.getOrDefault(valueColumnName, ""));
-
-                labels.forEach(label -> {
-                    final IntValues values = 
labeledValues.get(label).getValues();
-                    Long data = multipleValues.get(label);
-                    if (data == null) {
-                        data = (long) defaultValue;
-                    }
-                    KVInt kv = new KVInt();
-                    kv.setId(id);
-                    kv.setValue(data);
-                    values.addKVInt(kv);
-                });
-            }
-
+        Map<String, DataTable> idMap = new LinkedHashMap<>();

Review comment:
       Others are `HashMap`, this is `LinkedHashMap`. Why?

##########
File path: 
oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/query/IMetricsQueryDAO.java
##########
@@ -76,5 +87,45 @@ public static IntValues sortValues(IntValues origin, 
List<String> expectedOrder,
             }
             return origin;
         }
+
+        /**
+         * Compose the multiple metric result based on conditions.
+         */
+        public static List<MetricsValues> composeLabelValue(final 
MetricsCondition condition,
+            final List<String> labels,
+            final List<String> ids,
+            final Map<String, DataTable> idMap) {
+            List<String> allLabels;
+            if (Objects.isNull(labels) || labels.size() < 1) {
+                allLabels = idMap.values().stream()
+                    .flatMap(dataTable -> dataTable.keys().stream())
+                    .distinct().collect(Collectors.toList());
+            } else {
+                allLabels = labels;
+            }
+            final int defaultValue = 
ValueColumnMetadata.INSTANCE.getDefaultValue(condition.getName());

Review comment:
       I feel like you should just copy the old codes rather than re-implement. 

##########
File path: 
oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/query/IMetricsQueryDAO.java
##########
@@ -76,5 +87,45 @@ public static IntValues sortValues(IntValues origin, 
List<String> expectedOrder,
             }
             return origin;
         }
+
+        /**
+         * Compose the multiple metric result based on conditions.
+         */
+        public static List<MetricsValues> composeLabelValue(final 
MetricsCondition condition,
+            final List<String> labels,
+            final List<String> ids,
+            final Map<String, DataTable> idMap) {
+            List<String> allLabels;
+            if (Objects.isNull(labels) || labels.size() < 1) {
+                allLabels = idMap.values().stream()
+                    .flatMap(dataTable -> dataTable.keys().stream())
+                    .distinct().collect(Collectors.toList());
+            } else {
+                allLabels = labels;
+            }
+            final int defaultValue = 
ValueColumnMetadata.INSTANCE.getDefaultValue(condition.getName());
+            return allLabels.stream()
+                .flatMap(label -> ids.stream()
+                    .map(id -> Tuple.of(
+                        label,
+                        id,
+                        Optional.ofNullable(idMap.getOrDefault(id, new 
DataTable()).get(label)).orElse(0L))))

Review comment:
       Why `orElse(0L)`?

##########
File path: 
oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/query/IMetricsQueryDAO.java
##########
@@ -76,5 +87,45 @@ public static IntValues sortValues(IntValues origin, 
List<String> expectedOrder,
             }
             return origin;
         }
+
+        /**
+         * Compose the multiple metric result based on conditions.
+         */
+        public static List<MetricsValues> composeLabelValue(final 
MetricsCondition condition,
+            final List<String> labels,
+            final List<String> ids,
+            final Map<String, DataTable> idMap) {
+            List<String> allLabels;
+            if (Objects.isNull(labels) || labels.size() < 1) {
+                allLabels = idMap.values().stream()
+                    .flatMap(dataTable -> dataTable.keys().stream())
+                    .distinct().collect(Collectors.toList());
+            } else {
+                allLabels = labels;
+            }
+            final int defaultValue = 
ValueColumnMetadata.INSTANCE.getDefaultValue(condition.getName());

Review comment:
       Once you have the `allLabels`, copying is a safer way. This 
re-implementation is hard to be reviewed.

##########
File path: 
oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/query/IMetricsQueryDAO.java
##########
@@ -76,5 +87,45 @@ public static IntValues sortValues(IntValues origin, 
List<String> expectedOrder,
             }
             return origin;
         }
+
+        /**
+         * Compose the multiple metric result based on conditions.
+         */
+        public static List<MetricsValues> composeLabelValue(final 
MetricsCondition condition,
+            final List<String> labels,
+            final List<String> ids,
+            final Map<String, DataTable> idMap) {
+            List<String> allLabels;
+            if (Objects.isNull(labels) || labels.size() < 1) {
+                allLabels = idMap.values().stream()
+                    .flatMap(dataTable -> dataTable.keys().stream())
+                    .distinct().collect(Collectors.toList());
+            } else {
+                allLabels = labels;
+            }
+            final int defaultValue = 
ValueColumnMetadata.INSTANCE.getDefaultValue(condition.getName());

Review comment:
       Could you revert them? Is there any block there?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to