Skip to content

Commit

Permalink
Merge pull request #426 from Esri/p/downloads-collection-search
Browse files Browse the repository at this point in the history
Specify "Collection" when searching for multilayer KML or CSV exports
  • Loading branch information
rgwozdz authored Nov 18, 2020
2 parents 7d5f9b6 + a3a57df commit 6a247ff
Show file tree
Hide file tree
Showing 6 changed files with 651 additions and 130 deletions.
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"
}

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)
};
}
);
}

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];
}

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

0 comments on commit 6a247ff

Please sign in to comment.