From 2166ae30d8901fd8be57a48e391ae4af4493fdcc Mon Sep 17 00:00:00 2001 From: Andrew Schwenn Date: Thu, 11 Apr 2024 16:27:07 -0500 Subject: [PATCH] fix(generate-matrix): dont load-balance when paths is fewer than batches --- dist/839.index.js | 4 ++-- dist/839.index.js.map | 2 +- src/helpers/generate-matrix.ts | 6 +++--- test/helpers/generate-matrix.test.ts | 24 ++++++++++++++++++++++++ 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/dist/839.index.js b/dist/839.index.js index 6712317d4..2ef3508e3 100644 --- a/dist/839.index.js +++ b/dist/839.index.js @@ -39,7 +39,7 @@ class GenerateMatrix extends _types_generated__WEBPACK_IMPORTED_MODULE_1__/* .He const generateMatrix = ({ paths, batches: _batches = '1', load_balancing_sizes }) => { const matrixValues = paths.split(/[\n,]/); const batches = Number(_batches); - if (!load_balancing_sizes) { + if (!load_balancing_sizes || matrixValues.length <= batches) { return { include: (0,lodash__WEBPACK_IMPORTED_MODULE_0__.chunk)(matrixValues, Math.ceil(matrixValues.length / batches)).map(chunk => ({ path: chunk.join(',') })) }; @@ -47,7 +47,7 @@ const generateMatrix = ({ paths, batches: _batches = '1', load_balancing_sizes } const loadBalancingSizes = load_balancing_sizes.split(/[\n,]/).map(size => Number(size)); if (loadBalancingSizes.length !== matrixValues.length) throw new Error('load_balancing_sizes input must have the same length as paths input'); - const targetLoadSize = loadBalancingSizes.reduce((acc, size) => acc + size, 0) / batches; + const targetLoadSize = (0,lodash__WEBPACK_IMPORTED_MODULE_0__.sum)(loadBalancingSizes) / batches; const loadBalancedPaths = []; let currentLoadSize = 0; let currentBatch = []; diff --git a/dist/839.index.js.map b/dist/839.index.js.map index 330ffa038..aaa266193 100644 --- a/dist/839.index.js.map +++ b/dist/839.index.js.map @@ -1 +1 @@ -{"version":3,"file":"839.index.js","mappings":";;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;AAWA;AAEA;AACA;AAEA;AACA;AAEA;AAAA;;AACA;AAGA;AAAA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAAA;AACA;AACA;AACA;AACA;;;;;;;;;;;AC5DA;;;;;;;;;;;AAWA;AAEA;AAgDA","sources":[".././src/helpers/generate-matrix.ts",".././src/types/generated.ts"],"sourcesContent":["/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\n/* eslint-disable functional/immutable-data */\n/* eslint-disable functional/no-let */\n\nimport { HelperInputs } from '../types/generated';\nimport { chunk } from 'lodash';\n\nexport class GenerateMatrix extends HelperInputs {\n paths = '';\n batches?: string;\n load_balancing_sizes?: string;\n}\n\nexport const generateMatrix = ({ paths, batches: _batches = '1', load_balancing_sizes }: GenerateMatrix) => {\n const matrixValues = paths.split(/[\\n,]/);\n const batches = Number(_batches);\n if (!load_balancing_sizes) {\n return {\n include: chunk(matrixValues, Math.ceil(matrixValues.length / batches)).map(chunk => ({ path: chunk.join(',') }))\n };\n }\n const loadBalancingSizes = load_balancing_sizes.split(/[\\n,]/).map(size => Number(size));\n if (loadBalancingSizes.length !== matrixValues.length)\n throw new Error('load_balancing_sizes input must have the same length as paths input');\n const targetLoadSize = loadBalancingSizes.reduce((acc, size) => acc + size, 0) / batches;\n const loadBalancedPaths: string[] = [];\n let currentLoadSize = 0;\n let currentBatch: string[] = [];\n matrixValues.forEach((path, index) => {\n const possibleLoadSize = currentLoadSize + loadBalancingSizes[index];\n if (Math.abs(possibleLoadSize - targetLoadSize) <= Math.abs(loadBalancingSizes[index] - targetLoadSize)) {\n currentLoadSize += loadBalancingSizes[index];\n currentBatch.push(path);\n } else {\n loadBalancedPaths.push(currentBatch.join(','));\n currentBatch = [path];\n currentLoadSize = loadBalancingSizes[index];\n }\n if (currentLoadSize >= targetLoadSize) {\n loadBalancedPaths.push(currentBatch.join(','));\n currentBatch = [];\n currentLoadSize = 0;\n }\n });\n if (currentBatch.length > 0) loadBalancedPaths.push(currentBatch.join(','));\n return {\n include: loadBalancedPaths.map(path => ({ path }))\n };\n};\n","/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nexport class HelperInputs {\n helper?: string;\n github_token?: string;\n body?: string;\n project_name?: string;\n project_destination_column_name?: string;\n note?: string;\n project_origin_column_name?: string;\n sha?: string;\n context?: string;\n state?: string;\n description?: string;\n target_url?: string;\n environment?: string;\n environment_url?: string;\n label?: string;\n labels?: string;\n paths?: string;\n ignore_globs?: string;\n extensions?: string;\n override_filter_paths?: string;\n batches?: string;\n pattern?: string;\n teams?: string;\n users?: string;\n login?: string;\n paths_no_filter?: string;\n slack_webhook_url?: string;\n number_of_assignees?: string;\n number_of_reviewers?: string;\n globs?: string;\n override_filter_globs?: string;\n title?: string;\n seconds?: string;\n pull_number?: string;\n base?: string;\n head?: string;\n days?: string;\n no_evict_upon_conflict?: string;\n skip_if_already_set?: string;\n delimiter?: string;\n team?: string;\n ignore_deleted?: string;\n return_full_payload?: string;\n skip_auto_merge?: string;\n repo_name?: string;\n repo_owner_name?: string;\n load_balancing_sizes?: string;\n}\n"],"names":[],"sourceRoot":""} \ No newline at end of file +{"version":3,"file":"839.index.js","mappings":";;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;AAWA;AAEA;AACA;AAEA;AACA;AAEA;AAAA;;AACA;AAGA;AAAA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAAA;AACA;AACA;AACA;AACA;;;;;;;;;;;AC5DA;;;;;;;;;;;AAWA;AAEA;AAgDA","sources":[".././src/helpers/generate-matrix.ts",".././src/types/generated.ts"],"sourcesContent":["/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\n/* eslint-disable functional/immutable-data */\n/* eslint-disable functional/no-let */\n\nimport { HelperInputs } from '../types/generated';\nimport { chunk, sum } from 'lodash';\n\nexport class GenerateMatrix extends HelperInputs {\n paths = '';\n batches?: string;\n load_balancing_sizes?: string;\n}\n\nexport const generateMatrix = ({ paths, batches: _batches = '1', load_balancing_sizes }: GenerateMatrix) => {\n const matrixValues = paths.split(/[\\n,]/);\n const batches = Number(_batches);\n if (!load_balancing_sizes || matrixValues.length <= batches) {\n return {\n include: chunk(matrixValues, Math.ceil(matrixValues.length / batches)).map(chunk => ({ path: chunk.join(',') }))\n };\n }\n const loadBalancingSizes = load_balancing_sizes.split(/[\\n,]/).map(size => Number(size));\n if (loadBalancingSizes.length !== matrixValues.length)\n throw new Error('load_balancing_sizes input must have the same length as paths input');\n const targetLoadSize = sum(loadBalancingSizes) / batches;\n const loadBalancedPaths: string[] = [];\n let currentLoadSize = 0;\n let currentBatch: string[] = [];\n matrixValues.forEach((path, index) => {\n const possibleLoadSize = currentLoadSize + loadBalancingSizes[index];\n if (Math.abs(possibleLoadSize - targetLoadSize) <= Math.abs(loadBalancingSizes[index] - targetLoadSize)) {\n currentLoadSize += loadBalancingSizes[index];\n currentBatch.push(path);\n } else {\n loadBalancedPaths.push(currentBatch.join(','));\n currentBatch = [path];\n currentLoadSize = loadBalancingSizes[index];\n }\n if (currentLoadSize >= targetLoadSize) {\n loadBalancedPaths.push(currentBatch.join(','));\n currentBatch = [];\n currentLoadSize = 0;\n }\n });\n if (currentBatch.length > 0) loadBalancedPaths.push(currentBatch.join(','));\n return {\n include: loadBalancedPaths.map(path => ({ path }))\n };\n};\n","/*\nCopyright 2021 Expedia, Inc.\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n https://www.apache.org/licenses/LICENSE-2.0\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\nexport class HelperInputs {\n helper?: string;\n github_token?: string;\n body?: string;\n project_name?: string;\n project_destination_column_name?: string;\n note?: string;\n project_origin_column_name?: string;\n sha?: string;\n context?: string;\n state?: string;\n description?: string;\n target_url?: string;\n environment?: string;\n environment_url?: string;\n label?: string;\n labels?: string;\n paths?: string;\n ignore_globs?: string;\n extensions?: string;\n override_filter_paths?: string;\n batches?: string;\n pattern?: string;\n teams?: string;\n users?: string;\n login?: string;\n paths_no_filter?: string;\n slack_webhook_url?: string;\n number_of_assignees?: string;\n number_of_reviewers?: string;\n globs?: string;\n override_filter_globs?: string;\n title?: string;\n seconds?: string;\n pull_number?: string;\n base?: string;\n head?: string;\n days?: string;\n no_evict_upon_conflict?: string;\n skip_if_already_set?: string;\n delimiter?: string;\n team?: string;\n ignore_deleted?: string;\n return_full_payload?: string;\n skip_auto_merge?: string;\n repo_name?: string;\n repo_owner_name?: string;\n load_balancing_sizes?: string;\n}\n"],"names":[],"sourceRoot":""} \ No newline at end of file diff --git a/src/helpers/generate-matrix.ts b/src/helpers/generate-matrix.ts index bc3f6b760..f932c4bd7 100644 --- a/src/helpers/generate-matrix.ts +++ b/src/helpers/generate-matrix.ts @@ -15,7 +15,7 @@ limitations under the License. /* eslint-disable functional/no-let */ import { HelperInputs } from '../types/generated'; -import { chunk } from 'lodash'; +import { chunk, sum } from 'lodash'; export class GenerateMatrix extends HelperInputs { paths = ''; @@ -26,7 +26,7 @@ export class GenerateMatrix extends HelperInputs { export const generateMatrix = ({ paths, batches: _batches = '1', load_balancing_sizes }: GenerateMatrix) => { const matrixValues = paths.split(/[\n,]/); const batches = Number(_batches); - if (!load_balancing_sizes) { + if (!load_balancing_sizes || matrixValues.length <= batches) { return { include: chunk(matrixValues, Math.ceil(matrixValues.length / batches)).map(chunk => ({ path: chunk.join(',') })) }; @@ -34,7 +34,7 @@ export const generateMatrix = ({ paths, batches: _batches = '1', load_balancing_ const loadBalancingSizes = load_balancing_sizes.split(/[\n,]/).map(size => Number(size)); if (loadBalancingSizes.length !== matrixValues.length) throw new Error('load_balancing_sizes input must have the same length as paths input'); - const targetLoadSize = loadBalancingSizes.reduce((acc, size) => acc + size, 0) / batches; + const targetLoadSize = sum(loadBalancingSizes) / batches; const loadBalancedPaths: string[] = []; let currentLoadSize = 0; let currentBatch: string[] = []; diff --git a/test/helpers/generate-matrix.test.ts b/test/helpers/generate-matrix.test.ts index a2859bb77..783b00834 100644 --- a/test/helpers/generate-matrix.test.ts +++ b/test/helpers/generate-matrix.test.ts @@ -102,4 +102,28 @@ describe('generateMatrix', () => { ] }); }); + + it("shouldn't load balance if the number of paths is lower than the desired batches", () => { + const result = generateMatrix({ + paths: 'path/1,path/2,path/3,path/4', + load_balancing_sizes: '2,2,1,12', + batches: '8' + }); + expect(result).toEqual({ + include: [ + { + path: 'path/1' + }, + { + path: 'path/2' + }, + { + path: 'path/3' + }, + { + path: 'path/4' + } + ] + }); + }); });