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

Specify "Collection" when searching for multilayer KML or CSV exports #426

Merged
merged 5 commits into from
Nov 18, 2020
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
2 changes: 1 addition & 1 deletion demos/oauth2-browser/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/downloads/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
"eventemitter3": "^4.0.4",
"tslib": "^1.13.0"
},
"peerDependencies": {
"@esri/arcgis-rest-feature-layer": "^2.21.0",
"@esri/arcgis-rest-portal": "^2.21.0",
"@esri/arcgis-rest-request": "^2.21.0"
},
"devDependencies": {
"@esri/arcgis-rest-auth": "^2.21.0",
"@rollup/plugin-commonjs": "^15.0.0",
Expand Down
20 changes: 12 additions & 8 deletions packages/downloads/src/download-format.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
export type DownloadFormat = 'Shapefile' |
'CSV' |
'File Geodatabase' |
'Feature Collection' |
'GeoJson' |
'Scene Package' |
'KML' |
'Excel';
export enum DownloadFormats {
Shapefile = "Shapefile",
CSV = "CSV",
KML = "KML",
GeoJson = "GeoJson",
Excel = "Excel",
"File Geodatabase" = "File Geodatabase",
"Feature Collection" = "Feature Collection",
"Scene Package" = "Scene Package"
}

export type DownloadFormat = keyof typeof DownloadFormats;
96 changes: 72 additions & 24 deletions packages/downloads/src/portal/portal-request-download-metadata.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { getItem, IItem, searchItems } from "@esri/arcgis-rest-portal";
import { UserSession } from "@esri/arcgis-rest-auth";
import { getLayer } from "@esri/arcgis-rest-feature-layer";
import { DownloadFormat } from "../download-format";
import {
getService,
IFeatureServiceDefinition,
ILayerDefinition
} from "@esri/arcgis-rest-feature-layer";
import { DownloadFormat, DownloadFormats } from "../download-format";
import { urlBuilder, composeDownloadId } from "../utils";

enum ItemTypes {
FeatureService = "Feature Service",
MapService = "Map Service"
}
Comment on lines +11 to +14
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These could probably just be constants. An enum here might be overkill.


const isCollectionType = (format: DownloadFormat) =>
format === DownloadFormats.CSV || format === DownloadFormats.KML;

/**
* @private
*/
Expand All @@ -14,6 +26,14 @@ export interface IPortalDownloadMetadataRequestParams {
spatialRefId?: string;
}

/**
* @private
*/
export interface ICacheSearchMetadata {
modified: number;
format: string;
}

/**
* @private
*/
Expand All @@ -32,18 +52,19 @@ export function portalRequestDownloadMetadata(
const { type, modified, url } = item;
itemModifiedDate = modified;
itemType = type;
return fetchLastEditDate({
datasetId,
return fetchCacheSearchMetadata({
url,
authentication,
type,
modified
modified,
format
});
})
.then((result: number) => {
serviceLastEditDate = result;
.then((metadata: ICacheSearchMetadata) => {
const { modified, format: searchFormat } = metadata;
serviceLastEditDate = modified;
return searchItems({
q: `type:"${format}" AND typekeywords:"export:${datasetId},spatialRefId:${spatialRefId}"`,
q: `type:"${searchFormat}" AND typekeywords:"export:${datasetId},spatialRefId:${spatialRefId}"`,
num: 1,
sortField: "modified",
sortOrder: "DESC",
Expand All @@ -67,30 +88,57 @@ export function portalRequestDownloadMetadata(
});
}

function fetchLastEditDate(params: any): Promise<number | undefined> {
const { datasetId, url, type, modified, authentication } = params;
function fetchCacheSearchMetadata(params: any): Promise<ICacheSearchMetadata> {
const { format, layerId, url, type, modified, authentication } = params;

const layerId = datasetId.split("_")[1] || 0;

if (type !== "Feature Service" && type !== "Map Service") {
return Promise.resolve(modified);
if (type !== ItemTypes.FeatureService && type !== ItemTypes.MapService) {
return Promise.resolve({
modified,
format
});
}

return getLayer({
url: `${url}/${layerId}`,
authentication
}).then((layer: any) => {
const editingInfo: any | null = layer.editingInfo;
return editingInfo ? editingInfo.lastEditDate : undefined;
});
return getService({ url, authentication }).then(
(response: IFeatureServiceDefinition) => {
const layers: ILayerDefinition[] = response.layers || [];
const multilayer = isMultilayerRequest(layerId, layers);
return {
format:
multilayer && isCollectionType(format)
? `${format} Collection`
: format,
modified: extractLastEditDate(layers)
mikeringrose marked this conversation as resolved.
Show resolved Hide resolved
};
}
);
}

function isMultilayerRequest(layerId: string, layers: ILayerDefinition[]) {
return layerId === undefined && layers.length > 1;
}

function extractLastEditDate(layers: ILayerDefinition[]) {
const result = layers
.map((layer: ILayerDefinition) => {
const { editingInfo: { lastEditDate } = {} } = layer;
return lastEditDate;
})
.filter(timestamp => {
return timestamp !== undefined;
})
.sort((a, b) => {
return b - a;
});
return result[0];
mikeringrose marked this conversation as resolved.
Show resolved Hide resolved
}

function formatDownloadMetadata(params: any) {
const { cachedDownload, serviceLastEditDate, authentication } = params;

const lastEditDate = serviceLastEditDate
? new Date(serviceLastEditDate).toISOString()
: undefined;
const lastEditDate =
serviceLastEditDate !== undefined
? new Date(serviceLastEditDate).toISOString()
: undefined;

if (!cachedDownload) {
return {
Expand Down
Loading