-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into bump-banyandb-api
- Loading branch information
Showing
14 changed files
with
617 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
...server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/operation/SortLabelValuesOp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.apache.skywalking.mqe.rt.operation; | ||
|
||
import java.util.Comparator; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.skywalking.mqe.rt.exception.IllegalExpressionException; | ||
import org.apache.skywalking.mqe.rt.grammar.MQEParser; | ||
import org.apache.skywalking.mqe.rt.type.ExpressionResult; | ||
import org.apache.skywalking.mqe.rt.type.MQEValues; | ||
import org.apache.skywalking.oap.server.core.query.type.KeyValue; | ||
import org.apache.skywalking.oap.server.library.util.CollectionUtils; | ||
|
||
import static java.util.stream.Collectors.groupingBy; | ||
import static java.util.stream.Collectors.toList; | ||
|
||
@Slf4j | ||
public class SortLabelValuesOp { | ||
public static ExpressionResult doSortLabelValuesOp(ExpressionResult expResult, | ||
int order, | ||
List<String> labelNames) throws IllegalExpressionException { | ||
if (CollectionUtils.isNotEmpty(labelNames)) { | ||
labelNames = labelNames.stream().distinct().collect(toList()); | ||
if (MQEParser.ASC == order) { | ||
expResult.setResults( | ||
sort(expResult.getResults(), labelNames, labelNames.get(0), Comparator.naturalOrder())); | ||
} else if (MQEParser.DES == order) { | ||
expResult.setResults( | ||
sort(expResult.getResults(), labelNames, labelNames.get(0), Comparator.reverseOrder())); | ||
} else { | ||
throw new IllegalExpressionException("Unsupported sort order."); | ||
} | ||
} | ||
return expResult; | ||
} | ||
|
||
private static List<MQEValues> sort(List<MQEValues> results, | ||
List<String> sortLabels, | ||
String currentSortLabel, | ||
Comparator<String> comparator) { | ||
if (!sortLabels.contains(currentSortLabel)) { | ||
log.error("Current sort label {} not found in the sort labels {} ", currentSortLabel, sortLabels); | ||
return results; | ||
} | ||
if (sortLabels.indexOf( | ||
currentSortLabel) == sortLabels.size() - 1) { //only one label or the latest label no need to group | ||
results.sort(Comparator.comparing(mqeValues -> mqeValues.getMetric() | ||
.getLabels() | ||
.stream() | ||
.filter(kv -> kv.getKey().equals(currentSortLabel)) | ||
.findFirst() | ||
.orElse(new KeyValue(currentSortLabel, "")) | ||
.getValue(), comparator)); | ||
} else { | ||
LinkedHashMap<KeyValue, List<MQEValues>> groupResult = group(results, currentSortLabel); | ||
LinkedHashMap<KeyValue, List<MQEValues>> sortedGroup = new LinkedHashMap<>(groupResult.size()); | ||
for (Map.Entry<KeyValue, List<MQEValues>> entry : groupResult.entrySet()) { | ||
//sort by next label | ||
List<MQEValues> sortedResult = sort( | ||
entry.getValue(), sortLabels, sortLabels.get(sortLabels.indexOf(currentSortLabel) + 1), comparator); | ||
sortedGroup.put(entry.getKey(), sortedResult); | ||
} | ||
//combine the sorted group | ||
results = sortedGroup.keySet() | ||
.stream() | ||
.sorted(Comparator.comparing(KeyValue::getValue, comparator)) | ||
.flatMap(keyValue -> sortedGroup.get(keyValue).stream()) | ||
.collect(toList()); | ||
} | ||
return results; | ||
} | ||
|
||
//group by current label for sub sorting | ||
private static LinkedHashMap<KeyValue, List<MQEValues>> group(List<MQEValues> results, String labelName) { | ||
return results | ||
.stream() | ||
.collect(groupingBy( | ||
mqeValues -> mqeValues.getMetric().getLabels() | ||
.stream() | ||
.filter(kv -> kv.getKey().equals(labelName)) | ||
.findFirst().orElse(new KeyValue(labelName, "")), | ||
LinkedHashMap::new, | ||
toList() | ||
)); | ||
} | ||
} | ||
|
57 changes: 57 additions & 0 deletions
57
oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/operation/SortValuesOp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.apache.skywalking.mqe.rt.operation; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import org.apache.skywalking.mqe.rt.exception.IllegalExpressionException; | ||
import org.apache.skywalking.mqe.rt.grammar.MQEParser; | ||
import org.apache.skywalking.mqe.rt.type.ExpressionResult; | ||
import org.apache.skywalking.mqe.rt.type.MQEValue; | ||
|
||
public class SortValuesOp { | ||
public static ExpressionResult doSortValuesOp(ExpressionResult expResult, | ||
Optional<Integer> limit, | ||
int order) throws IllegalExpressionException { | ||
if (MQEParser.ASC == order || MQEParser.DES == order) { | ||
expResult.getResults().forEach(mqeValues -> { | ||
List<MQEValue> values = mqeValues.getValues() | ||
.stream() | ||
// Filter out empty values | ||
.filter(mqeValue -> !mqeValue.isEmptyValue()) | ||
.sorted(MQEParser.ASC == order ? Comparator.comparingDouble( | ||
MQEValue::getDoubleValue) : | ||
Comparator.comparingDouble(MQEValue::getDoubleValue) | ||
.reversed()) | ||
.collect( | ||
Collectors.toList()); | ||
if (limit.isPresent() && limit.get() < values.size()) { | ||
mqeValues.setValues(values.subList(0, limit.get())); | ||
} else { | ||
mqeValues.setValues(values); | ||
} | ||
}); | ||
} else { | ||
throw new IllegalExpressionException("Unsupported sort order."); | ||
} | ||
return expResult; | ||
} | ||
} |
Oops, something went wrong.