mirror of
https://github.com/PostHog/posthog.git
synced 2024-11-25 11:17:50 +01:00
612d62610e
This is currently one of our slowest queries. The query itself is fine but the amount of data returned is causing issues. `error` column constitutes >80% of data stored in the column - by removing it in the query we should see significant speedups. pganalyze page: https://app.pganalyze.com/databases/-675880137/queries/5301003665?t=7d
161 lines
5.4 KiB
TypeScript
161 lines
5.4 KiB
TypeScript
import { Hub, PluginError } from '../src/types'
|
|
import { createHub } from '../src/utils/db/hub'
|
|
import {
|
|
disablePlugin,
|
|
getPluginAttachmentRows,
|
|
getPluginConfigRows,
|
|
getPluginRows,
|
|
setError,
|
|
} from '../src/utils/db/sql'
|
|
import { commonOrganizationId, pluginConfig39 } from './helpers/plugins'
|
|
import { resetTestDatabase } from './helpers/sql'
|
|
|
|
jest.setTimeout(20_000)
|
|
jest.mock('../src/utils/status')
|
|
|
|
describe('sql', () => {
|
|
let hub: Hub
|
|
let closeHub: () => Promise<void>
|
|
|
|
beforeEach(async () => {
|
|
;[hub, closeHub] = await createHub()
|
|
await resetTestDatabase(`const processEvent = event => event`)
|
|
})
|
|
|
|
afterEach(async () => {
|
|
await closeHub()
|
|
})
|
|
|
|
test('getPluginAttachmentRows', async () => {
|
|
const rowsExpected = [
|
|
{
|
|
content_type: 'application/octet-stream',
|
|
contents: Buffer.from([116, 101, 115, 116]),
|
|
file_name: 'test.txt',
|
|
file_size: 4,
|
|
id: 42666,
|
|
key: 'maxmindMmdb',
|
|
plugin_config_id: 39,
|
|
team_id: 2,
|
|
},
|
|
]
|
|
|
|
const rows1 = await getPluginAttachmentRows(hub)
|
|
expect(rows1).toEqual(rowsExpected)
|
|
await hub.db.postgresQuery("update posthog_team set plugins_opt_in='f'", undefined, 'testTag')
|
|
const rows2 = await getPluginAttachmentRows(hub)
|
|
expect(rows2).toEqual(rowsExpected)
|
|
})
|
|
|
|
test('getPluginConfigRows', async () => {
|
|
const expectedRow = {
|
|
config: {
|
|
localhostIP: '94.224.212.175',
|
|
},
|
|
enabled: true,
|
|
has_error: false,
|
|
id: 39,
|
|
order: 0,
|
|
plugin_id: 60,
|
|
team_id: 2,
|
|
created_at: expect.anything(),
|
|
updated_at: expect.anything(),
|
|
}
|
|
|
|
const rows1 = await getPluginConfigRows(hub)
|
|
expect(rows1).toEqual([expectedRow])
|
|
|
|
await hub.db.postgresQuery("update posthog_team set plugins_opt_in='f'", undefined, 'testTag')
|
|
const pluginError: PluginError = { message: 'error happened', time: 'now' }
|
|
await setError(hub, pluginError, pluginConfig39)
|
|
|
|
const rows2 = await getPluginConfigRows(hub)
|
|
expect(rows2).toEqual([
|
|
{
|
|
...expectedRow,
|
|
has_error: true,
|
|
},
|
|
])
|
|
})
|
|
|
|
test('getPluginRows', async () => {
|
|
const rowsExpected = [
|
|
{
|
|
error: null,
|
|
from_json: false,
|
|
from_web: false,
|
|
id: 60,
|
|
is_global: false,
|
|
is_stateless: false,
|
|
organization_id: commonOrganizationId,
|
|
log_level: null,
|
|
name: 'test-maxmind-plugin',
|
|
plugin_type: 'custom',
|
|
public_jobs: null,
|
|
source__plugin_json:
|
|
'{"name":"posthog-maxmind-plugin","description":"just for testing","url":"http://example.com/plugin","config":{},"main":"index.js"}',
|
|
source__index_ts: 'const processEvent = event => event',
|
|
source__frontend_tsx: null,
|
|
tag: '0.0.2',
|
|
url: 'https://www.npmjs.com/package/posthog-maxmind-plugin',
|
|
capabilities: {},
|
|
},
|
|
]
|
|
|
|
const rows1 = await getPluginRows(hub)
|
|
expect(rows1).toEqual(rowsExpected)
|
|
await hub.db.postgresQuery("update posthog_team set plugins_opt_in='f'", undefined, 'testTag')
|
|
const rows2 = await getPluginRows(hub)
|
|
expect(rows2).toEqual(rowsExpected)
|
|
})
|
|
|
|
test('setError', async () => {
|
|
hub.db.postgresQuery = jest.fn() as any
|
|
|
|
await setError(hub, null, pluginConfig39)
|
|
expect(hub.db.postgresQuery).toHaveBeenCalledWith(
|
|
'UPDATE posthog_pluginconfig SET error = $1 WHERE id = $2',
|
|
[null, pluginConfig39.id],
|
|
'updatePluginConfigError'
|
|
)
|
|
|
|
const pluginError: PluginError = { message: 'error happened', time: 'now' }
|
|
await setError(hub, pluginError, pluginConfig39)
|
|
expect(hub.db.postgresQuery).toHaveBeenCalledWith(
|
|
'UPDATE posthog_pluginconfig SET error = $1 WHERE id = $2',
|
|
[pluginError, pluginConfig39.id],
|
|
'updatePluginConfigError'
|
|
)
|
|
})
|
|
|
|
describe('disablePlugin', () => {
|
|
test('disablePlugin query builds correctly', async () => {
|
|
hub.db.postgresQuery = jest.fn() as any
|
|
|
|
await disablePlugin(hub, 39)
|
|
expect(hub.db.postgresQuery).toHaveBeenCalledWith(
|
|
`UPDATE posthog_pluginconfig SET enabled='f' WHERE id=$1 AND enabled='t'`,
|
|
[39],
|
|
'disablePlugin'
|
|
)
|
|
})
|
|
|
|
test('disablePlugin disables a plugin', async () => {
|
|
const redis = await hub.db.redisPool.acquire()
|
|
const rowsBefore = await getPluginConfigRows(hub)
|
|
expect(rowsBefore[0].plugin_id).toEqual(60)
|
|
expect(rowsBefore[0].enabled).toEqual(true)
|
|
|
|
const receivedMessage = redis.subscribe(hub.PLUGINS_RELOAD_PUBSUB_CHANNEL)
|
|
await disablePlugin(hub, 39)
|
|
|
|
const rowsAfter = await getPluginConfigRows(hub)
|
|
|
|
expect(rowsAfter).toEqual([])
|
|
await expect(receivedMessage).resolves.toEqual(1)
|
|
|
|
await hub.db.redisPool.release(redis)
|
|
})
|
|
})
|
|
})
|