Skip to content

Commit

Permalink
FIXES open-metadata#14294: return null if no aggregation value is pre…
Browse files Browse the repository at this point in the history
…sent (open-metadata#14514)

* fix: return null is no aggregation value is present

* fix: added Optional typing

* style: java linting

* fix: use Optional type hint

* style: java linting
  • Loading branch information
TeddyCr authored Dec 29, 2023
1 parent 1f4ab99 commit 2f679a9
Show file tree
Hide file tree
Showing 40 changed files with 196 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.openmetadata.schema.analytics.DataAssetValues;
import org.openmetadata.schema.dataInsight.type.AggregatedUnusedAssetsCount;

Expand Down Expand Up @@ -62,5 +63,5 @@ public List<Object> aggregate() throws ParseException {

protected abstract S getAggregations(B bucket, String key);

protected abstract Double getValue(S aggregations);
protected abstract Optional<Double> getValue(S aggregations);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.openmetadata.schema.analytics.DataAssetValues;
import org.openmetadata.schema.dataInsight.type.AggregatedUnusedAssetsSize;

Expand Down Expand Up @@ -62,5 +63,5 @@ public List<Object> aggregate() throws ParseException {

protected abstract S getAggregations(B bucket, String key);

protected abstract Double getValue(S aggregations);
protected abstract Optional<Double> getValue(S aggregations);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.openmetadata.schema.dataInsight.type.AggregatedUsedVsUnusedAssetsCount;

public abstract class AggregatedUsedvsUnusedAssetsCountAggregator<A, H, B, S>
Expand All @@ -23,21 +23,17 @@ public List<Object> aggregate() throws ParseException {
Long timestamp = convertDatTimeStringToTimestamp(dateTimeString);
S totalUnused = getAggregations(bucket, "totalUnused");
S totalUsed = getAggregations(bucket, "totalUsed");
Double used = Objects.requireNonNullElse(getValue(totalUsed), 0.0);
Double unused = Objects.requireNonNullElse(getValue(totalUnused), 0.0);
Double total = used + unused;
double usedPercentage = 0.0;
double unusedPercentage = 0.0;
if (total != 0.0) {
usedPercentage = used / total;
unusedPercentage = unused / total;
}
Optional<Double> used = getValue(totalUsed);
Optional<Double> unused = getValue(totalUnused);
Optional<Double> total = used.flatMap(u -> unused.map(uu -> u + uu));
Double usedPercentage = used.flatMap(u -> total.map(t -> u / t)).orElse(null);
Double unusedPercentage = unused.flatMap(uu -> total.map(t -> uu / t)).orElse(null);
data.add(
new AggregatedUsedVsUnusedAssetsCount()
.withTimestamp(timestamp)
.withUnused(unused)
.withUnused(unused.orElse(null))
.withUnusedPercentage(unusedPercentage)
.withUsed(used)
.withUsed(used.orElse(null))
.withUsedPercentage(usedPercentage));
}
return data;
Expand All @@ -51,5 +47,5 @@ public List<Object> aggregate() throws ParseException {

protected abstract S getAggregations(B bucket, String key);

protected abstract Double getValue(S aggregations);
protected abstract Optional<Double> getValue(S aggregations);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.openmetadata.schema.dataInsight.type.AggregatedUsedVsUnusedAssetsSize;

public abstract class AggregatedUsedvsUnusedAssetsSizeAggregator<A, H, B, S>
Expand All @@ -23,21 +23,17 @@ public List<Object> aggregate() throws ParseException {
Long timestamp = convertDatTimeStringToTimestamp(dateTimeString);
S totalUnused = getAggregations(bucket, "totalUnused");
S totalUsed = getAggregations(bucket, "totalUsed");
Double used = Objects.requireNonNullElse(getValue(totalUsed), 0.0);
Double unused = Objects.requireNonNullElse(getValue(totalUnused), 0.0);
Double total = used + unused;
double usedPercentage = 0.0;
double unusedPercentage = 0.0;
if (total != 0.0) {
usedPercentage = used / total;
unusedPercentage = unused / total;
}
Optional<Double> used = getValue(totalUsed);
Optional<Double> unused = getValue(totalUnused);
Optional<Double> total = used.flatMap(u -> unused.map(uu -> u + uu));
Double usedPercentage = used.flatMap(u -> total.map(t -> u / t)).orElse(null);
Double unusedPercentage = unused.flatMap(uu -> total.map(t -> uu / t)).orElse(null);
data.add(
new AggregatedUsedVsUnusedAssetsSize()
.withTimestamp(timestamp)
.withUnused(unused)
.withUnused(unused.orElse(null))
.withUnusedPercentage(unusedPercentage)
.withUsed(used)
.withUsed(used.orElse(null))
.withUsedPercentage(usedPercentage));
}
return data;
Expand All @@ -51,5 +47,5 @@ public List<Object> aggregate() throws ParseException {

protected abstract S getAggregations(B bucket, String key);

protected abstract Double getValue(S aggregations);
protected abstract Optional<Double> getValue(S aggregations);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.openmetadata.schema.dataInsight.type.PercentageOfEntitiesWithDescriptionByType;

public abstract class EntitiesDescriptionAggregator<A, B, M, S>
Expand All @@ -26,15 +27,18 @@ public List<Object> aggregate() throws ParseException {
S sumCompletedDescriptions =
getAggregations(entityTypeBucket, COMPLETED_DESCRIPTION_FRACTION);
S sumEntityCount = getAggregations(entityTypeBucket, ENTITY_COUNT);
Optional<Double> entityCount = getValue(sumEntityCount);
Optional<Double> completedDescription = getValue(sumCompletedDescriptions);
Double completedDescriptionFraction =
completedDescription.flatMap(cd -> entityCount.map(ec -> cd / ec)).orElse(null);

data.add(
new PercentageOfEntitiesWithDescriptionByType()
.withTimestamp(timestamp)
.withEntityType(entityType)
.withEntityCount(getValue(sumEntityCount))
.withCompletedDescription(getValue(sumCompletedDescriptions))
.withCompletedDescriptionFraction(
getValue(sumCompletedDescriptions) / getValue(sumEntityCount)));
.withEntityCount(entityCount.orElse(null))
.withCompletedDescription(completedDescription.orElse(null))
.withCompletedDescriptionFraction(completedDescriptionFraction));
}
}
return data;
Expand All @@ -50,5 +54,5 @@ public List<Object> aggregate() throws ParseException {

protected abstract S getAggregations(B bucket, String key);

protected abstract Double getValue(S aggregations);
protected abstract Optional<Double> getValue(S aggregations);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.openmetadata.schema.dataInsight.type.PercentageOfEntitiesWithOwnerByType;

public abstract class EntitiesOwnerAggregator<A, B, M, S>
Expand All @@ -25,20 +26,24 @@ public List<Object> aggregate() throws ParseException {
String entityType = getKeyAsString(entityTypeBucket);
S sumHasOwner = getAggregations(entityTypeBucket, HAS_OWNER_FRACTION);
S sumEntityCount = getAggregations(entityTypeBucket, ENTITY_COUNT);
Optional<Double> entityCount = getValue(sumEntityCount);
Optional<Double> hasOwner = getValue(sumHasOwner);
Double hasOwnerFraction =
hasOwner.flatMap(ho -> entityCount.map(ec -> ho / ec)).orElse(null);

data.add(
new PercentageOfEntitiesWithOwnerByType()
.withTimestamp(timestamp)
.withEntityType(entityType)
.withEntityCount(getValue(sumEntityCount))
.withHasOwner(getValue(sumHasOwner))
.withHasOwnerFraction(getValue(sumHasOwner) / getValue(sumEntityCount)));
.withEntityCount(entityCount.orElse(null))
.withHasOwner(hasOwner.orElse(null))
.withHasOwnerFraction(hasOwnerFraction));
}
}
return data;
}

protected abstract Double getValue(S sumEntityCount);
protected abstract Optional<Double> getValue(S sumEntityCount);

protected abstract S getAggregations(B entityTypeBucket, String key);

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

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.openmetadata.schema.dataInsight.type.MostActiveUsers;

public abstract class MostActiveUsersAggregator<A, B, M, S, X>
Expand All @@ -28,23 +29,32 @@ public List<Object> aggregate() {
// we'll assign the first team in the list if user belongs to multiple teams
team = getKeyAsString(getBuckets(teamBucket).get(0));
}
Optional<Long> lastSessionOptional = getMaxValue(lastSession);
Optional<Double> sumPageViewsOptional = getSumValue(sumPageViews);
Optional<Double> sumSessionDurationOptional = getSumValue(sumSessionDuration);
Optional<Double> sumSessionOptional = getSumValue(sumSession);
Double avgSessionDuration =
sumSessionDurationOptional
.flatMap(s -> sumSessionOptional.map(ss -> s / ss))
.orElse(null);

data.add(
new MostActiveUsers()
.withUserName(userName)
.withLastSession(getMaxValue(lastSession))
.withPageViews(getSumValue(sumPageViews))
.withSessionDuration(getSumValue(sumSessionDuration))
.withSessions(getSumValue(sumSession))
.withLastSession(lastSessionOptional.orElse(null))
.withPageViews(sumPageViewsOptional.orElse(null))
.withSessionDuration(sumSessionDurationOptional.orElse(null))
.withSessions(sumSessionOptional.orElse(null))
.withTeam(team)
.withAvgSessionDuration(getSumValue(sumSessionDuration) / getSumValue(sumSession)));
.withAvgSessionDuration(avgSessionDuration));
}

