This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch percentile
in repository https://gitbox.apache.org/repos/asf/skywalking.git


The following commit(s) were added to refs/heads/percentile by this push:
     new 0d4f2cb  Support new query
0d4f2cb is described below

commit 0d4f2cbae12f28421a5cbc4845f7e323843c937e
Author: Wu Sheng <[email protected]>
AuthorDate: Fri Jan 10 22:02:19 2020 +0800

    Support new query
---
 .../oap/server/core/query/MetricQueryService.java  | 15 ++++
 .../oap/server/core/query/entity/IntValues.java    |  9 ++-
 .../core/storage/query/IMetricsQueryDAO.java       |  2 +
 .../analysis/metrics/PercentileMetricsTest.java    |  4 --
 .../oap/query/graphql/resolver/MetricQuery.java    |  7 ++
 .../src/main/resources/query-protocol              |  2 +-
 .../elasticsearch/query/MetricsQueryEsDAO.java     | 62 +++++++++++++---
 .../plugin/jdbc/h2/dao/H2MetricsQueryDAO.java      | 84 +++++++++++++++++++---
 8 files changed, 161 insertions(+), 24 deletions(-)

diff --git 
a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/MetricQueryService.java
 
b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/MetricQueryService.java
index 0335cb7..2eea8ff 100644
--- 
a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/MetricQueryService.java
+++ 
b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/MetricQueryService.java
@@ -96,6 +96,21 @@ public class MetricQueryService implements Service {
         return getMetricQueryDAO().getLinearIntValues(indName, downsampling, 
ids, ValueColumnIds.INSTANCE.getValueCName(indName));
     }
 
