Skip to content

Commit fb20fd6

Browse files
authored
Merge pull request #861 from oskarhane/format-tx-metadata
New format for tx metadata for logs
2 parents eea866d + d2f5411 commit fb20fd6

File tree

4 files changed

+176
-14
lines changed

4 files changed

+176
-14
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright (c) 2002-2018 "Neo4j, Inc"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
/* global jest, describe, afterEach, test, expect */
22+
import { version } from 'project-root/package.json'
23+
import { createEpicMiddleware } from 'redux-observable'
24+
import { createBus } from 'suber'
25+
import {
26+
executeSystemCommand,
27+
executeSingleCommand,
28+
handleSingleCommandEpic
29+
} from './commandsDuck'
30+
31+
jest.mock('services/bolt/bolt', () => {
32+
const orig = require.requireActual('services/bolt/bolt')
33+
return {
34+
...orig,
35+
routedWriteTransaction: jest.fn(() => [
36+
'id',
37+
Promise.resolve({ records: [] })
38+
])
39+
}
40+
})
41+
const bolt = require.requireMock('services/bolt/bolt')
42+
43+
jest.mock('shared/modules/settings/settingsDuck', () => {
44+
const orig = require.requireActual('shared/modules/settings/settingsDuck')
45+
return {
46+
...orig,
47+
getCmdChar: () => ':',
48+
shouldUseCypherThread: () => false
49+
}
50+
})
51+
52+
jest.mock('shared/modules/params/paramsDuck', () => {
53+
const orig = require.requireActual('shared/modules/params/paramsDuck')
54+
return {
55+
...orig,
56+
getParams: () => ({})
57+
}
58+
})
59+
60+
jest.mock('shared/modules/dbMeta/dbMetaDuck', () => {
61+
const orig = require.requireActual('shared/modules/dbMeta/dbMetaDuck')
62+
return {
63+
...orig,
64+
getVersion: () => '3.5.0' // support for tx metadata
65+
}
66+
})
67+
68+
describe('tx metadata with cypher', () => {
69+
afterEach(() => {
70+
bolt.routedWriteTransaction.mockReset()
71+
})
72+
73+
it('it adds tx metadata for user entered cypher queries', done => {
74+
// Given
75+
const bus = createBus()
76+
bus.applyReduxMiddleware(createEpicMiddleware(handleSingleCommandEpic))
77+
const $$responseChannel = 'test-channel'
78+
const action = executeSingleCommand('RETURN 1', 'id', 'rqid')
79+
action.$$responseChannel = $$responseChannel
80+
81+
bus.send(action.type, action)
82+
flushPromises().then(() => {
83+
expect(bolt.routedWriteTransaction).toHaveBeenCalledTimes(1)
84+
expect(bolt.routedWriteTransaction).toHaveBeenCalledWith(
85+
'RETURN 1',
86+
{},
87+
expect.objectContaining({
88+
txMetadata: { app: `neo4j-browser_v${version}`, type: 'user-direct' }
89+
})
90+
)
91+
done()
92+
})
93+
})
94+
95+
it('it adds tx metadata for system cypher queries', done => {
96+
// Given
97+
const bus = createBus()
98+
bus.applyReduxMiddleware(createEpicMiddleware(handleSingleCommandEpic))
99+
const $$responseChannel = 'test-channel2'
100+
const action = executeSystemCommand('RETURN 1')
101+
action.$$responseChannel = $$responseChannel
102+
103+
bus.send(action.type, action)
104+
flushPromises().then(() => {
105+
expect(bolt.routedWriteTransaction).toHaveBeenCalledTimes(1)
106+
expect(bolt.routedWriteTransaction).toHaveBeenCalledWith(
107+
'RETURN 1',
108+
{},
109+
expect.objectContaining({
110+
txMetadata: { app: `neo4j-browser_v${version}`, type: 'system' }
111+
})
112+
)
113+
done()
114+
})
115+
})
116+
})
117+
118+
function flushPromises () {
119+
return new Promise(resolve => setImmediate(resolve))
120+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2002-2018 "Neo4j, Inc"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
import { version } from 'project-root/package.json'
22+
import { getBackgroundTxMetadata, getUserTxMetadata } from './txMetadata'
23+
24+
test('getBackgroundTxMetadata has the expected format', () => {
25+
const res = getBackgroundTxMetadata({ hasServerSupport: true })
26+
expect(res.txMetadata).toEqual({
27+
type: 'system',
28+
app: `neo4j-browser_v${version}`
29+
})
30+
})
31+
32+
test('getUserTxMetadata has the expected format', () => {
33+
const res = getUserTxMetadata({ hasServerSupport: true })
34+
expect(res.txMetadata).toEqual({
35+
type: 'user-direct',
36+
app: `neo4j-browser_v${version}`
37+
})
38+
})

src/shared/services/bolt/txMetadata.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@
2020

2121
import { version } from 'project-root/package.json'
2222

23-
let counter = 0
24-
2523
// Application info
26-
const NEO4J_BROWSER_BACKGROUND_QUERY = `NEO4J_BROWSER_BACKGROUND_QUERY`
27-
const NEO4J_BROWSER_USER_QUERY = `NEO4J_BROWSER_USER_QUERY`
28-
const NEO4J_BROWSER_APP_ID = `NEO4J_BROWSER_V${version}`
24+
const NEO4J_BROWSER_BACKGROUND_QUERY = `system`
25+
const NEO4J_BROWSER_USER_QUERY = `user-direct`
26+
const NEO4J_BROWSER_APP_ID = `neo4j-browser_v${version}`
2927

3028
export const getBackgroundTxMetadata = ({ hasServerSupport = false }) => {
3129
if (!hasServerSupport) {
@@ -34,8 +32,7 @@ export const getBackgroundTxMetadata = ({ hasServerSupport = false }) => {
3432
return {
3533
txMetadata: {
3634
type: NEO4J_BROWSER_BACKGROUND_QUERY,
37-
application: NEO4J_BROWSER_APP_ID,
38-
query_number: ++counter
35+
app: NEO4J_BROWSER_APP_ID
3936
}
4037
}
4138
}
@@ -47,8 +44,7 @@ export const getUserTxMetadata = ({ hasServerSupport = false }) => {
4744
return {
4845
txMetadata: {
4946
type: NEO4J_BROWSER_USER_QUERY,
50-
application: NEO4J_BROWSER_APP_ID,
51-
query_number: ++counter
47+
app: NEO4J_BROWSER_APP_ID
5248
}
5349
}
5450
}

src/shared/services/commandInterpreterHelper.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ import {
3939
showErrorMessage,
4040
cypher,
4141
successfulCypher,
42-
unsuccessfulCypher
42+
unsuccessfulCypher,
43+
SINGLE_COMMAND_QUEUED
4344
} from 'shared/modules/commands/commandsDuck'
4445
import { handleParamsCommand } from 'shared/modules/commands/helpers/params'
4546
import {
@@ -59,7 +60,10 @@ import {
5960
import { fetchRemoteGrass } from 'shared/modules/commands/helpers/grass'
6061
import { parseGrass } from 'shared/services/grassUtils'
6162
import { shouldUseCypherThread } from 'shared/modules/settings/settingsDuck'
62-
import { getUserTxMetadata } from 'shared/services/bolt/txMetadata'
63+
import {
64+
getUserTxMetadata,
65+
getBackgroundTxMetadata
66+
} from 'shared/services/bolt/txMetadata'
6367

6468
const availableCommands = [
6569
{
@@ -154,9 +158,13 @@ const availableCommands = [
154158
put,
155159
getParams(state),
156160
shouldUseCypherThread(state),
157-
getUserTxMetadata({
158-
hasServerSupport: canSendTxMetadata(store.getState())
159-
})
161+
action.type === SINGLE_COMMAND_QUEUED
162+
? getUserTxMetadata({
163+
hasServerSupport: canSendTxMetadata(store.getState())
164+
})
165+
: getBackgroundTxMetadata({
166+
hasServerSupport: canSendTxMetadata(store.getState())
167+
})
160168
)
161169
put(cypher(action.cmd))
162170
put(frames.add({ ...action, type: 'cypher', requestId: id }))

0 commit comments

Comments
 (0)