Skip to content

Commit

Permalink
feat(gui): Disconnect with host without refresh (#297)
Browse files Browse the repository at this point in the history
* fix(gui): some styles

* feat(gui): Disconnect with host without refresh

* feat(gui): add Connect Hosts in HomePage
  • Loading branch information
zccz14 authored Dec 1, 2023
1 parent b70bae7 commit ae71a8a
Show file tree
Hide file tree
Showing 22 changed files with 188 additions and 140 deletions.
6 changes: 3 additions & 3 deletions ui/web/src/modules/AccountInfo/model.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { IAccountPerformance } from '@yuants/kernel';
import { IAccountInfo } from '@yuants/protocol';
import { BehaviorSubject, Observable, defer, shareReplay, switchMap } from 'rxjs';
import { BehaviorSubject, EMPTY, Observable, defer, of, shareReplay, switchMap } from 'rxjs';
import { terminal$ } from '../Terminals';

export const useAccountInfo = (() => {
const hub: Record<string, Observable<IAccountInfo>> = {};
return (account_id: string) =>
(hub[account_id] ??= defer(() => terminal$).pipe(
switchMap((terminal) => terminal.useAccountInfo(account_id)),
switchMap((terminal) => terminal?.useAccountInfo(account_id) ?? EMPTY),
shareReplay(1),
));
})();

export const accountIds$ = defer(() => terminal$).pipe(
switchMap((terminal) => terminal.accountIds$),
switchMap((terminal) => terminal?.accountIds$ ?? of([])),
shareReplay(1),
);

Expand Down
5 changes: 3 additions & 2 deletions ui/web/src/modules/Agent/AgentConfForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ runAgentAction$.subscribe(async () => {
console.error(validator.errors);
throw msg;
}
if (currentHostConfig$.value === null) {
const terminal = await firstValueFrom(terminal$);

if (terminal === null) {
const agentCode = await bundleCode(agentConf.entry!);
const scene = await LocalAgentScene({ ...agentConf, bundled_code: agentCode });
const accountFrameUnit = new AccountFrameUnit(
Expand All @@ -153,7 +155,6 @@ runAgentAction$.subscribe(async () => {
);
accountFrameSeries$.next(accountFrameUnit.data);
} else {
const terminal = await firstValueFrom(terminal$);
const agentCode = await bundleCode(agentConf.entry!);
const scene = await AgentScene(terminal, { ...agentConf, bundled_code: agentCode });
const accountFrameUnit = new AccountFrameUnit(
Expand Down
5 changes: 3 additions & 2 deletions ui/web/src/modules/Agent/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,9 @@ export interface IBatchAgentResultItem {

export const runBatchBackTestWorkItem = async (agentConf: IAgentConf): Promise<IBatchAgentResultItem[]> => {
if (!agentConf.bundled_code) throw new Error('No bundled_code');
const scene = currentHostConfig$.value
? await AgentScene(await firstValueFrom(terminal$), {
const terminal = await firstValueFrom(terminal$);
const scene = terminal
? await AgentScene(terminal, {
...agentConf,
disable_log: true,
})
Expand Down
2 changes: 2 additions & 0 deletions ui/web/src/modules/Fund/RealtimeAsset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ registerPage('RealtimeAsset', () => {
onClick={() => {
terminal$
.pipe(
filter((x): x is Exclude<typeof x, null> => !!x),
first(),
mergeMap((terminal) =>
from(fundInfo.config.investors).pipe(
Expand All @@ -343,6 +344,7 @@ registerPage('RealtimeAsset', () => {
onClick={() => {
terminal$
.pipe(
filter((x): x is Exclude<typeof x, null> => !!x),
first(),
mergeMap((terminal) => sendReportToInvestor(terminal, fundInfo, investor)),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Button, Modal, Popconfirm, Space, Table, Toast } from '@douyinfe/semi-u
import { IDataRecord } from '@yuants/protocol';
import { useObservable, useObservableState } from 'observable-hooks';
import { useState } from 'react';
import { combineLatest, filter, first, mergeMap, tap, toArray } from 'rxjs';
import { EMPTY, combineLatest, filter, first, mergeMap, tap, toArray } from 'rxjs';
import Form from '../Form';
import { registerPage } from '../Pages';
import { terminal$ } from '../Terminals';
Expand Down Expand Up @@ -63,28 +63,28 @@ registerPage('GeneralSpecificRelationList', () => {
combineLatest([terminal$, input$]).pipe(
//
mergeMap(([terminal, [searchFormData]]) =>
terminal
.queryDataRecords<IGeneralSpecificRelation>({
(
terminal?.queryDataRecords<IGeneralSpecificRelation>({
type: 'general_specific_relation',
tags: {},
options: {},
})
.pipe(
//
filter(
(record) =>
(searchFormData.general_product_id
? record.origin.general_product_id === searchFormData.general_product_id
: true) &&
(searchFormData.specific_datasource_id
? record.origin.specific_datasource_id === searchFormData.specific_datasource_id
: true) &&
(searchFormData.specific_product_id
? record.origin.specific_product_id === searchFormData.specific_product_id
: true),
),
toArray(),
}) ?? EMPTY
).pipe(
//
filter(
(record) =>
(searchFormData.general_product_id
? record.origin.general_product_id === searchFormData.general_product_id
: true) &&
(searchFormData.specific_datasource_id
? record.origin.specific_datasource_id === searchFormData.specific_datasource_id
: true) &&
(searchFormData.specific_product_id
? record.origin.specific_product_id === searchFormData.specific_product_id
: true),
),
toArray(),
),
),
),
[searchFormData, refreshId],
Expand Down Expand Up @@ -154,6 +154,7 @@ registerPage('GeneralSpecificRelationList', () => {
terminal$
.pipe(
//
filter((x): x is Exclude<typeof x, null> => !!x),
first(),
mergeMap((terminal) =>
terminal.removeDataRecords({
Expand Down Expand Up @@ -188,6 +189,7 @@ registerPage('GeneralSpecificRelationList', () => {
const record = mapGeneralSpecificRelationToDataRecord(formData);
terminal$
.pipe(
filter((x): x is Exclude<typeof x, null> => !!x),
first(),
mergeMap((terminal) => terminal.updateDataRecords([record])),
tap({
Expand Down
13 changes: 12 additions & 1 deletion ui/web/src/modules/Market/Market.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ import {
import { t } from 'i18next';
import { useObservable, useObservableState } from 'observable-hooks';
import { useEffect, useMemo, useState } from 'react';
import { BehaviorSubject, distinctUntilChanged, first, interval, map, mergeMap, tap, throwError } from 'rxjs';
import {
BehaviorSubject,
distinctUntilChanged,
filter,
first,
interval,
map,
mergeMap,
tap,
throwError,
} from 'rxjs';
import { CandlestickSeries, Chart, ChartGroup } from '../Chart/components/Charts';
import { executeCommand, registerCommand } from '../CommandCenter';
import { showForm } from '../Form';
Expand Down Expand Up @@ -167,6 +177,7 @@ registerCommand('fetchOHLCV', async (params) => {

terminal$
.pipe(
filter((x): x is Exclude<typeof x, null> => !!x),
first(),
tap(() => Toast.info(`开始拉取 ${datasource_id} / ${product_id} / ${period_in_sec} 历史数据...`)),
mergeMap((terminal) =>
Expand Down
5 changes: 3 additions & 2 deletions ui/web/src/modules/Market/SearchButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Button, Descriptions, Modal, Space, Typography } from '@douyinfe/semi-u
import { JSONSchema7 } from 'json-schema';
import { useObservable, useObservableState } from 'observable-hooks';
import React, { useMemo, useState } from 'react';
import { defer, first, mergeMap, of, shareReplay, switchMap } from 'rxjs';
import { defer, filter, first, mergeMap, of, shareReplay, switchMap } from 'rxjs';
import { executeCommand } from '../CommandCenter';
import { Form } from '../Form';
import { terminal$ } from '../Terminals';
Expand All @@ -20,7 +20,7 @@ const PERIOD_IN_SEC_TO_LABEL: Record<number, string> = {
};

export const datasourceIds$ = defer(() => terminal$).pipe(
switchMap((terminal) => terminal.datasourceIds$),
switchMap((terminal) => terminal?.datasourceIds$ ?? of([])),
shareReplay(1),
);

Expand All @@ -44,6 +44,7 @@ export const SearchButton = React.memo(() => {
datasource_id
? terminal$.pipe(
//
filter((x): x is Exclude<typeof x, null> => !!x),
first(),
mergeMap((terminal) => terminal.queryProducts({ datasource_id })),
)
Expand Down
5 changes: 4 additions & 1 deletion ui/web/src/modules/Order/ManualTradePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Button, Space, Toast } from '@douyinfe/semi-ui';
import { IOrder, OrderDirection, OrderType } from '@yuants/protocol';
import { useObservable, useObservableState } from 'observable-hooks';
import { useState } from 'react';
import { first, mergeMap, of } from 'rxjs';
import { filter, first, mergeMap, of } from 'rxjs';
import { accountIds$ } from '../AccountInfo/model';
import { Form } from '../Form';
import { registerPage } from '../Pages';
Expand All @@ -24,6 +24,7 @@ registerPage('ManualTradePanel', () => {
mergeMap(([account_id]) =>
account_id
? terminal$.pipe(
filter((x): x is Exclude<typeof x, null> => !!x),
first(),
mergeMap((terminal) => terminal.queryProducts({ datasource_id: account_id })),
)
Expand Down Expand Up @@ -93,6 +94,7 @@ registerPage('ManualTradePanel', () => {
order &&
terminal$
.pipe(
filter((x): x is Exclude<typeof x, null> => !!x),
first(),
mergeMap((terminal) => terminal.submitOrder(order)),
)
Expand Down Expand Up @@ -128,6 +130,7 @@ registerPage('ManualTradePanel', () => {
cancelFormData &&
terminal$
.pipe(
filter((x): x is Exclude<typeof x, null> => !!x),
first(),
mergeMap((terminal) => terminal.cancelOrder(cancelFormData as IOrder)),
)
Expand Down
17 changes: 9 additions & 8 deletions ui/web/src/modules/Products/ProductList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { IProduct } from '@yuants/protocol';
import { useObservable, useObservableState } from 'observable-hooks';
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { combineLatest, first, mergeMap, tap, toArray } from 'rxjs';
import { EMPTY, combineLatest, filter, first, mergeMap, tap, toArray } from 'rxjs';
import { executeCommand } from '../CommandCenter';
import Form, { showForm } from '../Form';
import { SearchButton } from '../Market/SearchButton';
Expand All @@ -25,8 +25,8 @@ registerPage('ProductList', () => {
combineLatest([terminal$, input$]).pipe(
//
mergeMap(([terminal, [searchFormData]]) =>
terminal
.queryDataRecords<IProduct>({
(
terminal?.queryDataRecords<IProduct>({
type: 'product',
tags: {
datasource_id: searchFormData.datasource_id || undefined,
Expand All @@ -40,11 +40,11 @@ registerPage('ProductList', () => {
['tags.product_id', 1],
],
},
})
.pipe(
//
toArray(),
),
}) ?? EMPTY
).pipe(
//
toArray(),
),
),
),
[searchFormData, refreshId],
Expand Down Expand Up @@ -163,6 +163,7 @@ registerPage('ProductList', () => {
onOk={() => {
terminal$
.pipe(
filter((x): x is Exclude<typeof x, null> => !!x),
first(),
mergeMap((terminal) => terminal.updateProducts([formData])),
tap({
Expand Down
4 changes: 2 additions & 2 deletions ui/web/src/modules/Products/model.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { IProduct } from '@yuants/protocol';
import { Observable, defer, shareReplay, switchMap } from 'rxjs';
import { Observable, defer, of, shareReplay, switchMap } from 'rxjs';
import { terminal$ } from '../Terminals';

export const useProducts = (() => {
const hub: Record<string, Observable<IProduct[]>> = {};
return (datasource_id: string) =>
(hub[datasource_id] ??= defer(() => terminal$).pipe(
switchMap((terminal) => terminal.useProducts(datasource_id)),
switchMap((terminal) => terminal?.useProducts(datasource_id) ?? of([])),
shareReplay(1),
));
})();
43 changes: 22 additions & 21 deletions ui/web/src/modules/PullSourceRelations/PullSourceRelationList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { IDataRecord } from '@yuants/protocol';
import { JSONSchema7 } from 'json-schema';
import { useObservable, useObservableState } from 'observable-hooks';
import { useState } from 'react';
import { combineLatest, filter, first, mergeMap, tap, toArray } from 'rxjs';
import { EMPTY, combineLatest, filter, first, mergeMap, tap, toArray } from 'rxjs';
import { executeCommand } from '../CommandCenter';
import { showForm } from '../Form';
import { registerPage } from '../Pages';
Expand Down Expand Up @@ -84,8 +84,8 @@ registerPage('PullSourceRelationList', () => {
combineLatest([terminal$, input$]).pipe(
//
mergeMap(([terminal, [searchFormData]]) =>
terminal
.queryDataRecords<IPullSourceRelation>({
(
terminal?.queryDataRecords<IPullSourceRelation>({
type: 'pull_source_relation',
tags: {},
options: {
Expand All @@ -95,25 +95,23 @@ registerPage('PullSourceRelationList', () => {
['origin.period_in_sec', 1],
],
},
})
.pipe(
filter(
(record) =>
!searchFormData.datasource_id ||
record.origin.datasource_id === searchFormData.datasource_id,
),
filter(
(record) =>
!searchFormData.product_id || record.origin.product_id === searchFormData.product_id,
),
filter(
(record) =>
!searchFormData.period_in_sec ||
record.origin.period_in_sec === searchFormData.period_in_sec,
),
//
toArray(),
}) ?? EMPTY
).pipe(
filter(
(record) =>
!searchFormData.datasource_id || record.origin.datasource_id === searchFormData.datasource_id,
),
filter(
(record) =>
!searchFormData.product_id || record.origin.product_id === searchFormData.product_id,
),
filter(
(record) =>
!searchFormData.period_in_sec || record.origin.period_in_sec === searchFormData.period_in_sec,
),
//
toArray(),
),
),
),
[searchFormData, refreshId],
Expand All @@ -124,6 +122,7 @@ registerPage('PullSourceRelationList', () => {
const record = mapPullSourceRelationToDataRecord(formData);
terminal$
.pipe(
filter((x): x is Exclude<typeof x, null> => !!x),
first(),
mergeMap((terminal) => terminal.updateDataRecords([record])),
tap({
Expand Down Expand Up @@ -186,6 +185,7 @@ registerPage('PullSourceRelationList', () => {
const next = mapPullSourceRelationToDataRecord({ ...record.origin, disabled: v });
terminal$
.pipe(
filter((x): x is Exclude<typeof x, null> => !!x),
first(),
mergeMap((terminal) => terminal.updateDataRecords([next])),
tap({
Expand Down Expand Up @@ -231,6 +231,7 @@ registerPage('PullSourceRelationList', () => {
terminal$
.pipe(
//
filter((x): x is Exclude<typeof x, null> => !!x),
first(),
mergeMap((terminal) =>
terminal.removeDataRecords({
Expand Down
Loading

0 comments on commit ae71a8a

Please sign in to comment.