Skip to content

Commit

Permalink
utilizes countBy and removes reduce in favor of a filter on getDuplic…
Browse files Browse the repository at this point in the history
…ates function
  • Loading branch information
dhurley14 committed Feb 11, 2020
1 parent f00a7c4 commit 64390df
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
*/

import Hapi from 'hapi';
import { isFunction } from 'lodash/fp';
import { isFunction, countBy } from 'lodash/fp';
import uuid from 'uuid';
import { DETECTION_ENGINE_RULES_URL } from '../../../../../common/constants';
import { createRules } from '../../rules/create_rules';
import { BulkRulesRequest } from '../../rules/types';
import { ServerFacade } from '../../../../types';
import { readRules } from '../../rules/read_rules';
import { transformOrBulkError, getMapDuplicates, getDuplicates } from './utils';
import { transformOrBulkError, getDuplicates } from './utils';
import { getIndexExists } from '../../index/get_index_exists';
import {
callWithRequestFactory,
Expand Down Expand Up @@ -48,7 +48,7 @@ export const createCreateRulesBulkRoute = (server: ServerFacade): Hapi.ServerRou
return headers.response().code(404);
}

const mappedDuplicates = getMapDuplicates(request.payload, 'rule_id');
const mappedDuplicates = countBy('rule_id', request.payload);
const dupes = getDuplicates(mappedDuplicates);

const rules = await Promise.all(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import Boom from 'boom';
import { Dictionary } from 'lodash';

import {
transformAlertToRule,
Expand All @@ -17,6 +18,7 @@ import {
transformRulesToNdjson,
transformAlertsToRules,
transformOrImportError,
getDuplicates,
} from './utils';
import { getResult } from '../__mocks__/request_responses';
import { INTERNAL_IDENTIFIER } from '../../../../../common/constants';
Expand Down Expand Up @@ -1172,4 +1174,25 @@ describe('utils', () => {
expect(output).toEqual(expected);
});
});

describe('getDuplicates', () => {
test("returns a string showing the duplicate keys of 'value2' and 'value3'", () => {
const output = getDuplicates({
value1: 1,
value2: 2,
value3: 2,
});
const expected = 'value2, value3';
expect(output).toEqual(expected);
});
test('returns null when given a map of no duplicates', () => {
const output = getDuplicates({
value1: 1,
value2: 1,
value3: 1,
});
const expected = null;
expect(output).toEqual(expected);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/

import Boom from 'boom';
import { pickBy } from 'lodash/fp';
import { pickBy, countBy } from 'lodash/fp';
import { Dictionary } from 'lodash';
import { SavedObject } from 'kibana/server';
import { INTERNAL_IDENTIFIER } from '../../../../../common/constants';
import {
Expand Down Expand Up @@ -207,30 +208,11 @@ export const transformOrImportError = (
}
};

export const getMapDuplicates = (
arr: Array<{ [key: string]: string }>,
prop: string
): Map<string, number> =>
arr.reduce<Map<string, number>>((acc, item) => {
if (item[prop] != null && acc.has(item[prop])) {
const totalView = acc.get(item[prop]) ?? 1;
acc.set(item[prop], totalView + 1);
} else if (item[prop] != null) {
acc.set(item[prop], 1);
}
return acc;
}, new Map());

export const getDuplicates = (someMap: Map<string, number>): string | null => {
const hasDuplicates = Array.from(someMap.values()).some(i => i > 1);
export const getDuplicates = (lodashDict: Dictionary<number>): string | null => {
const hasDuplicates = Object.values(lodashDict).some(i => i > 1);
if (hasDuplicates) {
return Array.from(someMap.entries())
.reduce<string[]>((acc, [key, val]) => {
if (val > 1) {
return [...acc, key];
}
return acc;
}, [])
return Object.keys(lodashDict)
.filter(key => lodashDict[key] > 1)
.join(', ');
}
return null;
Expand Down

0 comments on commit 64390df

Please sign in to comment.