Skip to content

Commit

Permalink
feat(views): apply views to homepage entity counts & recommendations (d…
Browse files Browse the repository at this point in the history
…atahub-project#10283)

Co-authored-by: gaurav2733 <[email protected]>
  • Loading branch information
ksrinath and gaurav2733 authored Apr 18, 2024
1 parent a041a2e commit 91e3dc8
Show file tree
Hide file tree
Showing 25 changed files with 224 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import com.linkedin.datahub.graphql.generated.Dataset;
import com.linkedin.datahub.graphql.generated.DatasetStatsSummary;
import com.linkedin.datahub.graphql.generated.Domain;
import com.linkedin.datahub.graphql.generated.ERModelRelationship;
import com.linkedin.datahub.graphql.generated.ERModelRelationshipProperties;
import com.linkedin.datahub.graphql.generated.EntityPath;
import com.linkedin.datahub.graphql.generated.EntityRelationship;
Expand Down Expand Up @@ -987,8 +988,10 @@ private void configureQueryResolvers(final RuntimeWiring.Builder builder) {
.dataFetcher("listUsers", new ListUsersResolver(this.entityClient))
.dataFetcher("listGroups", new ListGroupsResolver(this.entityClient))
.dataFetcher(
"listRecommendations", new ListRecommendationsResolver(recommendationsService))
.dataFetcher("getEntityCounts", new EntityCountsResolver(this.entityClient))
"listRecommendations",
new ListRecommendationsResolver(recommendationsService, viewService))
.dataFetcher(
"getEntityCounts", new EntityCountsResolver(this.entityClient, viewService))
.dataFetcher("getAccessToken", new GetAccessTokenResolver(statefulTokenService))
.dataFetcher("listAccessTokens", new ListAccessTokensResolver(this.entityClient))
.dataFetcher(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.linkedin.datahub.graphql.resolvers;

import static com.linkedin.datahub.graphql.resolvers.search.SearchUtils.*;
import static com.linkedin.metadata.Constants.*;

import com.datahub.authentication.Authentication;
import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableSet;
import com.linkedin.common.urn.Urn;
import com.linkedin.common.urn.UrnUtils;
import com.linkedin.data.template.StringArray;
import com.linkedin.datahub.graphql.QueryContext;
import com.linkedin.datahub.graphql.exception.ValidationException;
import com.linkedin.datahub.graphql.generated.AndFilterInput;
import com.linkedin.datahub.graphql.generated.FacetFilterInput;
import com.linkedin.datahub.graphql.resolvers.search.SearchUtils;
import com.linkedin.metadata.query.filter.Condition;
import com.linkedin.metadata.query.filter.ConjunctiveCriterion;
import com.linkedin.metadata.query.filter.ConjunctiveCriterionArray;
Expand All @@ -20,7 +23,10 @@
import com.linkedin.metadata.query.filter.Filter;
import com.linkedin.metadata.search.utils.ESUtils;
import com.linkedin.metadata.search.utils.QueryUtils;
import com.linkedin.metadata.service.ViewService;
import com.linkedin.view.DataHubViewInfo;
import graphql.schema.DataFetchingEnvironment;
import io.datahubproject.metadata.context.OperationContext;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -226,4 +232,14 @@ public static Filter buildFilterWithUrns(@Nonnull Set<Urn> urns, @Nullable Filte
}
return QueryUtils.newFilter(urnMatchCriterion);
}

public static Filter viewFilter(
OperationContext opContext, ViewService viewService, String viewUrn) {
if (viewUrn == null) {
return null;
}
DataHubViewInfo viewInfo = resolveView(opContext, viewService, UrnUtils.getUrn(viewUrn));
Filter result = SearchUtils.combineFilters(null, viewInfo.getDefinition().getFilter());
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.linkedin.datahub.graphql.generated.EntityCountResults;
import com.linkedin.datahub.graphql.types.entitytype.EntityTypeMapper;
import com.linkedin.entity.client.EntityClient;
import com.linkedin.metadata.service.ViewService;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import io.opentelemetry.extension.annotations.WithSpan;
Expand All @@ -20,8 +21,11 @@ public class EntityCountsResolver implements DataFetcher<CompletableFuture<Entit

private final EntityClient _entityClient;

public EntityCountsResolver(final EntityClient entityClient) {
private final ViewService _viewService;

public EntityCountsResolver(final EntityClient entityClient, final ViewService viewService) {
_entityClient = entityClient;
_viewService = viewService;
}

@Override
Expand All @@ -44,7 +48,8 @@ public CompletableFuture<EntityCountResults> get(final DataFetchingEnvironment e
context.getOperationContext(),
input.getTypes().stream()
.map(EntityTypeMapper::getName)
.collect(Collectors.toList()));
.collect(Collectors.toList()),
viewFilter(context.getOperationContext(), _viewService, input.getViewUrn()));

// bind to a result.
List<EntityCountResult> resultList =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.linkedin.metadata.recommendation.EntityRequestContext;
import com.linkedin.metadata.recommendation.RecommendationsService;
import com.linkedin.metadata.recommendation.SearchRequestContext;
import com.linkedin.metadata.service.ViewService;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import io.opentelemetry.extension.annotations.WithSpan;
Expand All @@ -44,6 +45,7 @@ public class ListRecommendationsResolver
new ListRecommendationsResult(Collections.emptyList());

private final RecommendationsService _recommendationsService;
private final ViewService _viewService;

@WithSpan
@Override
Expand All @@ -60,6 +62,7 @@ public CompletableFuture<ListRecommendationsResult> get(DataFetchingEnvironment
_recommendationsService.listRecommendations(
context.getOperationContext(),
mapRequestContext(input.getRequestContext()),
viewFilter(context.getOperationContext(), _viewService, input.getViewUrn()),
input.getLimit());
return ListRecommendationsResult.builder()
.setModules(
Expand Down
5 changes: 5 additions & 0 deletions datahub-graphql-core/src/main/resources/entity.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,11 @@ Input for the get entity counts endpoint
"""
input EntityCountInput {
types: [EntityType!]

"""
Optional - A View to apply when generating results
"""
viewUrn: String
}

"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ input ListRecommendationsInput {
Max number of modules to return
"""
limit: Int

"""
Optional - A View to apply when generating results
"""
viewUrn: String
}

"""
Expand Down
6 changes: 6 additions & 0 deletions datahub-web-react/src/app/home/HomePageRecommendations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from '../onboarding/config/HomePageOnboardingConfig';
import { useToggleEducationStepIdsAllowList } from '../onboarding/useToggleEducationStepIdsAllowList';
import { useBusinessAttributesFlag } from '../useAppConfig';
import { useUserContext } from '../context/useUserContext';

const PLATFORMS_MODULE_ID = 'Platforms';
const MOST_POPULAR_MODULE_ID = 'HighUsageEntities';
Expand Down Expand Up @@ -105,6 +106,9 @@ export const HomePageRecommendations = ({ user }: Props) => {
const browseEntityList = entityRegistry.getBrowseEntityTypes();
const userUrn = user?.urn;

const userContext = useUserContext();
const viewUrn = userContext.localState?.selectedViewUrn;

const businessAttributesFlag = useBusinessAttributesFlag();

const showSimplifiedHomepage = user?.settings?.appearance?.showSimplifiedHomepage;
Expand All @@ -113,6 +117,7 @@ export const HomePageRecommendations = ({ user }: Props) => {
variables: {
input: {
types: browseEntityList,
viewUrn
},
},
});
Expand All @@ -133,6 +138,7 @@ export const HomePageRecommendations = ({ user }: Props) => {
scenario,
},
limit: 10,
viewUrn
},
},
fetchPolicy: 'no-cache',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,9 +571,11 @@ public void setWritable(@Nonnull OperationContext opContext, boolean canWrite)
@Override
@Nonnull
public Map<String, Long> batchGetTotalEntityCount(
@Nonnull OperationContext opContext, @Nonnull List<String> entityNames)
@Nonnull OperationContext opContext,
@Nonnull List<String> entityNames,
@Nullable Filter filter)
throws RemoteInvocationException {
return searchService.docCountPerEntity(opContext, entityNames);
return searchService.docCountPerEntity(opContext, entityNames, filter);
}

/** List all urns existing for a particular Entity type. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.linkedin.metadata.datahubusage.DataHubUsageEventConstants;
import com.linkedin.metadata.datahubusage.DataHubUsageEventType;
import com.linkedin.metadata.entity.EntityService;
import com.linkedin.metadata.query.filter.Filter;
import com.linkedin.metadata.recommendation.RecommendationContent;
import com.linkedin.metadata.recommendation.RecommendationRenderType;
import com.linkedin.metadata.recommendation.RecommendationRequestContext;
Expand All @@ -23,6 +24,7 @@
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.opensearch.action.search.SearchRequest;
Expand Down Expand Up @@ -99,7 +101,9 @@ public boolean isEligible(
@Override
@WithSpan
public List<RecommendationContent> getRecommendations(
@Nonnull OperationContext opContext, @Nonnull RecommendationRequestContext requestContext) {
@Nonnull OperationContext opContext,
@Nonnull RecommendationRequestContext requestContext,
@Nullable Filter filter) {
SearchRequest searchRequest = buildSearchRequest(opContext);
try (Timer.Context ignored = MetricUtils.timer(this.getClass(), "getMostPopular").time()) {
final SearchResponse searchResponse =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.linkedin.metadata.datahubusage.DataHubUsageEventConstants;
import com.linkedin.metadata.datahubusage.DataHubUsageEventType;
import com.linkedin.metadata.entity.EntityService;
import com.linkedin.metadata.query.filter.Filter;
import com.linkedin.metadata.recommendation.RecommendationContent;
import com.linkedin.metadata.recommendation.RecommendationRenderType;
import com.linkedin.metadata.recommendation.RecommendationRequestContext;
Expand All @@ -22,6 +23,7 @@
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.opensearch.action.search.SearchRequest;
Expand Down Expand Up @@ -98,7 +100,9 @@ public boolean isEligible(
@Override
@WithSpan
public List<RecommendationContent> getRecommendations(
@Nonnull OperationContext opContext, @Nonnull RecommendationRequestContext requestContext) {
@Nonnull OperationContext opContext,
@Nonnull RecommendationRequestContext requestContext,
@Nullable Filter filter) {
SearchRequest searchRequest =
buildSearchRequest(opContext.getSessionActorContext().getActorUrn());
try (Timer.Context ignored = MetricUtils.timer(this.getClass(), "getRecentlyEdited").time()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.linkedin.metadata.datahubusage.DataHubUsageEventConstants;
import com.linkedin.metadata.datahubusage.DataHubUsageEventType;
import com.linkedin.metadata.entity.EntityService;
import com.linkedin.metadata.query.filter.Filter;
import com.linkedin.metadata.recommendation.RecommendationContent;
import com.linkedin.metadata.recommendation.RecommendationRenderType;
import com.linkedin.metadata.recommendation.RecommendationRequestContext;
Expand All @@ -22,6 +23,7 @@
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.opensearch.action.search.SearchRequest;
Expand Down Expand Up @@ -98,7 +100,9 @@ public boolean isEligible(
@Override
@WithSpan
public List<RecommendationContent> getRecommendations(
@Nonnull OperationContext opContext, @Nonnull RecommendationRequestContext requestContext) {
@Nonnull OperationContext opContext,
@Nonnull RecommendationRequestContext requestContext,
@Nullable Filter filter) {
SearchRequest searchRequest =
buildSearchRequest(opContext.getSessionActorContext().getActorUrn());
try (Timer.Context ignored = MetricUtils.timer(this.getClass(), "getRecentlyViewed").time()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,20 @@ public SearchService(

public Map<String, Long> docCountPerEntity(
@Nonnull OperationContext opContext, @Nonnull List<String> entityNames) {
return docCountPerEntity(opContext, entityNames, null);
}

public Map<String, Long> docCountPerEntity(
@Nonnull OperationContext opContext,
@Nonnull List<String> entityNames,
@Nullable Filter filter) {
return getEntitiesToSearch(opContext, entityNames, 0).stream()
.collect(
Collectors.toMap(
Function.identity(),
entityName ->
_entityDocCountCache
.getEntityDocCount(opContext)
.getEntityDocCount(opContext, filter)
.getOrDefault(entityName.toLowerCase(), 0L)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.base.Suppliers;
import com.linkedin.metadata.config.cache.EntityDocCountCacheConfiguration;
import com.linkedin.metadata.models.registry.EntityRegistry;
import com.linkedin.metadata.query.filter.Filter;
import com.linkedin.metadata.search.EntitySearchService;
import com.linkedin.metadata.utils.ConcurrencyUtils;
import io.datahubproject.metadata.context.OperationContext;
Expand All @@ -15,12 +16,22 @@
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;

public class EntityDocCountCache {
private final EntityRegistry entityRegistry;
private final EntitySearchService entitySearchService;
private final EntityDocCountCacheConfiguration config;
private final Map<String, Supplier<Map<String, Long>>> entityDocCounts;
private final Map<EntityDocCountsKey, Supplier<Map<String, Long>>> entityDocCounts;

@AllArgsConstructor
@EqualsAndHashCode
private static final class EntityDocCountsKey {
private final String searchContextId;
private final Filter filter;
}

public EntityDocCountCache(
EntityRegistry entityRegistry,
Expand All @@ -32,17 +43,27 @@ public EntityDocCountCache(
this.entityDocCounts = new ConcurrentHashMap<>();
}

private Map<String, Long> fetchEntityDocCount(@Nonnull OperationContext opContext) {
private Map<String, Long> fetchEntityDocCount(
@Nonnull OperationContext opContext, @Nullable Filter filter) {
return ConcurrencyUtils.transformAndCollectAsync(
entityRegistry.getEntitySpecs().keySet(),
Function.identity(),
Collectors.toMap(Function.identity(), v -> entitySearchService.docCount(opContext, v)));
Collectors.toMap(
Function.identity(), v -> entitySearchService.docCount(opContext, v, filter)));
}

@WithSpan
public Map<String, Long> getEntityDocCount(@Nonnull OperationContext opContext) {
return getEntityDocCount(opContext, null);
}

@WithSpan
public Map<String, Long> getEntityDocCount(
@Nonnull OperationContext opContext, @Nullable Filter filter) {
return entityDocCounts
.computeIfAbsent(opContext.getSearchContextId(), k -> buildSupplier(opContext))
.computeIfAbsent(
new EntityDocCountsKey(opContext.getSearchContextId(), filter),
k -> buildSupplier(opContext, filter))
.get();
}

Expand All @@ -53,8 +74,9 @@ public List<String> getNonEmptyEntities(@Nonnull OperationContext opContext) {
.collect(Collectors.toList());
}

private Supplier<Map<String, Long>> buildSupplier(@Nonnull OperationContext opContext) {
private Supplier<Map<String, Long>> buildSupplier(
@Nonnull OperationContext opContext, @Nullable Filter filter) {
return Suppliers.memoizeWithExpiration(
() -> fetchEntityDocCount(opContext), config.getTtlSeconds(), TimeUnit.SECONDS);
() -> fetchEntityDocCount(opContext, filter), config.getTtlSeconds(), TimeUnit.SECONDS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ public void clear(@Nonnull OperationContext opContext) {
}

@Override
public long docCount(@Nonnull OperationContext opContext, @Nonnull String entityName) {
public long docCount(
@Nonnull OperationContext opContext, @Nonnull String entityName, @Nullable Filter filter) {
return esSearchDAO.docCount(
opContext.withSearchFlags(
flags -> applyDefaultSearchFlags(flags, null, DEFAULT_SERVICE_SEARCH_FLAGS)),
entityName);
entityName,
filter);
}

@Override
Expand Down
Loading

0 comments on commit 91e3dc8

Please sign in to comment.