|
| 1 | +import { WorkspaceState } from '~/shared/api/backendApiTypes'; |
| 2 | +import type { Workspace, WorkspaceKindInfo } from '~/shared/api/backendApiTypes'; |
| 3 | + |
| 4 | +const generateMockWorkspace = ( |
| 5 | + name: string, |
| 6 | + namespace: string, |
| 7 | + state: WorkspaceState, |
| 8 | + paused: boolean, |
| 9 | + imageConfigId: string, |
| 10 | + imageConfigDisplayName: string, |
| 11 | + podConfigId: string, |
| 12 | + podConfigDisplayName: string, |
| 13 | + pvcName: string, |
| 14 | +): Workspace => { |
| 15 | + const currentTime = Date.now(); |
| 16 | + const lastActivityTime = currentTime - Math.floor(Math.random() * 1000000); |
| 17 | + const lastUpdateTime = currentTime - Math.floor(Math.random() * 100000); |
| 18 | + |
| 19 | + return { |
| 20 | + name, |
| 21 | + namespace, |
| 22 | + workspaceKind: { name: 'jupyterlab' } as WorkspaceKindInfo, |
| 23 | + deferUpdates: paused, |
| 24 | + paused, |
| 25 | + pausedTime: paused ? currentTime - Math.floor(Math.random() * 1000000) : 0, |
| 26 | + pendingRestart: Math.random() < 0.5, //to generate randomly True/False value |
| 27 | + state, |
| 28 | + stateMessage: |
| 29 | + state === WorkspaceState.WorkspaceStateRunning |
| 30 | + ? 'Workspace is running smoothly.' |
| 31 | + : state === WorkspaceState.WorkspaceStatePaused |
| 32 | + ? 'Workspace is paused.' |
| 33 | + : 'Workspace is operational.', |
| 34 | + podTemplate: { |
| 35 | + podMetadata: { |
| 36 | + labels: {}, |
| 37 | + annotations: {}, |
| 38 | + }, |
| 39 | + volumes: { |
| 40 | + home: { |
| 41 | + pvcName: `${pvcName}-home`, |
| 42 | + mountPath: '/home/jovyan', |
| 43 | + readOnly: false, |
| 44 | + }, |
| 45 | + data: [ |
| 46 | + { |
| 47 | + pvcName, |
| 48 | + mountPath: '/data/my-data', |
| 49 | + readOnly: paused, |
| 50 | + }, |
| 51 | + ], |
| 52 | + }, |
| 53 | + options: { |
| 54 | + imageConfig: { |
| 55 | + current: { |
| 56 | + id: imageConfigId, |
| 57 | + displayName: imageConfigDisplayName, |
| 58 | + description: 'JupyterLab environment', |
| 59 | + labels: [{ key: 'python_version', value: '3.11' }], |
| 60 | + }, |
| 61 | + }, |
| 62 | + podConfig: { |
| 63 | + current: { |
| 64 | + id: podConfigId, |
| 65 | + displayName: podConfigDisplayName, |
| 66 | + description: 'Pod configuration with resource limits', |
| 67 | + labels: [ |
| 68 | + { key: 'cpu', value: '100m' }, |
| 69 | + { key: 'memory', value: '128Mi' }, |
| 70 | + ], |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + activity: { |
| 76 | + lastActivity: lastActivityTime, |
| 77 | + lastUpdate: lastUpdateTime, |
| 78 | + }, |
| 79 | + services: [ |
| 80 | + { |
| 81 | + httpService: { |
| 82 | + displayName: 'Jupyter-lab', |
| 83 | + httpPath: `/workspace/${namespace}/${name}/Jupyter-lab/`, |
| 84 | + }, |
| 85 | + }, |
| 86 | + ], |
| 87 | + }; |
| 88 | +}; |
| 89 | + |
| 90 | +const generateMockWorkspaces = (numWorkspaces: number, byNamespace = false) => { |
| 91 | + const mockWorkspaces = []; |
| 92 | + const podConfigs = [ |
| 93 | + { id: 'small-cpu', displayName: 'Small CPU' }, |
| 94 | + { id: 'medium-cpu', displayName: 'Medium CPU' }, |
| 95 | + { id: 'large-cpu', displayName: 'Large CPU' }, |
| 96 | + ]; |
| 97 | + const imageConfigs = [ |
| 98 | + { id: 'jupyterlab_scipy_180', displayName: 'JupyterLab SciPy 1.8.0' }, |
| 99 | + { id: 'jupyterlab_tensorflow_230', displayName: 'JupyterLab TensorFlow 2.3.0' }, |
| 100 | + { id: 'jupyterlab_pytorch_120', displayName: 'JupyterLab PyTorch 1.2.0' }, |
| 101 | + ]; |
| 102 | + const namespaces = byNamespace ? ['kubeflow'] : ['kubeflow', 'system', 'user-example', 'default']; |
| 103 | + |
| 104 | + for (let i = 1; i <= numWorkspaces; i++) { |
| 105 | + const state = |
| 106 | + i % 3 === 0 |
| 107 | + ? WorkspaceState.WorkspaceStateError |
| 108 | + : i % 2 === 0 |
| 109 | + ? WorkspaceState.WorkspaceStatePaused |
| 110 | + : WorkspaceState.WorkspaceStateRunning; |
| 111 | + const paused = state === WorkspaceState.WorkspaceStatePaused; |
| 112 | + const name = `workspace-${i}`; |
| 113 | + const namespace = namespaces[i % namespaces.length]; |
| 114 | + const pvcName = `data-pvc-${i}`; |
| 115 | + |
| 116 | + const imageConfig = imageConfigs[i % imageConfigs.length]; |
| 117 | + const podConfig = podConfigs[i % podConfigs.length]; |
| 118 | + |
| 119 | + mockWorkspaces.push( |
| 120 | + generateMockWorkspace( |
| 121 | + name, |
| 122 | + namespace, |
| 123 | + state, |
| 124 | + paused, |
| 125 | + imageConfig.id, |
| 126 | + imageConfig.displayName, |
| 127 | + podConfig.id, |
| 128 | + podConfig.displayName, |
| 129 | + pvcName, |
| 130 | + ), |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + return mockWorkspaces; |
| 135 | +}; |
| 136 | + |
| 137 | +// Example usage |
| 138 | +export const mockWorkspaces = generateMockWorkspaces(5); |
| 139 | +export const mockWorkspacesByNS = generateMockWorkspaces(10, true); |
0 commit comments