Make handleCompareWith cleaner

This commit is contained in:
Robert
2023-04-19 16:59:12 +01:00
parent 7f3f3380ad
commit 3c77e81fd8
2 changed files with 72 additions and 86 deletions

View File

@@ -15,7 +15,6 @@ import {
import { QueryHistoryConfig } from "../config"; import { QueryHistoryConfig } from "../config";
import { import {
showAndLogErrorMessage, showAndLogErrorMessage,
showAndLogExceptionWithTelemetry,
showAndLogInformationMessage, showAndLogInformationMessage,
showAndLogWarningMessage, showAndLogWarningMessage,
showBinaryChoiceDialog, showBinaryChoiceDialog,
@@ -25,7 +24,7 @@ import { extLogger } from "../common";
import { URLSearchParams } from "url"; import { URLSearchParams } from "url";
import { DisposableObject } from "../pure/disposable-object"; import { DisposableObject } from "../pure/disposable-object";
import { ONE_HOUR_IN_MS, TWO_HOURS_IN_MS } from "../pure/time"; import { ONE_HOUR_IN_MS, TWO_HOURS_IN_MS } from "../pure/time";
import { asError, assertNever, getErrorMessage } from "../pure/helpers-pure"; import { assertNever, getErrorMessage } from "../pure/helpers-pure";
import { CompletedLocalQueryInfo, LocalQueryInfo } from "../query-results"; import { CompletedLocalQueryInfo, LocalQueryInfo } from "../query-results";
import { import {
getActionsWorkflowRunUrl, getActionsWorkflowRunUrl,
@@ -54,7 +53,6 @@ import { VariantAnalysisManager } from "../variant-analysis/variant-analysis-man
import { VariantAnalysisHistoryItem } from "./variant-analysis-history-item"; import { VariantAnalysisHistoryItem } from "./variant-analysis-history-item";
import { getTotalResultCount } from "../variant-analysis/shared/variant-analysis"; import { getTotalResultCount } from "../variant-analysis/shared/variant-analysis";
import { HistoryTreeDataProvider } from "./history-tree-data-provider"; import { HistoryTreeDataProvider } from "./history-tree-data-provider";
import { redactableError } from "../pure/errors";
import { QueryHistoryDirs } from "./query-history-dirs"; import { QueryHistoryDirs } from "./query-history-dirs";
import { QueryHistoryCommands } from "../common/commands"; import { QueryHistoryCommands } from "../common/commands";
import { App } from "../common/app"; import { App } from "../common/app";
@@ -587,43 +585,46 @@ export class QueryHistoryManager extends DisposableObject {
} }
} }
isSuccessfulCompletedLocalQueryInfo(
item: QueryHistoryInfo,
): item is CompletedLocalQueryInfo {
return item.t === "local" && item.completedQuery?.successful === true;
}
async handleCompareWith( async handleCompareWith(
singleItem: QueryHistoryInfo, singleItem: QueryHistoryInfo,
multiSelect: QueryHistoryInfo[] | undefined, multiSelect: QueryHistoryInfo[] | undefined,
) { ) {
const { finalSingleItem, finalMultiSelect } = this.determineSelection( multiSelect ||= [singleItem];
singleItem,
multiSelect,
);
try { if (
// local queries only !this.isSuccessfulCompletedLocalQueryInfo(singleItem) ||
if (finalSingleItem?.t !== "local") { !multiSelect.every(this.isSuccessfulCompletedLocalQueryInfo)
throw new Error("Please select a local query."); ) {
} throw new Error(
"Please only select local queries that have completed successfully.",
if (!finalSingleItem.completedQuery?.successful) {
throw new Error(
"Please select a query that has completed successfully.",
);
}
const from = this.compareWithItem || singleItem;
const to = await this.findOtherQueryToCompare(from, finalMultiSelect);
if (from.completed && to?.completed) {
await this.doCompareCallback(
from as CompletedLocalQueryInfo,
to as CompletedLocalQueryInfo,
);
}
} catch (e) {
void showAndLogExceptionWithTelemetry(
redactableError(
asError(e),
)`Failed to compare queries: ${getErrorMessage(e)}`,
); );
} }
const fromItem =
this.compareWithItem &&
this.isSuccessfulCompletedLocalQueryInfo(this.compareWithItem) &&
multiSelect.includes(this.compareWithItem)
? this.compareWithItem
: singleItem;
let toItem: CompletedLocalQueryInfo | undefined = undefined;
try {
toItem = await this.findOtherQueryToCompare(fromItem, multiSelect);
} catch (e) {
void showAndLogErrorMessage(
`Failed to compare queries: ${getErrorMessage(e)}`,
);
}
if (toItem !== undefined) {
await this.doCompareCallback(fromItem, toItem);
}
} }
async handleItemClicked( async handleItemClicked(
@@ -1177,57 +1178,40 @@ export class QueryHistoryManager extends DisposableObject {
} }
private async findOtherQueryToCompare( private async findOtherQueryToCompare(
singleItem: QueryHistoryInfo, fromItem: CompletedLocalQueryInfo,
multiSelect: QueryHistoryInfo[], allItemsSelected: CompletedLocalQueryInfo[],
): Promise<CompletedLocalQueryInfo | undefined> { ): Promise<CompletedLocalQueryInfo | undefined> {
// Variant analyses cannot be compared const dbName = fromItem.initialInfo.databaseInfo.name;
if (
singleItem.t !== "local" ||
multiSelect.some((s) => s.t !== "local") ||
!singleItem.completedQuery
) {
return undefined;
}
const dbName = singleItem.initialInfo.databaseInfo.name;
// if exactly 2 queries are selected, use those // if exactly 2 queries are selected, use those
if (multiSelect?.length === 2) { if (allItemsSelected.length === 2) {
// return the query that is not the first selected one const otherItem =
const otherQuery = ( fromItem === allItemsSelected[0]
singleItem === multiSelect[0] ? multiSelect[1] : multiSelect[0] ? allItemsSelected[1]
) as LocalQueryInfo; : allItemsSelected[0];
if (!otherQuery.completedQuery) { if (otherItem.initialInfo.databaseInfo.name !== dbName) {
throw new Error("Please select a completed query.");
}
if (!otherQuery.completedQuery.successful) {
throw new Error("Please select a successful query.");
}
if (otherQuery.initialInfo.databaseInfo.name !== dbName) {
throw new Error("Query databases must be the same."); throw new Error("Query databases must be the same.");
} }
return otherQuery as CompletedLocalQueryInfo; return otherItem;
} }
if (multiSelect?.length > 2) { if (allItemsSelected.length > 2) {
throw new Error("Please select no more than 2 queries."); throw new Error("Please select no more than 2 queries.");
} }
// otherwise, let the user choose // otherwise, let the user choose
const comparableQueryLabels = this.treeDataProvider.allHistory const comparableQueryLabels = this.treeDataProvider.allHistory
.filter(this.isSuccessfulCompletedLocalQueryInfo)
.filter( .filter(
(otherQuery) => (otherItem) =>
otherQuery !== singleItem && otherItem !== fromItem &&
otherQuery.t === "local" && otherItem.initialInfo.databaseInfo.name === dbName,
otherQuery.completedQuery &&
otherQuery.completedQuery.successful &&
otherQuery.initialInfo.databaseInfo.name === dbName,
) )
.map((item) => ({ .map((item) => ({
label: this.labelProvider.getLabel(item), label: this.labelProvider.getLabel(item),
description: (item as CompletedLocalQueryInfo).initialInfo.databaseInfo description: item.initialInfo.databaseInfo.name,
.name, detail: item.completedQuery.statusString,
detail: (item as CompletedLocalQueryInfo).completedQuery.statusString, query: item,
query: item as CompletedLocalQueryInfo,
})); }));
if (comparableQueryLabels.length < 1) { if (comparableQueryLabels.length < 1) {
throw new Error("No other queries available to compare with."); throw new Error("No other queries available to compare with.");

View File

@@ -977,24 +977,6 @@ describe("QueryHistoryManager", () => {
expect(showQuickPickSpy).not.toBeCalled(); expect(showQuickPickSpy).not.toBeCalled();
}); });
it("should throw an error when a query is not successful", async () => {
const thisQuery = localQueryHistory[3];
queryHistoryManager = await createMockQueryHistory(allHistory);
allHistory[0] = createMockLocalQueryInfo({
dbName: "a",
queryWithResults: createMockQueryWithResults({
didRunSuccessfully: false,
}),
});
await expect(
(queryHistoryManager as any).findOtherQueryToCompare(thisQuery, [
thisQuery,
allHistory[0],
]),
).rejects.toThrow("Please select a successful query.");
});
it("should throw an error when a databases are not the same", async () => { it("should throw an error when a databases are not the same", async () => {
queryHistoryManager = await createMockQueryHistory(allHistory); queryHistoryManager = await createMockQueryHistory(allHistory);
@@ -1043,6 +1025,26 @@ describe("QueryHistoryManager", () => {
]); ]);
expect(doCompareCallback).not.toBeCalled(); expect(doCompareCallback).not.toBeCalled();
}); });
it("should throw an error when a query is not successful", async () => {
const thisQuery = localQueryHistory[3];
queryHistoryManager = await createMockQueryHistory(allHistory);
allHistory[0] = createMockLocalQueryInfo({
dbName: "a",
queryWithResults: createMockQueryWithResults({
didRunSuccessfully: false,
}),
});
await expect(
queryHistoryManager.handleCompareWith(thisQuery, [
thisQuery,
allHistory[0],
]),
).rejects.toThrow(
"Please only select local queries that have completed successfully.",
);
});
}); });
describe("updateCompareWith", () => { describe("updateCompareWith", () => {