@seasketch/geoprocessing
Version:
Geoprocessing and reporting framework for SeaSketch 2.0
62 lines (61 loc) • 4.35 kB
TypeScript
import { GeoprocessingHandlerOptions, Sketch, SketchCollection, Geometry, Feature, FeatureCollection, Polygon, LineString, Point, JSONValue, GeoprocessingRequestModel } from "../types/index.js";
import TaskModel from "./tasks.js";
import { Context, APIGatewayProxyResult, APIGatewayProxyEvent } from "aws-lambda";
import WebSocket from "ws";
/**
* Manages the task of executing a geoprocessing function within an AWS Lambda function.
* This includes sending estimate of completion, caching the results, and getting them back to the client.
* Supports 2 different execution modes for running a geoprocessing function - sync and async
* These modes create 3 different request scenarios. A lambda is created for each scenario, and they all run
* this one handler.
* 1 - sync executionMode - immediately run gp function and return result in resolved promise to client
* 2 - async executionMode, ASYNC_REQUEST_TYPE=start - invoke a second lambda to run gp function and return incomplete task to client with socket for notification of result
* 3 - async executionMode, ASYNC_REQUEST_TYPE=run - run gp function started by scenario 2 and send completed task info on socket for client to pick up result
*
* @template T the return type of the geoprocessing function, automatically set from func return type
* @template G the geometry type of features for the geoprocessing function, automatically set from func feature type
* @template P extra parameters to pass to geoprocessing function, automatically set from func parameter type
*/
export declare class GeoprocessingHandler<T = JSONValue, G extends Geometry = Polygon | LineString | Point, P extends Record<string, JSONValue> = Record<string, JSONValue>> {
func: (feature: Feature<G> | FeatureCollection<G> | Sketch<G> | SketchCollection<G>,
/** Optional additional runtime parameters from report client for geoprocessing function. Validation left to implementing function */
extraParams?: P,
/** Original event params used to invoke geoprocessing function made accessible to func */
request?: GeoprocessingRequestModel<G>) => Promise<T>;
options: GeoprocessingHandlerOptions;
lastRequestId?: string;
Tasks: TaskModel;
/**
* @param func the geoprocessing function to run
* @param options geoprocessing function deployment options
* @template G the geometry type of features for the geoprocessing function, automatically set from func feature type
* @template P extra parameters to pass to geoprocessing function, automatically set from func parameter type
* @template T the return type of the geoprocessing function, automatically set from func return type
*/
constructor(func: (feature: Feature<G> | FeatureCollection<G>, extraParams: P, request?: GeoprocessingRequestModel<G>) => Promise<T>, options: GeoprocessingHandlerOptions);
constructor(func: (feature: Sketch<G> | SketchCollection<G>, extraParams: P, request?: GeoprocessingRequestModel<G>) => Promise<T>, options: GeoprocessingHandlerOptions);
/**
* Given request event, runs geoprocessing function and returns APIGatewayProxyResult with task status in the body
* If sync executionMode, then result is returned with task, if async executionMode, then returns socket for client to listen for task update
* If event.geometry present, assumes request is already a GeoprocessingRequest (from AWS console).
* If event.queryStringParameters present, request must be from API Gateway and need to coerce into GeoprocessingRequest
* If event.body present with JSON string, then parse as a GeoprocessingRequest
*/
lambdaHandler(event: APIGatewayProxyEvent, context: Context): Promise<APIGatewayProxyResult>;
/**
* Send task error message
*/
sendSocketErrorMessage(wss: string, cacheKey: string | undefined, serviceName: string, failureMessage: string): Promise<void>;
/**
* Send completed task message
*/
sendSocketMessage(wss: string, cacheKey: string | undefined, serviceName: string): Promise<void>;
/**
* Returns a new socket connection to send a message
*/
getSocket(wss: string): Promise<WebSocket>;
/**
* Parses event and returns GeoprocessingRequestModel object.
*/
parseRequest<G>(event: APIGatewayProxyEvent): GeoprocessingRequestModel<G>;
}