Skip to content

Commit d2ead10

Browse files
authored
fix: get correct data type by spec (#17)
1 parent 7a5305a commit d2ead10

File tree

7 files changed

+201
-2043
lines changed

7 files changed

+201
-2043
lines changed

packages/secretnote/src/components/log-viewer/view.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ const LogView = (props: IProps) => {
111111
});
112112
setTerminalInstance(instance);
113113
return () => {
114-
instance.dispose();
114+
// https://github.com/xtermjs/xterm.js/issues/4757#issuecomment-1712841561
115+
try {
116+
instance.dispose();
117+
} catch (e) {
118+
// pass
119+
}
115120
};
116121
// eslint-disable-next-line react-hooks/exhaustive-deps
117122
}, []);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import type { ComponentSpec, Value, IOType } from '@/components/component-form';
3+
4+
const clusterConfig = {
5+
public_config: {
6+
ray_fed_config: {
7+
parties: ['alice', 'bob'],
8+
addresses: ['alice:8000', 'bob:8000'],
9+
},
10+
spu_configs: [
11+
{
12+
name: 'spu',
13+
parties: ['alice', 'bob'],
14+
addresses: ['alice:8001', 'bob:8001'],
15+
},
16+
],
17+
},
18+
private_config: { self_party: '{self_party}', ray_head_addr: '127.0.0.1:6379' },
19+
desc: {
20+
parties: ['alice', 'bob'],
21+
devices: [
22+
{
23+
name: 'spu',
24+
type: 'spu',
25+
parties: ['alice', 'bob'],
26+
config:
27+
'{"runtime_config":{"protocol":"REF2K","field":"FM64"},"link_desc":{"connect_retry_times":60,"connect_retry_interval_ms":1000,"brpc_channel_protocol":"http","brpc_channel_connection_type":"pooled","recv_timeout_ms":1200000,"http_timeout_ms":1200000}}',
28+
},
29+
],
30+
},
31+
};
32+
33+
const getAttrValue = (component: ComponentSpec, key: string, value: any) => {
34+
const commonAttr = (component.attrs || []).find((attr) => attr.name === key);
35+
if (commonAttr) {
36+
const type = commonAttr.type;
37+
switch (type) {
38+
case 'AT_INT':
39+
return { i64: value };
40+
case 'AT_BOOL':
41+
return { b: value };
42+
case 'AT_STRING':
43+
return { s: value };
44+
default:
45+
return { s: value };
46+
}
47+
} else {
48+
// input has no marked data type in the attr, so it can only be fixed
49+
return { ss: [value] };
50+
}
51+
};
52+
53+
const getIOMetaType = (type: IOType) => {
54+
if (type === 'sf.table.individual') {
55+
return 'type.googleapis.com/secretflow.spec.v1.IndividualTable';
56+
} else if (type === 'sf.table.vertical_table') {
57+
return 'type.googleapis.com/secretflow.spec.v1.VerticalTable';
58+
}
59+
return '';
60+
};
61+
62+
const generateComponentCellCode = (component: ComponentSpec, config: Value) => {
63+
if (!(component && config)) {
64+
return '';
65+
}
66+
67+
const componentConfig: any = {
68+
domain: component.domain,
69+
name: component.name,
70+
version: component.version,
71+
attr_paths: [],
72+
attrs: [],
73+
inputs: [],
74+
output_uris: [],
75+
};
76+
77+
const { input, output, ...others } = config;
78+
79+
// attr
80+
Object.entries(others).forEach(([key, value]) => {
81+
componentConfig.attr_paths.push(key);
82+
const attrValue = getAttrValue(component, key, value);
83+
componentConfig.attrs.push(attrValue);
84+
});
85+
86+
// input
87+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
88+
Object.entries(input).forEach(([key, value]: [string, any]) => {
89+
const { type, tables } = value;
90+
91+
if (type && tables) {
92+
componentConfig.inputs.push({
93+
name: key,
94+
type: type,
95+
data_refs: tables.data_ref.map((ref: any) => ({
96+
uri: ref.uri,
97+
party: ref.party,
98+
format: 'csv',
99+
})),
100+
meta: {
101+
'@type': getIOMetaType(type),
102+
schema: tables.schema,
103+
line_count: -1,
104+
},
105+
});
106+
}
107+
});
108+
109+
// output
110+
Object.entries(output).forEach(([, value]) => {
111+
componentConfig.output_uris.push(value);
112+
});
113+
114+
return `
115+
from secretflow.spec.v1.evaluation_pb2 import NodeEvalParam
116+
from secretflow.spec.extend.cluster_pb2 import SFClusterConfig
117+
from secretflow.component.entry import comp_eval
118+
from google.protobuf.json_format import Parse
119+
from secretflow.spec.v1.data_pb2 import StorageConfig
120+
import os
121+
122+
cluster_config_str = r"""
123+
${JSON.stringify(clusterConfig)}
124+
"""
125+
126+
127+
component_config_str = r"""
128+
${JSON.stringify(componentConfig)}
129+
"""
130+
131+
self_party = os.getenv("SELF_PARTY", "alice")
132+
cluster_config = SFClusterConfig()
133+
Parse(cluster_config_str.replace('{self_party}', self_party), cluster_config)
134+
135+
node_eval_config = NodeEvalParam()
136+
Parse(component_config_str, node_eval_config)
137+
138+
storage_config = StorageConfig(
139+
type="local_fs",
140+
local_fs=StorageConfig.LocalFSConfig(wd="/home/vscode/examples"),
141+
)
142+
143+
res = comp_eval(node_eval_config, storage_config, cluster_config)
144+
145+
print(f"The execution is complete and the result is: \\n{res}")
146+
`;
147+
};
148+
149+
export { generateComponentCellCode };

