forked from PostHog/posthog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistorical-export-e2e.test.ts
148 lines (127 loc) · 5.26 KB
/
historical-export-e2e.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { PluginEvent } from '@posthog/plugin-scaffold'
import { defaultConfig } from '../src/config/config'
import { startPluginsServer } from '../src/main/pluginsServer'
import { EnqueuedPluginJob, Hub, LogLevel, PluginsServerConfig } from '../src/types'
import { UUIDT } from '../src/utils/utils'
import { EventPipelineRunner } from '../src/worker/ingestion/event-pipeline/runner'
import Piscina, { makePiscina } from '../src/worker/piscina'
import { writeToFile } from '../src/worker/vm/extensions/test-utils'
import { delayUntilEventIngested, resetTestDatabaseClickhouse } from './helpers/clickhouse'
import { resetGraphileWorkerSchema } from './helpers/graphile-worker'
import { resetKafka } from './helpers/kafka'
import { pluginConfig39 } from './helpers/plugins'
import { resetTestDatabase } from './helpers/sql'
jest.mock('../src/utils/status')
jest.setTimeout(60000) // 60 sec timeout
const { console: testConsole } = writeToFile
const extraServerConfig: Partial<PluginsServerConfig> = {
WORKER_CONCURRENCY: 2,
LOG_LEVEL: LogLevel.Log,
CONVERSION_BUFFER_ENABLED: false,
HISTORICAL_EXPORTS_ENABLED: true,
HISTORICAL_EXPORTS_FETCH_WINDOW_MULTIPLIER: 2,
HISTORICAL_EXPORTS_INITIAL_FETCH_TIME_WINDOW: 8 * 60 * 60 * 1000, // 8 hours
}
const indexJs = `
import { console as testConsole } from 'test-utils/write-to-file'
export async function exportEvents(events) {
for (const event of events) {
if (event.properties && event.properties['$$is_historical_export_event']) {
testConsole.log('exported historical event', event)
}
}
}
`
describe('Historical Export (v2)', () => {
let hub: Hub
let stopServer: () => Promise<void>
let piscina: Piscina
beforeAll(async () => {
await resetKafka(extraServerConfig)
})
beforeEach(async () => {
console.info = jest.fn()
testConsole.reset()
await Promise.all([
await resetTestDatabase(indexJs),
await resetTestDatabaseClickhouse(extraServerConfig),
await resetGraphileWorkerSchema(defaultConfig),
])
const startResponse = await startPluginsServer(extraServerConfig, makePiscina, undefined)
hub = startResponse.hub!
piscina = startResponse.piscina!
stopServer = startResponse.stop!
})
afterEach(async () => {
await stopServer()
})
afterAll(async () => {
await resetGraphileWorkerSchema(defaultConfig)
})
async function ingestEvent(timestamp: string, overrides: Partial<PluginEvent> = {}) {
const pluginEvent: PluginEvent = {
event: 'some_event',
distinct_id: 'some_user',
site_url: '',
team_id: 2,
timestamp: timestamp,
now: timestamp,
ip: '',
uuid: new UUIDT().toString(),
...overrides,
} as any as PluginEvent
const runner = new EventPipelineRunner(hub, pluginEvent)
await runner.runEventPipeline(pluginEvent)
}
it('exports a batch of events in a time range', async () => {
await ingestEvent('2021-07-28T00:00:00.000Z') // To avoid parallel person processing which we don't handle
await Promise.all([
ingestEvent('2021-08-01T00:00:00.000Z', { properties: { foo: 'bar' } }),
ingestEvent('2021-08-02T02:00:00.000Z'),
ingestEvent('2021-08-03T09:00:00.000Z'),
ingestEvent('2021-08-03T15:00:00.000Z'),
ingestEvent('2021-08-04T23:00:00.000Z'),
ingestEvent('2021-08-04T23:59:59.000Z'),
ingestEvent('2021-08-05T00:00:00.000Z'),
ingestEvent('2021-08-05T01:00:00.000Z'),
])
await hub.kafkaProducer.flush()
await delayUntilEventIngested(() => hub.db.fetchEvents(), 9)
await piscina.run({
task: 'runPluginJob',
args: {
job: {
type: 'Export historical events V2',
payload: {
dateRange: ['2021-08-01', '2021-08-04'],
parallelism: 5,
$operation: 'start',
},
pluginConfigId: pluginConfig39.id,
pluginConfigTeam: pluginConfig39.team_id,
timestamp: 0,
} as EnqueuedPluginJob,
},
})
await delayUntilEventIngested(() => Promise.resolve(testConsole.read()), 6, 1000, 50)
const exportedEventLogs = testConsole.read() as Array<[string, any]>
exportedEventLogs.sort((e1, e2) => e1[1].timestamp.localeCompare(e2[1].timestamp))
const timestamps = exportedEventLogs.map(([, event]) => event.timestamp)
expect(timestamps).toEqual([
'2021-08-01T00:00:00.000Z',
'2021-08-02T02:00:00.000Z',
'2021-08-03T09:00:00.000Z',
'2021-08-03T15:00:00.000Z',
'2021-08-04T23:00:00.000Z',
'2021-08-04T23:59:59.000Z',
])
expect(exportedEventLogs[0][1].properties).toEqual(
expect.objectContaining({
foo: 'bar',
$$historical_export_source_db: 'clickhouse',
$$is_historical_export_event: true,
$$historical_export_timestamp: expect.any(String),
})
)
})
})