-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Queries): inline suggestions [YTFRONT-4612]
- Loading branch information
1 parent
a6972a1
commit 4639d41
Showing
12 changed files
with
303 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import {Request, Response} from '@gravity-ui/expresskit'; | ||
import ServerFactory from '../ServerFactory'; | ||
import {ErrorWithCode, sendAndLogError} from '../utils'; | ||
import {QuerySuggestTelemetryData, QuerySuggestionsData} from '../../shared/suggestApi'; | ||
|
||
const getQuerySuggestApi = () => { | ||
const suggestApi = ServerFactory.createQuerySuggestApi(); | ||
|
||
if (!suggestApi) { | ||
throw new ErrorWithCode(400, 'Query suggest api is not configured'); | ||
} | ||
|
||
return suggestApi; | ||
}; | ||
|
||
export const getQuerySuggestions = async (req: Request, res: Response) => { | ||
try { | ||
const suggestApi = getQuerySuggestApi(); | ||
const requestId = req.ctx.getMetadata()['x-request-id']; | ||
const {contextId, query, line, column, engine} = req.query as Omit< | ||
QuerySuggestionsData, | ||
'requestId' | ||
>; | ||
|
||
const data = await suggestApi.getQuerySuggestions(req, { | ||
requestId, | ||
contextId, | ||
query, | ||
line, | ||
column, | ||
engine, | ||
}); | ||
res.status(200).json(data); | ||
} catch (e) { | ||
sendAndLogError(req.ctx, res, 500, e as Error); | ||
} | ||
}; | ||
|
||
export const sendTelemetry = async (req: Request, res: Response) => { | ||
try { | ||
const suggestApi = getQuerySuggestApi(); | ||
const telemetry: QuerySuggestTelemetryData = req.body.telemetry; | ||
|
||
await suggestApi.sendTelemetry(req, telemetry); | ||
res.status(200).json({success: true}); | ||
} catch (e) { | ||
sendAndLogError(req.ctx, res, 500, e as Error); | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import {Request} from '@gravity-ui/expresskit'; | ||
|
||
export interface SuggestApi { | ||
getQuerySuggestions( | ||
req: Request, | ||
queryData: QuerySuggestionsData, | ||
): Promise<{items: string[]; requestId: string}>; | ||
sendTelemetry(req: Request, telemetryData: QuerySuggestTelemetryData): Promise<void>; | ||
} | ||
|
||
export type QuerySuggestionsData = { | ||
requestId: string; | ||
contextId: string; | ||
query: string; | ||
line: string; | ||
column: string; | ||
engine: string; | ||
}; | ||
|
||
export type TelemetryData = { | ||
requestId: string; | ||
timestamp: number; | ||
}; | ||
|
||
export type AcceptedTelemetryData = TelemetryData & { | ||
type: 'accepted'; | ||
acceptedText: string; | ||
convertedText: string; | ||
}; | ||
|
||
export type DiscardedTelemetryData = TelemetryData & { | ||
type: 'discarded'; | ||
reason: 'OnCancel'; | ||
discardedText: string; | ||
}; | ||
|
||
export type IgnoredTelemetryData = TelemetryData & { | ||
type: 'ignored'; | ||
ignoredText: string; | ||
}; | ||
|
||
export type QuerySuggestTelemetryData = | ||
| AcceptedTelemetryData | ||
| DiscardedTelemetryData | ||
| IgnoredTelemetryData; |
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
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
26 changes: 26 additions & 0 deletions
26
packages/ui/src/ui/pages/query-tracker/querySuggestionsModule/api.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,26 @@ | ||
import {QueryEngine} from '../module/engines'; | ||
import axios from 'axios'; | ||
import {QuerySuggestTelemetryData} from '../../../../shared/suggestApi'; | ||
|
||
export type QuerySuggestionProps = { | ||
contextId: string; | ||
query: string; | ||
line: number; | ||
column: number; | ||
engine: QueryEngine; | ||
}; | ||
const BASE_PATH = '/api/query-suggestions'; | ||
|
||
export const getQuerySuggestions = async (data: QuerySuggestionProps) => { | ||
const response = await axios.get<{items: string[]; requestId: string}>(`${BASE_PATH}/suggest`, { | ||
params: data, | ||
}); | ||
return response.data; | ||
}; | ||
|
||
export const sendQuerySuggestionsTelemetry = async (data: QuerySuggestTelemetryData) => { | ||
const response = await axios.post(`${BASE_PATH}/telemetry`, { | ||
telemetry: data, | ||
}); | ||
return response.data; | ||
}; |
93 changes: 93 additions & 0 deletions
93
packages/ui/src/ui/pages/query-tracker/querySuggestionsModule/createInlineSuggestions.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,93 @@ | ||
import {CancellationToken, Position, editor, languages} from 'monaco-editor'; | ||
import {getRangeToInsertSuggestion} from '../../../libs/monaco-yql-languages/helpers/getRangeToInsertSuggestion'; | ||
import {QueryEngine} from '../module/engines'; | ||
import debounce_ from 'lodash/debounce'; | ||
import {getWindowStore} from '../../../store/window-store'; | ||
import {getQuerySuggestions, sendQuerySuggestionsTelemetry} from './api'; | ||
import { | ||
PrevAction, | ||
setPrevAction, | ||
setRequestId, | ||
setSuggestions, | ||
} from '../module/querySuggestions/querySuggestionsSlice'; | ||
import { | ||
selectPrevAction, | ||
selectQuerySuggestionsContextId, | ||
} from '../module/querySuggestions/selectors'; | ||
import {AcceptedTelemetryData} from '../../../../shared/suggestApi'; | ||
import {getQuerySuggestionsEnabled} from '../../../store/selectors/settings/settings-ts'; | ||
|
||
const debouncedGetSuggestions = debounce_(getQuerySuggestions, 200); | ||
const store = getWindowStore(); | ||
|
||
export const createInlineSuggestions = | ||
(engine: QueryEngine) => | ||
async ( | ||
model: editor.ITextModel, | ||
monacoCursorPosition: Position, | ||
_context: languages.InlineCompletionContext, | ||
_token: CancellationToken, | ||
): Promise<{items: languages.InlineCompletion[]}> => { | ||
const state = store.getState(); | ||
const contextId = selectQuerySuggestionsContextId(state); | ||
const prevAction = selectPrevAction(state); | ||
const enabled = getQuerySuggestionsEnabled(state); | ||
|
||
if (!enabled) { | ||
return { | ||
items: [], | ||
}; | ||
} | ||
|
||
const response = await debouncedGetSuggestions({ | ||
contextId, | ||
query: model.getValue(), | ||
line: monacoCursorPosition.lineNumber, | ||
column: monacoCursorPosition.column, | ||
engine, | ||
}); | ||
|
||
if (!response) { | ||
return { | ||
items: [], | ||
}; | ||
} | ||
|
||
let action: PrevAction = response.items.length > 0 ? 'received' : 'empty'; | ||
if (prevAction === 'received') { | ||
action = 'ignored'; | ||
await sendQuerySuggestionsTelemetry({ | ||
requestId: response.requestId, | ||
timestamp: Date.now(), | ||
type: 'ignored', | ||
ignoredText: response.items[0], | ||
}); | ||
} | ||
|
||
store.dispatch(setPrevAction(action)); | ||
store.dispatch(setSuggestions(response.items)); | ||
store.dispatch(setRequestId(response.requestId)); | ||
|
||
const range = getRangeToInsertSuggestion(model, monacoCursorPosition); | ||
return { | ||
items: response.items.map((item) => { | ||
const data: AcceptedTelemetryData = { | ||
requestId: response.requestId, | ||
timestamp: Date.now(), | ||
type: 'accepted', | ||
acceptedText: item, | ||
convertedText: item, | ||
}; | ||
|
||
return { | ||
insertText: item, | ||
range, | ||
command: { | ||
id: 'querySuggestionsTelemetry', | ||
title: 'string', | ||
arguments: [data], | ||
}, | ||
}; | ||
}), | ||
}; | ||
}; |
Oops, something went wrong.