Skip to content

Commit 23bbff2

Browse files
authored
Update variant analysis monitor to get the whole variant analysis object (#3415)
1 parent b884cff commit 23bbff2

File tree

3 files changed

+41
-37
lines changed

3 files changed

+41
-37
lines changed

extensions/ql-vscode/src/variant-analysis/variant-analysis-manager.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export class VariantAnalysisManager
146146
new VariantAnalysisMonitor(
147147
app,
148148
this.shouldCancelMonitorVariantAnalysis.bind(this),
149-
this.getVariantAnalysisStatus.bind(this),
149+
this.getVariantAnalysis.bind(this),
150150
),
151151
);
152152
this.variantAnalysisMonitor.onVariantAnalysisChange(
@@ -606,17 +606,14 @@ export class VariantAnalysisManager
606606
return !this.variantAnalyses.has(variantAnalysisId);
607607
}
608608

609-
private getVariantAnalysisStatus(
610-
variantAnalysisId: number,
611-
): VariantAnalysisStatus {
612-
const variantAnalysis = this.variantAnalyses.get(variantAnalysisId);
609+
private getVariantAnalysis(variantAnalysisId: number): VariantAnalysis {
610+
const variantAnalysis = this.tryGetVariantAnalysis(variantAnalysisId);
611+
613612
if (!variantAnalysis) {
614-
throw new Error(
615-
`No variant analysis found with id: ${variantAnalysisId}.`,
616-
);
613+
throw new Error(`No variant analysis with id: ${variantAnalysisId}`);
617614
}
618615

619-
return variantAnalysis.status;
616+
return variantAnalysis;
620617
}
621618

622619
public async onVariantAnalysisUpdated(

extensions/ql-vscode/src/variant-analysis/variant-analysis-monitor.ts

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { RequestError } from "@octokit/request-error";
55
import type {
66
VariantAnalysis,
77
VariantAnalysisScannedRepository,
8-
VariantAnalysisStatus,
98
} from "./shared/variant-analysis";
109
import {
1110
isFinalVariantAnalysisStatus,
@@ -18,6 +17,7 @@ import { sleep } from "../common/time";
1817
import { getErrorMessage } from "../common/helpers-pure";
1918
import type { App } from "../common/app";
2019
import { showAndLogWarningMessage } from "../common/logging";
20+
import type { QueryLanguage } from "../common/query-language";
2121

2222
export class VariantAnalysisMonitor extends DisposableObject {
2323
// With a sleep of 5 seconds, the maximum number of attempts takes
@@ -37,9 +37,9 @@ export class VariantAnalysisMonitor extends DisposableObject {
3737
private readonly shouldCancelMonitor: (
3838
variantAnalysisId: number,
3939
) => Promise<boolean>,
40-
private readonly getVariantAnalysisStatus: (
40+
private readonly getVariantAnalysis: (
4141
variantAnalysisId: number,
42-
) => VariantAnalysisStatus,
42+
) => VariantAnalysis,
4343
) {
4444
super();
4545
}
@@ -60,20 +60,28 @@ export class VariantAnalysisMonitor extends DisposableObject {
6060

6161
this.monitoringVariantAnalyses.add(variantAnalysis.id);
6262
try {
63-
await this._monitorVariantAnalysis(variantAnalysis);
63+
await this._monitorVariantAnalysis(
64+
variantAnalysis.id,
65+
variantAnalysis.controllerRepo.id,
66+
variantAnalysis.executionStartTime,
67+
variantAnalysis.query.name,
68+
variantAnalysis.language,
69+
);
6470
} finally {
6571
this.monitoringVariantAnalyses.delete(variantAnalysis.id);
6672
}
6773
}
6874

6975
private async _monitorVariantAnalysis(
70-
variantAnalysis: VariantAnalysis,
76+
variantAnalysisId: number,
77+
controllerRepoId: number,
78+
executionStartTime: number,
79+
queryName: string,
80+
language: QueryLanguage,
7181
): Promise<void> {
72-
const variantAnalysisLabel = `${variantAnalysis.query.name} (${
73-
variantAnalysis.language
74-
}) [${new Date(variantAnalysis.executionStartTime).toLocaleString(
75-
env.language,
76-
)}]`;
82+
const variantAnalysisLabel = `${queryName} (${language}) [${new Date(
83+
executionStartTime,
84+
).toLocaleString(env.language)}]`;
7785

7886
let attemptCount = 0;
7987
const scannedReposDownloaded: number[] = [];
@@ -83,16 +91,16 @@ export class VariantAnalysisMonitor extends DisposableObject {
8391
while (attemptCount <= VariantAnalysisMonitor.maxAttemptCount) {
8492
await sleep(VariantAnalysisMonitor.sleepTime);
8593

86-
if (await this.shouldCancelMonitor(variantAnalysis.id)) {
94+
if (await this.shouldCancelMonitor(variantAnalysisId)) {
8795
return;
8896
}
8997

9098
let variantAnalysisSummary: ApiVariantAnalysis;
9199
try {
92100
variantAnalysisSummary = await getVariantAnalysis(
93101
this.app.credentials,
94-
variantAnalysis.controllerRepo.id,
95-
variantAnalysis.id,
102+
controllerRepoId,
103+
variantAnalysisId,
96104
);
97105
} catch (e) {
98106
const errorMessage = getErrorMessage(e);
@@ -123,15 +131,10 @@ export class VariantAnalysisMonitor extends DisposableObject {
123131
continue;
124132
}
125133

126-
// Get the current status of the variant analysis as known by the rest
127-
// of the app, because it may have been changed by the user and this code
128-
// may not be aware of it yet.
129-
const currentStatus = this.getVariantAnalysisStatus(variantAnalysis.id);
130-
variantAnalysis = mapUpdatedVariantAnalysis(
131-
{
132-
...variantAnalysis,
133-
status: currentStatus,
134-
},
134+
const variantAnalysis = mapUpdatedVariantAnalysis(
135+
// Get the variant analysis as known by the rest of the app, because it may
136+
// have been changed by the user and the monitors may not be aware of it yet.
137+
this.getVariantAnalysis(variantAnalysisId),
135138
variantAnalysisSummary,
136139
);
137140

extensions/ql-vscode/test/vscode-tests/activated-extension/variant-analysis/variant-analysis-monitor.test.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe("Variant Analysis Monitor", () => {
3232
>;
3333
let variantAnalysisMonitor: VariantAnalysisMonitor;
3434
let shouldCancelMonitor: jest.Mock<Promise<boolean>, [number]>;
35-
let mockGetVariantAnalysisStatus: jest.Mock<VariantAnalysisStatus, [number]>;
35+
let mockGetVariantAnalysis: jest.Mock<VariantAnalysis, [number]>;
3636
let variantAnalysis: VariantAnalysis;
3737

3838
const onVariantAnalysisChangeSpy = jest.fn();
@@ -44,7 +44,7 @@ describe("Variant Analysis Monitor", () => {
4444
variantAnalysis = createMockVariantAnalysis({});
4545

4646
shouldCancelMonitor = jest.fn();
47-
mockGetVariantAnalysisStatus = jest.fn();
47+
mockGetVariantAnalysis = jest.fn();
4848

4949
logger = createMockLogger();
5050

@@ -56,14 +56,16 @@ describe("Variant Analysis Monitor", () => {
5656
logger,
5757
}),
5858
shouldCancelMonitor,
59-
mockGetVariantAnalysisStatus,
59+
mockGetVariantAnalysis,
6060
);
6161
variantAnalysisMonitor.onVariantAnalysisChange(onVariantAnalysisChangeSpy);
6262

6363
mockGetVariantAnalysisFromApi = jest
6464
.spyOn(ghApiClient, "getVariantAnalysis")
6565
.mockRejectedValue(new Error("Not mocked"));
6666

67+
mockGetVariantAnalysis.mockReturnValue(variantAnalysis);
68+
6769
limitNumberOfAttemptsToMonitor();
6870
});
6971

@@ -342,9 +344,11 @@ describe("Variant Analysis Monitor", () => {
342344

343345
describe("cancelation", () => {
344346
it("should maintain canceling status", async () => {
345-
mockGetVariantAnalysisStatus.mockReturnValueOnce(
346-
VariantAnalysisStatus.Canceling,
347-
);
347+
mockGetVariantAnalysis.mockReturnValueOnce({
348+
...variantAnalysis,
349+
status: VariantAnalysisStatus.Canceling,
350+
});
351+
348352
mockApiResponse = createMockApiResponse("in_progress");
349353
mockGetVariantAnalysisFromApi.mockResolvedValue(mockApiResponse);
350354

0 commit comments

Comments
 (0)