This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add baseline api and implementaion for entities (#55)
* Add baseline api and implementation for entities
- Loading branch information
1 parent
f660f94
commit b61884d
Showing
23 changed files
with
1,344 additions
and
60 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
gateway-service-api/src/main/proto/org/hypertrace/gateway/service/v1/baseline.proto
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,50 @@ | ||
syntax = "proto3"; | ||
|
||
option java_multiple_files = true; | ||
option java_package = "org.hypertrace.gateway.service.v1.baseline"; | ||
|
||
package org.hypertrace.gateway.service.v1.baseline; | ||
|
||
import "org/hypertrace/gateway/service/v1/gateway_query.proto"; | ||
|
||
message BaselineEntitiesRequest { | ||
string entity_type = 1; | ||
sfixed64 start_time_millis = 2; | ||
sfixed64 end_time_millis = 3; | ||
|
||
repeated string entity_ids = 4; | ||
repeated org.hypertrace.gateway.service.v1.common.FunctionExpression baseline_aggregate_request = 7; | ||
repeated BaselineTimeAggregation baseline_metric_series_request = 8; | ||
} | ||
|
||
message BaselineEntitiesResponse { | ||
repeated BaselineEntity baseline_entity = 1; | ||
} | ||
|
||
message BaselineEntity { | ||
string id = 1; | ||
string entity_type = 2; | ||
map<string, Baseline> baseline_aggregate_metric = 5; | ||
map<string, BaselineMetricSeries> baseline_metric_series = 6; | ||
} | ||
|
||
message BaselineTimeAggregation { | ||
org.hypertrace.gateway.service.v1.common.Period period = 1; | ||
org.hypertrace.gateway.service.v1.common.FunctionExpression aggregation = 2; | ||
} | ||
|
||
message Baseline { | ||
org.hypertrace.gateway.service.v1.common.Value value = 1; | ||
org.hypertrace.gateway.service.v1.common.Value lower_bound = 2; | ||
org.hypertrace.gateway.service.v1.common.Value upper_bound = 3; | ||
} | ||
|
||
message BaselineMetricSeries { | ||
repeated BaselineInterval baseline_value = 1; | ||
} | ||
|
||
message BaselineInterval { | ||
sfixed64 start_time_millis = 1; | ||
sfixed64 end_time_millis = 2; | ||
Baseline baseline = 3; | ||
} |
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
57 changes: 57 additions & 0 deletions
57
...ervice-impl/src/main/java/org/hypertrace/gateway/service/baseline/BaselineCalculator.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 @@ | ||
package org.hypertrace.gateway.service.baseline; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Preconditions; | ||
import org.apache.commons.math.stat.descriptive.moment.StandardDeviation; | ||
import org.apache.commons.math.stat.descriptive.rank.Median; | ||
import org.hypertrace.gateway.service.v1.baseline.Baseline; | ||
import org.hypertrace.gateway.service.v1.common.Value; | ||
import org.hypertrace.gateway.service.v1.common.ValueType; | ||
|
||
import java.util.List; | ||
|
||
public class BaselineCalculator { | ||
|
||
public static Baseline getBaseline(List<Value> metricValues) { | ||
if (metricValues.isEmpty()) { | ||
return Baseline.getDefaultInstance(); | ||
} | ||
double[] values = getValuesInDouble(metricValues); | ||
return getBaseline(values); | ||
} | ||
|
||
private static Baseline getBaseline(double[] metricValueArray) { | ||
Median median = new Median(); | ||
StandardDeviation standardDeviation = new StandardDeviation(); | ||
double medianValue = median.evaluate(metricValueArray); | ||
double sd = standardDeviation.evaluate(metricValueArray); | ||
double lowerBound = medianValue - (2 * sd); | ||
if (lowerBound < 0) { | ||
lowerBound = 0; | ||
} | ||
double upperBound = medianValue + (2 * sd); | ||
Baseline baseline = | ||
Baseline.newBuilder() | ||
.setLowerBound( | ||
Value.newBuilder().setValueType(ValueType.DOUBLE).setDouble(lowerBound).build()) | ||
.setUpperBound( | ||
Value.newBuilder().setValueType(ValueType.DOUBLE).setDouble(upperBound).build()) | ||
.setValue( | ||
Value.newBuilder().setValueType(ValueType.DOUBLE).setDouble(medianValue).build()) | ||
.build(); | ||
return baseline; | ||
} | ||
|
||
@VisibleForTesting | ||
private static double[] getValuesInDouble(List<Value> metricValues) { | ||
ValueType valueType = metricValues.get(0).getValueType(); | ||
switch (valueType) { | ||
case LONG: | ||
return metricValues.stream().mapToDouble(value -> (double) value.getLong()).toArray(); | ||
case DOUBLE: | ||
return metricValues.stream().mapToDouble(value -> value.getDouble()).toArray(); | ||
default: | ||
throw new IllegalArgumentException("Unsupported valueType " + valueType); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...c/main/java/org/hypertrace/gateway/service/baseline/BaselineEntitiesRequestValidator.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,40 @@ | ||
package org.hypertrace.gateway.service.baseline; | ||
|
||
import com.google.common.base.Preconditions; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.hypertrace.core.attribute.service.v1.AttributeMetadata; | ||
import org.hypertrace.gateway.service.common.validators.request.RequestValidator; | ||
import org.hypertrace.gateway.service.v1.baseline.BaselineEntitiesRequest; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Map; | ||
|
||
public class BaselineEntitiesRequestValidator extends RequestValidator<BaselineEntitiesRequest> { | ||
private static final Logger LOG = LoggerFactory.getLogger(BaselineEntitiesRequestValidator.class); | ||
|
||
@Override | ||
public void validate( | ||
BaselineEntitiesRequest request, Map<String, AttributeMetadata> attributeMetadataMap) { | ||
Preconditions.checkArgument( | ||
StringUtils.isNotBlank(request.getEntityType()), "EntityType is mandatory in the request."); | ||
|
||
Preconditions.checkArgument(request.getEntityIdsCount() > 0, "EntityIds cannot be empty"); | ||
|
||
Preconditions.checkArgument( | ||
(request.getBaselineAggregateRequestCount() > 0 | ||
|| request.getBaselineMetricSeriesRequestCount() > 0), | ||
"Both Selection list and TimeSeries list can't be empty in the request."); | ||
|
||
Preconditions.checkArgument( | ||
request.getStartTimeMillis() > 0 | ||
&& request.getEndTimeMillis() > 0 | ||
&& request.getStartTimeMillis() < request.getEndTimeMillis(), | ||
"Invalid time range. Both start and end times have to be valid timestamps."); | ||
} | ||
|
||
@Override | ||
protected Logger getLogger() { | ||
return LOG; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...ce-impl/src/main/java/org/hypertrace/gateway/service/baseline/BaselineRequestContext.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,24 @@ | ||
package org.hypertrace.gateway.service.baseline; | ||
|
||
import org.hypertrace.gateway.service.common.RequestContext; | ||
import org.hypertrace.gateway.service.v1.baseline.BaselineTimeAggregation; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class BaselineRequestContext extends RequestContext { | ||
|
||
private final Map<String, BaselineTimeAggregation> aliasToTimeAggregation = new HashMap(); | ||
|
||
public BaselineRequestContext(String tenantId, Map<String, String> headers) { | ||
super(tenantId, headers); | ||
} | ||
|
||
public void mapAliasToTimeAggregation(String alias, BaselineTimeAggregation timeAggregation) { | ||
aliasToTimeAggregation.put(alias, timeAggregation); | ||
} | ||
|
||
public BaselineTimeAggregation getTimeAggregationByAlias(String alias) { | ||
return aliasToTimeAggregation.get(alias); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...y-service-impl/src/main/java/org/hypertrace/gateway/service/baseline/BaselineService.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,11 @@ | ||
package org.hypertrace.gateway.service.baseline; | ||
|
||
import org.hypertrace.gateway.service.v1.baseline.BaselineEntitiesRequest; | ||
import org.hypertrace.gateway.service.v1.baseline.BaselineEntitiesResponse; | ||
|
||
import java.util.Map; | ||
|
||
public interface BaselineService { | ||
BaselineEntitiesResponse getBaselineForEntities( | ||
String tenantId, BaselineEntitiesRequest originalRequest, Map<String, String> requestHeaders); | ||
} |
Oops, something went wrong.