Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.glassfish.jersey.message.internal.OutboundJaxrsResponse;
import org.openmetadata.schema.EntityInterface;
import org.openmetadata.schema.dataInsight.custom.DataInsightCustomChart;
Expand Down Expand Up @@ -85,6 +86,37 @@ public class DataInsightSystemChartRepository extends EntityRepository<DataInsig

public static final String DI_SEARCH_INDEX = "di-data-assets-*";

/**
* Entity types written to the Data Insights indices that are governance artifacts rather than data
* assets, and so are left out of every "data assets" total.
*/
public static final List<String> NON_DATA_ASSET_ENTITY_TYPES =
List.of("tag", "glossaryTerm", "dataProduct");

/**
* Restricts a Data Insights chart to data-asset documents.
*
* <p>The {@code di-data-assets-*} pattern also resolves the data-quality aliases, whose documents
* are time series ({@code EntityTimeSeriesInterface}) rather than entities and therefore carry no
* {@code entityType}. Requiring that field to exist is what keeps them out of totals: a list of
* {@code must_not} term clauses cannot, because a document missing the field satisfies all of
* them. Breakdown charts grouped on {@code entityType.keyword} drop those documents already, so
* both sides must agree here or the summary card outruns the breakdown it sits above (#31478).
*/
public static final String DATA_ASSET_FILTER = buildDataAssetFilter();

private static String buildDataAssetFilter() {
String exclusions =
NON_DATA_ASSET_ENTITY_TYPES.stream()
.map(
entityType ->
String.format("{\"term\":{\"entityType.keyword\":\"%s\"}}", entityType))
.collect(Collectors.joining(","));
return String.format(
"{\"query\":{\"bool\":{\"must\":[{\"exists\":{\"field\":\"entityType\"}}],\"must_not\":[%s]}}}",
exclusions);
}

