@@ -3,14 +3,24 @@ import type { ModelingEvents } from "./modeling-events";
33import type { DatabaseItem } from "../databases/local-databases" ;
44import type { ModelEvaluationRun } from "./model-evaluation-run" ;
55import { DisposableObject } from "../common/disposable-object" ;
6- import { sleep } from "../common/time" ;
76import type { ModelEvaluationRunState } from "./shared/model-evaluation-run-state" ;
7+ import type { BaseLogger } from "../common/logging" ;
8+ import type { CodeQLCliServer } from "../codeql-cli/cli" ;
9+ import type { VariantAnalysisManager } from "../variant-analysis/variant-analysis-manager" ;
10+ import type { QueryLanguage } from "../common/query-language" ;
11+ import { resolveCodeScanningQueryPack } from "../variant-analysis/code-scanning-pack" ;
12+ import { withProgress } from "../common/vscode/progress" ;
13+ import type { VariantAnalysis } from "../variant-analysis/shared/variant-analysis" ;
814
915export class ModelEvaluator extends DisposableObject {
1016 public constructor (
17+ private readonly logger : BaseLogger ,
18+ private readonly cliServer : CodeQLCliServer ,
1119 private readonly modelingStore : ModelingStore ,
1220 private readonly modelingEvents : ModelingEvents ,
21+ private readonly variantAnalysisManager : VariantAnalysisManager ,
1322 private readonly dbItem : DatabaseItem ,
23+ private readonly language : QueryLanguage ,
1424 private readonly updateView : (
1525 run : ModelEvaluationRunState ,
1626 ) => Promise < void > ,
@@ -28,18 +38,48 @@ export class ModelEvaluator extends DisposableObject {
2838 } ;
2939 this . modelingStore . updateModelEvaluationRun ( this . dbItem , evaluationRun ) ;
3040
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- } ) ;
41+ // Build pack
42+ const qlPack = await resolveCodeScanningQueryPack (
43+ this . logger ,
44+ this . cliServer ,
45+ this . language ,
46+ ) ;
47+
48+ if ( ! qlPack ) {
49+ this . modelingStore . updateModelEvaluationRun ( this . dbItem , undefined ) ;
50+ throw new Error ( "Unable to trigger evaluation run" ) ;
51+ }
52+
53+ // Submit variant analysis and monitor progress
54+ return withProgress (
55+ async ( progress , token ) => {
56+ let variantAnalysisId : number | undefined = undefined ;
57+ try {
58+ variantAnalysisId =
59+ await this . variantAnalysisManager . runVariantAnalysis (
60+ qlPack ,
61+ progress ,
62+ token ,
63+ ) ;
64+ } catch ( e ) {
65+ this . modelingStore . updateModelEvaluationRun ( this . dbItem , undefined ) ;
66+ throw e ;
67+ }
68+
69+ if ( variantAnalysisId ) {
70+ this . monitorVariantAnalysis ( variantAnalysisId ) ;
71+ } else {
72+ this . modelingStore . updateModelEvaluationRun ( this . dbItem , undefined ) ;
73+ throw new Error (
74+ "Unable to trigger variant analysis for evaluation run" ,
75+ ) ;
76+ }
77+ } ,
78+ {
79+ title : "Run Variant Analysis" ,
80+ cancellable : true ,
81+ } ,
82+ ) ;
4383 }
4484
4585 public async stopEvaluation ( ) {
@@ -55,19 +95,51 @@ export class ModelEvaluator extends DisposableObject {
5595 private registerToModelingEvents ( ) {
5696 this . push (
5797 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 ) ;
98+ if ( event . dbUri === this . dbItem . databaseUri . toString ( ) ) {
99+ if ( ! event . evaluationRun ) {
100+ await this . updateView ( {
101+ isPreparing : false ,
102+ variantAnalysis : undefined ,
103+ } ) ;
104+ } else {
105+ const variantAnalysis = await this . getVariantAnalysisForRun (
106+ event . evaluationRun ,
107+ ) ;
108+ const run : ModelEvaluationRunState = {
109+ isPreparing : event . evaluationRun . isPreparing ,
110+ variantAnalysis,
111+ } ;
112+ await this . updateView ( run ) ;
113+ }
69114 }
70115 } ) ,
71116 ) ;
72117 }
118+
119+ private async getVariantAnalysisForRun (
120+ evaluationRun : ModelEvaluationRun ,
121+ ) : Promise < VariantAnalysis | undefined > {
122+ if ( evaluationRun . variantAnalysisId ) {
123+ return await this . variantAnalysisManager . getVariantAnalysis (
124+ evaluationRun . variantAnalysisId ,
125+ ) ;
126+ }
127+ return undefined ;
128+ }
129+
130+ private monitorVariantAnalysis ( variantAnalysisId : number ) {
131+ this . push (
132+ this . variantAnalysisManager . onVariantAnalysisStatusUpdated (
133+ async ( variantAnalysis ) => {
134+ // Make sure it's the variant analysis we're interested in
135+ if ( variantAnalysisId === variantAnalysis . id ) {
136+ await this . updateView ( {
137+ isPreparing : false ,
138+ variantAnalysis,
139+ } ) ;
140+ }
141+ } ,
142+ ) ,
143+ ) ;
144+ }
73145}
0 commit comments