Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,50 @@ import { get } from 'lodash';
const uiSettingDefaults = getUiSettingDefaults();
const defaultSearchQueryLanguageSetting = uiSettingDefaults['search:queryLanguage'].value;

export async function fetch(callCluster) {
const [response, config] = await Promise.all([
callCluster('get', {
index: '.kibana',
type: 'doc',
id: 'kql-telemetry:kql-telemetry',
ignore: [404],
}),
callCluster('search', {
index: '.kibana',
body: { query: { term: { type: 'config' } } },
ignore: [404],
}),
]);
export function fetchProvider(index) {
return async callCluster => {
const [response, config] = await Promise.all([
callCluster('get', {
index,
type: 'doc',
id: 'kql-telemetry:kql-telemetry',
ignore: [404],
}),
callCluster('search', {
index,
body: { query: { term: { type: 'config' } } },
ignore: [404],
}),
]);

const queryLanguageConfigValue = get(config, 'hits.hits[0]._source.config.search:queryLanguage');
const queryLanguageConfigValue = get(config, 'hits.hits[0]._source.config.search:queryLanguage');

// search:queryLanguage can potentially be in four states in the .kibana index:
// 1. undefined: this means the user has never touched this setting
// 2. null: this means the user has touched the setting, but the current value matches the default
// 3. 'kuery' or 'lucene': this means the user has explicitly selected the given non-default language
//
// It's nice to know if the user has never touched the setting or if they tried kuery then
// went back to the default, so I preserve this info by prefixing the language name with
// 'default-' when the value in .kibana is undefined (case #1).
let defaultLanguage;
if (queryLanguageConfigValue === undefined) {
defaultLanguage = `default-${defaultSearchQueryLanguageSetting}`;
} else if (queryLanguageConfigValue === null) {
defaultLanguage = defaultSearchQueryLanguageSetting;
} else {
defaultLanguage = queryLanguageConfigValue;
}
// search:queryLanguage can potentially be in four states in the .kibana index:
// 1. undefined: this means the user has never touched this setting
// 2. null: this means the user has touched the setting, but the current value matches the default
// 3. 'kuery' or 'lucene': this means the user has explicitly selected the given non-default language
//
// It's nice to know if the user has never touched the setting or if they tried kuery then
// went back to the default, so I preserve this info by prefixing the language name with
// 'default-' when the value in .kibana is undefined (case #1).
let defaultLanguage;
if (queryLanguageConfigValue === undefined) {
defaultLanguage = `default-${defaultSearchQueryLanguageSetting}`;
} else if (queryLanguageConfigValue === null) {
defaultLanguage = defaultSearchQueryLanguageSetting;
} else {
defaultLanguage = queryLanguageConfigValue;
}

const kqlTelemetryDoc = {
optInCount: 0,
optOutCount: 0,
...get(response, '_source.kql-telemetry', {}),
};
const kqlTelemetryDoc = {
optInCount: 0,
optOutCount: 0,
...get(response, '_source.kql-telemetry', {}),
};

return {
...kqlTelemetryDoc,
defaultQueryLanguage: defaultLanguage,
return {
...kqlTelemetryDoc,
defaultQueryLanguage: defaultLanguage,
};
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ jest.mock('../../../ui_setting_defaults', () => ({
getUiSettingDefaults: () => ({ 'search:queryLanguage': { value: 'lucene' } }),
}));

import { fetch } from './fetch';
import { fetchProvider } from './fetch';

let fetch;
let callCluster;

function setupMockCallCluster(optCount, language) {
Expand Down Expand Up @@ -74,6 +75,10 @@ function setupMockCallCluster(optCount, language) {

describe('makeKQLUsageCollector', () => {
describe('fetch method', () => {
beforeEach(() => {
fetch = fetchProvider('.kibana');
});

it('should return opt in data from the .kibana/kql-telemetry doc', async () => {
setupMockCallCluster({ optInCount: 1 }, 'kuery');
const fetchResponse = await fetch(callCluster);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
* under the License.
*/

import { fetch } from './fetch';
import { fetchProvider } from './fetch';

export function makeKQLUsageCollector(server) {
const index = server.config().get('kibana.index');
const fetch = fetchProvider(index);
const kqlUsageCollector = server.usage.collectorSet.makeUsageCollector({
type: 'kql',
fetch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('makeKQLUsageCollector', () => {
usage: {
collectorSet: { makeUsageCollector: makeUsageCollectorStub, register: registerStub },
},
config: () => ({ get: () => '.kibana' })
};
});

Expand Down