summaryrefslogtreecommitdiff
path: root/frontend/src/sagas/objects.js
blob: 174f9c3e1183890e5fc8aa4ea8e9a661dbc5f827 (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
import { call, put, select } from 'redux-saga/effects'
import { addToStore } from '../actions/objects'
import { getAllSchedulers } from '../api/routes/schedulers'
import { getSimulation } from '../api/routes/simulations'
import { getAllTraces } from '../api/routes/traces'
import { getUser } from '../api/routes/users'
import { getTopology, updateTopology } from '../api/routes/topologies'
import { uuid } from 'uuidv4'

export const OBJECT_SELECTORS = {
    simulation: (state) => state.objects.simulation,
    user: (state) => state.objects.user,
    authorization: (state) => state.objects.authorization,
    cpu: (state) => state.objects.cpu,
    gpu: (state) => state.objects.gpu,
    memory: (state) => state.objects.memory,
    storage: (state) => state.objects.storage,
    machine: (state) => state.objects.machine,
    rack: (state) => state.objects.rack,
    tile: (state) => state.objects.tile,
    room: (state) => state.objects.room,
    topology: (state) => state.objects.topology,
}

function* fetchAndStoreObject(objectType, id, apiCall) {
    const objectStore = yield select(OBJECT_SELECTORS[objectType])
    let object = objectStore[id]
    if (!object) {
        object = yield apiCall
        yield put(addToStore(objectType, object))
    }
    return object
}

function* fetchAndStoreObjects(objectType, apiCall) {
    const objects = yield apiCall
    for (let index in objects) {
        yield put(addToStore(objectType, objects[index]))
    }
    return objects
}

export const fetchAndStoreSimulation = (id) => fetchAndStoreObject('simulation', id, call(getSimulation, id))

export const fetchAndStoreUser = (id) => fetchAndStoreObject('user', id, call(getUser, id))

export const fetchAndStoreTopology = function* (id) {
    const topologyStore = yield select(OBJECT_SELECTORS['topology'])
    const roomStore = yield select(OBJECT_SELECTORS['room'])
    const tileStore = yield select(OBJECT_SELECTORS['tile'])
    const rackStore = yield select(OBJECT_SELECTORS['rack'])
    const machineStore = yield select(OBJECT_SELECTORS['machine'])

    let topology = topologyStore[id]
    if (!topology) {
        const fullTopology = yield call(getTopology, id)

        for (let roomIdx in fullTopology.rooms) {
            const fullRoom = fullTopology.rooms[roomIdx]

            generateIdIfNotPresent(fullRoom)

            if (!roomStore[fullRoom._id]) {
                for (let tileIdx in fullRoom.tiles) {
                    const fullTile = fullRoom.tiles[tileIdx]

                    generateIdIfNotPresent(fullTile)

                    if (!tileStore[fullTile._id]) {
                        if (fullTile.rack) {
                            const fullRack = fullTile.rack

                            generateIdIfNotPresent(fullTile)

                            if (!rackStore[fullRack._id]) {
                                for (let machineIdx in fullRack.machines) {
                                    const fullMachine = fullRoom.machines[machineIdx]

                                    generateIdIfNotPresent(fullMachine)

                                    if (!machineStore[fullMachine._id]) {
                                        let machine = (({ _id, position, cpuIds, gpuIds, memoryIds, storageIds }) => ({
                                            _id,
                                            rackId: fullRack._id,
                                            position,
                                            cpuIds,
                                            gpuIds,
                                            memoryIds,
                                            storageIds,
                                        }))(fullMachine)
                                        yield put(addToStore('machine', machine))
                                    }
                                }

                                const filledSlots = new Array(fullRack.capacity).fill(null)
                                fullRack.machines.forEach(
                                    (machine) => (filledSlots[machine.position - 1] = machine._id)
                                )
                                let rack = (({ _id, capacity, powerCapacityW }) => ({
                                    _id,
                                    capacity,
                                    powerCapacityW,
                                    machineIds: filledSlots,
                                }))(fullRack)
                                yield put(addToStore('rack', rack))
                            }
                        }

                        let tile = (({ _id, positionX, positionY, rack }) => ({
                            _id,
                            roomId: fullRoom._id,
                            positionX,
                            positionY,
                            rackId: rack ? rack._id : undefined,
                        }))(fullTile)
                        yield put(addToStore('tile', tile))
                    }
                }

                let room = (({ _id, name, tiles }) => ({ _id, name, tileIds: tiles.map((t) => t._id) }))(fullRoom)
                yield put(addToStore('room', room))
            }
        }

        topology = (({ _id, name, rooms }) => ({ _id, name, roomIds: rooms.map((r) => r._id) }))(fullTopology)
        yield put(addToStore('topology', topology))

        console.log('Full topology after insertion', fullTopology)
        // TODO consider pushing the IDs
    }

    return topology
}

const generateIdIfNotPresent = (obj) => {
    if (!obj._id) {
        obj._id = uuid()
    }
}

export const updateTopologyOnServer = function* (id) {
    const topologyStore = yield select(OBJECT_SELECTORS['topology'])
    const roomStore = yield select(OBJECT_SELECTORS['room'])
    const tileStore = yield select(OBJECT_SELECTORS['tile'])
    const rackStore = yield select(OBJECT_SELECTORS['rack'])
    const machineStore = yield select(OBJECT_SELECTORS['machine'])

    const topology = {
        _id: id,
        name: topologyStore[id].name,
        rooms: topologyStore[id].roomIds.map((roomId) => ({
            _id: roomId,
            name: roomStore[roomId].name,
            tiles: roomStore[roomId].tileIds.map((tileId) => ({
                _id: tileId,
                positionX: tileStore[tileId].positionX,
                positionY: tileStore[tileId].positionY,
                rack: !tileStore[tileId].rackId
                    ? undefined
                    : {
                          _id: rackStore[tileStore[tileId].rackId]._id,
                          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: machineId,
                                  position: machineStore[machineId].position,
                                  cpuIds: machineStore[machineId].cpuIds,
                                  gpuIds: machineStore[machineId].gpuIds,
                                  memoryIds: machineStore[machineId].memoryIds,
                                  storageIds: machineStore[machineId].storageIds,
                              })),
                      },
            })),
        })),
    }

    yield call(updateTopology, topology)
}

export const fetchAndStoreAllTraces = () => fetchAndStoreObjects('trace', call(getAllTraces))

export const fetchAndStoreAllSchedulers = function* () {
    const objects = yield call(getAllSchedulers)
    for (let index in objects) {
        objects[index]._id = objects[index].name
        yield put(addToStore('scheduler', objects[index]))
    }
    return objects
}