summaryrefslogtreecommitdiff
path: root/src/sagas/topology.js
blob: 815cd8427b30def4cd6f7f1a19750209ae6b5c3f (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
import {call, put, select} from "redux-saga/effects";
import {addPropToStoreObject, addToStore} from "../actions/objects";
import {
    addTileSucceeded,
    cancelNewRoomConstructionSucceeded,
    deleteTileSucceeded,
    fetchLatestDatacenterSucceeded,
    startNewRoomConstructionSucceeded
} from "../actions/topology";
import {addRoomToDatacenter} from "../api/routes/datacenters";
import {addTileToRoom, deleteRoom} from "../api/routes/rooms";
import {deleteTile} from "../api/routes/tiles";
import {
    fetchAndStoreCoolingItem,
    fetchAndStoreDatacenter,
    fetchAndStorePathsOfSimulation,
    fetchAndStorePSU,
    fetchAndStoreRackOnTile,
    fetchAndStoreRoomsOfDatacenter,
    fetchAndStoreSectionsOfPath,
    fetchAndStoreTilesOfRoom
} from "./objects";

export function* onFetchLatestDatacenter(action) {
    try {
        const paths = yield fetchAndStorePathsOfSimulation(action.currentSimulationId);
        const latestPath = paths[paths.length - 1];
        const sections = yield fetchAndStoreSectionsOfPath(latestPath.id);
        const latestSection = sections[sections.length - 1];
        yield fetchDatacenter(latestSection.datacenterId);
        yield put(fetchLatestDatacenterSucceeded(latestSection.datacenterId));
    } catch (error) {
        console.log(error);
    }
}

export function* fetchDatacenter(datacenterId) {
    try {
        yield fetchAndStoreDatacenter(datacenterId);
        const rooms = yield fetchAndStoreRoomsOfDatacenter(datacenterId);
        yield put(addPropToStoreObject("datacenter", datacenterId, {roomIds: rooms.map(room => room.id)}));

        for (let index in rooms) {
            yield fetchRoom(rooms[index].id);
        }
    } catch (error) {
        console.log(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}));
            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 object type encountered while fetching tile objects");
    }
}

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(startNewRoomConstructionSucceeded(room.id));
    } catch (error) {
        console.log(error);
    }
}

export function* onCancelNewRoomConstruction() {
    try {
        const roomId = yield select(state => state.currentRoomInConstruction);
        yield call(deleteRoom, roomId);
        yield put(cancelNewRoomConstructionSucceeded());
    } catch (error) {
        console.log(error);
    }
}

export function* onAddTile(action) {
    try {
        const roomId = yield select(state => state.currentRoomInConstruction);
        const tile = yield call(addTileToRoom, {
            roomId,
            positionX: action.positionX,
            positionY: action.positionY
        });
        yield put(addToStore("tile", tile));
        yield put(addTileSucceeded(tile.id));
    } catch (error) {
        console.log(error);
    }
}

export function* onDeleteTile(action) {
    try {
        yield call(deleteTile, action.tileId);
        yield put(deleteTileSucceeded(action.tileId));
    } catch (error) {
        console.log(error);
    }
}