-
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.
- Loading branch information
Showing
29 changed files
with
457 additions
and
41 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
oap-server/mqe-rt/src/main/java/org/apache/skywalking/mqe/rt/operation/TrendOp.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.ArrayList; | ||
import java.util.List; | ||
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; | ||
import org.apache.skywalking.oap.server.core.query.enumeration.Step; | ||
|
||
public class TrendOp { | ||
public static ExpressionResult doTrendOp(ExpressionResult expResult, | ||
int opType, | ||
int trendRange, | ||
Step step) throws IllegalExpressionException { | ||
switch (opType) { | ||
case MQEParser.INCREASE: | ||
return TrendOp.calculateIncrease(expResult, trendRange); | ||
case MQEParser.RATE: | ||
return TrendOp.calculateRate(expResult, trendRange, step); | ||
} | ||
|
||
throw new IllegalExpressionException("Unsupported function."); | ||
} | ||
|
||
private static ExpressionResult calculateIncrease(ExpressionResult expResult, int trendRange) { | ||
expResult.getResults().forEach(resultValues -> { | ||
List<MQEValue> mqeValues = resultValues.getValues(); | ||
List<MQEValue> newMqeValues = new ArrayList<>(); | ||
for (int i = trendRange; i < mqeValues.size(); i++) { | ||
MQEValue mqeValue = mqeValues.get(i); | ||
//if the current value is empty, then the trend value is empty | ||
if (mqeValue.isEmptyValue()) { | ||
newMqeValues.add(mqeValue); | ||
continue; | ||
} | ||
MQEValue newMqeValue = new MQEValue(); | ||
newMqeValue.setId(mqeValue.getId()); | ||
|
||
//if the previous value is empty, then the trend value is empty | ||
if (mqeValues.get(i - trendRange).isEmptyValue()) { | ||
newMqeValue.setEmptyValue(true); | ||
newMqeValues.add(newMqeValue); | ||
continue; | ||
} | ||
|
||
newMqeValue.setEmptyValue(mqeValue.isEmptyValue()); | ||
newMqeValue.setId(mqeValue.getId()); | ||
newMqeValue.setTraceID(mqeValue.getTraceID()); | ||
double newValue = mqeValue.getDoubleValue() - mqeValues.get(i - trendRange).getDoubleValue(); | ||
newMqeValue.setDoubleValue(newValue); | ||
newMqeValues.add(newMqeValue); | ||
} | ||
resultValues.setValues(newMqeValues); | ||
}); | ||
return expResult; | ||
} | ||
|
||
private static ExpressionResult calculateRate(ExpressionResult expResult, int trendRange, Step step) { | ||
ExpressionResult result = calculateIncrease(expResult, trendRange); | ||
long rangeSeconds; | ||
switch (step) { | ||
case SECOND: | ||
rangeSeconds = trendRange; | ||
break; | ||
case MINUTE: | ||
rangeSeconds = trendRange * 60; | ||
break; | ||
case HOUR: | ||
rangeSeconds = trendRange * 3600; | ||
break; | ||
case DAY: | ||
rangeSeconds = trendRange * 86400; | ||
break; | ||
default: | ||
throw new IllegalArgumentException("Unsupported step: " + step); | ||
} | ||
result.getResults().forEach(resultValues -> { | ||
resultValues.getValues().forEach(mqeValue -> { | ||
if (!mqeValue.isEmptyValue()) { | ||
double newValue = mqeValue.getDoubleValue() / rangeSeconds; | ||
mqeValue.setDoubleValue(newValue); | ||
} | ||
}); | ||
}); | ||
return result; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
oap-server/mqe-rt/src/test/java/org/apache/skywalking/mqe/rt/TrendOpTest.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,72 @@ | ||
/* | ||
* 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; | ||
|
||
import org.apache.skywalking.mqe.rt.grammar.MQEParser; | ||
import org.apache.skywalking.mqe.rt.operation.TrendOp; | ||
import org.apache.skywalking.mqe.rt.type.ExpressionResult; | ||
import org.apache.skywalking.mqe.rt.type.ExpressionResultType; | ||
import org.apache.skywalking.oap.server.core.query.enumeration.Step; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class TrendOpTest { | ||
private final MockData mockData = new MockData(); | ||
|
||
@Test | ||
public void increaseNoLabeledTest() throws Exception { | ||
ExpressionResult increase = TrendOp.doTrendOp( | ||
mockData.newSeriesNoLabeledResult(100, 280), MQEParser.INCREASE, 1, Step.MINUTE); | ||
assertEquals(ExpressionResultType.TIME_SERIES_VALUES, increase.getType()); | ||
assertEquals("300", increase.getResults().get(0).getValues().get(0).getId()); | ||
assertEquals(180, increase.getResults().get(0).getValues().get(0).getDoubleValue()); | ||
} | ||
|
||
@Test | ||
public void increaseLabeledTest() throws Exception { | ||
ExpressionResult increase = TrendOp.doTrendOp( | ||
mockData.newSeriesLabeledResult(100, 280, 100, 220), MQEParser.INCREASE, 1, Step.MINUTE); | ||
assertEquals(ExpressionResultType.TIME_SERIES_VALUES, increase.getType()); | ||
assertEquals("300", increase.getResults().get(0).getValues().get(0).getId()); | ||
assertEquals(180, increase.getResults().get(0).getValues().get(0).getDoubleValue()); | ||
assertEquals("300", increase.getResults().get(1).getValues().get(0).getId()); | ||
assertEquals(120, increase.getResults().get(1).getValues().get(0).getDoubleValue()); | ||
} | ||
|
||
@Test | ||
public void rateNoLabeledTest() throws Exception { | ||
ExpressionResult rate = TrendOp.doTrendOp( | ||
mockData.newSeriesNoLabeledResult(100, 280), MQEParser.RATE, 1, Step.MINUTE); | ||
assertEquals(ExpressionResultType.TIME_SERIES_VALUES, rate.getType()); | ||
assertEquals("300", rate.getResults().get(0).getValues().get(0).getId()); | ||
assertEquals(3, rate.getResults().get(0).getValues().get(0).getDoubleValue()); | ||
} | ||
|
||
@Test | ||
public void rateLabeledTest() throws Exception { | ||
ExpressionResult rate = TrendOp.doTrendOp( | ||
mockData.newSeriesLabeledResult(100, 280, 100, 220), MQEParser.RATE, 1, Step.MINUTE); | ||
assertEquals(ExpressionResultType.TIME_SERIES_VALUES, rate.getType()); | ||
assertEquals("300", rate.getResults().get(0).getValues().get(0).getId()); | ||
assertEquals(3, rate.getResults().get(0).getValues().get(0).getDoubleValue()); | ||
assertEquals("300", rate.getResults().get(1).getValues().get(0).getId()); | ||
assertEquals(2, rate.getResults().get(1).getValues().get(0).getDoubleValue()); | ||
} | ||
} |
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
Oops, something went wrong.