Skip to content

Commit

Permalink
refactor code and fix some npe (#1201)
Browse files Browse the repository at this point in the history
Signed-off-by: tomsun28 <tomsun28@outlook.com>
Co-authored-by: Carpe-Wang <wangcarpe@126.com>
Co-authored-by: tomsun28 <tomsun28@outlook.com>
  • Loading branch information
3 people authored Aug 23, 2023
1 parent 237b0f9 commit cf43a4c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ private Statement getConnection(String username, String password, String url,Int
private void queryOneRow(Statement statement, String sql, List<String> columns,
CollectRep.MetricsData.Builder builder, long startTime) throws Exception {
statement.setMaxRows(1);
ResultSet resultSet = statement.executeQuery(sql);
try {
try (ResultSet resultSet = statement.executeQuery(sql)) {
if (resultSet.next()) {
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
for (String column : columns) {
Expand All @@ -213,8 +212,6 @@ private void queryOneRow(Statement statement, String sql, List<String> columns,
}
builder.addValues(valueRowBuilder.build());
}
} finally {
resultSet.close();
}
}

Expand All @@ -231,8 +228,7 @@ private void queryOneRow(Statement statement, String sql, List<String> columns,
*/
private void queryOneRowByMatchTwoColumns(Statement statement, String sql, List<String> columns,
CollectRep.MetricsData.Builder builder, long startTime) throws Exception {
ResultSet resultSet = statement.executeQuery(sql);
try {
try (ResultSet resultSet = statement.executeQuery(sql)) {
HashMap<String, String> values = new HashMap<>(columns.size());
while (resultSet.next()) {
if (resultSet.getString(1) != null) {
Expand All @@ -251,8 +247,6 @@ private void queryOneRowByMatchTwoColumns(Statement statement, String sql, List<
}
}
builder.addValues(valueRowBuilder.build());
} finally {
resultSet.close();
}
}

Expand All @@ -268,8 +262,7 @@ private void queryOneRowByMatchTwoColumns(Statement statement, String sql, List<
*/
private void queryMultiRow(Statement statement, String sql, List<String> columns,
CollectRep.MetricsData.Builder builder, long startTime) throws Exception {
ResultSet resultSet = statement.executeQuery(sql);
try {
try (ResultSet resultSet = statement.executeQuery(sql)) {
while (resultSet.next()) {
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
for (String column : columns) {
Expand All @@ -284,8 +277,6 @@ private void queryMultiRow(Statement statement, String sql, List<String> columns
}
builder.addValues(valueRowBuilder.build());
}
} finally {
resultSet.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* ftp protocol collection implementation
Expand Down Expand Up @@ -51,11 +52,7 @@ public void collect(CollectRep.MetricsData.Builder builder, long appId, String a
metrics.getAliasFields().forEach(it -> {
if (valueMap.containsKey(it)) {
String fieldValue = valueMap.get(it);
if (fieldValue == null) {
valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
} else {
valueRowBuilder.addColumns(fieldValue);
}
valueRowBuilder.addColumns(Objects.requireNonNullElse(fieldValue, CommonConstants.NULL_VALUE));
} else {
valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class PrometheusMatrixParser extends AbstractPrometheusParse {
public Boolean checkType(String responseStr) {
try {
PromVectorOrMatrix promVectorOrMatrix = JsonUtil.fromJson(responseStr, PromVectorOrMatrix.class);
if (DispatchConstants.PARSE_PROM_QL_MATRIX.equals(promVectorOrMatrix.getData().getResultType())) {
return true;
if(promVectorOrMatrix != null && promVectorOrMatrix.getData() != null) {
return DispatchConstants.PARSE_PROM_QL_MATRIX.equals(promVectorOrMatrix.getData().getResultType());
}
return false;
} catch (Exception e) {
Expand All @@ -36,6 +36,9 @@ public Boolean checkType(String responseStr) {
@Override
public void parse(String resp, List<String> aliasFields, HttpProtocol http, CollectRep.MetricsData.Builder builder) {
PromVectorOrMatrix promVectorOrMatrix = JsonUtil.fromJson(resp, PromVectorOrMatrix.class);
if (promVectorOrMatrix == null){
return;
}
List<PromVectorOrMatrix.Result> result = promVectorOrMatrix.getData().getResult();
for (PromVectorOrMatrix.Result r : result) {
for (List<Object> value : r.getValues()) {
Expand All @@ -54,7 +57,7 @@ public void parse(String resp, List<String> aliasFields, HttpProtocol http, Coll
if (CommonConstants.PROM_TIME.equals(aliasField)) {
for (Object o : value) {
if (o instanceof Double) {
valueRowBuilder.addColumns(String.valueOf(new BigDecimal((Double) o * 1000)));
valueRowBuilder.addColumns(String.valueOf(BigDecimal.valueOf((Double) o * 1000)));
setTimeFlag = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class PrometheusVectorParser extends AbstractPrometheusParse {
public Boolean checkType(String responseStr) {
try {
PromVectorOrMatrix promVectorOrMatrix = JsonUtil.fromJson(responseStr, PromVectorOrMatrix.class);
if (DispatchConstants.PARSE_PROM_QL_VECTOR.equals(promVectorOrMatrix.getData().getResultType())) {
return true;
if (promVectorOrMatrix != null && promVectorOrMatrix.getData() != null) {
return DispatchConstants.PARSE_PROM_QL_VECTOR.equals(promVectorOrMatrix.getData().getResultType());
}
return false;
} catch (Exception e) {
Expand All @@ -38,6 +38,9 @@ public void parse(String resp, List<String> aliasFields, HttpProtocol http, Coll
boolean setTimeFlag = false;
boolean setValueFlag = false;
PromVectorOrMatrix promVectorOrMatrix = JsonUtil.fromJson(resp, PromVectorOrMatrix.class);
if (promVectorOrMatrix == null){
return;
}
List<PromVectorOrMatrix.Result> result = promVectorOrMatrix.getData().getResult();
for (PromVectorOrMatrix.Result r : result) {
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
Expand All @@ -53,7 +56,7 @@ public void parse(String resp, List<String> aliasFields, HttpProtocol http, Coll
if (CommonConstants.PROM_TIME.equals(aliasField)) {
for (Object o : r.getValue()) {
if (o instanceof Double) {
valueRowBuilder.addColumns(String.valueOf(new BigDecimal((Double) o * 1000)));
valueRowBuilder.addColumns(String.valueOf(BigDecimal.valueOf((Double) o * 1000)));
setTimeFlag = true;
}
}
Expand Down

0 comments on commit cf43a4c

Please sign in to comment.