summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjc0b <j@jc0b.computer>2020-07-27 16:48:51 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2020-08-24 19:48:24 +0200
commit697a4be020d58e7d93966dd577fa590d73bf8d34 (patch)
tree37843f6dc88174b31b69dd72a63414f657e42f80
parentac32bdb6c33f546133dd4e1fd1dd400195354679 (diff)
Create prefabs from topologies
-rw-r--r--frontend/src/components/app/sidebars/topology/rack/AddPrefabComponent.js10
-rw-r--r--frontend/src/components/app/sidebars/topology/rack/RackSidebarComponent.js2
-rw-r--r--frontend/src/containers/app/sidebars/topology/rack/AddPrefabContainer.js13
-rw-r--r--frontend/src/sagas/index.js4
-rw-r--r--frontend/src/sagas/objects.js67
-rw-r--r--frontend/src/sagas/prefabs.js17
6 files changed, 85 insertions, 28 deletions
diff --git a/frontend/src/components/app/sidebars/topology/rack/AddPrefabComponent.js b/frontend/src/components/app/sidebars/topology/rack/AddPrefabComponent.js
new file mode 100644
index 00000000..75418f9d
--- /dev/null
+++ b/frontend/src/components/app/sidebars/topology/rack/AddPrefabComponent.js
@@ -0,0 +1,10 @@
+import React from 'react'
+
+const AddPrefabComponent = ({ onClick }) => (
+ <div className="btn btn-primary btn-block" onClick={onClick}>
+ <span className="fa fa-floppy-o mr-2" />
+ Save this rack to a prefab
+ </div>
+)
+
+export default AddPrefabComponent
diff --git a/frontend/src/components/app/sidebars/topology/rack/RackSidebarComponent.js b/frontend/src/components/app/sidebars/topology/rack/RackSidebarComponent.js
index c04e46d8..78414736 100644
--- a/frontend/src/components/app/sidebars/topology/rack/RackSidebarComponent.js
+++ b/frontend/src/components/app/sidebars/topology/rack/RackSidebarComponent.js
@@ -4,6 +4,7 @@ import DeleteRackContainer from '../../../../../containers/app/sidebars/topology
import MachineListContainer from '../../../../../containers/app/sidebars/topology/rack/MachineListContainer'
import RackNameContainer from '../../../../../containers/app/sidebars/topology/rack/RackNameContainer'
import './RackSidebarComponent.css'
+import AddPrefabContainer from "../../../../../containers/app/sidebars/topology/rack/AddPrefabContainer";
const RackSidebarComponent = () => {
return (
@@ -11,6 +12,7 @@ const RackSidebarComponent = () => {
<div className="rack-sidebar-header-container">
<RackNameContainer />
<BackToRoomContainer />
+ <AddPrefabContainer />
<DeleteRackContainer />
</div>
<div className="machine-list-container mt-2">
diff --git a/frontend/src/containers/app/sidebars/topology/rack/AddPrefabContainer.js b/frontend/src/containers/app/sidebars/topology/rack/AddPrefabContainer.js
new file mode 100644
index 00000000..9d0a2c74
--- /dev/null
+++ b/frontend/src/containers/app/sidebars/topology/rack/AddPrefabContainer.js
@@ -0,0 +1,13 @@
+import { connect } from 'react-redux'
+import {addPrefab} from "../../../../../actions/prefabs";
+import AddPrefabComponent from "../../../../../components/app/sidebars/topology/rack/AddPrefabComponent";
+
+const mapDispatchToProps = (dispatch) => {
+ return {
+ onClick: () => dispatch(addPrefab('name')),
+ }
+}
+
+const AddPrefabContainer = connect(undefined, mapDispatchToProps)(AddPrefabComponent)
+
+export default AddPrefabContainer
diff --git a/frontend/src/sagas/index.js b/frontend/src/sagas/index.js
index 9f6c4809..6a1f2af3 100644
--- a/frontend/src/sagas/index.js
+++ b/frontend/src/sagas/index.js
@@ -36,6 +36,8 @@ import { onFetchAuthorizationsOfCurrentUser, onFetchLoggedInUser } from './users
import { ADD_TOPOLOGY, DELETE_TOPOLOGY } from '../actions/topologies'
import { ADD_SCENARIO, DELETE_SCENARIO, OPEN_SCENARIO_SUCCEEDED, UPDATE_SCENARIO } from '../actions/scenarios'
import { onAddScenario, onDeleteScenario, onOpenScenarioSucceeded, onUpdateScenario } from './scenarios'
+import {onAddPrefab} from "./prefabs";
+import {ADD_PREFAB} from "../actions/prefabs";
export default function* rootSaga() {
yield takeEvery(LOG_IN, onFetchLoggedInUser)
@@ -73,4 +75,6 @@ export default function* rootSaga() {
yield takeEvery(ADD_SCENARIO, onAddScenario)
yield takeEvery(UPDATE_SCENARIO, onUpdateScenario)
yield takeEvery(DELETE_SCENARIO, onDeleteScenario)
+
+ yield takeEvery(ADD_PREFAB, onAddPrefab)
}
diff --git a/frontend/src/sagas/objects.js b/frontend/src/sagas/objects.js
index 83d71a42..a561e4b4 100644
--- a/frontend/src/sagas/objects.js
+++ b/frontend/src/sagas/objects.js
@@ -149,7 +149,31 @@ export const updateTopologyOnServer = function* (id) {
export const getTopologyAsObject = function* (id, keepIds) {
const topologyStore = yield select(OBJECT_SELECTORS['topology'])
const roomStore = yield select(OBJECT_SELECTORS['room'])
+
+ return {
+ _id: keepIds ? id : undefined,
+ name: topologyStore[id].name,
+ rooms: topologyStore[id].roomIds.map((roomId) => ({
+ _id: keepIds ? roomId : undefined,
+ name: roomStore[roomId].name,
+ tiles: roomStore[roomId].tileIds.map((tileId) => (call(getTileById, tileId, keepIds))),
+ })),
+ }
+}
+
+export const getTileById = function* (id, keepIds) {
const tileStore = yield select(OBJECT_SELECTORS['tile'])
+ return {
+ _id: keepIds ? id : undefined,
+ positionX: tileStore[id].positionX,
+ positionY: tileStore[id].positionY,
+ rack: !tileStore[id].rackId
+ ? undefined
+ : call(getRackById, tileStore[id].rackId, keepIds),
+ }
+}
+
+export const getRackById = function* (id, keepIds) {
const rackStore = yield select(OBJECT_SELECTORS['rack'])
const machineStore = yield select(OBJECT_SELECTORS['machine'])
const cpuStore = yield select(OBJECT_SELECTORS['cpu'])
@@ -158,38 +182,25 @@ export const getTopologyAsObject = function* (id, keepIds) {
const storageStore = yield select(OBJECT_SELECTORS['storage'])
return {
- _id: keepIds ? id : undefined,
- name: topologyStore[id].name,
- rooms: topologyStore[id].roomIds.map((roomId) => ({
- _id: keepIds ? roomId : undefined,
- name: roomStore[roomId].name,
- tiles: roomStore[roomId].tileIds.map((tileId) => ({
- _id: keepIds ? tileId : undefined,
- positionX: tileStore[tileId].positionX,
- positionY: tileStore[tileId].positionY,
- rack: !tileStore[tileId].rackId
- ? undefined
- : {
- _id: keepIds ? rackStore[tileStore[tileId].rackId]._id : undefined,
- name: rackStore[tileStore[tileId].rackId].name,
- capacity: rackStore[tileStore[tileId].rackId].capacity,
- powerCapacityW: rackStore[tileStore[tileId].rackId].powerCapacityW,
- machines: rackStore[tileStore[tileId].rackId].machineIds
- .filter((m) => m !== null)
- .map((machineId) => ({
- _id: keepIds ? machineId : undefined,
- position: machineStore[machineId].position,
- cpus: machineStore[machineId].cpuIds.map((id) => cpuStore[id]),
- gpus: machineStore[machineId].gpuIds.map((id) => gpuStore[id]),
- memories: machineStore[machineId].memoryIds.map((id) => memoryStore[id]),
- storages: machineStore[machineId].storageIds.map((id) => storageStore[id]),
- })),
- },
+ _id: keepIds ? rackStore[id]._id : undefined,
+ name: rackStore[id].name,
+ capacity: rackStore[id].capacity,
+ powerCapacityW: rackStore[id].powerCapacityW,
+ machines: rackStore[id].machineIds
+ .filter((m) => m !== null)
+ .map((machineId) => ({
+ _id: keepIds ? machineId : undefined,
+ position: machineStore[machineId].position,
+ cpus: machineStore[machineId].cpuIds.map((id) => cpuStore[id]),
+ gpus: machineStore[machineId].gpuIds.map((id) => gpuStore[id]),
+ memories: machineStore[machineId].memoryIds.map((id) => memoryStore[id]),
+ storages: machineStore[machineId].storageIds.map((id) => storageStore[id]),
})),
- })),
}
}
+
+
export const fetchAndStoreAllTraces = () => fetchAndStoreObjects('trace', call(getAllTraces))
export const fetchAndStoreAllSchedulers = function* () {
diff --git a/frontend/src/sagas/prefabs.js b/frontend/src/sagas/prefabs.js
new file mode 100644
index 00000000..94570404
--- /dev/null
+++ b/frontend/src/sagas/prefabs.js
@@ -0,0 +1,17 @@
+import {call, put, select} from "redux-saga/effects";
+import {addToStore} from "../actions/objects";
+import {addPrefab} from "../api/routes/prefabs";
+import {getTopologyAsObject} from "./objects";
+
+export function* onAddPrefab(action) {
+ try {
+ const currentRackId = yield select((state) => state.objects.tile[state.interactionLevel.tileId].rack._id)
+ const currentRackJson = yield call(getTopologyAsObject, currentRackId)
+ //TODO: yield call the function in saga to export the specific part of the topology
+ const prefab = yield call(addPrefab, { name: action.name, rack: currentRackJson })
+ yield put(addToStore('prefab', prefab))
+
+ } catch (error) {
+ console.error(error)
+ }
+}