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

org.openmetadata.service.dataInsight.TotalEntitiesByTierAggregator 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.TotalEntitiesByTier;

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

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

  @Override
  public List aggregate() throws ParseException {
    M timestampBuckets = getTimestampBuckets(this.aggregations);
    List data = new ArrayList<>();
    for (B timestampBucket : getBuckets(timestampBuckets)) {
      List timestampData = new ArrayList<>();
      double totalEntityCount = 0.0;
      String dateTimeString = getKeyAsString(timestampBucket);
      Long timestamp = convertDatTimeStringToTimestamp(dateTimeString);

      M entityTierBuckets = getEntityTierBuckets(timestampBucket);
      for (B entityTierBucket : getBuckets(entityTierBuckets)) {
        String entityTier = getKeyAsString(entityTierBucket);
        S sumEntityCount = getSumAggregations(entityTierBucket, ENTITY_COUNT);
        timestampData.add(
            new TotalEntitiesByTier()
                .withTimestamp(timestamp)
                .withEntityTier(entityTier)
                .withEntityCount(getValue(sumEntityCount)));
        totalEntityCount = totalEntityCount + getValue(sumEntityCount);
      }
      for (TotalEntitiesByTier el : timestampData) {
        if (totalEntityCount != 0.0) {
          el.withEntityCountFraction(el.getEntityCount() / totalEntityCount);
        } else {
          el.withEntityCountFraction(Double.NaN);
        }
        data.add((el));
      }
    }
    return data;
  }

  protected abstract Double getValue(S key);

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

  protected abstract M getEntityTierBuckets(B bucket);

  protected abstract String getKeyAsString(B bucket);

  protected abstract List getBuckets(M buckets);

  protected abstract M getTimestampBuckets(A aggregations);
}