packages/secretnote/src/modules/component-cell/cell-component/index.less

+8
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@
5454
}
5555
}
5656
}
57+
58+
.loading {
59+
display: flex;
60+
width: 100%;
61+
height: 100%;
62+
align-items: center;
63+
justify-content: center;
64+
}
5765
}
5866

5967
.component-cascader {

packages/secretnote/src/modules/component-cell/cell-component/index.tsx

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import type { IOutput } from '@difizen/libro-jupyter';
2-
import { Tabs, type TabsProps, Empty } from 'antd';
3-
import { forwardRef, type ForwardedRef, useMemo, useEffect } from 'react';
2+
import { Tabs, type TabsProps, Empty, Spin } from 'antd';
3+
import { forwardRef, type ForwardedRef, useMemo } from 'react';
44

55
import { ComponentForm } from '@/components/component-form';
66
import type { ComponentSpec, Value } from '@/components/component-form';
77
import LogView from '@/components/log-viewer';
88

9+
import { generateComponentCellCode } from './cell-code';
910
import { ComponentOptions, getComponentByIds, getComponentIds } from './options';
1011
import './index.less';
1112

@@ -15,6 +16,7 @@ interface CellComponentProps {
1516
defaultComponentConfig?: Value;
1617
onComponentConfigChange?: (changedValues: Value, values: Value) => void;
1718
outputs: IOutput[];
19+
loading?: boolean;
1820
}
1921

2022
const CellComponent = forwardRef(
@@ -26,6 +28,7 @@ const CellComponent = forwardRef(
2628
defaultComponentConfig,
2729
onComponentConfigChange,
2830
outputs,
31+
loading,
2932
} = props;
3033

3134
const outputLogs = useMemo(() => {
@@ -49,7 +52,17 @@ const CellComponent = forwardRef(
4952
{
5053
key: '1',
5154
label: 'Log',
52-
children: <div className="sf-component-log">{outputLogs}</div>,
55+
children: (
56+
<div className="sf-component-log">
57+
{loading ? (
58+
<div className="loading">
59+
<Spin />
60+
</div>
61+
) : (
62+
outputLogs
63+
)}
64+
</div>
65+
),
5366
},
5467
{
5568
key: '2',
@@ -102,4 +115,4 @@ const CellComponent = forwardRef(
102115
);
103116
CellComponent.displayName = 'CellComponent';
104117

105-
export { CellComponent, getComponentByIds, getComponentIds };
118+
export { CellComponent, getComponentByIds, getComponentIds, generateComponentCellCode };

0 commit comments

Comments
 (0)