Skip to content
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

feat: session space #427

Merged
merged 4 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/config/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ const service = {
// execNGQL: (params, config?) => {
// return post('/api-nebula/db/exec')(params, config);
// },
execNGQL: (params, config?) => {
execNGQL: (...args: Parameters<typeof ngqlRunner.runNgql>) => {
const [params, config] = args;
return ngqlRunner.runNgql(params, config);
},
// batchExecNGQL: (params, config?) => {
// return post('/api-nebula/db/batchExec')(params, config);
// },
batchExecNGQL: (params, config?) => {
batchExecNGQL: (...args: Parameters<typeof ngqlRunner.runBatchNgql>) => {
const [params, config] = args;
return ngqlRunner.runBatchNgql(params, config);
},
execSeqNGQL: (params, config?) => {
Expand Down
1 change: 1 addition & 0 deletions app/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ interface Window {
gConfig: {
maxBytes: number
};
__ngqlRunner__: any;
}
4 changes: 3 additions & 1 deletion app/pages/Console/OutputBox/ForceGraph/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ const ForceGraphBox = (props: IProps) => {
await initGraph({
container: grapfDomRef.current,
id: uuid,
space,
spaceVidType,
});
onGraphInit(graphs[uuid]);
initTooltip({ container: grapfDomRef.current, id: uuid });
initBrushSelect({ container: grapfDomRef.current, id: uuid });
await renderData({ space, graph: graphs[uuid], data: { vertexes, edges } });
await renderData({ graph: graphs[uuid], data: { vertexes, edges } });
setLoading(false);
};

Expand Down
9 changes: 4 additions & 5 deletions app/pages/Console/OutputBox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ import { parseSubGraph } from '@app/utils/parseData';
import cls from 'classnames';
import { GraphStore } from '@app/stores/graph';
import { useI18n } from '@vesoft-inc/i18n';
import type { HistoryResult } from '@app/stores/console';
import Graphviz from './Graphviz';
import ForceGraph from './ForceGraph';

import styles from './index.module.less';

interface IProps {
index: number;
gql: string;
space?: string;
spaceVidType?: string;
result: any;
result: HistoryResult;
onHistoryItem: (gql: string) => void;
onExplorer?: (params: {
space: string;
Expand All @@ -36,11 +34,12 @@ interface IProps {
}

const OutputBox = (props: IProps) => {
const { gql, space, spaceVidType, result: { code, data, message }, onHistoryItem, index, onExplorer, onResultConfig, templateRender } = props;
const { result, onHistoryItem, index, onExplorer, onResultConfig, templateRender } = props;
const { console } = useStore();
const { intl } = useI18n();
const [visible, setVisible] = useState(true);
const { results, update, favorites, saveFavorite, deleteFavorite, getFavoriteList } = console;
const { code, data, message, gql, space, spaceVidType } = result;
const [columns, setColumns] = useState<any>([]);
const [dataSource, setDataSource] = useState<any>([]);
const [isFavorited, setIsFavorited] = useState(false);
Expand Down
31 changes: 17 additions & 14 deletions app/pages/Console/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Button, Select, Tooltip, message } from 'antd';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import { observer } from 'mobx-react-lite';
import { trackEvent, trackPageView } from '@app/utils/stat';
import { useStore } from '@app/stores';
Expand Down Expand Up @@ -38,7 +38,18 @@ const Console = (props: IProps) => {
const { intl } = useI18n();
const { onExplorer, templateRender } = props;
const { spaces, getSpaces } = schema;
const { runGQL, currentGQL, results, runGQLLoading, getParams, update, paramsMap, getFavoriteList, currentSpace } = console;
const {
runGQL,
currentGQL,
results,
runGQLLoading,
getParams,
update,
paramsMap,
getFavoriteList,
currentSpace,
updateCurrentSpace,
} = console;
const [modalVisible, setModalVisible] = useState(false);
const [modalData, setModalData] = useState<{
space: string;
Expand All @@ -52,7 +63,7 @@ const Console = (props: IProps) => {
getParams();
getFavoriteList();
}, []);
const handleSpaceSwitch = useCallback((space: string) => update({ currentSpace: space }), []);

const checkSwitchSpaceGql = (query: string) => {
const queryList = query.split(SEMICOLON_REG).filter(Boolean);
const reg = /^USE `?.+`?(?=[\s*;?]?)/gim;
Expand Down Expand Up @@ -86,11 +97,7 @@ const Console = (props: IProps) => {

editor.current!.editor.execCommand('goDocEnd');
handleSaveQuery(query);
await runGQL({
gql: query,
space: currentSpace,
editorValue: value
});
await runGQL({ gql: query, editorValue: value });
}
};

Expand Down Expand Up @@ -126,7 +133,7 @@ const Console = (props: IProps) => {
<span className={styles.title}>Nebula Console</span>
<div className={styles.operations}>
<div className={styles.spaceSelect}>
<Select value={currentSpace || null} placeholder={intl.get('console.selectSpace')} onDropdownVisibleChange={handleGetSpaces} onChange={handleSpaceSwitch}>
<Select value={currentSpace || null} placeholder={intl.get('console.selectSpace')} onDropdownVisibleChange={handleGetSpaces} onChange={updateCurrentSpace}>
{spaces.map(space => (
<Option value={space} key={space}>
{space}
Expand Down Expand Up @@ -170,9 +177,6 @@ const Console = (props: IProps) => {
key={item.id}
index={index}
result={item}
gql={item.gql}
space={item.space}
spaceVidType={item.spaceVidType}
templateRender={templateRender}
onExplorer={onExplorer ? handleExplorer : undefined}
onHistoryItem={gql => updateGql(gql)}
Expand All @@ -181,8 +185,7 @@ const Console = (props: IProps) => {
)) : <OutputBox
key="empty"
index={0}
result={{ code: 0, data: { headers: [], tables: [] } }}
gql={''}
result={{ id: 'empty', gql: '', code: 0, data: { headers: [], tables: [] } }}
onHistoryItem={gql => updateGql(gql)}
/>}
</div>
Expand Down
8 changes: 2 additions & 6 deletions app/pages/Schema/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,8 @@ const Schema = () => {
};

const handleSwitchSpace = async (space: string) => {
const err = await switchSpace(space);
if (!err) {
history.push(`/schema/tag/list`);
} else if (err && err.toLowerCase().includes('spacenotfound')) {
message.warning(intl.get('schema.useSpaceErrTip'));
}
await switchSpace(space);
history.push(`/schema/tag/list`);
};
const getSpaces = async () => {
setLoading(true);
Expand Down
83 changes: 46 additions & 37 deletions app/stores/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { v4 as uuidv4 } from 'uuid';
import { message } from 'antd';
import { getI18n } from '@vesoft-inc/i18n';
import { safeParse } from '@app/utils/function';
import { NgqlRes } from '@app/utils/websocket';
import { getRootStore } from '.';

const { intl } = getI18n();
Expand Down Expand Up @@ -31,17 +32,24 @@ export const splitQuery = (query: string) => {
return result;
};

export type HistoryResult = NgqlRes & {
id: string;
gql: string;
space?: string;
spaceVidType?: string;
}

const DEFAULT_GQL = 'SHOW SPACES;';
export class ConsoleStore {
runGQLLoading = false;
currentGQL = DEFAULT_GQL;
currentSpace: string = localStorage.getItem('currentSpace') || '';
results: any[] = safeParse(sessionStorage.getItem('consoleResults')) || [];
results: HistoryResult[] = safeParse(sessionStorage.getItem('consoleResults')) || [];
paramsMap = null as any;
favorites = [] as {
id: string;
content: string;
}[] ;
}[];
constructor() {
makeAutoObservable(this, {
results: observable.ref,
Expand All @@ -64,31 +72,36 @@ export class ConsoleStore {
};

update = (param: Partial<ConsoleStore>) => {
Object.keys(param).forEach(key => (this[key] = param[key]));
Object.keys(param).forEach((key) => (this[key] = param[key]));
};

runGQL = async (payload: {
gql: string,
space: string,
editorValue?: string
}) => {
const { gql, space, editorValue } = payload;
updateCurrentSpace = (space: string) => {
this.currentSpace = space;
localStorage.setItem('currentSpace', space);
};

runGQL = async (payload: { gql: string; editorValue?: string }) => {
const { gql, editorValue } = payload;
this.update({ runGQLLoading: true });
try {
if(space) {
const err = await this.rootStore.schema.switchSpace(space);
if(err) {
let spaceVidType = undefined as unknown as string;
if (this.currentSpace) {
const { data } = await this.rootStore.schema.getSpaceInfo(this.currentSpace);
spaceVidType = data?.tables?.[0]?.['Vid Type'];
if (!spaceVidType) {
return;
}
}
const spaceVidType = this.rootStore.schema.spaceVidType;
const { gqlList, paramList } = splitQuery(gql);
const data = await service.batchExecNGQL(
{
gqls: gqlList.filter(item => item !== '').map(item => {
return item.endsWith('\\') ? item.slice(0, -1) : item;
}),
gqls: gqlList
.filter((item) => item !== '')
.map((item) => {
return item.endsWith('\\') ? item.slice(0, -1) : item;
}),
paramList,
space: this.currentSpace,
},
{
trackEventConfig: {
Expand All @@ -97,12 +110,12 @@ export class ConsoleStore {
},
},
);
data.data.forEach(item => {
data.data.forEach((item) => {
item.id = uuidv4();
item.space = space;
item.space = this.currentSpace;
item.spaceVidType = spaceVidType;
});
const updateQuerys = paramList.filter(item => {
const updateQuerys = paramList.filter((item) => {
const reg = /^\s*:params/gim;
return !reg.test(item);
});
Expand All @@ -121,39 +134,35 @@ export class ConsoleStore {
};

getParams = async () => {
const results = (await service.execNGQL(
{
gql: '',
paramList: [':params'],
},
)) as any;
this.update({
paramsMap: results.data?.localParams || {},
});
const results = await service.execNGQL({ gql: '', paramList: [':params'] });
this.update({ paramsMap: results.data?.localParams || {} });
};
saveFavorite = async (content: string) => {
const res = await service.saveFavorite({ content }, {
trackEventConfig: {
category: 'console',
action: 'save_favorite',
const res = await service.saveFavorite(
{ content },
{
trackEventConfig: {
category: 'console',
action: 'save_favorite',
},
},
});
if(res.code === 0) {
);
if (res.code === 0) {
message.success(intl.get('sketch.saveSuccess'));
}
};
deleteFavorite = async (id?: string) => {
const res = id !== undefined ? await service.deleteFavorite(id) : await service.deleteAllFavorites();
if(res.code === 0) {
if (res.code === 0) {
message.success(intl.get('common.deleteSuccess'));
}
};

getFavoriteList = async () => {
const res = await service.getFavoriteList();
if(res.code === 0) {
if (res.code === 0) {
this.update({
favorites: res.data.items
favorites: res.data.items,
});
}
return res;
Expand Down
Loading