Skip to content

Commit b4e24c0

Browse files
Created streamToView and added tests.
1 parent fffc62c commit b4e24c0

File tree

4 files changed

+97
-31
lines changed

4 files changed

+97
-31
lines changed

packages/rum-core/src/domain/assembly.ts

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import {
1212
} from '@datadog/browser-core'
1313
import type { RumEventDomainContext } from '../domainContext.types'
1414
import { RumEventType } from '../rawRumEvent.types'
15-
import type { RumEvent, RumViewEvent } from '../rumEvent.types'
15+
import type { RumEvent } from '../rumEvent.types'
1616
import type { LifeCycle } from './lifeCycle'
1717
import { LifeCycleEventType } from './lifeCycle'
1818
import type { RumConfiguration } from './configuration'
1919
import type { ModifiableFieldPaths } from './limitModification'
2020
import { limitModification } from './limitModification'
2121
import type { Hooks } from './hooks'
22+
import { mapStreamToView } from './streamToView'
2223

2324
const VIEW_MODIFIABLE_FIELD_PATHS: ModifiableFieldPaths = {
2425
'view.name': 'string',
@@ -114,7 +115,7 @@ export function startRumAssembly(
114115
LifeCycleEventType.RAW_RUM_EVENT_COLLECTED,
115116
({ startTime, duration, rawRumEvent, domainContext }) => {
116117
const defaultRumEventAttributes = hooks.triggerHook(HookNames.Assemble, {
117-
eventType: rawRumEvent.type === 'stream' ? 'view' : rawRumEvent.type,
118+
eventType: rawRumEvent.type === RumEventType.STREAM ? RumEventType.VIEW : rawRumEvent.type,
118119
startTime,
119120
duration,
120121
})!
@@ -132,35 +133,10 @@ export function startRumAssembly(
132133
delete serverRumEvent.context
133134
}
134135

135-
if (rawRumEvent.type === 'stream') {
136-
const streamEvent = {
137-
...(serverRumEvent as RumViewEvent),
138-
_dd: {
139-
...serverRumEvent._dd,
140-
document_version: serverRumEvent.stream?.document_version,
141-
},
142-
stream: {
143-
...serverRumEvent.stream,
144-
time_spent: undefined,
145-
},
146-
view: {
147-
...serverRumEvent.view,
148-
id: serverRumEvent.stream?.id,
149-
action: {
150-
count: 0,
151-
},
152-
error: {
153-
count: 0,
154-
},
155-
resource: {
156-
count: 0,
157-
},
158-
time_spent: serverRumEvent.stream?.time_spent,
159-
},
160-
type: 'view',
161-
}
136+
if (rawRumEvent.type === RumEventType.STREAM) {
137+
const streamEvent = mapStreamToView(serverRumEvent)
162138

163-
lifeCycle.notify(LifeCycleEventType.RUM_EVENT_COLLECTED, streamEvent as RumEvent & Context)
139+
lifeCycle.notify(LifeCycleEventType.RUM_EVENT_COLLECTED, streamEvent)
164140
} else {
165141
lifeCycle.notify(LifeCycleEventType.RUM_EVENT_COLLECTED, serverRumEvent)
166142
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import type { Context } from '@datadog/browser-core'
2+
import { RumEventType } from '../rawRumEvent.types'
3+
import type { RumEvent, RumViewEvent } from '../rumEvent.types'
4+
import { mapStreamToView } from './streamToView'
5+
6+
describe('mapStreamToView', () => {
7+
it('maps stream to view correctly', () => {
8+
const input = {
9+
type: RumEventType.STREAM,
10+
stream: {
11+
id: 'stream-id-456',
12+
document_version: 25,
13+
time_spent: 3_000_000_000,
14+
},
15+
view: { id: 'original-view-id', url: '/test-page' },
16+
_dd: { replay: true },
17+
context: { foo: 'bar' },
18+
} as unknown as RumEvent & Context
19+
20+
const out = mapStreamToView(input) as unknown as RumViewEvent & Context
21+
22+
expect(out.type).toBe(RumEventType.VIEW)
23+
expect(out._dd.document_version).toBe(25)
24+
expect(out.view.id).toBe('stream-id-456')
25+
expect(out.view.time_spent).toBe(3_000_000_000)
26+
expect(out.stream!.time_spent).toBeUndefined()
27+
expect(out.view.action.count).toBe(0)
28+
expect(out.view.error.count).toBe(0)
29+
expect(out.view.resource.count).toBe(0)
30+
})
31+
32+
it('does not mutate the input', () => {
33+
const input = {
34+
type: RumEventType.STREAM,
35+
stream: { id: 'x', document_version: 1, time_spent: 1 },
36+
view: { id: 'y' },
37+
} as unknown as RumEvent & Context
38+
const snapshot = JSON.parse(JSON.stringify(input))
39+
40+
mapStreamToView(input)
41+
42+
expect(input).toEqual(snapshot)
43+
})
44+
45+
it('handles missing fields gracefully', () => {
46+
const out = mapStreamToView({
47+
type: RumEventType.STREAM,
48+
stream: { id: 'only-id' },
49+
view: {},
50+
} as unknown as RumEvent & Context) as RumViewEvent
51+
52+
expect(out.view.id).toBe('only-id')
53+
expect(out._dd?.document_version).toBeUndefined()
54+
expect(out.view.time_spent).toBeUndefined()
55+
})
56+
})
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { Context } from '@datadog/browser-core'
2+
import { RumEventType } from '../rawRumEvent.types'
3+
import type { RumEvent, RumViewEvent } from '../rumEvent.types'
4+
5+
export function mapStreamToView(serverRumEvent: RumEvent & Context): RumEvent & Context {
6+
const streamEvent = {
7+
...(serverRumEvent as RumViewEvent),
8+
_dd: {
9+
...serverRumEvent._dd,
10+
document_version: serverRumEvent.stream?.document_version,
11+
},
12+
stream: {
13+
...serverRumEvent.stream,
14+
time_spent: undefined,
15+
},
16+
view: {
17+
...serverRumEvent.view,
18+
id: serverRumEvent.stream?.id,
19+
action: {
20+
count: 0,
21+
},
22+
error: {
23+
count: 0,
24+
},
25+
resource: {
26+
count: 0,
27+
},
28+
time_spent: serverRumEvent.stream?.time_spent,
29+
},
30+
type: RumEventType.VIEW,
31+
}
32+
33+
return streamEvent as RumEvent & Context
34+
}

packages/rum-core/src/domain/trackEventCounts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function trackEventCounts({
3030
}
3131

3232
const subscription = lifeCycle.subscribe(LifeCycleEventType.RUM_EVENT_COLLECTED, (event): void => {
33-
if (event.type === 'view' || event.type === 'vital' || !isChildEvent(event) || ['stream'].includes(event.type)) {
33+
if (event.type === RumEventType.VIEW || event.type === RumEventType.VITAL || !isChildEvent(event)) {
3434
return
3535
}
3636
switch (event.type) {

0 commit comments

Comments
 (0)