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

exclude jobspec internal fields from operator_ui definition #1296

Merged
merged 1 commit into from
May 30, 2019
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
39 changes: 39 additions & 0 deletions operator_ui/__tests__/utils/jobSpecDefinition.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import jobSpecDefinition from 'utils/jobSpecDefinition'

const input = `{
"initiators": [
{
"type": "web",
"params": {
}
}
],
"tasks": [
{
"ID": 1,
"CreatedAt": "2019-05-27T11:31:27.81655-04:00",
"UpdatedAt": "2019-05-27T11:31:27.81655-04:00",
"DeletedAt": null,
"type": "httpget",
"confirmations": 0,
"params": {
"ID": "KeepMeBecauseIAmUserDefined",
"get": "https://dimroc-public.s3.amazonaws.com/etl-language-comparison/tweets20140416.tar.gz"
}
}
],
"startAt": null,
"endAt": null
}`

describe('utils/jobSpecDefinition', () => {
it('scrubs unwanted keys', () => {
const output = jobSpecDefinition(JSON.parse(input))
expect(output.tasks).toHaveLength(1)
expect(output.tasks[0].ID).toBeUndefined()
expect(output.tasks[0].CreatedAt).toBeUndefined()
expect(output.tasks[0].DeletedAt).toBeUndefined()
expect(output.tasks[0].UpdatedAt).toBeUndefined()
expect(output.tasks[0].params.ID).toEqual('KeepMeBecauseIAmUserDefined')
})
})
17 changes: 16 additions & 1 deletion operator_ui/src/utils/jobSpecDefinition.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
const DEFINITION_KEYS = ['initiators', 'tasks', 'startAt', 'endAt']
const SCRUBBED_KEYS = ['ID', 'CreatedAt', 'DeletedAt', 'UpdatedAt']

const scrub = payload => {
if (Array.isArray(payload)) {
return payload.map(p => scrub(p))
}
if (typeof payload !== 'object' || payload === null) {
return payload
}
const keepers = Object.keys(payload).filter(k => !SCRUBBED_KEYS.includes(k))
return keepers.reduce((obj, key) => ({ ...obj, [key]: payload[key] }), {})
}

export default jobSpec =>
DEFINITION_KEYS.reduce((obj, key) => ({ ...obj, [key]: jobSpec[key] }), {})
DEFINITION_KEYS.reduce(
(obj, key) => ({ ...obj, [key]: scrub(jobSpec[key]) }),
{}
)