github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/ui/dashboard/src/utils/schema.ts (about) 1 import { 2 DashboardActions, 3 DashboardExecutionCompleteEvent, 4 DashboardExecutionEventWithSchema, 5 DashboardExecutionStartedEvent, 6 DashboardSnapshot, 7 } from "../types"; 8 import { 9 EXECUTION_COMPLETE_SCHEMA_VERSION_LATEST, 10 EXECUTION_SCHEMA_VERSION_20220614, 11 EXECUTION_SCHEMA_VERSION_20220929, 12 EXECUTION_SCHEMA_VERSION_20221222, 13 EXECUTION_STARTED_SCHEMA_VERSION_LATEST, 14 } from "../constants/versions"; 15 import { migratePanelStatuses } from "./dashboardEventHandlers"; 16 17 const executedStartedMigrations = [ 18 { 19 version: EXECUTION_SCHEMA_VERSION_20220614, 20 up: function ( 21 current: DashboardExecutionEventWithSchema 22 ): DashboardExecutionStartedEvent { 23 const { 24 action, 25 execution_id, 26 inputs, 27 layout, 28 panels = {}, 29 variables, 30 } = current; 31 return { 32 action, 33 execution_id, 34 inputs, 35 layout, 36 panels: migratePanelStatuses(panels, EXECUTION_SCHEMA_VERSION_20220614), 37 variables, 38 start_time: new Date().toISOString(), 39 schema_version: EXECUTION_SCHEMA_VERSION_20221222, 40 }; 41 }, 42 }, 43 { 44 version: EXECUTION_SCHEMA_VERSION_20221222, 45 up: function ( 46 current: DashboardExecutionEventWithSchema 47 ): DashboardExecutionStartedEvent { 48 // Nothing to do here as this event is already in the latest supported schema 49 return current as DashboardExecutionStartedEvent; 50 }, 51 }, 52 ]; 53 54 const executedCompletedMigrations = [ 55 { 56 version: EXECUTION_SCHEMA_VERSION_20220614, 57 up: function ( 58 current: DashboardExecutionEventWithSchema 59 ): DashboardExecutionCompleteEvent { 60 const { 61 action, 62 execution_id, 63 layout, 64 panels, 65 inputs, 66 variables, 67 search_path, 68 start_time, 69 end_time, 70 } = current; 71 return { 72 action, 73 schema_version: EXECUTION_SCHEMA_VERSION_20220929, 74 execution_id, 75 snapshot: { 76 schema_version: EXECUTION_SCHEMA_VERSION_20220929, 77 layout, 78 panels, 79 inputs, 80 variables, 81 search_path, 82 start_time, 83 end_time, 84 }, 85 }; 86 }, 87 }, 88 { 89 version: EXECUTION_SCHEMA_VERSION_20220929, 90 up: function ( 91 current: DashboardExecutionEventWithSchema 92 ): DashboardExecutionCompleteEvent { 93 // The shape is already correct - just need to bump the version 94 const { 95 action, 96 execution_id, 97 snapshot: { schema_version, panels = {}, ...snapshotRest }, 98 } = current; 99 return { 100 action, 101 schema_version: EXECUTION_SCHEMA_VERSION_20221222, 102 execution_id, 103 snapshot: { 104 schema_version: EXECUTION_SCHEMA_VERSION_20221222, 105 panels: migratePanelStatuses( 106 panels, 107 EXECUTION_SCHEMA_VERSION_20220929 108 ), 109 ...snapshotRest, 110 }, 111 }; 112 }, 113 }, 114 { 115 version: EXECUTION_SCHEMA_VERSION_20221222, 116 up: function ( 117 current: DashboardExecutionEventWithSchema 118 ): DashboardExecutionCompleteEvent { 119 // Nothing to do here as this event is already in the latest supported schema 120 return current as DashboardExecutionCompleteEvent; 121 }, 122 }, 123 ]; 124 125 const snapshotDataToExecutionCompleteMigrations = [ 126 { 127 version: EXECUTION_SCHEMA_VERSION_20220614, 128 toExecutionComplete: function ( 129 current: DashboardExecutionEventWithSchema 130 ): DashboardExecutionCompleteEvent { 131 const { 132 layout, 133 panels, 134 inputs, 135 variables, 136 search_path, 137 start_time, 138 end_time, 139 } = current; 140 return { 141 action: DashboardActions.EXECUTION_COMPLETE, 142 execution_id: "", 143 schema_version: EXECUTION_SCHEMA_VERSION_20220614, 144 // @ts-ignore 145 layout, 146 panels, 147 inputs, 148 variables, 149 search_path, 150 start_time, 151 end_time, 152 }; 153 }, 154 }, 155 { 156 version: EXECUTION_SCHEMA_VERSION_20220929, 157 toExecutionComplete: function ( 158 current: DashboardExecutionEventWithSchema 159 ): DashboardExecutionCompleteEvent { 160 const { 161 layout, 162 panels, 163 inputs, 164 variables, 165 search_path, 166 start_time, 167 end_time, 168 } = current; 169 return { 170 action: DashboardActions.EXECUTION_COMPLETE, 171 execution_id: "", 172 schema_version: EXECUTION_SCHEMA_VERSION_20220929, 173 snapshot: { 174 schema_version: EXECUTION_SCHEMA_VERSION_20220929, 175 layout, 176 panels, 177 inputs, 178 variables, 179 search_path, 180 start_time, 181 end_time, 182 }, 183 }; 184 }, 185 }, 186 { 187 version: EXECUTION_SCHEMA_VERSION_20221222, 188 toExecutionComplete: function ( 189 current: DashboardExecutionEventWithSchema 190 ): DashboardExecutionCompleteEvent { 191 const { 192 layout, 193 panels, 194 inputs, 195 variables, 196 search_path, 197 start_time, 198 end_time, 199 } = current; 200 return { 201 action: DashboardActions.EXECUTION_COMPLETE, 202 execution_id: "", 203 schema_version: EXECUTION_SCHEMA_VERSION_20221222, 204 snapshot: { 205 schema_version: EXECUTION_SCHEMA_VERSION_20221222, 206 layout, 207 panels, 208 inputs, 209 variables, 210 search_path, 211 start_time, 212 end_time, 213 }, 214 }; 215 }, 216 }, 217 ]; 218 219 const executionStartedVersionMigratorIndexLookup = {}; 220 for (const [index, migrator] of executedStartedMigrations.entries()) { 221 executionStartedVersionMigratorIndexLookup[migrator.version] = index; 222 } 223 224 const executionCompleteVersionMigratorIndexLookup = {}; 225 for (const [index, migrator] of executedCompletedMigrations.entries()) { 226 executionCompleteVersionMigratorIndexLookup[migrator.version] = index; 227 } 228 229 const snapshotDataToExecutionCompleteVersionMigratorIndexLookup = {}; 230 for (const [ 231 index, 232 migrator, 233 ] of snapshotDataToExecutionCompleteMigrations.entries()) { 234 snapshotDataToExecutionCompleteVersionMigratorIndexLookup[migrator.version] = 235 index; 236 } 237 238 class ExecutionStartedSchemaMigrator { 239 toLatest( 240 current: DashboardExecutionEventWithSchema 241 ): DashboardExecutionStartedEvent { 242 if (current.schema_version === EXECUTION_STARTED_SCHEMA_VERSION_LATEST) { 243 return current as DashboardExecutionStartedEvent; 244 } 245 const startingIndex = 246 executionStartedVersionMigratorIndexLookup[current.schema_version]; 247 if (startingIndex === undefined) { 248 throw new Error( 249 `Unsupported dashboard event schema ${current.schema_version}` 250 ); 251 } 252 let migrated = current; 253 for ( 254 let idx = startingIndex; 255 idx < executedStartedMigrations.length; 256 idx++ 257 ) { 258 const migrator = executedStartedMigrations[idx]; 259 migrated = migrator.up(migrated); 260 } 261 return migrated as DashboardExecutionStartedEvent; 262 } 263 } 264 265 class ExecutionCompleteSchemaMigrator { 266 toLatest( 267 current: DashboardExecutionEventWithSchema 268 ): DashboardExecutionCompleteEvent { 269 if (current.schema_version === EXECUTION_COMPLETE_SCHEMA_VERSION_LATEST) { 270 return current as DashboardExecutionCompleteEvent; 271 } 272 const startingIndex = 273 executionCompleteVersionMigratorIndexLookup[current.schema_version]; 274 if (startingIndex === undefined) { 275 throw new Error( 276 `Unsupported dashboard event schema ${current.schema_version}` 277 ); 278 } 279 let migrated = current; 280 for ( 281 let idx = startingIndex; 282 idx < executedCompletedMigrations.length; 283 idx++ 284 ) { 285 const migrator = executedCompletedMigrations[idx]; 286 migrated = migrator.up(migrated); 287 } 288 return migrated as DashboardExecutionCompleteEvent; 289 } 290 } 291 292 class SnapshotDataToExecutionCompleteSchemaMigrator { 293 toLatest(current: DashboardSnapshot): DashboardExecutionCompleteEvent { 294 const migratorIndex = 295 snapshotDataToExecutionCompleteVersionMigratorIndexLookup[ 296 current.schema_version 297 ]; 298 if (migratorIndex === undefined) { 299 throw new Error( 300 `Unsupported dashboard event schema ${current.schema_version}` 301 ); 302 } 303 const snapshotMigrator = 304 snapshotDataToExecutionCompleteMigrations[migratorIndex]; 305 const executionCompleteEvent = 306 snapshotMigrator.toExecutionComplete(current); 307 const executionCompleteEventMigrator = 308 new ExecutionCompleteSchemaMigrator(); 309 return executionCompleteEventMigrator.toLatest(executionCompleteEvent); 310 } 311 } 312 313 export { 314 ExecutionStartedSchemaMigrator, 315 ExecutionCompleteSchemaMigrator, 316 SnapshotDataToExecutionCompleteSchemaMigrator, 317 };