Skip to content

Commit 948c1e2

Browse files
authored
Add model evaluation run to store and view (#3385)
1 parent 07d9c4e commit 948c1e2

File tree

9 files changed

+188
-16
lines changed

9 files changed

+188
-16
lines changed

extensions/ql-vscode/src/common/interface-types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import type {
2525
UrlValueResolvable,
2626
} from "./raw-result-types";
2727
import type { AccessPathSuggestionOptions } from "../model-editor/suggestions";
28+
import type { ModelEvaluationRunState } from "../model-editor/shared/model-evaluation-run-state";
2829

2930
/**
3031
* This module contains types and code that are shared between
@@ -638,6 +639,11 @@ interface SetAccessPathSuggestionsMessage {
638639
accessPathSuggestions: AccessPathSuggestionOptions;
639640
}
640641

642+
interface SetModelEvaluationRunMessage {
643+
t: "setModelEvaluationRun";
644+
run: ModelEvaluationRunState | undefined;
645+
}
646+
641647
export type ToModelEditorMessage =
642648
| SetExtensionPackStateMessage
643649
| SetMethodsMessage
@@ -646,7 +652,8 @@ export type ToModelEditorMessage =
646652
| SetInProgressMethodsMessage
647653
| SetProcessedByAutoModelMethodsMessage
648654
| RevealMethodMessage
649-
| SetAccessPathSuggestionsMessage;
655+
| SetAccessPathSuggestionsMessage
656+
| SetModelEvaluationRunMessage;
650657

651658
export type FromModelEditorMessage =
652659
| CommonFromViewMessages

extensions/ql-vscode/src/model-editor/model-editor-view.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,15 @@ import { LSPErrorCodes } from "vscode-languageclient";
5757
import type { AccessPathSuggestionOptions } from "./suggestions";
5858
import { runSuggestionsQuery } from "./suggestion-queries";
5959
import { parseAccessPathSuggestionRowsToOptions } from "./suggestions-bqrs";
60+
import { ModelEvaluator } from "./model-evaluator";
61+
import type { ModelEvaluationRunState } from "./shared/model-evaluation-run-state";
6062

6163
export class ModelEditorView extends AbstractWebview<
6264
ToModelEditorMessage,
6365
FromModelEditorMessage
6466
> {
6567
private readonly autoModeler: AutoModeler;
68+
private readonly modelEvaluator: ModelEvaluator;
6669
private readonly languageDefinition: ModelsAsDataLanguage;
6770

6871
public constructor(
@@ -101,6 +104,14 @@ export class ModelEditorView extends AbstractWebview<
101104
},
102105
);
103106
this.languageDefinition = getModelsAsDataLanguage(language);
107+
108+
this.modelEvaluator = new ModelEvaluator(
109+
modelingStore,
110+
modelingEvents,
111+
databaseItem,
112+
this.updateModelEvaluationRun.bind(this),
113+
);
114+
this.push(this.modelEvaluator);
104115
}
105116

106117
public async openView() {
@@ -338,10 +349,10 @@ export class ModelEditorView extends AbstractWebview<
338349
break;
339350
}
340351
case "startModelEvaluation":
341-
this.startModelEvaluation();
352+
await this.modelEvaluator.startEvaluation();
342353
break;
343354
case "stopModelEvaluation":
344-
this.stopModelEvaluation();
355+
await this.modelEvaluator.stopEvaluation();
345356
break;
346357
case "telemetry":
347358
telemetryListener?.sendUIInteraction(msg.action);
@@ -920,11 +931,10 @@ export class ModelEditorView extends AbstractWebview<
920931
this.modelingStore.addModifiedMethod(this.databaseItem, signature);
921932
}
922933

923-
private startModelEvaluation() {
924-
// Do nothing for now. This will be fleshed out in the near future.
925-
}
926-
927-
private stopModelEvaluation() {
928-
// Do nothing for now. This will be fleshed out in the near future.
934+
private async updateModelEvaluationRun(run: ModelEvaluationRunState) {
935+
await this.postMessage({
936+
t: "setModelEvaluationRun",
937+
run,
938+
});
929939
}
930940
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface ModelEvaluationRun {
2+
isPreparing: boolean;
3+
variantAnalysisId: number | undefined;
4+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import type { ModelingStore } from "./modeling-store";
2+
import type { ModelingEvents } from "./modeling-events";
3+
import type { DatabaseItem } from "../databases/local-databases";
4+
import type { ModelEvaluationRun } from "./model-evaluation-run";
5+
import { DisposableObject } from "../common/disposable-object";
6+
import { sleep } from "../common/time";
7+
import type { ModelEvaluationRunState } from "./shared/model-evaluation-run-state";
8+
9+
export class ModelEvaluator extends DisposableObject {
10+
public constructor(
11+
private readonly modelingStore: ModelingStore,
12+
private readonly modelingEvents: ModelingEvents,
13+
private readonly dbItem: DatabaseItem,
14+
private readonly updateView: (
15+
run: ModelEvaluationRunState,
16+
) => Promise<void>,
17+
) {
18+
super();
19+
20+
this.registerToModelingEvents();
21+
}
22+
23+
public async startEvaluation() {
24+
// Update store with evaluation run status
25+
const evaluationRun: ModelEvaluationRun = {
26+
isPreparing: true,
27+
variantAnalysisId: undefined,
28+
};
29+
this.modelingStore.updateModelEvaluationRun(this.dbItem, evaluationRun);
30+
31+
// For now, just wait 5 seconds and then update the store.
32+
// In the future, this will be replaced with the actual evaluation process.
33+
void sleep(5000).then(() => {
34+
const completedEvaluationRun: ModelEvaluationRun = {
35+
isPreparing: false,
36+
variantAnalysisId: undefined,
37+
};
38+
this.modelingStore.updateModelEvaluationRun(
39+
this.dbItem,
40+
completedEvaluationRun,
41+
);
42+
});
43+
}
44+
45+
public async stopEvaluation() {
46+
// For now just update the store.
47+
// This will be fleshed out in the near future.
48+
const evaluationRun: ModelEvaluationRun = {
49+
isPreparing: false,
50+
variantAnalysisId: undefined,
51+
};
52+
this.modelingStore.updateModelEvaluationRun(this.dbItem, evaluationRun);
53+
}
54+
55+
private registerToModelingEvents() {
56+
this.push(
57+
this.modelingEvents.onModelEvaluationRunChanged(async (event) => {
58+
if (
59+
event.evaluationRun &&
60+
event.dbUri === this.dbItem.databaseUri.toString()
61+
) {
62+
const run: ModelEvaluationRunState = {
63+
isPreparing: event.evaluationRun.isPreparing,
64+
65+
// TODO: Get variant analysis from id.
66+
variantAnalysis: undefined,
67+
};
68+
await this.updateView(run);
69+
}
70+
}),
71+
);
72+
}
73+
}

extensions/ql-vscode/src/model-editor/modeling-events.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { DisposableObject } from "../common/disposable-object";
33
import type { AppEvent, AppEventEmitter } from "../common/events";
44
import type { DatabaseItem } from "../databases/local-databases";
55
import type { Method, Usage } from "./method";
6+
import type { ModelEvaluationRun } from "./model-evaluation-run";
67
import type { ModeledMethod } from "./modeled-method";
78
import type { Mode } from "./shared/mode";
89

@@ -55,6 +56,11 @@ interface ProcessedByAutoModelMethodsChangedEvent {
5556
readonly methods: ReadonlySet<string>;
5657
}
5758

59+
interface ModelEvaluationRunChangedEvent {
60+
readonly dbUri: string;
61+
readonly evaluationRun: ModelEvaluationRun | undefined;
62+
}
63+
5864
interface RevealInModelEditorEvent {
5965
dbUri: string;
6066
method: Method;
@@ -76,6 +82,7 @@ export class ModelingEvents extends DisposableObject {
7682
public readonly onSelectedMethodChanged: AppEvent<SelectedMethodChangedEvent>;
7783
public readonly onInProgressMethodsChanged: AppEvent<InProgressMethodsChangedEvent>;
7884
public readonly onProcessedByAutoModelMethodsChanged: AppEvent<ProcessedByAutoModelMethodsChangedEvent>;
85+
public readonly onModelEvaluationRunChanged: AppEvent<ModelEvaluationRunChangedEvent>;
7986
public readonly onRevealInModelEditor: AppEvent<RevealInModelEditorEvent>;
8087
public readonly onFocusModelEditor: AppEvent<FocusModelEditorEvent>;
8188

@@ -90,6 +97,7 @@ export class ModelingEvents extends DisposableObject {
9097
private readonly onSelectedMethodChangedEventEmitter: AppEventEmitter<SelectedMethodChangedEvent>;
9198
private readonly onInProgressMethodsChangedEventEmitter: AppEventEmitter<InProgressMethodsChangedEvent>;
9299
private readonly onProcessedByAutoModelMethodsChangedEventEmitter: AppEventEmitter<ProcessedByAutoModelMethodsChangedEvent>;
100+
private readonly onModelEvaluationRunChangedEventEmitter: AppEventEmitter<ModelEvaluationRunChangedEvent>;
93101
private readonly onRevealInModelEditorEventEmitter: AppEventEmitter<RevealInModelEditorEvent>;
94102
private readonly onFocusModelEditorEventEmitter: AppEventEmitter<FocusModelEditorEvent>;
95103

@@ -155,6 +163,12 @@ export class ModelingEvents extends DisposableObject {
155163
this.onProcessedByAutoModelMethodsChanged =
156164
this.onProcessedByAutoModelMethodsChangedEventEmitter.event;
157165

166+
this.onModelEvaluationRunChangedEventEmitter = this.push(
167+
app.createEventEmitter<ModelEvaluationRunChangedEvent>(),
168+
);
169+
this.onModelEvaluationRunChanged =
170+
this.onModelEvaluationRunChangedEventEmitter.event;
171+
158172
this.onRevealInModelEditorEventEmitter = this.push(
159173
app.createEventEmitter<RevealInModelEditorEvent>(),
160174
);
@@ -273,6 +287,16 @@ export class ModelingEvents extends DisposableObject {
273287
});
274288
}
275289

290+
public fireModelEvaluationRunChangedEvent(
291+
dbUri: string,
292+
evaluationRun: ModelEvaluationRun | undefined,
293+
) {
294+
this.onModelEvaluationRunChangedEventEmitter.fire({
295+
dbUri,
296+
evaluationRun,
297+
});
298+
}
299+
276300
public fireRevealInModelEditorEvent(dbUri: string, method: Method) {
277301
this.onRevealInModelEditorEventEmitter.fire({
278302
dbUri,

extensions/ql-vscode/src/model-editor/modeling-store.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { DisposableObject } from "../common/disposable-object";
22
import type { DatabaseItem } from "../databases/local-databases";
33
import type { Method, Usage } from "./method";
4+
import type { ModelEvaluationRun } from "./model-evaluation-run";
45
import type { ModeledMethod } from "./modeled-method";
56
import type { ModelingEvents } from "./modeling-events";
67
import { INITIAL_HIDE_MODELED_METHODS_VALUE } from "./shared/hide-modeled-methods";
@@ -17,6 +18,7 @@ interface InternalDbModelingState {
1718
processedByAutoModelMethods: Set<string>;
1819
selectedMethod: Method | undefined;
1920
selectedUsage: Usage | undefined;
21+
modelEvaluationRun: ModelEvaluationRun | undefined;
2022
}
2123

2224
interface DbModelingState {
@@ -30,6 +32,7 @@ interface DbModelingState {
3032
readonly processedByAutoModelMethods: ReadonlySet<string>;
3133
readonly selectedMethod: Method | undefined;
3234
readonly selectedUsage: Usage | undefined;
35+
readonly modelEvaluationRun: ModelEvaluationRun | undefined;
3336
}
3437

3538
interface SelectedMethodDetails {
@@ -66,6 +69,7 @@ export class ModelingStore extends DisposableObject {
6669
selectedMethod: undefined,
6770
selectedUsage: undefined,
6871
inProgressMethods: new Set(),
72+
modelEvaluationRun: undefined,
6973
});
7074

7175
this.modelingEvents.fireDbOpenedEvent(databaseItem);
@@ -372,6 +376,15 @@ export class ModelingStore extends DisposableObject {
372376
});
373377
}
374378

379+
public updateModelEvaluationRun(
380+
dbItem: DatabaseItem,
381+
evaluationRun: ModelEvaluationRun,
382+
) {
383+
this.changeModelEvaluationRun(dbItem, (state) => {
384+
state.modelEvaluationRun = evaluationRun;
385+
});
386+
}
387+
375388
public getSelectedMethodDetails(): SelectedMethodDetails | undefined {
376389
const dbState = this.getInternalStateForActiveDb();
377390
if (!dbState) {
@@ -465,4 +478,18 @@ export class ModelingStore extends DisposableObject {
465478
state.processedByAutoModelMethods,
466479
);
467480
}
481+
482+
private changeModelEvaluationRun(
483+
dbItem: DatabaseItem,
484+
updateState: (state: InternalDbModelingState) => void,
485+
) {
486+
const state = this.getState(dbItem);
487+
488+
updateState(state);
489+
490+
this.modelingEvents.fireModelEvaluationRunChangedEvent(
491+
dbItem.databaseUri.toString(),
492+
state.modelEvaluationRun,
493+
);
494+
}
468495
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { VariantAnalysisStatus } from "../../variant-analysis/shared/variant-analysis";
2+
import type { VariantAnalysis } from "../../variant-analysis/shared/variant-analysis";
3+
4+
export interface ModelEvaluationRunState {
5+
isPreparing: boolean;
6+
variantAnalysis: VariantAnalysis | undefined;
7+
}
8+
9+
export function modelEvaluationRunIsRunning(
10+
run: ModelEvaluationRunState,
11+
): boolean {
12+
return (
13+
run.isPreparing ||
14+
!!(
15+
run.variantAnalysis &&
16+
run.variantAnalysis.status === VariantAnalysisStatus.InProgress
17+
)
18+
);
19+
}

0 commit comments

Comments
 (0)