Skip to content

Commit

Permalink
Use fetcher + refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
davelopez committed Sep 20, 2023
1 parent e99d7ce commit 4380d66
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 40 deletions.
3 changes: 2 additions & 1 deletion client/src/components/Common/models/exportRecordModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import type { components } from "@/schema";
type ExportObjectRequestMetadata = components["schemas"]["ExportObjectRequestMetadata"];

export type StoreExportPayload = components["schemas"]["StoreExportPayload"];
export type ModelStoreFormat = components["schemas"]["ModelStoreFormat"];
export type ObjectExportTaskResponse = components["schemas"]["ObjectExportTaskResponse"];

export interface ExportParams {
readonly modelStoreFormat: string;
readonly modelStoreFormat: ModelStoreFormat;
readonly includeFiles: boolean;
readonly includeDeleted: boolean;
readonly includeHidden: boolean;
Expand Down
104 changes: 65 additions & 39 deletions client/src/composables/shortTermStorage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from "axios";
import { readonly, ref } from "vue";

import { ExportParams } from "@/components/Common/models/exportRecordModel";
import { ExportParams, StoreExportPayload } from "@/components/Common/models/exportRecordModel";
import { fetcher } from "@/schema";
import { withPrefix } from "@/utils/redirect";

export const DEFAULT_EXPORT_PARAMS: ExportParams = {
Expand All @@ -11,8 +11,32 @@ export const DEFAULT_EXPORT_PARAMS: ExportParams = {
includeHidden: false,
};

interface Options {
exportParams: ExportParams;
pollDelayInMs: number;
}

interface StorageRequestResponse {
storage_request_id: string;
}

type StartPreparingDownloadCallback = (objectId: string, params: StoreExportPayload) => Promise<StorageRequestResponse>;

const DEFAULT_POLL_DELAY = 1000;
const DEFAULT_OPTIONS = { exportParams: DEFAULT_EXPORT_PARAMS, pollDelayInMs: DEFAULT_POLL_DELAY };
const DEFAULT_OPTIONS: Options = { exportParams: DEFAULT_EXPORT_PARAMS, pollDelayInMs: DEFAULT_POLL_DELAY };

const startPreparingHistoryDownload = fetcher
.path("/api/histories/{history_id}/prepare_store_download")
.method("post")
.create();
const startPreparingInvocationDownload = fetcher
.path("/api/invocations/{invocation_id}/prepare_store_download")
.method("post")
.create();
const getTempStorageRequestReady = fetcher
.path("/api/short_term_storage/{storage_request_id}/ready")
.method("get")
.create();

/**
* Composable to simplify and reuse the logic for downloading objects using Galaxy's Short Term Storage system.
Expand All @@ -23,20 +47,29 @@ export function useShortTermStorage() {

const isPreparing = ref(false);

const forHistory: StartPreparingDownloadCallback = async (id: string, params: StoreExportPayload) => {
const { data } = await startPreparingHistoryDownload({ history_id: id, ...params });
return data;
};

const forInvocation: StartPreparingDownloadCallback = async (id: string, params: StoreExportPayload) => {
const { data } = await startPreparingInvocationDownload({ invocation_id: id, ...params });
return data;
};

async function prepareHistoryDownload(historyId: string, options = DEFAULT_OPTIONS) {
return prepareObjectDownload(historyId, "histories", options, false);
return prepareObjectDownload(forHistory, historyId, options, false);
}

async function downloadHistory(historyId: string, options = DEFAULT_OPTIONS) {
return prepareObjectDownload(historyId, "histories", options, true);
return prepareObjectDownload(forHistory, historyId, options, true);
}

async function prepareWorkflowInvocationDownload(invocationId: string, options = DEFAULT_OPTIONS) {
return prepareObjectDownload(invocationId, "invocations", options, false);
return prepareObjectDownload(forInvocation, invocationId, options, false);
}

async function downloadWorkflowInvocation(invocationId: string, options = DEFAULT_OPTIONS) {
return prepareObjectDownload(invocationId, "invocations", options, true);
return prepareObjectDownload(forInvocation, invocationId, options, true);
}

function getDownloadObjectUrl(storageRequestId: string) {
Expand All @@ -50,55 +83,48 @@ export function useShortTermStorage() {
}

async function prepareObjectDownload(
object_id: string,
object_api: string,
startPreparingDownloadAsync: StartPreparingDownloadCallback,
objectId: string,
options = DEFAULT_OPTIONS,
downloadWhenReady = true
) {
const finalOptions = Object.assign(DEFAULT_OPTIONS, options);
resetTimeout();
isPreparing.value = true;
const finalOptions = Object.assign(DEFAULT_OPTIONS, options);
pollDelay = finalOptions.pollDelayInMs;
const url = withPrefix(`/api/${object_api}/${object_id}/prepare_store_download`);
const exportParams = {
const exportParams: StoreExportPayload = {
model_store_format: finalOptions.exportParams.modelStoreFormat,
include_files: finalOptions.exportParams.includeFiles,
include_deleted: finalOptions.exportParams.includeDeleted,
include_hidden: finalOptions.exportParams.includeHidden,
};

const response = await axios.post(url, exportParams).catch(handleError);
handleInitialize(response, downloadWhenReady);
}

function handleInitialize(response: any, downloadWhenReady: boolean) {
const storageRequestId = response.data.storage_request_id;
pollStorageRequestId(storageRequestId, downloadWhenReady);
}

function pollStorageRequestId(storageRequestId: string, downloadWhenReady: boolean) {
const url = withPrefix(`/api/short_term_storage/${storageRequestId}/ready`);
axios
.get(url)
.then((r) => {
handlePollResponse(r, storageRequestId, downloadWhenReady);
})
.catch(handleError);
try {
const response = await startPreparingDownloadAsync(objectId, exportParams);
const storageRequestId = response.storage_request_id;
pollStorageRequestId(storageRequestId, downloadWhenReady);
} catch (err) {
stopPreparing();
}
}

function handlePollResponse(response: any, storageRequestId: string, downloadWhenReady: boolean) {
const ready = response.data;
if (ready) {
isPreparing.value = false;
if (downloadWhenReady) {
downloadObjectByRequestId(storageRequestId);
async function pollStorageRequestId(storageRequestId: string, downloadWhenReady: boolean) {
try {
const { data: ready } = await getTempStorageRequestReady({ storage_request_id: storageRequestId });
if (ready) {
isPreparing.value = false;
if (downloadWhenReady) {
downloadObjectByRequestId(storageRequestId);
}
} else {
pollAfterDelay(storageRequestId, downloadWhenReady);
}
} else {
pollAfterDelay(storageRequestId, downloadWhenReady);
} catch (err) {
stopPreparing();
}
}

function handleError(_err: any) {
function stopPreparing() {
isPreparing.value = false;
}

Expand Down

0 comments on commit 4380d66

Please sign in to comment.