private static final Set IGNORE_OTHER_SERVICE_CHARTS =
Set.of(
"healthy_data_assets",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.openmetadata.service.migration.mysql.v1135;

import static org.openmetadata.service.migration.utils.v1135.DataInsightChartMigration.alignDataAssetChartScope;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.openmetadata.service.migration.api.MigrationProcessImpl;
import org.openmetadata.service.migration.utils.MigrationFile;

@Slf4j
public class Migration extends MigrationProcessImpl {

public Migration(MigrationFile migrationFile) {
super(migrationFile);
}

@Override
@SneakyThrows
public void runDataMigration() {
try {
alignDataAssetChartScope();
} catch (Exception e) {
LOG.error("v1135: failed to align the data asset scope of the Data Insights charts", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.openmetadata.service.migration.postgres.v1135;

import static org.openmetadata.service.migration.utils.v1135.DataInsightChartMigration.alignDataAssetChartScope;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.openmetadata.service.migration.api.MigrationProcessImpl;
import org.openmetadata.service.migration.utils.MigrationFile;

@Slf4j
public class Migration extends MigrationProcessImpl {

public Migration(MigrationFile migrationFile) {
super(migrationFile);
}

@Override
@SneakyThrows
public void runDataMigration() {
try {
alignDataAssetChartScope();
} catch (Exception e) {
LOG.error("v1135: failed to align the data asset scope of the Data Insights charts", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright 2021 Collate
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.openmetadata.service.migration.utils.v1135;

import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.openmetadata.schema.dataInsight.custom.DataInsightCustomChart;
import org.openmetadata.service.jdbi3.DataInsightSystemChartRepository;
import org.openmetadata.service.util.EntityUtil;

/**
* Realigns the system Data Insights charts that report on data assets so a summary card and the
* breakdown below it count the same documents.
*
* <p>The {@code di-data-assets-*} pattern the charts read also resolves the data-quality aliases,
* and those documents carry no {@code entityType}. A breakdown grouped on {@code
* entityType.keyword} drops them, while the summary cards' old {@code must_not} filter let them
* through, so the card always ran ahead of the chart by the number of test case results in the
* window (#31478). The charts were created back in 1.5.0 and every deployment already holds those
* definitions, so re-apply the scope to each affected chart.
*
* <p>Only the two scope-defining properties are rewritten. The metrics' formulas and the rest of
* each chart are left as stored, so a definition changed by a later migration survives.
*/
@Slf4j
public class DataInsightChartMigration {

private static final String METRICS_KEY = "metrics";
private static final String FILTER_KEY = "filter";
private static final String GROUP_BY_KEY = "groupBy";
private static final String EXCLUDE_GROUPS_KEY = "excludeGroups";
private static final String ENTITY_TYPE_GROUP_BY = "entityType.keyword";

/** System charts that report on data assets and must therefore share one scope. */
private static final List<String> DATA_ASSET_CHARTS =
List.of(
"total_data_assets",
"total_data_assets_by_tier",
"total_data_assets_summary_card",
"total_data_assets_with_tier_summary_card",
"percentage_of_data_asset_with_description",
"percentage_of_data_asset_with_owner",
"percentage_of_service_with_description",
"percentage_of_service_with_owner",
"data_assets_with_description_summary_card",
"data_assets_with_owner_summary_card",
"percentage_of_data_asset_with_description_kpi",
"percentage_of_data_asset_with_owner_kpi",
"number_of_data_asset_with_description_kpi",
"number_of_data_asset_with_owner_kpi");

private DataInsightChartMigration() {
/* Static migration entry point only */
}

public static void alignDataAssetChartScope() {
DataInsightSystemChartRepository repository = new DataInsightSystemChartRepository();
for (String chartName : DATA_ASSET_CHARTS) {
applyDataAssetScope(repository, chartName);
}
}

private static void applyDataAssetScope(
DataInsightSystemChartRepository repository, String chartName) {
try {
DataInsightCustomChart chart =
repository.getByName(null, chartName, EntityUtil.Fields.EMPTY_FIELDS);
Map<String, Object> chartDetails = asChartDetails(chart);
if (chartDetails == null) {
LOG.warn("Chart {} has no chart details, skipping data asset scope alignment", chartName);
} else {
scopeToDataAssets(chartDetails);
chart.setChartDetails(chartDetails);
repository.prepareInternal(chart, false);
repository.getDao().update(chart);
}
} catch (Exception ex) {
Comment thread
ulixius9 marked this conversation as resolved.
Resolved
LOG.warn("Failed to align data asset scope for chart {}: {}", chartName, ex.getMessage());
}
Comment thread
ulixius9 marked this conversation as resolved.
Resolved
Comment thread
ulixius9 marked this conversation as resolved.
Resolved
}

static void scopeToDataAssets(Map<String, Object> chartDetails) {
List<Map<String, Object>> metrics = asMetrics(chartDetails.get(METRICS_KEY));
if (metrics != null) {
for (Map<String, Object> metric : metrics) {
metric.put(FILTER_KEY, DataInsightSystemChartRepository.DATA_ASSET_FILTER);
}
}
if (ENTITY_TYPE_GROUP_BY.equals(chartDetails.get(GROUP_BY_KEY))) {
chartDetails.put(
EXCLUDE_GROUPS_KEY, DataInsightSystemChartRepository.NON_DATA_ASSET_ENTITY_TYPES);
}
}

@SuppressWarnings("unchecked")
private static Map<String, Object> asChartDetails(DataInsightCustomChart chart) {
Map<String, Object> chartDetails = null;
if (chart.getChartDetails() instanceof Map<?, ?> details) {
chartDetails = (Map<String, Object>) details;
}
return chartDetails;
}

@SuppressWarnings("unchecked")
private static List<Map<String, Object>> asMetrics(Object metrics) {
List<Map<String, Object>> typedMetrics = null;
if (metrics instanceof List<?> metricList) {
typedMetrics = (List<Map<String, Object>>) metricList;
}
return typedMetrics;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright 2024 Collate.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openmetadata.service.migration.utils.v1135;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.openmetadata.service.jdbi3.DataInsightSystemChartRepository;

/**
* Covers the transform the 1.13.5 migration applies to a stored chart definition. The definitions
* arrive as the maps they deserialize to out of the chart table, so the transform has to work on
* maps rather than the typed builders (issue #31478).
*/
class DataInsightChartMigrationTest {

@Test
void everyMetricGetsTheDataAssetFilter() {
Map<String, Object> chartDetails = chartDetails("entityType.keyword", 2);

DataInsightChartMigration.scopeToDataAssets(chartDetails);

for (Map<String, Object> metric : metricsOf(chartDetails)) {
assertEquals(DataInsightSystemChartRepository.DATA_ASSET_FILTER, metric.get("filter"));
}
}

@Test
void anExistingFilterIsReplacedRatherThanLeftStale() {
Map<String, Object> chartDetails = chartDetails("entityType.keyword", 1);
metricsOf(chartDetails).getFirst().put("filter", "{\"query\":{\"match_all\":{}}}");

DataInsightChartMigration.scopeToDataAssets(chartDetails);

assertEquals(
DataInsightSystemChartRepository.DATA_ASSET_FILTER,
metricsOf(chartDetails).getFirst().get("filter"));
}

@Test
void groupingOnEntityTypeAlsoExcludesGovernanceArtifacts() {
Map<String, Object> chartDetails = chartDetails("entityType.keyword", 1);

DataInsightChartMigration.scopeToDataAssets(chartDetails);

assertEquals(
DataInsightSystemChartRepository.NON_DATA_ASSET_ENTITY_TYPES,
chartDetails.get("excludeGroups"));
}

@Test
void groupingOnAnotherFieldKeepsItsOwnGroups() {
Map<String, Object> chartDetails = chartDetails("tier.keyword", 1);

DataInsightChartMigration.scopeToDataAssets(chartDetails);

assertFalse(
chartDetails.containsKey("excludeGroups"),
"excludeGroups lists entity types, so it must not be forced onto a tier breakdown");
assertEquals(
DataInsightSystemChartRepository.DATA_ASSET_FILTER,
metricsOf(chartDetails).getFirst().get("filter"));
}

@Test
void aSummaryCardWithoutAGroupByIsStillScoped() {
Map<String, Object> chartDetails = chartDetails(null, 1);

DataInsightChartMigration.scopeToDataAssets(chartDetails);

assertFalse(chartDetails.containsKey("excludeGroups"));
assertEquals(
DataInsightSystemChartRepository.DATA_ASSET_FILTER,
metricsOf(chartDetails).getFirst().get("filter"));
}

@Test
void aDefinitionWithNoMetricsIsLeftAlone() {
Map<String, Object> chartDetails = new LinkedHashMap<>();
chartDetails.put("type", "SummaryCard");

DataInsightChartMigration.scopeToDataAssets(chartDetails);

assertEquals(Map.of("type", "SummaryCard"), chartDetails);
}

@Test
void theFilterExcludesDocumentsWithoutAnEntityType() {
String filter = DataInsightSystemChartRepository.DATA_ASSET_FILTER;

assertTrue(
filter.contains("{\"exists\":{\"field\":\"entityType\"}}"),
"A must_not list alone cannot drop the data-quality documents: they have no entityType, "
+ "so they satisfy every must_not clause");
}

@SuppressWarnings("unchecked")
private static List<Map<String, Object>> metricsOf(Map<String, Object> chartDetails) {
return (List<Map<String, Object>>) chartDetails.get("metrics");
}

private static Map<String, Object> chartDetails(String groupBy, int metricCount) {
List<Map<String, Object>> metrics = new ArrayList<>();
for (int i = 0; i < metricCount; i++) {
Map<String, Object> metric = new HashMap<>();
metric.put("formula", "count(k='id.keyword')");
metrics.add(metric);
}
Map<String, Object> chartDetails = new LinkedHashMap<>();
chartDetails.put("metrics", metrics);
if (groupBy != null) {
chartDetails.put("groupBy", groupBy);
}
return chartDetails;
}
}
Loading
Loading