return data;
}

protected abstract Double getSumValue(S key);
protected abstract Optional<Double> getSumValue(S key);

protected abstract Long getMaxValue(X key);
protected abstract Optional<Long> getMaxValue(X key);

protected abstract String getKeyAsString(B bucket);

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

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.openmetadata.schema.dataInsight.type.MostViewedEntities;

public abstract class MostViewedEntitiesAggregator<A, B, M, S>
Expand All @@ -25,20 +26,21 @@ public List<Object> aggregate() {
String owner = getFirstValueFromBucketOrNull(ownerBucket);
String entityType = getFirstValueFromBucketOrNull(entityTypeBucket);
String entityHref = getFirstValueFromBucketOrNull(entityHrefBucket);
Optional<Double> pageViews = getValue(sumPageViews);

data.add(
new MostViewedEntities()
.withEntityFqn(tableFqn)
.withOwner(owner)
.withEntityType(entityType)
.withEntityHref(entityHref)
.withPageViews(getValue(sumPageViews)));
.withPageViews(pageViews.orElse(null)));
}

return data;
}

protected abstract Double getValue(S sumPageViews);
protected abstract Optional<Double> getValue(S sumPageViews);

protected abstract M getBucketAggregation(B bucket, String key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.openmetadata.schema.dataInsight.type.PageViewsByEntities;

public abstract class PageViewsByEntitiesAggregator<A, B, M, S>
Expand All @@ -24,18 +25,19 @@ public List<Object> aggregate() throws ParseException {
for (B entityTypeBucket : getBuckets(entityTypeBuckets)) {
String entityType = getKeyAsString(entityTypeBucket);
S sumPageViews = getSumAggregations(entityTypeBucket, "pageViews");
Optional<Double> pageViews = getValue(sumPageViews);

data.add(
new PageViewsByEntities()
.withEntityType(entityType)
.withTimestamp(timestamp)
.withPageViews(getValue(sumPageViews)));
.withPageViews(pageViews.orElse(null)));
}
}
return data;
}

