Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] FIX 19311: New DI Email Template Implementation #19436

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,39 @@ private DataInsightTotalAssetTemplate createTotalAssetTemplate(

int changeInTotalAssets = (int) Math.abs(currentCount - previousCount);

Pair<Long, Long> previousWeekRange = Utilities.getPreviousWeekRange(timeConfig.endTime());
long previousWeekStart = previousWeekRange.getLeft();
long previousWeekEnd = previousWeekRange.getRight();

// data for that previous Monday–Sunday window
Map<String, Double> prevWeekDateWithCount =
getDateMapWithCountFromChart("total_data_assets", previousWeekStart, previousWeekEnd, team);
Comment on lines +227 to +229
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it mean we always compare to the previous (vs last 7 days).

e.g.
I get an alert on Wednesday, then the comparison will happen between Last Week (Monday - Sunday) vs Current Week (Monday - Tuesday)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get an alert on Wednesday, then the comparison will happen between Last Week (Monday - Sunday) vs Current Week (Monday - Tuesday)?

Yes @TeddyCr


// Sum the previous week's total and reuse the *current* week's map (dateWithCount) for
// comparison
double previousWeekTotal = sumMapValues(prevWeekDateWithCount);
double currentWeekTotal = sumMapValues(dateWithCount);

double weeklyDifference = Math.abs(currentWeekTotal - previousWeekTotal);
double weeklyGrowthPercent = 0.0;
if (previousWeekTotal != 0.0) {
weeklyGrowthPercent = ((currentWeekTotal - previousWeekTotal) / previousWeekTotal) * 100.0;
}

if (previousCount == 0D) {
// it should be undefined
return new DataInsightTotalAssetTemplate(
weeklyDifference,
weeklyGrowthPercent,
String.valueOf(currentCount.intValue()),
currentCount.intValue(),
0d,
timeConfig.numberOfDaysChange(),
dateMap);
} else {
return new DataInsightTotalAssetTemplate(
weeklyDifference,
weeklyGrowthPercent,
String.valueOf(currentCount.intValue()),
changeInTotalAssets,
((currentCount - previousCount) / previousCount) * 100,
Expand All @@ -238,6 +261,10 @@ private DataInsightTotalAssetTemplate createTotalAssetTemplate(
}
}

private double sumMapValues(Map<String, Double> map) {
return map.values().stream().mapToDouble(Double::doubleValue).sum();
}

private DataInsightDescriptionAndOwnerTemplate createDescriptionTemplate(
SearchClient searchClient,
String team,
Expand Down Expand Up @@ -278,6 +305,27 @@ private DataInsightDescriptionAndOwnerTemplate createDescriptionTemplate(

int changeCount = (int) Math.abs(currentCompletedDescription - previousCompletedDescription);

Pair<Long, Long> prevWeekRange = Utilities.getPreviousWeekRange(timeConfig.endTime());
long previousWeekStart = prevWeekRange.getLeft();
long previousWeekEnd = prevWeekRange.getRight();

// Query the "description KPI" for the PREVIOUS week
Map<String, Double> prevWeekDescData =
getDateMapWithCountFromChart(
"number_of_data_asset_with_description_kpi", previousWeekStart, previousWeekEnd, team);

double prevWeekDescTotal = sumMapValues(prevWeekDescData);
double currWeekDescTotal = sumMapValues(dateWithCount);

// Weekly difference (absolute)
double weeklyDifference = Math.abs(currWeekDescTotal - prevWeekDescTotal);

// Weekly growth %
double weeklyGrowthPercent = 0.0;
if (prevWeekDescTotal != 0.0) {
weeklyGrowthPercent = ((currWeekDescTotal - prevWeekDescTotal) / prevWeekDescTotal) * 100.0;
}

return getTemplate(
DataInsightDescriptionAndOwnerTemplate.MetricType.DESCRIPTION,
"percentage_of_data_asset_with_description_kpi",
Expand All @@ -286,7 +334,9 @@ private DataInsightDescriptionAndOwnerTemplate createDescriptionTemplate(
currentPercentCompleted - previousPercentCompleted,
currentCompletedDescription.intValue(),
timeConfig.numberOfDaysChange(),
dateMap);
dateMap,
weeklyDifference,
weeklyGrowthPercent);
}

private DataInsightDescriptionAndOwnerTemplate createOwnershipTemplate(
Expand Down Expand Up @@ -329,6 +379,24 @@ private DataInsightDescriptionAndOwnerTemplate createOwnershipTemplate(

int changeCount = (int) Math.abs(currentHasOwner - previousHasOwner);

Pair<Long, Long> prevWeekRange = Utilities.getPreviousWeekRange(timeConfig.endTime());
long previousWeekStart = prevWeekRange.getLeft();
long previousWeekEnd = prevWeekRange.getRight();

Map<String, Double> prevWeekOwnerData =
getDateMapWithCountFromChart(
"number_of_data_asset_with_owner_kpi", previousWeekStart, previousWeekEnd, team);

double prevWeekOwnerTotal = sumMapValues(prevWeekOwnerData);
double currWeekOwnerTotal = sumMapValues(dateWithCount);

double weeklyDifference = Math.abs(currWeekOwnerTotal - prevWeekOwnerTotal);

double weeklyGrowthPercent = 0.0;
if (prevWeekOwnerTotal != 0.0) {
weeklyGrowthPercent =
((currWeekOwnerTotal - prevWeekOwnerTotal) / prevWeekOwnerTotal) * 100.0;
}
return getTemplate(
DataInsightDescriptionAndOwnerTemplate.MetricType.OWNER,
"percentage_of_data_asset_with_owner_kpi",
Expand All @@ -337,7 +405,9 @@ private DataInsightDescriptionAndOwnerTemplate createOwnershipTemplate(
currentPercentCompleted - previousPercentCompleted,
currentHasOwner.intValue(),
timeConfig.numberOfDaysChange(),
dateMap);
dateMap,
weeklyDifference,
weeklyGrowthPercent);
}

private DataInsightDescriptionAndOwnerTemplate createTierTemplate(
Expand Down Expand Up @@ -381,7 +451,28 @@ private DataInsightDescriptionAndOwnerTemplate createTierTemplate(
// TODO: Understand if we actually use this tierData for anything.
Map<String, Double> tierData = new HashMap<>();

Pair<Long, Long> prevWeekRange = Utilities.getPreviousWeekRange(timeConfig.endTime());
long previousWeekStart = prevWeekRange.getLeft();
long previousWeekEnd = prevWeekRange.getRight();

Map<String, Double> prevWeekOwnerData =
getDateMapWithCountFromChart(
"total_data_assets_by_tier", previousWeekStart, previousWeekEnd, team);

double prevWeekOwnerTotal = sumMapValues(prevWeekOwnerData);
double currWeekOwnerTotal = sumMapValues(dateWithCount);

double weeklyDifference = Math.abs(currWeekOwnerTotal - prevWeekOwnerTotal);

double weeklyGrowthPercent = 0.0;
if (prevWeekOwnerTotal != 0.0) {
weeklyGrowthPercent =
((currWeekOwnerTotal - prevWeekOwnerTotal) / prevWeekOwnerTotal) * 100.0;
}

return new DataInsightDescriptionAndOwnerTemplate(
weeklyDifference,
weeklyGrowthPercent,
DataInsightDescriptionAndOwnerTemplate.MetricType.TIER,
null,
String.valueOf(currentHasTier.intValue()),
Expand Down Expand Up @@ -469,7 +560,9 @@ private DataInsightDescriptionAndOwnerTemplate getTemplate(
Double percentChange,
int totalAssets,
int numberOfDaysChange,
Map<String, Integer> dateMap) {
Map<String, Integer> dateMap,
double weeklyDifference,
double weeklyGrowthPercent) {

List<Kpi> kpiList = getAvailableKpi();
Kpi validKpi = null;
Expand Down Expand Up @@ -507,6 +600,8 @@ private DataInsightDescriptionAndOwnerTemplate getTemplate(
}

return new DataInsightDescriptionAndOwnerTemplate(
weeklyDifference,
weeklyGrowthPercent,
metricType,
criteria,
String.valueOf(totalAssets),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ public enum KpiCriteria {
@Setter private int numberOfDaysChange;
@Setter private Map<String, Double> tierMap;
@Setter private Map<String, Integer> dateMap;
@Getter @Setter double weeklyGrowth;
@Getter @Setter double weeklyGrowthPercent;

public DataInsightDescriptionAndOwnerTemplate(
double weeklyDifference,
double weeklyGrowthPercent,
MetricType metricType,
KpiCriteria criteria,
String totalAssets,
Expand All @@ -58,6 +62,8 @@ public DataInsightDescriptionAndOwnerTemplate(
int numberOfDaysChange,
Map<String, Double> tierMap,
Map<String, Integer> dateMap) {
this.weeklyGrowth = weeklyDifference;
this.weeklyGrowthPercent = weeklyGrowthPercent;
this.percentCompleted = String.format("%.2f", percentCompleted);
this.targetKpi = targetKpi;
this.changeCount = String.valueOf(changeCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ public class DataInsightTotalAssetTemplate {
@Setter private String completeMessage;
@Setter private int numberOfDaysChange;
@Setter private Map<String, Integer> dateMap;
@Getter @Setter double weeklyGrowth;
@Getter @Setter double weeklyGrowthPercent;

public DataInsightTotalAssetTemplate(
double weeklyDifference,
double weeklyGrowthPercent,
String totalDataAssets,
int assetsAddedOrRemoved,
Double percentChangeTotalAssets,
int numberOfDaysChange,
Map<String, Integer> dateMap) {
this.weeklyGrowth = weeklyDifference;
this.weeklyGrowthPercent = weeklyGrowthPercent;
this.totalDataAssets = totalDataAssets;
this.changeInTotalAssets = String.valueOf(assetsAddedOrRemoved);
this.percentChangeTotalAssets = String.format("%.2f", percentChangeTotalAssets);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package org.openmetadata.service.util;

import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.tuple.Pair;

public class Utilities {
private Utilities() {}
Expand All @@ -30,6 +35,29 @@ public static List<String> getLastSevenDays(long currentEpochTimestampInMilli) {
return lastSevenDays;
}

// Adjust endTime to Monday (previousOrSame) and define "previous" week as that Monday minus 1
// week.
public static Pair<Long, Long> getPreviousWeekRange(long endTimeMillis) {
// Define the previous week's time range (Monday to Sunday)
ZonedDateTime endZdt =
ZonedDateTime.ofInstant(Instant.ofEpochMilli(endTimeMillis), ZoneOffset.UTC);

// Current week's Monday
ZonedDateTime currentWeekMonday =
endZdt.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));

// Previous week's Monday
ZonedDateTime previousWeekMonday = currentWeekMonday.minusWeeks(1);

// Sunday = Monday + 6
ZonedDateTime previousWeekSunday = previousWeekMonday.plusDays(6);

long start = previousWeekMonday.toInstant().toEpochMilli();
long end = previousWeekSunday.toInstant().toEpochMilli();

return Pair.of(start, end);
}

public static String getMonthAndDateFromEpoch(long epochTimestamp) {
return getFormattedDateFromEpoch(epochTimestamp, "MMM d");
}
Expand Down

Large diffs are not rendered by default.

Loading