Skip to content

Commit 85201cf

Browse files
authored
Merge pull request #612 from tapdata/TAP-12260-data-trace-dev
fix(TAP-12260): [T3 to dev]The traceability interface returns data, but the fron…
2 parents 5640ff5 + a1b6b22 commit 85201cf

2 files changed

Lines changed: 56 additions & 21 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
export interface TraceStreamProcessor {
2+
(text: string): void
3+
flush: () => void
4+
}
5+
6+
export function createTraceStreamProcessor(
7+
onTraceValue: (nodeId: string, data: Record<string, any>) => void,
8+
): TraceStreamProcessor {
9+
let lastIndex = 0
10+
let pendingLine = ''
11+
12+
function parseLine(line: string) {
13+
const trimmed = line.trim()
14+
if (!trimmed) return
15+
16+
const parsed = JSON.parse(trimmed)
17+
if (parsed.nodeId !== undefined && parsed.type === 'TRACE_VALUE') {
18+
onTraceValue(parsed.nodeId, parsed)
19+
}
20+
}
21+
22+
const processChunk = ((text: string) => {
23+
const newText = text.slice(lastIndex)
24+
lastIndex = text.length
25+
26+
const lines = (pendingLine + newText).split('\n')
27+
pendingLine = lines.pop() ?? ''
28+
29+
for (const line of lines) {
30+
try {
31+
parseLine(line)
32+
} catch {
33+
// Ignore malformed non-trace lines from the stream.
34+
}
35+
}
36+
}) as TraceStreamProcessor
37+
38+
processChunk.flush = () => {
39+
if (!pendingLine.trim()) return
40+
41+
try {
42+
parseLine(pendingLine)
43+
} catch {
44+
// Ignore an incomplete final line.
45+
} finally {
46+
pendingLine = ''
47+
}
48+
}
49+
50+
return processChunk
51+
}

packages/api/src/core/data-trace.ts

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { requestClient } from '../request'
2+
import { createTraceStreamProcessor } from './data-trace-stream-parser'
23

34
const BASE_URL = '/api/lineage/wide-table'
45

@@ -98,27 +99,9 @@ export function getTraceData(
9899
callbacks: TraceStreamCallbacks,
99100
): AbortController {
100101
const controller = new AbortController()
101-
let lastIndex = 0
102-
103-
function processChunk(text: string) {
104-
const newText = text.slice(lastIndex)
105-
lastIndex = text.length
106-
107-
const lines = newText.split('\n')
108-
for (const line of lines) {
109-
const trimmed = line.trim()
110-
if (!trimmed) continue
111-
try {
112-
const parsed = JSON.parse(trimmed)
113-
if (parsed.nodeId !== undefined && parsed.type === 'TRACE_VALUE') {
114-
console.log('parsed', parsed)
115-
callbacks.onNodeData?.(parsed.nodeId, parsed)
116-
}
117-
} catch {
118-
// ignore incomplete line (will be completed in next chunk)
119-
}
120-
}
121-
}
102+
const processChunk = createTraceStreamProcessor((nodeId, parsed) => {
103+
callbacks.onNodeData?.(nodeId, parsed)
104+
})
122105

123106
requestClient
124107
.post(`${BASE_URL}/trace/stream`, data, {
@@ -135,6 +118,7 @@ export function getTraceData(
135118
},
136119
})
137120
.then(() => {
121+
processChunk.flush()
138122
callbacks.onDone?.()
139123
})
140124
.catch((error: any) => {

0 commit comments

Comments
 (0)