protected abstract Double getValue(S key);
protected abstract Optional<Double> getValue(S key);

protected abstract S getSumAggregations(B bucket, String key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.openmetadata.schema.dataInsight.type.PercentageOfServicesWithDescription;

public abstract class ServicesDescriptionAggregator<A, B, M, S>
Expand All @@ -26,21 +27,24 @@ public List<Object> aggregate() throws ParseException {
S sumCompletedDescriptions =
getSumAggregations(serviceBucket, COMPLETED_DESCRIPTION_FRACTION);
S sumEntityCount = getSumAggregations(serviceBucket, ENTITY_COUNT);
Optional<Double> completedDescription = getValue(sumCompletedDescriptions);
Optional<Double> entityCount = getValue(sumEntityCount);
Double completedDescriptionFraction =
completedDescription.flatMap(cdf -> entityCount.map(ec -> cdf / ec)).orElse(null);

data.add(
new PercentageOfServicesWithDescription()
.withTimestamp(timestamp)
.withServiceName(serviceName)
.withEntityCount(getValue(sumEntityCount))
.withCompletedDescription(getValue(sumCompletedDescriptions))
.withCompletedDescriptionFraction(
getValue(sumCompletedDescriptions) / getValue(sumEntityCount)));
.withEntityCount(entityCount.orElse(null))
.withCompletedDescription(completedDescription.orElse(null))
.withCompletedDescriptionFraction(completedDescriptionFraction));
}
}
return data;
}

protected abstract Double getValue(S key);
protected abstract Optional<Double> getValue(S key);

protected abstract S getSumAggregations(B bucket, String key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.openmetadata.schema.dataInsight.type.PercentageOfServicesWithOwner;

public abstract class ServicesOwnerAggregator<A, B, M, S>
Expand All @@ -25,20 +26,24 @@ public List<Object> aggregate() throws ParseException {
String serviceName = getKeyAsString(serviceBucket);
S sumHasOwner = getSumAggregations(serviceBucket, HAS_OWNER_FRACTION);
S sumEntityCount = getSumAggregations(serviceBucket, ENTITY_COUNT);
Optional<Double> entityCount = getValue(sumEntityCount);
Optional<Double> hasOwner = getValue(sumHasOwner);
Double hasOwnerFraction =
hasOwner.flatMap(hof -> entityCount.map(ec -> hof / ec)).orElse(null);

data.add(
new PercentageOfServicesWithOwner()
.withTimestamp(timestamp)
.withServiceName(serviceName)
.withEntityCount(getValue(sumEntityCount))
.withHasOwner(getValue(sumHasOwner))
.withHasOwnerFraction(getValue(sumHasOwner) / getValue(sumEntityCount)));
.withEntityCount(entityCount.orElse(null))
.withHasOwner(hasOwner.orElse(null))
.withHasOwnerFraction(hasOwnerFraction));
}
}
return data;
}

protected abstract Double getValue(S key);
protected abstract Optional<Double> getValue(S key);

protected abstract S getSumAggregations(B bucket, String key);

Expand Down
Loading

0 comments on commit 2f679a9

Please sign in to comment.