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

feat(): add executed query length to search response #1733

Merged
merged 6 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion packages/common/src/search/_internal/portalSearchGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
IHubSearchResult,
IQuery,
} from "../types";
import { getNextFunction } from "../utils";
import { getNextFunction, getKilobyteSizeOfQuery } from "../utils";
import { expandPredicate } from "./expandPredicate";

/**
Expand Down Expand Up @@ -106,6 +106,7 @@ async function searchPortal(
resp.total,
searchPortal
),
executedQuerySize: getKilobyteSizeOfQuery(searchOptions.q),
};
}

Expand Down
8 changes: 7 additions & 1 deletion packages/common/src/search/_internal/portalSearchItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import {
IPredicate,
IQuery,
} from "../types";
import { addDefaultItemSearchPredicates, getNextFunction } from "../utils";
import {
addDefaultItemSearchPredicates,
getNextFunction,
getKilobyteSizeOfQuery,
} from "../utils";
import { convertPortalAggregations } from "./portalSearchUtils";
import { expandPredicate } from "./expandPredicate";
import HubError from "../../HubError";
Expand Down Expand Up @@ -166,6 +170,7 @@ async function searchPortalAsItem(
resp.total,
searchPortalAsItem
),
executedQuerySize: getKilobyteSizeOfQuery(searchOptions.q),
};
}

Expand Down Expand Up @@ -209,6 +214,7 @@ async function searchPortalAsHubSearchResult(
resp.total,
searchPortalAsHubSearchResult
),
executedQuerySize: getKilobyteSizeOfQuery(searchOptions.q),
};
}

Expand Down
3 changes: 3 additions & 0 deletions packages/common/src/search/_internal/portalSearchUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { getNextFunction } from "../utils";
import { expandPredicate } from "./expandPredicate";
import { cloneObject } from "../../util";
import { getKilobyteSizeOfQuery } from "../utils";

function buildSearchOptions(
query: IQuery,
Expand Down Expand Up @@ -203,6 +204,7 @@ async function searchPortal(
resp.total,
searchPortal
),
executedQuerySize: getKilobyteSizeOfQuery(searchOptions.q),
};
}

Expand Down Expand Up @@ -232,6 +234,7 @@ async function searchCommunity(
resp.total,
searchCommunity
),
executedQuerySize: getKilobyteSizeOfQuery(searchOptions.q),
};
}

Expand Down
7 changes: 7 additions & 0 deletions packages/common/src/search/types/IHubSearchResponse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IMessage } from "../../types/IMessage";
import { IHubAggregation } from "./IHubAggregation";
import { Kilobyte } from "./types";

/**
* Defines a generic search response interface with parameterized result type
Expand Down Expand Up @@ -36,4 +37,10 @@ export interface IHubSearchResponse<T> {
* Array of messages / warnings
*/
messages?: IMessage[];

/**
* The length of the query string that was just executed in the search,
* measured in kilobytes
*/
executedQuerySize?: Kilobyte;
}
7 changes: 7 additions & 0 deletions packages/common/src/search/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,10 @@ export interface ICatalogSearchResponse {

export interface ISearchResponseHash
extends Record<string, IHubSearchResponse<IHubSearchResult>> {}

/**
* Type wrapper for a kilobyte
* This is complete syntactic sugar, it makes sizes easier to understand
* with units as a type
*/
export type Kilobyte = number;
25 changes: 25 additions & 0 deletions packages/common/src/search/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
IGroup,
ISearchGroupUsersOptions,
ISearchOptions,
SearchQueryBuilder,
} from "@esri/arcgis-rest-portal";
import { isPageType } from "../content/_internal/internalContentUtils";
import { IHubSite } from "../core";
Expand All @@ -22,6 +23,7 @@ import {
IWellKnownApis,
IApiDefinition,
NamedApis,
Kilobyte,
} from "./types/types";
import { WellKnownCollection } from "./wellKnownCatalog";
import {
Expand Down Expand Up @@ -369,3 +371,26 @@ export function addDefaultItemSearchPredicates(query: IQuery): IQuery {
queryWithDefaultItemPredicates.filters.push(defaultPredicates);
return queryWithDefaultItemPredicates;
}

/**
* Returns the size in kilobytes of a query string or a SearchQueryBuilder.
* This is used to later determine if a query is too large or almost too large to be sent to the server.
* @param query
* @returns
*/
export function getKilobyteSizeOfQuery(
query: string | SearchQueryBuilder
): Kilobyte {
// convert query to string if it isn't already
const queryString = typeof query === "string" ? query : query.toParam();

// make blob of string so we accurately represent string's binary data,
// accounting for special characters and encoding
const blob = new Blob([queryString]);

// size of string in bytes
const bytes = blob.size;

// return size of string in kilobytes
return bytes / 1024;
}
Loading