+    public IntValues[] getMultipleLinearIntValues(final String indName, final 
String id, final int numOfLinear,
+        final Downsampling downsampling,
+        final long startTB,
+        final long endTB) throws IOException, ParseException {
+        List<DurationPoint> durationPoints = 
DurationUtils.INSTANCE.getDurationPoints(downsampling, startTB, endTB);
+        List<String> ids = new ArrayList<>();
+        if (StringUtil.isEmpty(id)) {
+            durationPoints.forEach(durationPoint -> 
ids.add(String.valueOf(durationPoint.getPoint())));
+        } else {
+            durationPoints.forEach(durationPoint -> 
ids.add(durationPoint.getPoint() + Const.ID_SPLIT + id));
+        }
+
+        return getMetricQueryDAO().getMultipleLinearIntValues(indName, 
downsampling, ids, numOfLinear, ValueColumnIds.INSTANCE.getValueCName(indName));
+    }
+
     public Thermodynamic getThermodynamic(final String indName, final String 
id, final Downsampling downsampling,
         final long startTB,
         final long endTB) throws IOException, ParseException {
diff --git 
a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/entity/IntValues.java
 
b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/entity/IntValues.java
index 720fbf4..3525c60 100644
--- 
a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/entity/IntValues.java
+++ 
b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/query/entity/IntValues.java
@@ -19,14 +19,13 @@
 package org.apache.skywalking.oap.server.core.query.entity;
 
 import java.util.LinkedList;
-import java.util.List;
 
 /**
- * @author peng-yongsheng
+ * @author peng-yongsheng, wusheng
  */
 public class IntValues {
 
-    private List<KVInt> values = new LinkedList<>();
+    private LinkedList<KVInt> values = new LinkedList<>();
 
     public void addKVInt(KVInt e) {
         values.add(e);
@@ -40,4 +39,8 @@ public class IntValues {
         }
         return defaultValue;
     }
+
+    public KVInt getLast() {
+        return values.getLast();
+    }
 }
diff --git 
a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/query/IMetricsQueryDAO.java
 
b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/query/IMetricsQueryDAO.java
index ff92d41..c10aafa 100644
--- 
a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/query/IMetricsQueryDAO.java
+++ 
b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/query/IMetricsQueryDAO.java
@@ -34,5 +34,7 @@ public interface IMetricsQueryDAO extends DAO {
 
     IntValues getLinearIntValues(String indName, Downsampling downsampling, 
List<String> ids, String valueCName) throws IOException;
 
+    IntValues[] getMultipleLinearIntValues(String indName, Downsampling 
downsampling, List<String> ids, int numOfLinear, String valueCName) throws 
IOException;
+
     Thermodynamic getThermodynamic(String indName, Downsampling downsampling, 
List<String> ids, String valueCName) throws IOException;
 }
diff --git 
a/oap-server/server-core/src/test/java/org/apache/skywalking/oap/server/core/analysis/metrics/PercentileMetricsTest.java
 
b/oap-server/server-core/src/test/java/org/apache/skywalking/oap/server/core/analysis/metrics/PercentileMetricsTest.java
index 9a7fad2..8526cec 100644
--- 
a/oap-server/server-core/src/test/java/org/apache/skywalking/oap/server/core/analysis/metrics/PercentileMetricsTest.java
+++ 
b/oap-server/server-core/src/test/java/org/apache/skywalking/oap/server/core/analysis/metrics/PercentileMetricsTest.java
@@ -47,10 +47,6 @@ public class PercentileMetricsTest {
         metricsMocker.calculate();
 
         Assert.assertArrayEquals(new int[] {70, 90, 90, 90, 110}, 
metricsMocker.getValues());
-
-        for (int value : metricsMocker.getValues()) {
-            System.out.println(value);
-        }
     }
 
     public class PercentileMetricsMocker extends PercentileMetrics {
diff --git 
a/oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/resolver/MetricQuery.java
 
b/oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/resolver/MetricQuery.java
index 690420a..08b5079 100644
--- 
a/oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/resolver/MetricQuery.java
+++ 
b/oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/resolver/MetricQuery.java
@@ -60,6 +60,13 @@ public class MetricQuery implements GraphQLQueryResolver {
         return getMetricQueryService().getLinearIntValues(metrics.getName(), 
metrics.getId(), StepToDownsampling.transform(duration.getStep()), 
startTimeBucket, endTimeBucket);
     }
 
+    public IntValues[] getMultipleLinearIntValues(final MetricCondition 
metrics, final int numOfLinear, final Duration duration) throws IOException, 
ParseException {
+        long startTimeBucket = 
DurationUtils.INSTANCE.exchangeToTimeBucket(duration.getStart());
+        long endTimeBucket = 
DurationUtils.INSTANCE.exchangeToTimeBucket(duration.getEnd());
+
+        return 
getMetricQueryService().getMultipleLinearIntValues(metrics.getName(), 
metrics.getId(), numOfLinear, StepToDownsampling.transform(duration.getStep()), 
startTimeBucket, endTimeBucket);
+    }
+
     public Thermodynamic getThermodynamic(final MetricCondition metrics, final 
Duration duration) throws IOException, ParseException {
         long startTimeBucket = 
DurationUtils.INSTANCE.exchangeToTimeBucket(duration.getStart());
         long endTimeBucket = 
DurationUtils.INSTANCE.exchangeToTimeBucket(duration.getEnd());
diff --git 
a/oap-server/server-query-plugin/query-graphql-plugin/src/main/resources/query-protocol
 
b/oap-server/server-query-plugin/query-graphql-plugin/src/main/resources/query-protocol
index 90dfc1c..249adde 160000
--- 
a/oap-server/server-query-plugin/query-graphql-plugin/src/main/resources/query-protocol
+++ 
b/oap-server/server-query-plugin/query-graphql-plugin/src/main/resources/query-protocol
@@ -1 +1 @@
-Subproject commit 90dfc1c7ddc34e0c6f0291ffe3d96cd205d88c05
+Subproject commit 249addeaaf524c0dd990444e5f4bcaf355ce8e01
diff --git 
a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/MetricsQueryEsDAO.java
 
b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/MetricsQueryEsDAO.java
index fde2d6d..aac9224 100644
--- 
a/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/MetricsQueryEsDAO.java
+++ 
b/oap-server/server-storage-plugin/storage-elasticsearch-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/elasticsearch/query/MetricsQueryEsDAO.java
@@ -19,11 +19,20 @@
 package org.apache.skywalking.oap.server.storage.plugin.elasticsearch.query;
 
 import java.io.IOException;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 import org.apache.skywalking.oap.server.core.analysis.Downsampling;
-import org.apache.skywalking.oap.server.core.analysis.metrics.*;
-import org.apache.skywalking.oap.server.core.query.entity.*;
-import org.apache.skywalking.oap.server.core.query.sql.*;
+import org.apache.skywalking.oap.server.core.analysis.metrics.IntKeyLongValue;
+import 
org.apache.skywalking.oap.server.core.analysis.metrics.IntKeyLongValueHashMap;
+import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics;
+import 
org.apache.skywalking.oap.server.core.analysis.metrics.ThermodynamicMetrics;
+import org.apache.skywalking.oap.server.core.query.entity.IntValues;
+import org.apache.skywalking.oap.server.core.query.entity.KVInt;
+import org.apache.skywalking.oap.server.core.query.entity.Thermodynamic;
+import org.apache.skywalking.oap.server.core.query.sql.Function;
+import org.apache.skywalking.oap.server.core.query.sql.Where;
 import org.apache.skywalking.oap.server.core.storage.model.ModelName;
 import org.apache.skywalking.oap.server.core.storage.query.IMetricsQueryDAO;
 import 
org.apache.skywalking.oap.server.library.client.elasticsearch.ElasticSearchClient;
@@ -31,7 +40,8 @@ import 
org.apache.skywalking.oap.server.storage.plugin.elasticsearch.base.EsDAO;
 import org.elasticsearch.action.search.SearchResponse;
 import org.elasticsearch.search.SearchHit;
 import org.elasticsearch.search.aggregations.AggregationBuilders;
-import org.elasticsearch.search.aggregations.bucket.terms.*;
+import org.elasticsearch.search.aggregations.bucket.terms.Terms;
+import 
org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
 import org.elasticsearch.search.aggregations.metrics.avg.Avg;
 import org.elasticsearch.search.aggregations.metrics.sum.Sum;
 import org.elasticsearch.search.builder.SearchSourceBuilder;
@@ -45,7 +55,9 @@ public class MetricsQueryEsDAO extends EsDAO implements 
IMetricsQueryDAO {
         super(client);
     }
 
-    @Override public IntValues getValues(String indName, Downsampling 
downsampling, long startTB, long endTB, Where where, String valueCName,
+    @Override
+    public IntValues getValues(String indName, Downsampling downsampling, long 
startTB, long endTB, Where where,
+        String valueCName,
         Function function) throws IOException {
         String indexName = ModelName.build(downsampling, indName);
 
@@ -100,7 +112,8 @@ public class MetricsQueryEsDAO extends EsDAO implements 
IMetricsQueryDAO {
         }
     }
 
-    @Override public IntValues getLinearIntValues(String indName, Downsampling 
downsampling, List<String> ids, String valueCName) throws IOException {
+    @Override public IntValues getLinearIntValues(String indName, Downsampling 
downsampling, List<String> ids,
+        String valueCName) throws IOException {
         String indexName = ModelName.build(downsampling, indName);
 
         SearchResponse response = getClient().ids(indexName, ids.toArray(new 
String[0]));
@@ -121,7 +134,40 @@ public class MetricsQueryEsDAO extends EsDAO implements 
IMetricsQueryDAO {
         return intValues;
     }
 
-    @Override public Thermodynamic getThermodynamic(String indName, 
Downsampling downsampling, List<String> ids, String valueCName) throws 
IOException {
+    @Override public IntValues[] getMultipleLinearIntValues(String indName, 
Downsampling downsampling,
+        List<String> ids, int numOfLinear, String valueCName) throws 
IOException {
+        String indexName = ModelName.build(downsampling, indName);
+
+        SearchResponse response = getClient().ids(indexName, ids.toArray(new 
String[0]));
+        Map<String, Map<String, Object>> idMap = toMap(response);
+
+        IntValues[] intValues = new IntValues[numOfLinear];
+
+        for (String id : ids) {
+            for (IntValues value : intValues) {
+                KVInt kvInt = new KVInt();
+                kvInt.setId(id);
+                kvInt.setValue(0);
+
+            }
+
+            if (idMap.containsKey(id)) {
+                Map<String, Object> source = idMap.get(id);
+                IntKeyLongValueHashMap multipleValues = new 
IntKeyLongValueHashMap(5);
+                
multipleValues.toObject((String)source.getOrDefault(valueCName, ""));
+
+                for (int i = 0; i < intValues.length; i++) {
+                    
intValues[i].getLast().setValue(multipleValues.get(i).getValue());
+                }
+            }
+
+        }
+
+        return intValues;
+    }
+
+    @Override public Thermodynamic getThermodynamic(String indName, 
Downsampling downsampling, List<String> ids,
+        String valueCName) throws IOException {
         String indexName = ModelName.build(downsampling, indName);
 
         Thermodynamic thermodynamic = new Thermodynamic();
diff --git 
a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2MetricsQueryDAO.java
 
b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2MetricsQueryDAO.java
index 5937252..7f4f875 100644
--- 
a/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2MetricsQueryDAO.java
+++ 
b/oap-server/server-storage-plugin/storage-jdbc-hikaricp-plugin/src/main/java/org/apache/skywalking/oap/server/storage/plugin/jdbc/h2/dao/H2MetricsQueryDAO.java
@@ -19,12 +19,24 @@
 package org.apache.skywalking.oap.server.storage.plugin.jdbc.h2.dao;
 
 import java.io.IOException;
-import java.sql.*;
-import java.util.*;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 import org.apache.skywalking.oap.server.core.analysis.Downsampling;
-import org.apache.skywalking.oap.server.core.analysis.metrics.*;
-import org.apache.skywalking.oap.server.core.query.entity.*;
-import org.apache.skywalking.oap.server.core.query.sql.*;
+import org.apache.skywalking.oap.server.core.analysis.metrics.IntKeyLongValue;
+import 
org.apache.skywalking.oap.server.core.analysis.metrics.IntKeyLongValueHashMap;
+import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics;
+import 
org.apache.skywalking.oap.server.core.analysis.metrics.ThermodynamicMetrics;
+import org.apache.skywalking.oap.server.core.query.entity.IntValues;
+import org.apache.skywalking.oap.server.core.query.entity.KVInt;
+import org.apache.skywalking.oap.server.core.query.entity.Thermodynamic;
+import org.apache.skywalking.oap.server.core.query.sql.Function;
+import org.apache.skywalking.oap.server.core.query.sql.KeyValues;
+import org.apache.skywalking.oap.server.core.query.sql.Where;
 import org.apache.skywalking.oap.server.core.storage.model.ModelName;
 import org.apache.skywalking.oap.server.core.storage.query.IMetricsQueryDAO;
 import 
org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCHikariCPClient;
@@ -41,7 +53,8 @@ public class H2MetricsQueryDAO extends H2SQLExecutor 
implements IMetricsQueryDAO
     }
 
     @Override
-    public IntValues getValues(String indName, Downsampling downsampling, long 
startTB, long endTB, Where where, String valueCName,
+    public IntValues getValues(String indName, Downsampling downsampling, long 
startTB, long endTB, Where where,
+        String valueCName,
         Function function) throws IOException {
         String tableName = ModelName.build(downsampling, indName);
 
@@ -100,7 +113,8 @@ public class H2MetricsQueryDAO extends H2SQLExecutor 
implements IMetricsQueryDAO
         return orderWithDefault0(intValues, ids);
     }
 
-    @Override public IntValues getLinearIntValues(String indName, Downsampling 
downsampling, List<String> ids, String valueCName) throws IOException {
+    @Override public IntValues getLinearIntValues(String indName, Downsampling 
downsampling, List<String> ids,
+        String valueCName) throws IOException {
         String tableName = ModelName.build(downsampling, indName);
 
         StringBuilder idValues = new StringBuilder();
@@ -129,6 +143,45 @@ public class H2MetricsQueryDAO extends H2SQLExecutor 
implements IMetricsQueryDAO
         return orderWithDefault0(intValues, ids);
     }
 
+    @Override public IntValues[] getMultipleLinearIntValues(String indName, 
Downsampling downsampling,
+        List<String> ids,
+        int numOfLinear,
+        String valueCName) throws IOException {
+        String tableName = ModelName.build(downsampling, indName);
+
+        StringBuilder idValues = new StringBuilder();
+        for (int valueIdx = 0; valueIdx < ids.size(); valueIdx++) {
+            if (valueIdx != 0) {
+                idValues.append(",");
+            }
+            idValues.append("'").append(ids.get(valueIdx)).append("'");
+        }
+
+        IntValues[] intValuesArray = new IntValues[numOfLinear];
+
+        try (Connection connection = h2Client.getConnection()) {
+            try (ResultSet resultSet = h2Client.executeQuery(connection, 
"select id, " + valueCName + " from " + tableName + " where id in (" + 
idValues.toString() + ")")) {
+                while (resultSet.next()) {
+                    String id = resultSet.getString("id");
+
+                    IntKeyLongValueHashMap multipleValues = new 
IntKeyLongValueHashMap(5);
+                    multipleValues.toObject(resultSet.getString(valueCName));
+
+                    for (int i = 0; i < intValuesArray.length; i++) {
+                        KVInt kv = new KVInt();
+                        kv.setId(id);
+                        kv.setValue(multipleValues.get(i).getValue());
+                        intValuesArray[i].addKVInt(kv);
+                    }
+                }
+            }
+        } catch (SQLException e) {
+            throw new IOException(e);
+        }
+
+        return orderWithDefault0(intValuesArray, ids);
+    }
+
     /**
      * Make sure the order is same as the expected order, and keep default 
value as 0.
      *
@@ -149,7 +202,22 @@ public class H2MetricsQueryDAO extends H2SQLExecutor 
implements IMetricsQueryDAO
         return intValues;
     }
 
-    @Override public Thermodynamic getThermodynamic(String indName, 
Downsampling downsampling, List<String> ids, String valueCName) throws 
IOException {
+    /**
+     * Make sure the order is same as the expected order, and keep default 
value as 0.
+     *
+     * @param origin
+     * @param expectedOrder
+     * @return
+     */
+    private IntValues[] orderWithDefault0(IntValues[] origin, List<String> 
expectedOrder) {
+        for (int i = 0; i < origin.length; i++) {
+            origin[i] = orderWithDefault0(origin[i], expectedOrder);
+        }
+        return origin;
+    }
+
+    @Override public Thermodynamic getThermodynamic(String indName, 
Downsampling downsampling, List<String> ids,
+        String valueCName) throws IOException {
         String tableName = ModelName.build(downsampling, indName);
 
         StringBuilder idValues = new StringBuilder();

Reply via email to