Skip to content

Commit

Permalink
perf: Do not query dataset counts
Browse files Browse the repository at this point in the history
...as we can compute them dynamically. This commit also fixes incorrect dataset counts in some cases.
  • Loading branch information
bprusinowski committed Oct 25, 2023
1 parent 6143c45 commit c82f421
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 212 deletions.
45 changes: 23 additions & 22 deletions app/browser/dataset-browse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ import {
useBrowseContext,
} from "./context";
import { BrowseFilter } from "./filters";
import useDatasetCount from "./use-dataset-count";

export const SearchDatasetInput = ({
browseState,
Expand Down Expand Up @@ -433,7 +432,7 @@ export const Subthemes = ({
}: {
organization: DataCubeOrganization;
filters: BrowseFilter[];
counts: ReturnType<typeof useDatasetCount>;
counts: Record<string, number>;
}) => {
const termsetIri = organizationIriToTermsetParentIri[organization.iri];
const { dataSource } = useDataSourceStore();
Expand Down Expand Up @@ -606,7 +605,7 @@ const NavSection = ({
export const SearchFilters = ({ data }: { data?: DataCubesQuery }) => {
const { dataSource } = useDataSourceStore();
const locale = useLocale();
const { filters, search, includeDrafts } = useBrowseContext();
const { filters } = useBrowseContext();
const [{ data: allThemes }] = useThemesQuery({
variables: {
sourceType: dataSource.type,
Expand All @@ -622,29 +621,23 @@ export const SearchFilters = ({ data }: { data?: DataCubesQuery }) => {
},
});

const allCounts = useDatasetCount(filters, includeDrafts);
const resultsCounts = useMemo(() => {
const counts = useMemo(() => {
if (!data?.dataCubes) {
return {};
} else {
const res = {} as Record<string, number>;
for (const cube of data.dataCubes) {
const countables = [
...cube.dataCube.themes,
cube.dataCube.creator,
].filter(truthy);
for (const item of countables) {
res[item.iri] = res[item.iri] || 0;
res[item.iri] += 1;
}
}

const result: Record<string, number> = {};

for (const { dataCube } of data.dataCubes) {
const countables = [...dataCube.themes, dataCube.creator].filter(truthy);

for (const { iri } of countables) {
result[iri] = (result[iri] ?? 0) + 1;
}
return res;
}
}, [data]);
const total = Object.values(resultsCounts).reduce((acc, n) => acc + n, 0);

const counts =
search && search != "" && total > 0 ? resultsCounts : allCounts;
return result;
}, [data?.dataCubes]);

const themeFilter = filters.find(isAttrEqual("__typename", "DataCubeTheme"));
const orgFilter = filters.find(
Expand All @@ -662,25 +655,31 @@ export const SearchFilters = ({ data }: { data?: DataCubesQuery }) => {
if (!theme.label) {
return false;
}

if (!counts[theme.iri]) {
return false;
}

if (themeFilter && themeFilter !== theme) {
return false;
}

return true;
});

const displayedOrgs = allOrgsAlpha?.filter((org) => {
if (!org.label) {
return false;
}

if (!counts[org.iri] && orgFilter !== org) {
return false;
}

if (orgFilter && orgFilter !== org) {
return false;
}

return true;
});

Expand Down Expand Up @@ -727,7 +726,9 @@ export const SearchFilters = ({ data }: { data?: DataCubesQuery }) => {
}
/>
) : null;

let navs = [themeNav, orgNav];

if (filters[0]?.__typename === "DataCubeTheme") {
navs = [themeNav, orgNav];
} else if (filters[0]?.__typename === "DataCubeOrganization") {
Expand All @@ -745,7 +746,6 @@ export const SearchFilters = ({ data }: { data?: DataCubesQuery }) => {
role="search"
key={filters.length}
>
{/* Theme tree */}
<Stack spacing={5}>
{navs[0]}
{navs[1]}
Expand All @@ -762,6 +762,7 @@ export const DatasetResults = ({
query: UseQueryState<DataCubesQuery>;
}) => {
const { fetching, data, error } = query;

if (fetching) {
return (
<Box sx={{ alignItems: "center" }}>
Expand Down
38 changes: 0 additions & 38 deletions app/browser/use-dataset-count.tsx

This file was deleted.

21 changes: 0 additions & 21 deletions app/graphql/queries/data-cubes.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -475,24 +475,3 @@ query DimensionHierarchy(
}
}
}

query DatasetCount(
$sourceType: String!
$sourceUrl: String!
$theme: String
$organization: String
$subtheme: String
$includeDrafts: Boolean
) {
datasetcount(
sourceType: $sourceType
sourceUrl: $sourceUrl
theme: $theme
organization: $organization
subtheme: $subtheme
includeDrafts: $includeDrafts
) {
count
iri
}
}
48 changes: 0 additions & 48 deletions app/graphql/query-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,6 @@ export type DataCubeTheme = {
label?: Maybe<Scalars['String']>;
};

export type DatasetCount = {
__typename: 'DatasetCount';
iri: Scalars['String'];
count: Scalars['Int'];
};

export type Dimension = {
iri: Scalars['String'];
label: Scalars['String'];
Expand Down Expand Up @@ -394,7 +388,6 @@ export type Query = {
themes: Array<DataCubeTheme>;
subthemes: Array<DataCubeTheme>;
organizations: Array<DataCubeOrganization>;
datasetcount?: Maybe<Array<DatasetCount>>;
};


Expand Down Expand Up @@ -451,16 +444,6 @@ export type QueryOrganizationsArgs = {
};


export type QueryDatasetcountArgs = {
sourceType: Scalars['String'];
sourceUrl: Scalars['String'];
theme?: Maybe<Scalars['String']>;
organization?: Maybe<Scalars['String']>;
subtheme?: Maybe<Scalars['String']>;
includeDrafts?: Maybe<Scalars['Boolean']>;
};


export type RelatedDimension = {
__typename: 'RelatedDimension';
type: Scalars['String'];
Expand Down Expand Up @@ -1146,18 +1129,6 @@ export type DimensionHierarchyQuery = { __typename: 'Query', dataCubeByIri?: May
& HierarchyMetadata_TemporalOrdinalDimension_Fragment
)> }> };

export type DatasetCountQueryVariables = Exact<{
sourceType: Scalars['String'];
sourceUrl: Scalars['String'];
theme?: Maybe<Scalars['String']>;
organization?: Maybe<Scalars['String']>;
subtheme?: Maybe<Scalars['String']>;
includeDrafts?: Maybe<Scalars['Boolean']>;
}>;


export type DatasetCountQuery = { __typename: 'Query', datasetcount?: Maybe<Array<{ __typename: 'DatasetCount', count: number, iri: string }>> };

export const DimensionMetadataFragmentDoc = gql`
fragment dimensionMetadata on Dimension {
iri
Expand Down Expand Up @@ -1615,23 +1586,4 @@ export const DimensionHierarchyDocument = gql`

export function useDimensionHierarchyQuery(options: Omit<Urql.UseQueryArgs<DimensionHierarchyQueryVariables>, 'query'> = {}) {
return Urql.useQuery<DimensionHierarchyQuery>({ query: DimensionHierarchyDocument, ...options });
};
export const DatasetCountDocument = gql`
query DatasetCount($sourceType: String!, $sourceUrl: String!, $theme: String, $organization: String, $subtheme: String, $includeDrafts: Boolean) {
datasetcount(
sourceType: $sourceType
sourceUrl: $sourceUrl
theme: $theme
organization: $organization
subtheme: $subtheme
includeDrafts: $includeDrafts
) {
count
iri
}
}
`;

export function useDatasetCountQuery(options: Omit<Urql.UseQueryArgs<DatasetCountQueryVariables>, 'query'> = {}) {
return Urql.useQuery<DatasetCountQuery>({ query: DatasetCountDocument, ...options });
};
27 changes: 0 additions & 27 deletions app/graphql/resolver-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,6 @@ export type DataCubeTheme = {
label?: Maybe<Scalars['String']>;
};

export type DatasetCount = {
__typename?: 'DatasetCount';
iri: Scalars['String'];
count: Scalars['Int'];
};

export type Dimension = {
iri: Scalars['String'];
label: Scalars['String'];
Expand Down Expand Up @@ -396,7 +390,6 @@ export type Query = {
themes: Array<DataCubeTheme>;
subthemes: Array<DataCubeTheme>;
organizations: Array<DataCubeOrganization>;
datasetcount?: Maybe<Array<DatasetCount>>;
};


Expand Down Expand Up @@ -453,16 +446,6 @@ export type QueryOrganizationsArgs = {
};


export type QueryDatasetcountArgs = {
sourceType: Scalars['String'];
sourceUrl: Scalars['String'];
theme?: Maybe<Scalars['String']>;
organization?: Maybe<Scalars['String']>;
subtheme?: Maybe<Scalars['String']>;
includeDrafts?: Maybe<Scalars['Boolean']>;
};


export type RelatedDimension = {
__typename?: 'RelatedDimension';
type: Scalars['String'];
Expand Down Expand Up @@ -657,7 +640,6 @@ export type ResolversTypes = ResolversObject<{
DataCubeResultOrder: DataCubeResultOrder;
DataCubeSearchFilter: DataCubeSearchFilter;
DataCubeTheme: ResolverTypeWrapper<DataCubeTheme>;
DatasetCount: ResolverTypeWrapper<DatasetCount>;
Dimension: ResolverTypeWrapper<ResolvedDimension>;
DimensionValue: ResolverTypeWrapper<Scalars['DimensionValue']>;
FilterValue: ResolverTypeWrapper<Scalars['FilterValue']>;
Expand Down Expand Up @@ -698,7 +680,6 @@ export type ResolversParentTypes = ResolversObject<{
Float: Scalars['Float'];
DataCubeSearchFilter: DataCubeSearchFilter;
DataCubeTheme: DataCubeTheme;
DatasetCount: DatasetCount;
Dimension: ResolvedDimension;
DimensionValue: Scalars['DimensionValue'];
FilterValue: Scalars['FilterValue'];
Expand Down Expand Up @@ -770,12 +751,6 @@ export type DataCubeThemeResolvers<ContextType = VisualizeGraphQLContext, Parent
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
}>;

export type DatasetCountResolvers<ContextType = VisualizeGraphQLContext, ParentType extends ResolversParentTypes['DatasetCount'] = ResolversParentTypes['DatasetCount']> = ResolversObject<{
iri?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
count?: Resolver<ResolversTypes['Int'], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
}>;

export type DimensionResolvers<ContextType = VisualizeGraphQLContext, ParentType extends ResolversParentTypes['Dimension'] = ResolversParentTypes['Dimension']> = ResolversObject<{
__resolveType: TypeResolveFn<'GeoCoordinatesDimension' | 'GeoShapesDimension' | 'NominalDimension' | 'NumericalMeasure' | 'OrdinalDimension' | 'OrdinalMeasure' | 'StandardErrorDimension' | 'TemporalDimension' | 'TemporalOrdinalDimension', ParentType, ContextType>;
iri?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
Expand Down Expand Up @@ -961,7 +936,6 @@ export type QueryResolvers<ContextType = VisualizeGraphQLContext, ParentType ext
themes?: Resolver<Array<ResolversTypes['DataCubeTheme']>, ParentType, ContextType, RequireFields<QueryThemesArgs, 'sourceType' | 'sourceUrl' | 'locale'>>;
subthemes?: Resolver<Array<ResolversTypes['DataCubeTheme']>, ParentType, ContextType, RequireFields<QuerySubthemesArgs, 'sourceType' | 'sourceUrl' | 'locale' | 'parentIri'>>;
organizations?: Resolver<Array<ResolversTypes['DataCubeOrganization']>, ParentType, ContextType, RequireFields<QueryOrganizationsArgs, 'sourceType' | 'sourceUrl' | 'locale'>>;
datasetcount?: Resolver<Maybe<Array<ResolversTypes['DatasetCount']>>, ParentType, ContextType, RequireFields<QueryDatasetcountArgs, 'sourceType' | 'sourceUrl'>>;
}>;

export interface RawObservationScalarConfig extends GraphQLScalarTypeConfig<ResolversTypes['RawObservation'], any> {
Expand Down Expand Up @@ -1037,7 +1011,6 @@ export type Resolvers<ContextType = VisualizeGraphQLContext> = ResolversObject<{
DataCubeOrganization?: DataCubeOrganizationResolvers<ContextType>;
DataCubeResult?: DataCubeResultResolvers<ContextType>;
DataCubeTheme?: DataCubeThemeResolvers<ContextType>;
DatasetCount?: DatasetCountResolvers<ContextType>;
Dimension?: DimensionResolvers<ContextType>;
DimensionValue?: GraphQLScalarType;
FilterValue?: GraphQLScalarType;
Expand Down
4 changes: 0 additions & 4 deletions app/graphql/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ export const Query: QueryResolvers = {
const source = getSource(args.sourceType);
return await source.organizations(parent, args, context, info);
},
datasetcount: async (parent, args, context, info) => {
const source = getSource(args.sourceType);
return await source.datasetcount(parent, args, context, info);
},
};

const DataCube: DataCubeResolvers = {
Expand Down
Loading

0 comments on commit c82f421

Please sign in to comment.