From 643b4cdc2036e69bc8e9ee7d438406c73fa969e4 Mon Sep 17 00:00:00 2001 From: Dunemask Date: Mon, 8 Aug 2022 13:01:39 +0000 Subject: [PATCH] Pipeline Magic --- lib/jobs/pipelines.js | 31 +++++++++ src/Queries.jsx | 30 ++++---- src/ctx/JobContext.jsx | 4 +- .../jobs/builder/PipelineTrackSelector.jsx | 68 ++++++------------- 4 files changed, 68 insertions(+), 65 deletions(-) create mode 100644 lib/jobs/pipelines.js diff --git a/lib/jobs/pipelines.js b/lib/jobs/pipelines.js new file mode 100644 index 0000000..6781dbd --- /dev/null +++ b/lib/jobs/pipelines.js @@ -0,0 +1,31 @@ +import _ from "lodash"; + +const nest = (arr) => { + const obj = {}; + arr.reduce((o, s) => (o[s] = {}), obj); + return obj; +}; + +export const asTree = (branches) => { + const nests = branches.map((b)=>nest(b)); + return _.merge(...nests); + }; + +export const asBranches = (array) => { + const merged = []; + array.forEach((p, i) => { + p.forEach((v, i) => { + if (!merged[i]) merged[i] = []; + if (!merged[i].includes(v)) merged[i].push(v); + }); + }); + return merged; + } + +export const as1d = (a) => [].concat.apply([], a); + +export const selectBranch = (map,test) => { + const pipeline = map.find((pm)=>pm.includes(test)); + const testIndex = pipeline.findIndex((t) => t === test); + return pipeline.slice(0, testIndex + 1); + } diff --git a/src/Queries.jsx b/src/Queries.jsx index 1856066..68d91d3 100644 --- a/src/Queries.jsx +++ b/src/Queries.jsx @@ -2,26 +2,24 @@ import { useQuery } from '@tanstack/react-query' const QUALITEER_URL = "https://qualiteer.elijahparker3.repl.co/api"; -export const useCatalogTests = () => useQuery(['catalogTests'], () => - fetch(`${QUALITEER_URL}/catalog/tests`).then(res => - res.json() - ) +const useMock = true; + +const asMock = (data) => ({ data }); + +const fetchApi = (subPath)=> async ()=> fetch(`${QUALITEER_URL}${subPath}`).then((res)=>res.json()); + +export const useCatalogTests = () => useMock ? asMock([]): useQuery(['catalogTests'], fetchApi("/catalog/tests") ) -export const usePipelineMappings = () => useQuery(['pipelineMappings'], () => - fetch(`${QUALITEER_URL}/catalog/pipeline-mappings`).then(res => - res.json() - ) +export const usePipelineMappings = () => useMock ? asMock([ + ["primary", "secondary1", "tertiary1"], + ["primary", "secondary1", "tertiary2"], + ["primary", "secondary2", "tertiary3"], +]) : useQuery(['pipelineMappings'], fetchApi("/catalog/pipeline-mappings") ) -export const useSilencedAlerts = () => useQuery(['silenced'], () => - fetch(`${QUALITEER_URL}/alerting/silenced`).then(res => - res.json() - ) +export const useSilencedAlerts = () => useMock? asMock([]) : useQuery(['silenced'], fetchApi("/alerting/silenced") ) -export const useCurrentlyFailing = () => useQuery(['failing'], () => - fetch(`${QUALITEER_URL}/results/failing`).then(res => - res.json() - ) +export const useCurrentlyFailing = () => useQuery(['failing'], useMock? asMock([]) : fetchApi("/results/failing") ) \ No newline at end of file diff --git a/src/ctx/JobContext.jsx b/src/ctx/JobContext.jsx index 073b748..10c9ec6 100644 --- a/src/ctx/JobContext.jsx +++ b/src/ctx/JobContext.jsx @@ -94,11 +94,11 @@ export const JobProvider = ({ children }) => { function pipelineFactory(builderCache) { console.log("Would create pipeline with cache"); console.log(builderCache); - return jobFactory({testNames: ["single"]}); + return jobFactory({testNames: ["primary"]}); } function jobFactory(builderCache) { - if(builderCache.pipelineMasterTrack) return pipelineFactory(builderCache); + if(builderCache.tracks) return pipelineFactory(builderCache); // Find test const i = new Initiator(url); const jobId = `j${Date.now()}`; diff --git a/src/views/jobs/builder/PipelineTrackSelector.jsx b/src/views/jobs/builder/PipelineTrackSelector.jsx index 41ebd03..3f0293c 100644 --- a/src/views/jobs/builder/PipelineTrackSelector.jsx +++ b/src/views/jobs/builder/PipelineTrackSelector.jsx @@ -8,72 +8,46 @@ import AccordionSummary from "@mui/material/AccordionSummary"; import Box from "@mui/material/Box"; import Typography from "@mui/material/Typography"; import Stack from "@mui/material/Stack"; +import {selectBranch, asTree, asBranches, as1d} from "../../../../lib/jobs/pipelines.js"; function PipelineTrackSelector(props) { const { cache, setCache, back, next } = props; - - const pipelineTracks = []; - for (var track of cache.primarySelectedMappings) { - for (var i in track) { - if (!pipelineTracks[i]) pipelineTracks[i] = []; - if (pipelineTracks[i].includes(track[i])) continue; - pipelineTracks[i].push(track[i]); - } - } - + const {primarySelectedMappings: primaries} = cache; const addTrack = (test) => { - const pipelineMasterTrack = cache.pipelineMasterTrack ?? []; - for (var track of cache.primarySelectedMappings) { - const trackIndex = track.indexOf(test); - if (trackIndex === -1) continue; - const trackSlice = track.slice(0, trackIndex + 1); - trackSlice.forEach((t, i) => { - if (!pipelineMasterTrack[i]) pipelineMasterTrack[i] = []; - if (!pipelineMasterTrack[i].includes(t)) pipelineMasterTrack[i].push(t); - }); - break; - } - - console.log(pipelineMasterTrack); - setCache({ ...cache, pipelineMasterTrack }); + const tracks = cache.tracks ?? []; tracks.push(selectBranch(primaries, test)); + setCache({ ...cache, tracks}); }; const removeTrack = (test) => { - const pipelineMasterTrack = cache.pipelineMasterTrack ?? []; - const toRemove = []; - for (var mapping of cache.primarySelectedMappings) { - const testIndex = mapping.indexOf(test); - if (testIndex === -1) continue; - const removeSlice = mapping.slice(testIndex, mapping.length); - toRemove.push(...removeSlice); + const {tracks} = cache; + for(var i in tracks){ + const index = tracks[i].indexOf(test); + if(index===-1) continue; + tracks[i] = tracks[i].slice(0,index); } - - for (var i in pipelineMasterTrack) { - pipelineMasterTrack[i] = pipelineMasterTrack[i].filter( - (t) => !toRemove.includes(t) - ); - } - setCache({ ...cache, pipelineMasterTrack }); + setCache({ ...cache, tracks}); }; const selectTrack = (test) => () => { - const pipelineMasterTrack = cache.pipelineMasterTrack ?? []; - if (![].concat.apply([], pipelineMasterTrack).includes(test)) - return addTrack(test); - removeTrack(test); + if(as1d(cache.tracks).includes(test)) return removeTrack(test); + addTrack(test); }; - const getColor = (test) => { - return [].concat.apply([], cache.pipelineMasterTrack).includes(test) + const getColor = (test) => as1d(cache.tracks).includes(test) ? "primary" : null; - }; + + const nextClick = () => { + + setCache({...cache, branches: asBranches(primaries), tree: asTree(cache.tracks)}); + next(); + } return (

Select Track

- {pipelineTracks.map((track, i) => ( + {asBranches(primaries).map((track, i) => ( {i + 1} @@ -108,7 +82,7 @@ function PipelineTrackSelector(props) {
-