summaryrefslogtreecommitdiff
path: root/frontend/src/sagas/topology.js
blob: 8b67c1964c4fae8d9c497376b3960faad4fbf7f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import { call, put, select } from 'redux-saga/effects'
import { goDownOneInteractionLevel } from '../actions/interaction-level'
import {
    addIdToStoreObjectListProp,
    addPropToStoreObject,
    addToStore,
    removeIdFromStoreObjectListProp,
} from '../actions/objects'
import {
    cancelNewRoomConstructionSucceeded,
    setCurrentTopology,
    startNewRoomConstructionSucceeded,
} from '../actions/topology/building'
import {
    DEFAULT_RACK_POWER_CAPACITY,
    DEFAULT_RACK_SLOT_CAPACITY,
    MAX_NUM_UNITS_PER_MACHINE,
} from '../components/app/map/MapConstants'
import { fetchAndStoreTopology, getTopologyAsObject, updateTopologyOnServer } from './objects'
import { uuid } from 'uuidv4'
import { addTopology, deleteTopology } from '../api/routes/topologies'

export function* fetchAndStoreAllTopologiesOfProject(projectId, setTopology = false) {
    try {
        const project = yield select((state) => state.objects.project[projectId])

        for (let i in project.topologyIds) {
            yield fetchAndStoreTopology(project.topologyIds[i])
        }

        if (setTopology) {
            yield put(setCurrentTopology(project.topologyIds[0]))
        }
    } catch (error) {
        console.error(error)
    }
}

export function* onAddTopology(action) {
    try {
        const currentProjectId = yield select((state) => state.currentProjectId)

        let topologyToBeCreated
        if (action.duplicateId) {
            topologyToBeCreated = yield getTopologyAsObject(action.duplicateId, false)
        } else {
            topologyToBeCreated = { name: action.name, rooms: [] }
        }

        const topology = yield call(
            addTopology,
            Object.assign({}, topologyToBeCreated, {
                projectId: currentProjectId,
            })
        )
        yield fetchAndStoreTopology(topology._id)

        const topologyIds = yield select((state) => state.objects.project[currentProjectId].topologyIds)
        yield put(
            addPropToStoreObject('project', currentProjectId, {
                topologyIds: topologyIds.concat([topology._id]),
            })
        )
        yield put(setCurrentTopology(topology._id))
    } catch (error) {
        console.error(error)
    }
}

export function* onDeleteTopology(action) {
    try {
        const currentProjectId = yield select((state) => state.currentProjectId)
        const topologyIds = yield select((state) => state.objects.project[currentProjectId].topologyIds)
        const currentTopologyId = yield select((state) => state.currentTopologyId)
        if (currentTopologyId === action.id) {
            yield put(setCurrentTopology(topologyIds.filter((t) => t !== action.id)[0]))
        }

        yield call(deleteTopology, action.id)

        yield put(
            addPropToStoreObject('project', currentProjectId, {
                topologyIds: topologyIds.filter((id) => id !== action.id),
            })
        )
    } catch (error) {
        console.error(error)
    }
}

export function* onStartNewRoomConstruction() {
    try {
        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)
    }
}

export function* onCancelNewRoomConstruction() {
    try {
        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)
    }
}

export function* onAddTile(action) {
    try {
        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 updateTopologyOnServer(topologyId)
    } catch (error) {
        console.error(error)
    }
}

export function* onDeleteTile(action) {
    try {
        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)
    }
}

export function* onEditRoomName(action) {
    try {
        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 put(addPropToStoreObject('room', roomId, { name: action.name }))
        yield updateTopologyOnServer(topologyId)
    } catch (error) {
        console.error(error)
    }
}

export function* onDeleteRoom() {
    try {
        const topologyId = yield select((state) => state.currentTopologyId)
        const roomId = yield select((state) => state.interactionLevel.roomId)
        yield put(goDownOneInteractionLevel())
        yield put(removeIdFromStoreObjectListProp('topology', topologyId, 'roomIds', roomId))
        yield updateTopologyOnServer(topologyId)
    } catch (error) {
        console.error(error)
    }
}

export function* onEditRackName(action) {
    try {
        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 put(addPropToStoreObject('rack', rackId, { name: action.name }))
        yield updateTopologyOnServer(topologyId)
    } catch (error) {
        console.error(error)
    }
}

export function* onDeleteRack() {
    try {
        const topologyId = yield select((state) => state.currentTopologyId)
        const tileId = yield select((state) => state.interactionLevel.tileId)
        yield put(goDownOneInteractionLevel())
        yield put(addPropToStoreObject('tile', tileId, { rackId: undefined }))
        yield updateTopologyOnServer(topologyId)
    } catch (error) {
        console.error(error)
    }
}

export function* onAddRackToTile(action) {
    try {
        const topologyId = yield select((state) => state.currentTopologyId)
        const rack = {
            _id: uuid(),
            name: 'Rack',
            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, { rackId: rack._id }))
        yield updateTopologyOnServer(topologyId)
    } catch (error) {
        console.error(error)
    }
}

export function* onAddMachine(action) {
    try {
        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 = {
            _id: uuid(),
            rackId,
            position: action.position,
            cpuIds: [],
            gpuIds: [],
            memoryIds: [],
            storageIds: [],
        }
        yield put(addToStore('machine', machine))

        const machineIds = [...rack.machineIds]
        machineIds[machine.position - 1] = machine._id
        yield put(addPropToStoreObject('rack', rackId, { machineIds }))
        yield updateTopologyOnServer(topologyId)
    } catch (error) {
        console.error(error)
    }
}

export function* onDeleteMachine() {
    try {
        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 updateTopologyOnServer(topologyId)
    } catch (error) {
        console.error(error)
    }
}

export function* onAddUnit(action) {
    try {
        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].rackId].machineIds[position - 1]]
        )

        if (machine[action.unitType + 'Ids'].length >= MAX_NUM_UNITS_PER_MACHINE) {
            return
        }

        const units = [...machine[action.unitType + 'Ids'], action.id]
        yield put(
            addPropToStoreObject('machine', machine._id, {
                [action.unitType + 'Ids']: units,
            })
        )
        yield updateTopologyOnServer(topologyId)
    } catch (error) {
        console.error(error)
    }
}

export function* onDeleteUnit(action) {
    try {
        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].rackId].machineIds[position - 1]]
        )
        const unitIds = machine[action.unitType + 'Ids'].slice()
        unitIds.splice(action.index, 1)

        yield put(
            addPropToStoreObject('machine', machine._id, {
                [action.unitType + 'Ids']: unitIds,
            })
        )
        yield updateTopologyOnServer(topologyId)
    } catch (error) {
        console.error(error)
    }
}