Skip to content

Instantly share code, notes, and snippets.

@tkh44
Created February 26, 2021 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tkh44/8248189a8b04af917decc356dbad26b0 to your computer and use it in GitHub Desktop.
Save tkh44/8248189a8b04af917decc356dbad26b0 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const targetingMachine = Machine({
id: "adHoc",
initial: "targetCategory",
context: {
targetType: "host",
targetSelection: "parameter", // param / exact
strategy: "random",
selectedTargets: new Set(),
targetParams: {
/*[key]: new Set() */
},
targetAll: false,
sampling: {
type: "Even",
percent: 100
}
},
states: {
targetCategory: {
on: {
SELECT_INFRA: "infrastructure",
SELECT_SERVICES: "services",
SELECT_ALFI: "applications"
}
},
infrastructure: {
initial: 'host',
on: {
SELECT_HOSTS: ".host",
SELECT_CONTAINERS: ".container",
SELECT_KUBERNETES: ".kubernetes"
},
states: {
"host": {
initial: "random",
on: {
SET_RANDOM: {
target: ".random",
actions: "setRandomStrategy"
},
SET_EXACT: {
target: ".exact",
actions: "setExactStrategy"
}
},
states: {
random: {
on: {
ADD_TARGET_PARAM: {
actions: "addTargetParam"
},
REMOVE_TARGET_PARAM: {
actions: "removeTargetParam"
}
}
},
exact: {
on: {
SELECT_TARGET: {
actions: "selectTarget"
},
DESELECT_TARGET: {
actions: "deselectTarget"
}
}
}
},
},
"container": {
initial: "random",
on: {
SET_RANDOM: {
target: ".random",
actions: "setRandomStrategy"
},
SET_EXACT: {
target: ".exact",
actions: "setExactStrategy"
}
},
states: {
random: {
on: {
ADD_TARGET_PARAM: {
actions: "addTargetParam"
},
REMOVE_TARGET_PARAM: {
actions: "removeTargetParam"
}
}
},
exact: {
on: {
SELECT_TARGET: {
actions: "selectTarget"
},
DESELECT_TARGET: {
actions: "deselectTarget"
}
}
}
}
},
"kubernetes": {
initial: "random",
states: {
random: {
on: {
ADD_TARGET_PARAM: {
actions: "addTargetParam"
},
REMOVE_TARGET_PARAM: {
actions: "removeTargetParam"
}
}
}
}
}
}
},
services: {
type: 'final'
},
applications: {
type: 'final'
},
}
},
{
actions: {
setRandomStrategy: assign({ strategy: "random" }),
setExactStrategy: assign({ strategy: "exact" }),
addTargetParam: assign({
targetParams: (context, event) => {
const copy = { ...context.targetParams };
if (copy[event.paramKey]) {
copy[event.paramKey].add(event.paramValue);
} else {
copy[event.paramKey] = new Set([event.paramValue]);
}
return copy;
}
}),
removeTargetParam: assign({
targetParams: (context, event) => {
const copy = { ...context.targetParams };
if (copy[event.paramKey]) {
copy[event.paramKey].delete(event.paramValue);
}
return copy;
}
}),
selectTarget: assign({ selectedTargets: (context, event) => context.selectedTargets.add(event.targetId) }),
deselectTarget: assign({ selectedTargets: (context, event) => context.selectedTargets.delete(event.targetId) })
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment