Skip to content

Commit fd4b602

Browse files
authored
Refactor: Invert dependency between query history and remote quries managers (#1396)
1 parent 58bbb59 commit fd4b602

File tree

5 files changed

+249
-165
lines changed

5 files changed

+249
-165
lines changed

extensions/ql-vscode/src/extension.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ import { RemoteQueriesManager } from './remote-queries/remote-queries-manager';
9595
import { RemoteQueryResult } from './remote-queries/remote-query-result';
9696
import { URLSearchParams } from 'url';
9797
import { handleDownloadPacks, handleInstallPackDependencies } from './packaging';
98-
import { RemoteQueryHistoryItem } from './remote-queries/remote-query-history-item';
9998
import { HistoryItemLabelProvider } from './history-item-label-provider';
10099
import { exportRemoteQueryResults } from './remote-queries/export-results';
100+
import { RemoteQuery } from './remote-queries/remote-query';
101101

102102
/**
103103
* extension.ts
@@ -451,10 +451,20 @@ async function activateWithInstalledDistribution(
451451
await fs.ensureDir(queryStorageDir);
452452
const labelProvider = new HistoryItemLabelProvider(queryHistoryConfigurationListener);
453453

454+
void logger.log('Initializing results panel interface.');
455+
const intm = new InterfaceManager(ctx, dbm, cliServer, queryServerLogger, labelProvider);
456+
ctx.subscriptions.push(intm);
457+
458+
void logger.log('Initializing variant analysis manager.');
459+
const rqm = new RemoteQueriesManager(ctx, cliServer, queryStorageDir, logger);
460+
ctx.subscriptions.push(rqm);
461+
454462
void logger.log('Initializing query history.');
455463
const qhm = new QueryHistoryManager(
456464
qs,
457465
dbm,
466+
intm,
467+
rqm,
458468
queryStorageDir,
459469
ctx,
460470
queryHistoryConfigurationListener,
@@ -463,17 +473,11 @@ async function activateWithInstalledDistribution(
463473
showResultsForComparison(from, to),
464474
);
465475

466-
qhm.onWillOpenQueryItem(async item => {
467-
if (item.t === 'local' && item.completed) {
468-
await showResultsForCompletedQuery(item as CompletedLocalQueryInfo, WebviewReveal.Forced);
469-
}
470-
});
471476

472477
ctx.subscriptions.push(qhm);
473478

474-
void logger.log('Initializing results panel interface.');
475-
const intm = new InterfaceManager(ctx, dbm, cliServer, queryServerLogger, labelProvider);
476-
ctx.subscriptions.push(intm);
479+
void logger.log('Reading query history');
480+
await qhm.readQueryHistory();
477481

478482
void logger.log('Initializing compare panel interface.');
479483
const cmpm = new CompareInterfaceManager(
@@ -844,14 +848,6 @@ async function activateWithInstalledDistribution(
844848
)
845849
);
846850

847-
void logger.log('Initializing variant analysis results view.');
848-
const rqm = new RemoteQueriesManager(ctx, cliServer, qhm, queryStorageDir, logger);
849-
ctx.subscriptions.push(rqm);
850-
851-
// wait until after the remote queries manager is initialized to read the query history
852-
// since the rqm is notified of queries being added.
853-
await qhm.readQueryHistory();
854-
855851

856852
registerRemoteQueryTextProvider();
857853

@@ -884,9 +880,10 @@ async function activateWithInstalledDistribution(
884880

885881
ctx.subscriptions.push(
886882
commandRunner('codeQL.monitorRemoteQuery', async (
887-
queryItem: RemoteQueryHistoryItem,
883+
queryId: string,
884+
query: RemoteQuery,
888885
token: CancellationToken) => {
889-
await rqm.monitorRemoteQuery(queryItem, token);
886+
await rqm.monitorRemoteQuery(queryId, query, token);
890887
}));
891888

892889
ctx.subscriptions.push(

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

Lines changed: 71 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ import { CliVersionConstraint } from './cli';
4040
import { HistoryItemLabelProvider } from './history-item-label-provider';
4141
import { Credentials } from './authentication';
4242
import { cancelRemoteQuery } from './remote-queries/gh-actions-api-client';
43+
import { RemoteQueriesManager } from './remote-queries/remote-queries-manager';
44+
import { RemoteQueryHistoryItem } from './remote-queries/remote-query-history-item';
45+
import { InterfaceManager } from './interface';
46+
import { WebviewReveal } from './interface-utils';
4347

4448
/**
4549
* query-history.ts
@@ -307,21 +311,11 @@ export class QueryHistoryManager extends DisposableObject {
307311
queryHistoryScrubber: Disposable | undefined;
308312
private queryMetadataStorageLocation;
309313

310-
private readonly _onDidAddQueryItem = super.push(new EventEmitter<QueryHistoryInfo>());
311-
readonly onDidAddQueryItem: Event<QueryHistoryInfo> = this
312-
._onDidAddQueryItem.event;
313-
314-
private readonly _onDidRemoveQueryItem = super.push(new EventEmitter<QueryHistoryInfo>());
315-
readonly onDidRemoveQueryItem: Event<QueryHistoryInfo> = this
316-
._onDidRemoveQueryItem.event;
317-
318-
private readonly _onWillOpenQueryItem = super.push(new EventEmitter<QueryHistoryInfo>());
319-
readonly onWillOpenQueryItem: Event<QueryHistoryInfo> = this
320-
._onWillOpenQueryItem.event;
321-
322314
constructor(
323315
private readonly qs: QueryServerClient,
324316
private readonly dbm: DatabaseManager,
317+
private readonly localQueriesInterfaceManager: InterfaceManager,
318+
private readonly remoteQueriesManager: RemoteQueriesManager,
325319
private readonly queryStorageDir: string,
326320
private readonly ctx: ExtensionContext,
327321
private readonly queryHistoryConfigListener: QueryHistoryConfig,
@@ -525,6 +519,7 @@ export class QueryHistoryManager extends DisposableObject {
525519
}));
526520

527521
this.registerQueryHistoryScrubber(queryHistoryConfigListener, ctx);
522+
this.registerToRemoteQueriesEvents();
528523
}
529524

530525
private getCredentials() {
@@ -548,12 +543,49 @@ export class QueryHistoryManager extends DisposableObject {
548543
);
549544
}
550545

546+
private registerToRemoteQueriesEvents() {
547+
const queryAddedSubscription = this.remoteQueriesManager.onRemoteQueryAdded(event => {
548+
this.addQuery({
549+
t: 'remote',
550+
status: QueryStatus.InProgress,
551+
completed: false,
552+
queryId: event.queryId,
553+
remoteQuery: event.query,
554+
});
555+
});
556+
557+
const queryRemovedSubscription = this.remoteQueriesManager.onRemoteQueryRemoved(async (event) => {
558+
const item = this.treeDataProvider.allHistory.find(i => i.t === 'remote' && i.queryId === event.queryId);
559+
if (item) {
560+
await this.removeRemoteQuery(item as RemoteQueryHistoryItem);
561+
}
562+
});
563+
564+
const queryStatusUpdateSubscription = this.remoteQueriesManager.onRemoteQueryStatusUpdate(async (event) => {
565+
const item = this.treeDataProvider.allHistory.find(i => i.t === 'remote' && i.queryId === event.queryId);
566+
if (item) {
567+
const remoteQueryHistoryItem = item as RemoteQueryHistoryItem;
568+
remoteQueryHistoryItem.status = event.status;
569+
remoteQueryHistoryItem.failureReason = event.failureReason;
570+
await this.refreshTreeView();
571+
} else {
572+
void logger.log('Variant analysis status update event received for unknown variant analysis');
573+
}
574+
});
575+
576+
this.push(queryAddedSubscription);
577+
this.push(queryRemovedSubscription);
578+
this.push(queryStatusUpdateSubscription);
579+
}
580+
551581
async readQueryHistory(): Promise<void> {
552582
void logger.log(`Reading cached query history from '${this.queryMetadataStorageLocation}'.`);
553583
const history = await slurpQueryHistory(this.queryMetadataStorageLocation);
554584
this.treeDataProvider.allHistory = history;
555-
this.treeDataProvider.allHistory.forEach((item) => {
556-
this._onDidAddQueryItem.fire(item);
585+
this.treeDataProvider.allHistory.forEach(async (item) => {
586+
if (item.t === 'remote') {
587+
await this.remoteQueriesManager.rehydrateRemoteQuery(item.queryId, item.remoteQuery, item.status);
588+
}
557589
});
558590
}
559591

@@ -619,26 +651,30 @@ export class QueryHistoryManager extends DisposableObject {
619651
await item.completedQuery?.query.deleteQuery();
620652
}
621653
} else {
622-
// Remote queries can be removed locally, but not remotely.
623-
// The user must cancel the query on GitHub Actions explicitly.
624-
this.treeDataProvider.remove(item);
625-
void logger.log(`Deleted ${this.labelProvider.getLabel(item)}.`);
626-
if (item.status === QueryStatus.InProgress) {
627-
void logger.log('The variant analysis is still running on GitHub Actions. To cancel there, you must go to the workflow run in your browser.');
628-
}
629-
630-
this._onDidRemoveQueryItem.fire(item);
654+
await this.removeRemoteQuery(item);
631655
}
632-
633656
}));
657+
634658
await this.writeQueryHistory();
635659
const current = this.treeDataProvider.getCurrent();
636660
if (current !== undefined) {
637661
await this.treeView.reveal(current, { select: true });
638-
this._onWillOpenQueryItem.fire(current);
662+
await this.openQueryResults(current);
639663
}
640664
}
641665

666+
private async removeRemoteQuery(item: RemoteQueryHistoryItem): Promise<void> {
667+
// Remote queries can be removed locally, but not remotely.
668+
// The user must cancel the query on GitHub Actions explicitly.
669+
this.treeDataProvider.remove(item);
670+
void logger.log(`Deleted ${this.labelProvider.getLabel(item)}.`);
671+
if (item.status === QueryStatus.InProgress) {
672+
void logger.log('The variant analysis is still running on GitHub Actions. To cancel there, you must go to the workflow run in your browser.');
673+
}
674+
675+
await this.remoteQueriesManager.removeRemoteQuery(item.queryId);
676+
}
677+
642678
async handleSortByName() {
643679
if (this.treeDataProvider.sortOrder === SortOrder.NameAsc) {
644680
this.treeDataProvider.sortOrder = SortOrder.NameDesc;
@@ -739,7 +775,7 @@ export class QueryHistoryManager extends DisposableObject {
739775
} else {
740776
// show results on single click only if query is completed successfully.
741777
if (finalSingleItem.status === QueryStatus.Completed) {
742-
await this._onWillOpenQueryItem.fire(finalSingleItem);
778+
await this.openQueryResults(finalSingleItem);
743779
}
744780
}
745781
}
@@ -1027,7 +1063,6 @@ export class QueryHistoryManager extends DisposableObject {
10271063
addQuery(item: QueryHistoryInfo) {
10281064
this.treeDataProvider.pushQuery(item);
10291065
this.updateTreeViewSelectionIfVisible();
1030-
this._onDidAddQueryItem.fire(item);
10311066
}
10321067

10331068
/**
@@ -1228,4 +1263,13 @@ the file in the file explorer and dragging it into the workspace.`
12281263
this.treeDataProvider.refresh();
12291264
await this.writeQueryHistory();
12301265
}
1266+
1267+
private async openQueryResults(item: QueryHistoryInfo) {
1268+
if (item.t === 'local') {
1269+
await this.localQueriesInterfaceManager.showResults(item as CompletedLocalQueryInfo, WebviewReveal.Forced, false);
1270+
}
1271+
else if (item.t === 'remote') {
1272+
await this.remoteQueriesManager.openRemoteQueryResults(item.queryId);
1273+
}
1274+
}
12311275
}

0 commit comments

Comments
 (0)