-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
213 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
src/extension/commands/utils/composer_message_manager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import * as vscode from 'vscode'; | ||
import {v1 as uuid} from 'uuid'; | ||
import {WebviewMessageManager} from '../../webview_message_manager'; | ||
import { | ||
ComposerMessage, | ||
ComposerMessageType, | ||
ComposerPageMessage, | ||
ComposerPageMessageRunQuery, | ||
ComposerPageMessageType, | ||
} from '../../../common/types/message_types'; | ||
import {DocumentMetadata, QuerySpec} from '../../../common/types/query_spec'; | ||
import {ModelDef, SearchValueMapResult} from '@malloydata/malloy'; | ||
import {runMalloyQuery} from './run_query_utils'; | ||
import {WorkerConnection} from '../../worker_connection'; | ||
import {getSourceDef} from '../../../common/schema'; | ||
|
||
export class ComposerMessageManager | ||
extends WebviewMessageManager<ComposerMessage, ComposerPageMessage> | ||
implements vscode.Disposable | ||
{ | ||
constructor( | ||
private worker: WorkerConnection, | ||
composerPanel: vscode.WebviewPanel, | ||
private documentMeta: DocumentMetadata, | ||
private modelDef: ModelDef, | ||
private sourceName: string, | ||
viewName?: string | ||
) { | ||
super(composerPanel); | ||
|
||
this.postMessage({ | ||
type: ComposerMessageType.NewModel, | ||
documentMeta, | ||
modelDef, | ||
sourceName, | ||
viewName, | ||
}); | ||
|
||
this.onReceiveMessage(async message => { | ||
switch (message.type) { | ||
case ComposerPageMessageType.RunQuery: | ||
await this.runQuery(message); | ||
break; | ||
} | ||
}); | ||
|
||
void this.initializeIndex(); | ||
} | ||
|
||
async runQuery(message: ComposerPageMessageRunQuery) { | ||
{ | ||
const {id, query, queryName} = message; | ||
try { | ||
const result = await this.runQueryWithProgress(id, queryName, query); | ||
if (result) { | ||
this.postMessage({ | ||
type: ComposerMessageType.ResultSuccess, | ||
id, | ||
result, | ||
}); | ||
} else { | ||
this.postMessage({ | ||
type: ComposerMessageType.ResultError, | ||
id, | ||
error: 'No results', | ||
}); | ||
} | ||
} catch (error) { | ||
this.postMessage({ | ||
type: ComposerMessageType.ResultError, | ||
id, | ||
error: error instanceof Error ? error.message : `${error}`, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
async initializeIndex(): Promise<SearchValueMapResult[] | undefined> { | ||
const sourceDef = getSourceDef(this.modelDef, this.sourceName); | ||
const indexQuery = sourceDef.fields.find( | ||
({name, as}) => (as || name) === 'search_index' | ||
); | ||
const limit = 10; | ||
|
||
if (indexQuery) { | ||
const searchMapMalloy = ` | ||
run: ${this.sourceName} | ||
-> ${indexQuery.as || indexQuery.name} | ||
-> { | ||
where: fieldType = 'string' | ||
group_by: fieldName | ||
aggregate: cardinality is count(fieldValue) | ||
nest: values is { | ||
select: fieldValue, weight | ||
order_by: weight desc | ||
limit: ${limit} | ||
} | ||
limit: 1000 | ||
} | ||
`; | ||
const result = await this.runQueryWithProgress( | ||
uuid(), | ||
`Search index for ${this.sourceName}`, | ||
searchMapMalloy | ||
); | ||
if (result) { | ||
this.postMessage({ | ||
type: ComposerMessageType.SearchIndex, | ||
result, | ||
}); | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
async runQueryWithProgress(id: string, queryName: string, query: string) { | ||
return vscode.window.withProgress( | ||
{ | ||
location: vscode.ProgressLocation.Notification, | ||
title: queryName, | ||
cancellable: true, | ||
}, | ||
async (progress, cancellationToken) => { | ||
const querySpec: QuerySpec = { | ||
type: 'string', | ||
text: query, | ||
documentMeta: this.documentMeta, | ||
}; | ||
const result = await runMalloyQuery( | ||
this.worker, | ||
querySpec, | ||
id, | ||
queryName, | ||
{withWebview: false}, | ||
cancellationToken, | ||
progress | ||
); | ||
return result; | ||
} | ||
); | ||
} | ||
|
||
dispose() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters