-
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.
chore(Queries): telemetry [YTFRONT-4612]
- Loading branch information
1 parent
a0c94d0
commit ced7ff9
Showing
8 changed files
with
175 additions
and
46 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
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 |
---|---|---|
@@ -1,5 +1,33 @@ | ||
import {Request} from '@gravity-ui/expresskit'; | ||
|
||
export interface NeuralNetworkApi { | ||
getQuerySuggestions(req: Request): Promise<string[]>; | ||
getQuerySuggestions(req: Request): Promise<{items: string[]; requestId: string}>; | ||
sendTelemetry(req: Request): Promise<void>; | ||
} | ||
|
||
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 NeuralNetworkTelemetryData = | ||
| 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
29 changes: 29 additions & 0 deletions
29
packages/ui/src/ui/libs/monaco-yql-languages/neuralNetwork/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,29 @@ | ||
import {QueryEngine} from '../../../pages/query-tracker/module/engines'; | ||
import axios from 'axios'; | ||
import {NeuralNetworkTelemetryData} from '../../../../shared/neuralNetwork'; | ||
|
||
export type QuerySuggestionProps = { | ||
contextId: string; | ||
query: string; | ||
line: number; | ||
column: number; | ||
engine: QueryEngine; | ||
}; | ||
const BASE_PATH = '/api/neural-network'; | ||
|
||
export const getNeuralNetworkSuggestions = async (data: QuerySuggestionProps) => { | ||
const response = await axios.get<{items: string[]; requestId: string}>( | ||
`${BASE_PATH}/query-suggestions`, | ||
{ | ||
params: data, | ||
}, | ||
); | ||
return response.data; | ||
}; | ||
|
||
export const sendNeuralNetworkTelemetry = async (data: NeuralNetworkTelemetryData) => { | ||
const response = await axios.post(`${BASE_PATH}/send-telemetry`, { | ||
telemetry: data, | ||
}); | ||
return response.data; | ||
}; |
46 changes: 46 additions & 0 deletions
46
packages/ui/src/ui/libs/monaco-yql-languages/neuralNetwork/useMonacoNeuralNetwork.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,46 @@ | ||
import {useCallback, useEffect} from 'react'; | ||
import * as monaco from 'monaco-editor'; | ||
import { | ||
selectNeuralNetworkRequestId, | ||
selectNeuralNetworkSuggestions, | ||
} from '../../../pages/query-tracker/module/neuralNetwork/selectors'; | ||
import {sendNeuralNetworkTelemetry} from './api'; | ||
import {useSelector} from 'react-redux'; | ||
import {DiscardedTelemetryData} from '../../../../shared/neuralNetwork'; | ||
|
||
export const useMonacoNeuralNetwork = (editor?: monaco.editor.IStandaloneCodeEditor) => { | ||
const requestId = useSelector(selectNeuralNetworkRequestId); | ||
const suggestions = useSelector(selectNeuralNetworkSuggestions); | ||
|
||
const handleCancelTelemetry = useCallback( | ||
(e: KeyboardEvent) => { | ||
if (e.key === 'Escape' && editor) { | ||
const data: DiscardedTelemetryData = { | ||
requestId, | ||
timestamp: Date.now(), | ||
type: 'discarded', | ||
reason: 'OnCancel', | ||
discardedText: suggestions[0], | ||
}; | ||
editor.trigger(undefined, 'neuralNetworkTelemetry', data); | ||
} | ||
}, | ||
[editor, requestId, suggestions], | ||
); | ||
|
||
useEffect(() => { | ||
monaco.editor.registerCommand('neuralNetworkTelemetry', async (_accessor, ...args) => { | ||
if (args.length > 0) { | ||
await sendNeuralNetworkTelemetry(args[0]); | ||
} | ||
}); | ||
}, []); | ||
|
||
useEffect(() => { | ||
document.addEventListener('keydown', handleCancelTelemetry, true); | ||
|
||
return () => { | ||
document.removeEventListener('keydown', handleCancelTelemetry, true); | ||
}; | ||
}, [handleCancelTelemetry, editor]); | ||
}; |
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