diff options
Diffstat (limited to 'frontend/src/sagas/topology.js')
| -rw-r--r-- | frontend/src/sagas/topology.js | 370 |
1 files changed, 119 insertions, 251 deletions
diff --git a/frontend/src/sagas/topology.js b/frontend/src/sagas/topology.js index b38358a4..0be619a9 100644 --- a/frontend/src/sagas/topology.js +++ b/frontend/src/sagas/topology.js @@ -8,181 +8,95 @@ import { } from '../actions/objects' import { cancelNewRoomConstructionSucceeded, - setCurrentDatacenter, + setCurrentTopology, startNewRoomConstructionSucceeded, } from '../actions/topology/building' -import { addRoomToDatacenter } from '../api/routes/datacenters' -import { addTileToRoom, deleteRoom, updateRoom } from '../api/routes/rooms' -import { - addMachineToRackOnTile, - addRackToTile, - deleteMachineInRackOnTile, - deleteRackFromTile, - deleteTile, - updateMachineInRackOnTile, - updateRackOnTile, -} from '../api/routes/tiles' import { DEFAULT_RACK_POWER_CAPACITY, DEFAULT_RACK_SLOT_CAPACITY, MAX_NUM_UNITS_PER_MACHINE, } from '../components/app/map/MapConstants' -import { - fetchAndStoreAllCPUs, - fetchAndStoreAllGPUs, - fetchAndStoreAllMemories, - fetchAndStoreAllStorages, - fetchAndStoreCoolingItem, - fetchAndStoreCPU, - fetchAndStoreDatacenter, - fetchAndStoreGPU, - fetchAndStoreMachinesOfTile, - fetchAndStoreMemory, - fetchAndStorePath, - fetchAndStorePathsOfSimulation, - fetchAndStorePSU, - fetchAndStoreRackOnTile, - fetchAndStoreRoomsOfDatacenter, - fetchAndStoreSectionsOfPath, - fetchAndStoreStorage, - fetchAndStoreTilesOfRoom, -} from './objects' +import { fetchAndStoreTopology, updateTopologyOnServer } from './objects' +import { uuid } from 'uuidv4' +import { addTopology, deleteTopology } from '../api/routes/topologies' -export function* fetchLatestDatacenter(simulationId) { +export function* fetchTopologyOfExperiment(experiment) { try { - const paths = yield fetchAndStorePathsOfSimulation(simulationId) - const latestPath = paths[paths.length - 1] - const sections = yield fetchAndStoreSectionsOfPath(latestPath.id) - const latestSection = sections[sections.length - 1] - yield fetchAllUnitSpecifications() - yield fetchDatacenter(latestSection.datacenterId) - yield put(setCurrentDatacenter(latestSection.datacenterId)) + yield fetchAndStoreTopology(experiment.topologyId) + yield put(setCurrentTopology(experiment.topologyId)) } catch (error) { console.error(error) } } -export function* fetchAllDatacentersOfExperiment(experiment) { +export function* fetchAndStoreAllTopologiesOfSimulation(simulationId) { try { - const path = yield fetchAndStorePath(experiment.pathId) - const sections = yield fetchAndStoreSectionsOfPath(path.id) - path.sectionIds = sections.map(section => section.id) - yield fetchAllUnitSpecifications() + const simulation = yield select((state) => state.objects.simulation[simulationId]) - for (let i in sections) { - yield fetchDatacenter(sections[i].datacenterId) + for (let i in simulation.topologyIds) { + yield fetchAndStoreTopology(simulation.topologyIds[i]) } - yield put(setCurrentDatacenter(sections[0].datacenterId)) + + yield put(setCurrentTopology(simulation.topologyIds[0])) } catch (error) { console.error(error) } } -function* fetchDatacenter(datacenterId) { +export function* onAddTopology(action) { try { - yield fetchAndStoreDatacenter(datacenterId) - const rooms = yield fetchAndStoreRoomsOfDatacenter(datacenterId) - yield put( - addPropToStoreObject('datacenter', datacenterId, { - roomIds: rooms.map(room => room.id), - }), + const currentSimulationId = yield select((state) => state.currentSimulationId) + + const topology = yield call( + addTopology, + Object.assign({}, action.topology, { + _id: -1, + simulationId: currentSimulationId, + }) ) + yield put(addToStore('topology', topology)) - for (let index in rooms) { - yield fetchRoom(rooms[index].id) - } + const topologyIds = yield select((state) => state.objects.simulation[currentSimulationId].topologyIds) + yield put( + addPropToStoreObject('simulation', currentSimulationId, { + topologyIds: topologyIds.concat([topology._id]), + }) + ) } catch (error) { console.error(error) } } -function* fetchAllUnitSpecifications() { +export function* onDeleteTopology(action) { try { - yield fetchAndStoreAllCPUs() - yield fetchAndStoreAllGPUs() - yield fetchAndStoreAllMemories() - yield fetchAndStoreAllStorages() - } catch (error) { - console.error(error) - } -} - -function* fetchRoom(roomId) { - const tiles = yield fetchAndStoreTilesOfRoom(roomId) - yield put( - addPropToStoreObject('room', roomId, { - tileIds: tiles.map(tile => tile.id), - }), - ) - - for (let index in tiles) { - yield fetchTile(tiles[index]) - } -} - -function* fetchTile(tile) { - if (!tile.objectType) { - return - } - - switch (tile.objectType) { - case 'RACK': - const rack = yield fetchAndStoreRackOnTile(tile.objectId, tile.id) - yield put(addPropToStoreObject('tile', tile.id, { rackId: rack.id })) - yield fetchMachinesOfRack(tile.id, rack) - break - case 'COOLING_ITEM': - const coolingItem = yield fetchAndStoreCoolingItem(tile.objectId) - yield put( - addPropToStoreObject('tile', tile.id, { coolingItemId: coolingItem.id }), - ) - break - case 'PSU': - const psu = yield fetchAndStorePSU(tile.objectId) - yield put(addPropToStoreObject('tile', tile.id, { psuId: psu.id })) - break - default: - console.warn('Unknown rack type encountered while fetching tile objects') - } -} - -function* fetchMachinesOfRack(tileId, rack) { - const machines = yield fetchAndStoreMachinesOfTile(tileId) - const machineIds = new Array(rack.capacity).fill(null) - machines.forEach(machine => (machineIds[machine.position - 1] = machine.id)) + yield call(deleteTopology, action.id) - yield put(addPropToStoreObject('rack', rack.id, { machineIds })) + const currentSimulationId = yield select((state) => state.currentSimulationId) + const topologyIds = yield select((state) => state.objects.simulation[currentSimulationId].topologyIds) - for (let index in machines) { - for (let i in machines[index].cpuIds) { - yield fetchAndStoreCPU(machines[index].cpuIds[i]) - } - for (let i in machines[index].gpuIds) { - yield fetchAndStoreGPU(machines[index].gpuIds[i]) - } - for (let i in machines[index].memoryIds) { - yield fetchAndStoreMemory(machines[index].memoryIds[i]) - } - for (let i in machines[index].storageIds) { - yield fetchAndStoreStorage(machines[index].storageIds[i]) - } + yield put( + addPropToStoreObject('simulation', currentSimulationId, { + topologyIds: topologyIds.filter((id) => id !== action.id), + }) + ) + } catch (error) { + console.error(error) } } export function* onStartNewRoomConstruction() { try { - const datacenterId = yield select(state => state.currentDatacenterId) - const room = yield call(addRoomToDatacenter, { - id: -1, - datacenterId, - roomType: 'SERVER', - }) - const roomWithEmptyTileList = Object.assign({}, room, { tileIds: [] }) - yield put(addToStore('room', roomWithEmptyTileList)) - yield put( - addIdToStoreObjectListProp('datacenter', datacenterId, 'roomIds', room.id), - ) - yield put(startNewRoomConstructionSucceeded(room.id)) + const topologyId = yield select((state) => state.currentTopologyId) + const room = { + _id: uuid(), + name: 'Room', + topologyId, + tileIds: [], + } + yield put(addToStore('room', room)) + yield put(addIdToStoreObjectListProp('topology', topologyId, 'roomIds', room._id)) + yield updateTopologyOnServer(topologyId) + yield put(startNewRoomConstructionSucceeded(room._id)) } catch (error) { console.error(error) } @@ -190,19 +104,11 @@ export function* onStartNewRoomConstruction() { export function* onCancelNewRoomConstruction() { try { - const datacenterId = yield select(state => state.currentDatacenterId) - const roomId = yield select( - state => state.construction.currentRoomInConstruction, - ) - yield call(deleteRoom, roomId) - yield put( - removeIdFromStoreObjectListProp( - 'datacenter', - datacenterId, - 'roomIds', - roomId, - ), - ) + const topologyId = yield select((state) => state.currentTopologyId) + const roomId = yield select((state) => state.construction.currentRoomInConstruction) + yield put(removeIdFromStoreObjectListProp('topology', topologyId, 'roomIds', roomId)) + // TODO remove room from store, too + yield updateTopologyOnServer(topologyId) yield put(cancelNewRoomConstructionSucceeded()) } catch (error) { console.error(error) @@ -211,16 +117,17 @@ export function* onCancelNewRoomConstruction() { export function* onAddTile(action) { try { - const roomId = yield select( - state => state.construction.currentRoomInConstruction, - ) - const tile = yield call(addTileToRoom, { + const topologyId = yield select((state) => state.currentTopologyId) + const roomId = yield select((state) => state.construction.currentRoomInConstruction) + const tile = { + _id: uuid(), roomId, positionX: action.positionX, positionY: action.positionY, - }) + } yield put(addToStore('tile', tile)) - yield put(addIdToStoreObjectListProp('room', roomId, 'tileIds', tile.id)) + yield put(addIdToStoreObjectListProp('room', roomId, 'tileIds', tile._id)) + yield updateTopologyOnServer(topologyId) } catch (error) { console.error(error) } @@ -228,13 +135,10 @@ export function* onAddTile(action) { export function* onDeleteTile(action) { try { - const roomId = yield select( - state => state.construction.currentRoomInConstruction, - ) - yield call(deleteTile, action.tileId) - yield put( - removeIdFromStoreObjectListProp('room', roomId, 'tileIds', action.tileId), - ) + const topologyId = yield select((state) => state.currentTopologyId) + const roomId = yield select((state) => state.construction.currentRoomInConstruction) + yield put(removeIdFromStoreObjectListProp('room', roomId, 'tileIds', action.tileId)) + yield updateTopologyOnServer(topologyId) } catch (error) { console.error(error) } @@ -242,14 +146,12 @@ export function* onDeleteTile(action) { export function* onEditRoomName(action) { try { - const roomId = yield select(state => state.interactionLevel.roomId) - const room = Object.assign( - {}, - yield select(state => state.objects.room[roomId]), - ) + const topologyId = yield select((state) => state.currentTopologyId) + const roomId = yield select((state) => state.interactionLevel.roomId) + const room = Object.assign({}, yield select((state) => state.objects.room[roomId])) room.name = action.name - yield call(updateRoom, room) yield put(addPropToStoreObject('room', roomId, { name: action.name })) + yield updateTopologyOnServer(topologyId) } catch (error) { console.error(error) } @@ -257,18 +159,11 @@ export function* onEditRoomName(action) { export function* onDeleteRoom() { try { - const datacenterId = yield select(state => state.currentDatacenterId) - const roomId = yield select(state => state.interactionLevel.roomId) - yield call(deleteRoom, roomId) + const topologyId = yield select((state) => state.currentTopologyId) + const roomId = yield select((state) => state.interactionLevel.roomId) yield put(goDownOneInteractionLevel()) - yield put( - removeIdFromStoreObjectListProp( - 'datacenter', - datacenterId, - 'roomIds', - roomId, - ), - ) + yield put(removeIdFromStoreObjectListProp('topology', topologyId, 'roomIds', roomId)) + yield updateTopologyOnServer(topologyId) } catch (error) { console.error(error) } @@ -276,17 +171,12 @@ export function* onDeleteRoom() { export function* onEditRackName(action) { try { - const tileId = yield select(state => state.interactionLevel.tileId) - const rackId = yield select( - state => state.objects.tile[state.interactionLevel.tileId].objectId, - ) - const rack = Object.assign( - {}, - yield select(state => state.objects.rack[rackId]), - ) + const topologyId = yield select((state) => state.currentTopologyId) + const rackId = yield select((state) => state.objects.tile[state.interactionLevel.tileId].rackId) + const rack = Object.assign({}, yield select((state) => state.objects.rack[rackId])) rack.name = action.name - yield call(updateRackOnTile, tileId, rack) yield put(addPropToStoreObject('rack', rackId, { name: action.name })) + yield updateTopologyOnServer(topologyId) } catch (error) { console.error(error) } @@ -294,11 +184,11 @@ export function* onEditRackName(action) { export function* onDeleteRack() { try { - const tileId = yield select(state => state.interactionLevel.tileId) - yield call(deleteRackFromTile, tileId) + const topologyId = yield select((state) => state.currentTopologyId) + const tileId = yield select((state) => state.interactionLevel.tileId) yield put(goDownOneInteractionLevel()) - yield put(addPropToStoreObject('tile', tileId, { objectType: undefined })) - yield put(addPropToStoreObject('tile', tileId, { objectId: undefined })) + yield put(addPropToStoreObject('tile', tileId, { rackId: undefined })) + yield updateTopologyOnServer(topologyId) } catch (error) { console.error(error) } @@ -306,20 +196,16 @@ export function* onDeleteRack() { export function* onAddRackToTile(action) { try { - const rack = yield call(addRackToTile, action.tileId, { - id: -1, - name: 'Rack', + const topologyId = yield select((state) => state.currentTopologyId) + const rack = { + _id: uuid(), capacity: DEFAULT_RACK_SLOT_CAPACITY, powerCapacityW: DEFAULT_RACK_POWER_CAPACITY, - }) + } rack.machineIds = new Array(rack.capacity).fill(null) yield put(addToStore('rack', rack)) - yield put( - addPropToStoreObject('tile', action.tileId, { objectId: rack.id }), - ) - yield put( - addPropToStoreObject('tile', action.tileId, { objectType: 'RACK' }), - ) + yield put(addPropToStoreObject('tile', action.tileId, { rackId: rack._id })) + yield updateTopologyOnServer(topologyId) } catch (error) { console.error(error) } @@ -327,27 +213,24 @@ export function* onAddRackToTile(action) { export function* onAddMachine(action) { try { - const tileId = yield select(state => state.interactionLevel.tileId) - const rackId = yield select( - state => state.objects.tile[state.interactionLevel.tileId].objectId, - ) - const rack = yield select(state => state.objects.rack[rackId]) + const topologyId = yield select((state) => state.currentTopologyId) + const rackId = yield select((state) => state.objects.tile[state.interactionLevel.tileId].rackId) + const rack = yield select((state) => state.objects.rack[rackId]) - const machine = yield call(addMachineToRackOnTile, tileId, { - id: -1, - rackId, + const machine = { + _id: -1, position: action.position, - tags: [], cpuIds: [], gpuIds: [], memoryIds: [], storageIds: [], - }) + } yield put(addToStore('machine', machine)) const machineIds = [...rack.machineIds] - machineIds[machine.position - 1] = machine.id + machineIds[machine.position - 1] = machine._id yield put(addPropToStoreObject('rack', rackId, { machineIds })) + yield updateTopologyOnServer(topologyId) } catch (error) { console.error(error) } @@ -355,16 +238,15 @@ export function* onAddMachine(action) { export function* onDeleteMachine() { try { - const tileId = yield select(state => state.interactionLevel.tileId) - const position = yield select(state => state.interactionLevel.position) - const rack = yield select( - state => state.objects.rack[state.objects.tile[tileId].objectId], - ) - yield call(deleteMachineInRackOnTile, tileId, position) + const topologyId = yield select((state) => state.currentTopologyId) + const tileId = yield select((state) => state.interactionLevel.tileId) + const position = yield select((state) => state.interactionLevel.position) + const rack = yield select((state) => state.objects.rack[state.objects.tile[tileId].rackId]) const machineIds = [...rack.machineIds] machineIds[position - 1] = null yield put(goDownOneInteractionLevel()) - yield put(addPropToStoreObject('rack', rack.id, { machineIds })) + yield put(addPropToStoreObject('rack', rack._id, { machineIds })) + yield updateTopologyOnServer(topologyId) } catch (error) { console.error(error) } @@ -372,15 +254,12 @@ export function* onDeleteMachine() { export function* onAddUnit(action) { try { - const tileId = yield select(state => state.interactionLevel.tileId) - const position = yield select(state => state.interactionLevel.position) + const topologyId = yield select((state) => state.currentTopologyId) + const tileId = yield select((state) => state.interactionLevel.tileId) + const position = yield select((state) => state.interactionLevel.position) const machine = yield select( - state => - state.objects.machine[ - state.objects.rack[state.objects.tile[tileId].objectId].machineIds[ - position - 1 - ] - ], + (state) => + state.objects.machine[state.objects.rack[state.objects.tile[tileId].rackId].machineIds[position - 1]] ) if (machine[action.unitType + 'Ids'].length >= MAX_NUM_UNITS_PER_MACHINE) { @@ -388,17 +267,12 @@ export function* onAddUnit(action) { } const units = [...machine[action.unitType + 'Ids'], action.id] - const updatedMachine = Object.assign({}, machine, { - [action.unitType + 'Ids']: units, - }) - - yield call(updateMachineInRackOnTile, tileId, position, updatedMachine) - yield put( - addPropToStoreObject('machine', machine.id, { + addPropToStoreObject('machine', machine._id, { [action.unitType + 'Ids']: units, - }), + }) ) + yield updateTopologyOnServer(topologyId) } catch (error) { console.error(error) } @@ -406,28 +280,22 @@ export function* onAddUnit(action) { export function* onDeleteUnit(action) { try { - const tileId = yield select(state => state.interactionLevel.tileId) - const position = yield select(state => state.interactionLevel.position) + const topologyId = yield select((state) => state.currentTopologyId) + const tileId = yield select((state) => state.interactionLevel.tileId) + const position = yield select((state) => state.interactionLevel.position) const machine = yield select( - state => - state.objects.machine[ - state.objects.rack[state.objects.tile[tileId].objectId].machineIds[ - position - 1 - ] - ], + (state) => + state.objects.machine[state.objects.rack[state.objects.tile[tileId].rackId].machineIds[position - 1]] ) const unitIds = machine[action.unitType + 'Ids'].slice() unitIds.splice(action.index, 1) - const updatedMachine = Object.assign({}, machine, { - [action.unitType + 'Ids']: unitIds, - }) - yield call(updateMachineInRackOnTile, tileId, position, updatedMachine) yield put( - addPropToStoreObject('machine', machine.id, { + addPropToStoreObject('machine', machine._id, { [action.unitType + 'Ids']: unitIds, - }), + }) ) + yield updateTopologyOnServer(topologyId) } catch (error) { console.error(error) } |
