Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CCR] Update auto-follow pattern serialization #26885

Merged
merged 4 commits into from
Dec 11, 2018
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 @@ -8,21 +8,27 @@ const Chance = require('chance'); // eslint-disable-line import/no-extraneous-de
const chance = new Chance();

export const getAutoFollowPatternMock = (
name = chance.string(),
remoteCluster = chance.string(),
leaderIndexPatterns = [chance.string()],
followIndexPattern = chance.string()
) => ({
remote_cluster: remoteCluster,
leader_index_patterns: leaderIndexPatterns,
follow_index_pattern: followIndexPattern
name,
pattern: {
remote_cluster: remoteCluster,
leader_index_patterns: leaderIndexPatterns,
follow_index_pattern: followIndexPattern
}
});

export const getAutoFollowPatternListMock = (total = 3) => {
const list = {};
const list = {
patterns: []
};

let i = total;
while(i--) {
list[chance.string()] = getAutoFollowPatternMock();
list.patterns.push(getAutoFollowPatternMock());
}

return list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ export const wait = (time = 1000) => (data) => {
});
};

export const object = {
toArray(obj) {
return Object.keys(obj).map(k => ({ ...obj[k], __id__: k }));
},
};
export const objectToArray = (obj) => (
Object.keys(obj).map(k => ({ ...obj[k], __id__: k }))
);

export const arrayToObject = (array, keyProp = 'id') => (
array.reduce((acc, item) => {
acc[item[keyProp]] = item;
return acc;
}, {})
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { objectToArray, arrayToObject } from './utils';

describe('utils', () => {
describe('objectToArray()', () => {
it('should convert object to an array', () => {
const item1 = { name: 'foo' };
const item2 = { name: 'bar' };
const expected = [{ ...item1, __id__: 'item1' }, { ...item2, __id__: 'item2' }];
const output = objectToArray({ item1, item2 });

expect(output).toEqual(expected);
});
});

describe('arrayToObject()', () => {
it('should convert an array to array', () => {
const item1 = { name: 'foo', customKey: 'key1' };
const item2 = { name: 'bar', customKey: 'key2' };
const expected = { key1: item1, key2: item2 };
const output = arrayToObject([item1, item2], 'customKey');

expect(output).toEqual(expected);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import * as t from '../action_types';
import { arrayToObject } from '../../services/utils';

const initialState = {
byId: {},
Expand All @@ -16,7 +17,7 @@ const success = action => `${action}_SUCCESS`;
export const reducer = (state = initialState, action) => {
switch (action.type) {
case success(t.AUTO_FOLLOW_PATTERN_LOAD): {
return { ...state, byId: action.payload };
return { ...state, byId: arrayToObject(action.payload.patterns, 'name') };
}
case success(t.AUTO_FOLLOW_PATTERN_GET): {
return { ...state, byId: { ...state.byId, [action.payload.name]: action.payload } };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
*/

import { createSelector } from 'reselect';
import { object as objectUtils } from '../../services/utils';

const { toArray } = objectUtils;
import { objectToArray } from '../../services/utils';

// Api
export const getApiState = (state) => state.api;
Expand All @@ -29,4 +27,4 @@ export const getSelectedAutoFollowPattern = createSelector(getAutoFollowPatternS
}
return autoFollowPatternsState.byId[autoFollowPatternsState.selectedId];
});
export const getListAutoFollowPatterns = createSelector(getAutoFollowPatterns, (autoFollowPatterns) => toArray(autoFollowPatterns));
export const getListAutoFollowPatterns = createSelector(getAutoFollowPatterns, (autoFollowPatterns) => objectToArray(autoFollowPatterns));
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
*/

export const deserializeAutoFollowPattern = (
name,
{ remote_cluster, leader_index_patterns, follow_index_pattern } = {} // eslint-disable-line camelcase
{ name, pattern: { remote_cluster, leader_index_patterns, follow_index_pattern } } = { pattern: {} } // eslint-disable-line camelcase
) => ({
name,
remoteCluster: remote_cluster,
Expand All @@ -15,13 +14,7 @@ export const deserializeAutoFollowPattern = (
});

export const deserializeListAutoFollowPatterns = autofollowPatterns =>
Object.entries(autofollowPatterns).reduce(
(deserialized, [name, autofollowPattern]) => ({
...deserialized,
[name]: deserializeAutoFollowPattern(name, autofollowPattern),
}),
{}
);
autofollowPatterns.map(deserializeAutoFollowPattern);

export const serializeAutoFollowPattern = ({
remoteCluster,
Expand All @@ -32,4 +25,3 @@ export const serializeAutoFollowPattern = ({
leader_index_patterns: leaderIndexPatterns,
follow_index_pattern: followIndexPattern,
});

Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ describe('[CCR] auto-follow_serialization', () => {
};

const esObject = {
remote_cluster: expected.remoteCluster,
leader_index_patterns: expected.leaderIndexPatterns,
follow_index_pattern: expected.followIndexPattern
name: 'some-name',
pattern: {
remote_cluster: expected.remoteCluster,
leader_index_patterns: expected.leaderIndexPatterns,
follow_index_pattern: expected.followIndexPattern
}
};

expect(deserializeAutoFollowPattern('some-name', esObject)).toEqual(expected);
expect(deserializeAutoFollowPattern(esObject)).toEqual(expected);
});
});

Expand All @@ -39,35 +42,43 @@ describe('[CCR] auto-follow_serialization', () => {
const name1 = 'foo1';
const name2 = 'foo2';

const expected = {
[name1]: {
const expected = [
{
name: name1,
remoteCluster: 'foo1',
leaderIndexPatterns: ['foo1-*'],
followIndexPattern: 'bar2'
},
[name2]: {
{
name: name2,
remoteCluster: 'foo2',
leaderIndexPatterns: ['foo2-*'],
followIndexPattern: 'bar2'
}
};
];

const esObjects = {
[name1]: {
remote_cluster: expected[name1].remoteCluster,
leader_index_patterns: expected[name1].leaderIndexPatterns,
follow_index_pattern: expected[name1].followIndexPattern
},
[name2]: {
remote_cluster: expected[name2].remoteCluster,
leader_index_patterns: expected[name2].leaderIndexPatterns,
follow_index_pattern: expected[name2].followIndexPattern
}
patterns: [
{
name: name1,
pattern: {
remote_cluster: expected[0].remoteCluster,
leader_index_patterns: expected[0].leaderIndexPatterns,
follow_index_pattern: expected[0].followIndexPattern
}
},
{
name: name2,
pattern: {
remote_cluster: expected[1].remoteCluster,
leader_index_patterns: expected[1].leaderIndexPatterns,
follow_index_pattern: expected[1].followIndexPattern
}
}
]
};

expect(deserializeListAutoFollowPatterns(esObjects)).toEqual(expected);
expect(deserializeListAutoFollowPatterns(esObjects.patterns)).toEqual(expected);
});
});

Expand Down

This file was deleted.

This file was deleted.

Loading