Skip to content

Commit 3c77e81

Browse files
Make handleCompareWith cleaner
1 parent 7f3f338 commit 3c77e81

File tree

2 files changed

+69
-83
lines changed

2 files changed

+69
-83
lines changed

extensions/ql-vscode/src/query-history/query-history-manager.ts

Lines changed: 49 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
import { QueryHistoryConfig } from "../config";
1616
import {
1717
showAndLogErrorMessage,
18-
showAndLogExceptionWithTelemetry,
1918
showAndLogInformationMessage,
2019
showAndLogWarningMessage,
2120
showBinaryChoiceDialog,
@@ -25,7 +24,7 @@ import { extLogger } from "../common";
2524
import { URLSearchParams } from "url";
2625
import { DisposableObject } from "../pure/disposable-object";
2726
import { ONE_HOUR_IN_MS, TWO_HOURS_IN_MS } from "../pure/time";
28-
import { asError, assertNever, getErrorMessage } from "../pure/helpers-pure";
27+
import { assertNever, getErrorMessage } from "../pure/helpers-pure";
2928
import { CompletedLocalQueryInfo, LocalQueryInfo } from "../query-results";
3029
import {
3130
getActionsWorkflowRunUrl,
@@ -54,7 +53,6 @@ import { VariantAnalysisManager } from "../variant-analysis/variant-analysis-man
5453
import { VariantAnalysisHistoryItem } from "./variant-analysis-history-item";
5554
import { getTotalResultCount } from "../variant-analysis/shared/variant-analysis";
5655
import { HistoryTreeDataProvider } from "./history-tree-data-provider";
57-
import { redactableError } from "../pure/errors";
5856
import { QueryHistoryDirs } from "./query-history-dirs";
5957
import { QueryHistoryCommands } from "../common/commands";
6058
import { App } from "../common/app";
@@ -587,43 +585,46 @@ export class QueryHistoryManager extends DisposableObject {
587585
}
588586
}
589587

588+
isSuccessfulCompletedLocalQueryInfo(
589+
item: QueryHistoryInfo,
590+
): item is CompletedLocalQueryInfo {
591+
return item.t === "local" && item.completedQuery?.successful === true;
592+
}
593+
590594
async handleCompareWith(
591595
singleItem: QueryHistoryInfo,
592596
multiSelect: QueryHistoryInfo[] | undefined,
593597
) {
594-
const { finalSingleItem, finalMultiSelect } = this.determineSelection(
595-
singleItem,
596-
multiSelect,
597-
);
598-
599-
try {
600-
// local queries only
601-
if (finalSingleItem?.t !== "local") {
602-
throw new Error("Please select a local query.");
603-
}
598+
multiSelect ||= [singleItem];
604599

605-
if (!finalSingleItem.completedQuery?.successful) {
606-
throw new Error(
607-
"Please select a query that has completed successfully.",
608-
);
609-
}
600+
if (
601+
!this.isSuccessfulCompletedLocalQueryInfo(singleItem) ||
602+
!multiSelect.every(this.isSuccessfulCompletedLocalQueryInfo)
603+
) {
604+
throw new Error(
605+
"Please only select local queries that have completed successfully.",
606+
);
607+
}
610608

611-
const from = this.compareWithItem || singleItem;
612-
const to = await this.findOtherQueryToCompare(from, finalMultiSelect);
609+
const fromItem =
610+
this.compareWithItem &&
611+
this.isSuccessfulCompletedLocalQueryInfo(this.compareWithItem) &&
612+
multiSelect.includes(this.compareWithItem)
613+
? this.compareWithItem
614+
: singleItem;
613615

614-
if (from.completed && to?.completed) {
615-
await this.doCompareCallback(
616-
from as CompletedLocalQueryInfo,
617-
to as CompletedLocalQueryInfo,
618-
);
619-
}
616+
let toItem: CompletedLocalQueryInfo | undefined = undefined;
617+
try {
618+
toItem = await this.findOtherQueryToCompare(fromItem, multiSelect);
620619
} catch (e) {
621-
void showAndLogExceptionWithTelemetry(
622-
redactableError(
623-
asError(e),
624-
)`Failed to compare queries: ${getErrorMessage(e)}`,
620+
void showAndLogErrorMessage(
621+
`Failed to compare queries: ${getErrorMessage(e)}`,
625622
);
626623
}
624+
625+
if (toItem !== undefined) {
626+
await this.doCompareCallback(fromItem, toItem);
627+
}
627628
}
628629

629630
async handleItemClicked(
@@ -1177,57 +1178,40 @@ export class QueryHistoryManager extends DisposableObject {
11771178
}
11781179

11791180
private async findOtherQueryToCompare(
1180-
singleItem: QueryHistoryInfo,
1181-
multiSelect: QueryHistoryInfo[],
1181+
fromItem: CompletedLocalQueryInfo,
1182+
allItemsSelected: CompletedLocalQueryInfo[],
11821183
): Promise<CompletedLocalQueryInfo | undefined> {
1183-
// Variant analyses cannot be compared
1184-
if (
1185-
singleItem.t !== "local" ||
1186-
multiSelect.some((s) => s.t !== "local") ||
1187-
!singleItem.completedQuery
1188-
) {
1189-
return undefined;
1190-
}
1191-
const dbName = singleItem.initialInfo.databaseInfo.name;
1184+
const dbName = fromItem.initialInfo.databaseInfo.name;
11921185

11931186
// if exactly 2 queries are selected, use those
1194-
if (multiSelect?.length === 2) {
1195-
// return the query that is not the first selected one
1196-
const otherQuery = (
1197-
singleItem === multiSelect[0] ? multiSelect[1] : multiSelect[0]
1198-
) as LocalQueryInfo;
1199-
if (!otherQuery.completedQuery) {
1200-
throw new Error("Please select a completed query.");
1201-
}
1202-
if (!otherQuery.completedQuery.successful) {
1203-
throw new Error("Please select a successful query.");
1204-
}
1205-
if (otherQuery.initialInfo.databaseInfo.name !== dbName) {
1187+
if (allItemsSelected.length === 2) {
1188+
const otherItem =
1189+
fromItem === allItemsSelected[0]
1190+
? allItemsSelected[1]
1191+
: allItemsSelected[0];
1192+
if (otherItem.initialInfo.databaseInfo.name !== dbName) {
12061193
throw new Error("Query databases must be the same.");
12071194
}
1208-
return otherQuery as CompletedLocalQueryInfo;
1195+
return otherItem;
12091196
}
12101197

1211-
if (multiSelect?.length > 2) {
1198+
if (allItemsSelected.length > 2) {
12121199
throw new Error("Please select no more than 2 queries.");
12131200
}
12141201

12151202
// otherwise, let the user choose
12161203
const comparableQueryLabels = this.treeDataProvider.allHistory
1204+
.filter(this.isSuccessfulCompletedLocalQueryInfo)
12171205
.filter(
1218-
(otherQuery) =>
1219-
otherQuery !== singleItem &&
1220-
otherQuery.t === "local" &&
1221-
otherQuery.completedQuery &&
1222-
otherQuery.completedQuery.successful &&
1223-
otherQuery.initialInfo.databaseInfo.name === dbName,
1206+
(otherItem) =>
1207+
otherItem !== fromItem &&
1208+
otherItem.initialInfo.databaseInfo.name === dbName,
12241209
)
12251210
.map((item) => ({
12261211
label: this.labelProvider.getLabel(item),
1227-
description: (item as CompletedLocalQueryInfo).initialInfo.databaseInfo
1228-
.name,
1229-
detail: (item as CompletedLocalQueryInfo).completedQuery.statusString,
1230-
query: item as CompletedLocalQueryInfo,
1212+
description: item.initialInfo.databaseInfo.name,
1213+
detail: item.completedQuery.statusString,
1214+
query: item,
12311215
}));
12321216
if (comparableQueryLabels.length < 1) {
12331217
throw new Error("No other queries available to compare with.");

extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/query-history-manager.test.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -977,24 +977,6 @@ describe("QueryHistoryManager", () => {
977977
expect(showQuickPickSpy).not.toBeCalled();
978978
});
979979

980-
it("should throw an error when a query is not successful", async () => {
981-
const thisQuery = localQueryHistory[3];
982-
queryHistoryManager = await createMockQueryHistory(allHistory);
983-
allHistory[0] = createMockLocalQueryInfo({
984-
dbName: "a",
985-
queryWithResults: createMockQueryWithResults({
986-
didRunSuccessfully: false,
987-
}),
988-
});
989-
990-
await expect(
991-
(queryHistoryManager as any).findOtherQueryToCompare(thisQuery, [
992-
thisQuery,
993-
allHistory[0],
994-
]),
995-
).rejects.toThrow("Please select a successful query.");
996-
});
997-
998980
it("should throw an error when a databases are not the same", async () => {
999981
queryHistoryManager = await createMockQueryHistory(allHistory);
1000982

@@ -1043,6 +1025,26 @@ describe("QueryHistoryManager", () => {
10431025
]);
10441026
expect(doCompareCallback).not.toBeCalled();
10451027
});
1028+
1029+
it("should throw an error when a query is not successful", async () => {
1030+
const thisQuery = localQueryHistory[3];
1031+
queryHistoryManager = await createMockQueryHistory(allHistory);
1032+
allHistory[0] = createMockLocalQueryInfo({
1033+
dbName: "a",
1034+
queryWithResults: createMockQueryWithResults({
1035+
didRunSuccessfully: false,
1036+
}),
1037+
});
1038+
1039+
await expect(
1040+
queryHistoryManager.handleCompareWith(thisQuery, [
1041+
thisQuery,
1042+
allHistory[0],
1043+
]),
1044+
).rejects.toThrow(
1045+
"Please only select local queries that have completed successfully.",
1046+
);
1047+
});
10461048
});
10471049

10481050
describe("updateCompareWith", () => {

0 commit comments

Comments
 (0)