All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openmetadata.service.dataInsight.TotalEntitiesAggregator Maven / Gradle / Ivy

There is a newer version: 1.5.11
Show newest version
package org.openmetadata.service.dataInsight;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import org.openmetadata.schema.dataInsight.type.TotalEntitiesByType;

public abstract class TotalEntitiesAggregator
    implements DataInsightAggregatorInterface {
  private final A aggregations;

  protected TotalEntitiesAggregator(A aggregations) {
    this.aggregations = aggregations;
  }

  @Override
  public List aggregate() throws ParseException {
    M timestampBuckets = getTimestampBuckets(this.aggregations);
    List data = new ArrayList<>();
    List entityCount = new ArrayList<>();

    for (B timestampBucket : getBuckets(timestampBuckets)) {
      String dateTimeString = getKeyAsString(timestampBucket);
      Long timestamp = convertDatTimeStringToTimestamp(dateTimeString);
      M entityTypeBuckets = getEntityBuckets(timestampBucket);
      for (B entityTypeBucket : getBuckets(entityTypeBuckets)) {
        String entityType = getKeyAsString(entityTypeBucket);
        S sumEntityCount = getSumAggregations(entityTypeBucket, ENTITY_COUNT);

        data.add(
            new TotalEntitiesByType()
                .withTimestamp(timestamp)
                .withEntityType(entityType)
                .withEntityCount(getValue(sumEntityCount)));
        entityCount.add(getValue(sumEntityCount));
      }
    }

    double totalEntities = entityCount.stream().mapToDouble(v -> v).sum();
    for (Object o : data) {
      TotalEntitiesByType totalEntitiesByType = (TotalEntitiesByType) o;
      totalEntitiesByType.withEntityCountFraction(
          totalEntitiesByType.getEntityCount() / totalEntities);
    }

    return data;
  }

  protected abstract Double getValue(S key);

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

  protected abstract M getEntityBuckets(B bucket);

  protected abstract String getKeyAsString(B bucket);

  protected abstract List getBuckets(M buckets);

  protected abstract M getTimestampBuckets(A aggregations);
}