-
Notifications
You must be signed in to change notification settings - Fork 17
feat: add endpoint to connect to db code snippets #2198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,14 @@ import React from 'react'; | |
|
|
||
| import NiceModal from '@ebay/nice-modal-react'; | ||
| import {Dialog, Tabs} from '@gravity-ui/uikit'; | ||
| import {skipToken} from '@reduxjs/toolkit/query'; | ||
|
|
||
| import {tenantApi} from '../../store/reducers/tenant/tenant'; | ||
| import {cn} from '../../utils/cn'; | ||
| import {useTypedSelector} from '../../utils/hooks'; | ||
| import {useClusterNameFromQuery} from '../../utils/hooks/useDatabaseFromQuery'; | ||
| import {LinkWithIcon} from '../LinkWithIcon/LinkWithIcon'; | ||
| import {LoaderWrapper} from '../LoaderWrapper/LoaderWrapper'; | ||
| import {YDBSyntaxHighlighterLazy} from '../SyntaxHighlighter/lazy'; | ||
|
|
||
| import {getDocsLink} from './getDocsLink'; | ||
|
|
@@ -32,9 +37,26 @@ interface ConnectToDBDialogProps extends SnippetParams { | |
| onClose: VoidFunction; | ||
| } | ||
|
|
||
| function ConnectToDBDialog({open, onClose, database, endpoint}: ConnectToDBDialogProps) { | ||
| function ConnectToDBDialog({ | ||
| open, | ||
| onClose, | ||
| database, | ||
| endpoint: endpointFromProps, | ||
| }: ConnectToDBDialogProps) { | ||
| const [activeTab, setActiveTab] = React.useState<SnippetLanguage>('bash'); | ||
|
|
||
| const clusterName = useClusterNameFromQuery(); | ||
| const singleClusterMode = useTypedSelector((state) => state.singleClusterMode); | ||
|
|
||
| // If there is enpdoint from props, we don't need to request tenant data | ||
| // Also we should not request tenant data if we are in single cluster mode | ||
| // Since there is no ControlPlane data in this case | ||
| const shouldRequestTenantData = database && !endpointFromProps && !singleClusterMode; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I left |
||
| const params = shouldRequestTenantData ? {path: database, clusterName} : skipToken; | ||
| const {currentData: tenantData, isLoading: isTenantDataLoading} = | ||
| tenantApi.useGetTenantInfoQuery(params); | ||
| const endpoint = endpointFromProps ?? tenantData?.ControlPlane?.endpoint; | ||
|
|
||
| const snippet = getSnippetCode(activeTab, {database, endpoint}); | ||
| const docsLink = getDocsLink(activeTab); | ||
|
|
||
|
|
@@ -52,12 +74,14 @@ function ConnectToDBDialog({open, onClose, database, endpoint}: ConnectToDBDialo | |
| className={b('dialog-tabs')} | ||
| /> | ||
| <div className={b('snippet-container')}> | ||
| <YDBSyntaxHighlighterLazy | ||
| language={activeTab} | ||
| text={snippet} | ||
| transparentBackground={false} | ||
| withClipboardButton={{alwaysVisible: true}} | ||
| /> | ||
| <LoaderWrapper loading={isTenantDataLoading}> | ||
| <YDBSyntaxHighlighterLazy | ||
| language={activeTab} | ||
| text={snippet} | ||
| transparentBackground={false} | ||
| withClipboardButton={{alwaysVisible: true}} | ||
| /> | ||
| </LoaderWrapper> | ||
| </div> | ||
| {docsLink ? ( | ||
| <LinkWithIcon | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import {prepareEndpoint} from '../utils'; | ||
|
|
||
| describe('prepareEndpoint', () => { | ||
| test('should remove all search params', () => { | ||
| const input = 'grpc://example.com:2139/?database=/root/test¶m=value'; | ||
| const expected = 'grpc://example.com:2139'; | ||
| expect(prepareEndpoint(input)).toBe(expected); | ||
| }); | ||
| test('should handle URL without path or params', () => { | ||
| const input = 'grpc://example.com:2139'; | ||
| const expected = 'grpc://example.com:2139'; | ||
| expect(prepareEndpoint(input)).toBe(expected); | ||
| }); | ||
| test('should remove trailing slash from path', () => { | ||
| const input = 'grpc://example.com:2139/'; | ||
| const expected = 'grpc://example.com:2139'; | ||
| expect(prepareEndpoint(input)).toBe(expected); | ||
| }); | ||
| test('should handle complex paths', () => { | ||
| const input = 'grpc://example.com:2139/multi/level/path/?database=/root/test'; | ||
| const expected = 'grpc://example.com:2139/multi/level/path'; | ||
| expect(prepareEndpoint(input)).toBe(expected); | ||
| }); | ||
| test('should handle empty string', () => { | ||
| expect(prepareEndpoint('')).toBeUndefined(); | ||
| }); | ||
| test('should handle undefined input', () => { | ||
| expect(prepareEndpoint()).toBeUndefined(); | ||
| }); | ||
| test('should return undefined for invalid URL', () => { | ||
| const input = 'invalid-url'; | ||
| expect(prepareEndpoint(input)).toBeUndefined(); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // We have endpoint in format grpc://example.com:2139/?database=/root/test | ||
| // We need it to be like grpc://example.com:2139 to make code in snippets work | ||
| // We pass database to snippets as a separate param | ||
| export function prepareEndpoint(connectionString = '') { | ||
| try { | ||
| const urlObj = new URL(connectionString); | ||
| urlObj.search = ''; | ||
|
|
||
| let endpoint = urlObj.toString(); | ||
|
|
||
| // Remove trailing slash if present | ||
| if (endpoint.endsWith('/')) { | ||
| endpoint = endpoint.slice(0, -1); | ||
| } | ||
|
|
||
| return endpoint; | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,11 +12,7 @@ import {schemaApi} from '../../../../store/reducers/schema/schema'; | |
| import {tableSchemaDataApi} from '../../../../store/reducers/tableSchemaData'; | ||
| import type {EPathType, TEvDescribeSchemeResult} from '../../../../types/api/schema'; | ||
| import {valueIsDefined} from '../../../../utils'; | ||
| import { | ||
| useQueryExecutionSettings, | ||
| useTypedDispatch, | ||
| useTypedSelector, | ||
| } from '../../../../utils/hooks'; | ||
| import {useTypedDispatch, useTypedSelector} from '../../../../utils/hooks'; | ||
| import {getConfirmation} from '../../../../utils/hooks/withConfirmation/useChangeInputWithConfirmation'; | ||
| import {getSchemaControls} from '../../utils/controls'; | ||
| import { | ||
|
|
@@ -48,7 +44,6 @@ export function SchemaTree(props: SchemaTreeProps) { | |
| {currentData: actionsSchemaData, isFetching: isActionsDataFetching}, | ||
| ] = tableSchemaDataApi.useLazyGetTableSchemaDataQuery(); | ||
|
|
||
| const [querySettings] = useQueryExecutionSettings(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was used only in hook dependencies |
||
| const [createDirectoryOpen, setCreateDirectoryOpen] = React.useState(false); | ||
| const [parentPath, setParentPath] = React.useState(''); | ||
| const setSchemaTreeKey = useDispatchTreeKey(); | ||
|
|
@@ -144,8 +139,8 @@ export function SchemaTree(props: SchemaTreeProps) { | |
| dispatch, | ||
| input, | ||
| isActionsDataFetching, | ||
| isDirty, | ||
| onActivePathUpdate, | ||
| querySettings, | ||
| rootPath, | ||
| ]); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| import {createSelector, createSlice} from '@reduxjs/toolkit'; | ||
| import type {Dispatch, PayloadAction} from '@reduxjs/toolkit'; | ||
| import {skipToken} from '@reduxjs/toolkit/query'; | ||
| import {StringParam, useQueryParam} from 'use-query-params'; | ||
|
|
||
| import type {ClusterTab} from '../../../containers/Cluster/utils'; | ||
| import {clusterTabsIds, isClusterTab} from '../../../containers/Cluster/utils'; | ||
|
|
@@ -10,6 +9,7 @@ import {isClusterInfoV2} from '../../../types/api/cluster'; | |
| import type {TClusterInfo} from '../../../types/api/cluster'; | ||
| import type {TTabletStateInfo} from '../../../types/api/tablet'; | ||
| import {CLUSTER_DEFAULT_TITLE, DEFAULT_CLUSTER_TAB_KEY} from '../../../utils/constants'; | ||
| import {useClusterNameFromQuery} from '../../../utils/hooks/useDatabaseFromQuery'; | ||
| import {isQueryErrorResponse} from '../../../utils/query'; | ||
| import type {RootState} from '../../defaultStore'; | ||
| import {api} from '../api'; | ||
|
|
@@ -136,16 +136,24 @@ export const clusterApi = api.injectEndpoints({ | |
| }); | ||
|
|
||
| export function useClusterBaseInfo() { | ||
| const [clusterName] = useQueryParam('clusterName', StringParam); | ||
| const clusterNameFromQuery = useClusterNameFromQuery(); | ||
|
|
||
| const {currentData} = clusterApi.useGetClusterBaseInfoQuery(clusterName ?? skipToken); | ||
| const {currentData} = clusterApi.useGetClusterBaseInfoQuery(clusterNameFromQuery ?? skipToken); | ||
|
|
||
| const {solomon: monitoring, name, trace_view: traceView, ...data} = currentData || {}; | ||
| const {solomon: monitoring, name, title, trace_view: traceView, ...data} = currentData || {}; | ||
|
|
||
| // name is used for requests, title is used for display | ||
| // Example: | ||
| // Name: ydb_vla_dev02 | ||
| // Title: YDB DEV VLA02 | ||
| const clusterName = name ?? clusterNameFromQuery ?? undefined; | ||
| const clusterTitle = title ?? clusterName; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a little refactoring to make code more clear |
||
|
|
||
| return { | ||
| ...data, | ||
| ...parseTraceFields({traceView}), | ||
| name: name ?? clusterName ?? undefined, | ||
| name: clusterName, | ||
| title: clusterTitle, | ||
| monitoring, | ||
| }; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.