-
Notifications
You must be signed in to change notification settings - Fork 13
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(hub-common): add viewDefinition and recordCount to fetchContent(… #744
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { queryFeatures } from "@esri/arcgis-rest-feature-layer"; | ||
import { getItem } from "@esri/arcgis-rest-portal"; | ||
import { composeContent } from "./compose"; | ||
import { IHubContent } from "../core"; | ||
import { | ||
ItemOrServerEnrichment, | ||
fetchItemEnrichments, | ||
IItemAndEnrichments, | ||
} from "../items/_enrichments"; | ||
import { IHubRequestOptions } from "../types"; | ||
import { isNil } from "../util"; | ||
|
@@ -14,6 +16,7 @@ import { | |
getContentEnrichments, | ||
} from "./_fetch"; | ||
import { canUseHubApiForItem } from "./_internal"; | ||
import { composeContent, getItemLayer, isLayerView } from "./compose"; | ||
|
||
interface IFetchItemAndEnrichmentsOptions extends IHubRequestOptions { | ||
enrichments?: ItemOrServerEnrichment[]; | ||
|
@@ -24,21 +27,42 @@ export interface IFetchContentOptions extends IFetchItemAndEnrichmentsOptions { | |
siteOrgKey?: string; | ||
} | ||
|
||
const maybeFetchLayerEnrichments = async ( | ||
itemAndEnrichments: IItemAndEnrichments, | ||
options?: IFetchContentOptions | ||
) => { | ||
// determine if this is a client-side feature layer view | ||
const { item, data, layers } = itemAndEnrichments; | ||
const layer = | ||
layers && getItemLayer(item, layers, options && options.layerId); | ||
// TODO: add recordCount here too? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to fetch the record count here, where we fetch other enrichments, however, I need a lot of logic that currently only happens in |
||
const layerEnrichments = | ||
layer && isLayerView(layer) && !data | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I copied the current logic in isLayerView() from composer.js, which only checks |
||
? // NOTE: I'm not sure what conditions causes a layer view | ||
// to store (at least part of) it's view definition in item data | ||
// it seems that most do not, but until we have a reliable signal | ||
// we just fetch the item data for all layer views | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Furthermore, spot checking a handful of such items from this: |
||
await fetchItemEnrichments(item, ["data"], options) | ||
: undefined; | ||
// TODO: merge errors | ||
return { ...itemAndEnrichments, ...layerEnrichments }; | ||
}; | ||
|
||
const fetchItemAndEnrichments = async ( | ||
itemId: string, | ||
options?: IFetchItemAndEnrichmentsOptions | ||
options?: IFetchContentOptions | ||
) => { | ||
// TODO: add error handling | ||
// fetch the item | ||
const item = await getItem(itemId, options); | ||
// TODO: allow override enrichments | ||
// fetch the enrichments | ||
const enrichmentsToFetch = | ||
options?.enrichments || getContentEnrichments(item); | ||
const enrichments = await fetchItemEnrichments( | ||
item, | ||
enrichmentsToFetch, | ||
options | ||
); | ||
return { ...enrichments, item }; | ||
return maybeFetchLayerEnrichments({ ...enrichments, item }, options); | ||
}; | ||
|
||
const fetchContentById = async ( | ||
|
@@ -52,8 +76,7 @@ const fetchContentById = async ( | |
options | ||
); | ||
// did the caller request a specific layer | ||
const specifiedLayerId = | ||
options && !isNil(options.layerId) && options.layerId; | ||
const specifiedLayerId = options && options.layerId; | ||
// if this is a public item and we're not in enterprise | ||
// fetch the slug and remaining enrichments from the Hub API | ||
const { slug, layerId, boundary, extent, searchDescription, statistics } = | ||
|
@@ -65,7 +88,7 @@ const fetchContentById = async ( | |
requestOptions: options, | ||
...itemEnrichments, | ||
slug, | ||
layerId: specifiedLayerId || layerId, | ||
layerId: isNil(specifiedLayerId) ? layerId : specifiedLayerId, | ||
tomwayson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
boundary, | ||
extent, | ||
searchDescription, | ||
|
@@ -119,6 +142,17 @@ const fetchContentBySlug = async ( | |
}); | ||
}; | ||
|
||
const fetchContentRecordCount = async (content: IHubContent) => { | ||
const { url, viewDefinition } = content; | ||
const where = viewDefinition?.definitionExpression; | ||
const response: any = await queryFeatures({ | ||
url, | ||
where, | ||
returnCountOnly: true, | ||
}); | ||
return response.count; | ||
}; | ||
|
||
/** | ||
* Fetch enriched content from the Portal and Hub APIs. | ||
* @param identifier content slug or id | ||
|
@@ -131,14 +165,19 @@ const fetchContentBySlug = async ( | |
* const content = await fetchContent('my-org::item-name') | ||
* ``` | ||
*/ | ||
export const fetchContent = ( | ||
export const fetchContent = async ( | ||
identifier: string, | ||
options?: IFetchContentOptions | ||
) => { | ||
return isSlug(identifier) | ||
? fetchContentBySlug( | ||
const content = isSlug(identifier) | ||
? await fetchContentBySlug( | ||
addContextToSlug(identifier, options?.siteOrgKey), | ||
options | ||
) | ||
: fetchContentById(identifier, options); | ||
: await fetchContentById(identifier, options); | ||
// fetch record count for layers, tables, or proxied CSVs | ||
const { isProxied, layer } = content; | ||
content.recordCount = | ||
isProxied || !!layer ? await fetchContentRecordCount(content) : undefined; | ||
return content; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this fn was just hoisted out of
composeContent()
below