summaryrefslogtreecommitdiff
path: root/src/sagas/objects.js
blob: 508f1cf5f6ff642e7e18835c01ff6cb9d4b0a8ca (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
import {call, put, select} from "redux-saga/effects";
import {addToStore} from "../actions/objects";
import {getDatacenter, getRoomsOfDatacenter} from "../api/routes/datacenters";
import {getPath, getSectionsOfPath} from "../api/routes/paths";
import {getTilesOfRoom} from "../api/routes/rooms";
import {getSection} from "../api/routes/sections";
import {getPathsOfSimulation, getSimulation} from "../api/routes/simulations";
import {
    getAllCPUs,
    getAllGPUs,
    getAllMemories,
    getAllStorages,
    getCoolingItem,
    getCPU,
    getFailureModel,
    getGPU,
    getMemory,
    getPSU,
    getStorage
} from "../api/routes/specifications";
import {getMachinesOfRackByTile, getRackByTile} from "../api/routes/tiles";
import {getUser} from "../api/routes/users";

export const OBJECT_SELECTORS = {
    simulation: state => state.objects.simulation,
    user: state => state.objects.user,
    authorization: state => state.objects.authorization,
    failureModel: state => state.objects.failureModel,
    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,
    coolingItem: state => state.objects.coolingItem,
    psu: state => state.objects.psu,
    tile: state => state.objects.tile,
    room: state => state.objects.room,
    datacenter: state => state.objects.datacenter,
    section: state => state.objects.section,
    path: state => state.objects.path,
};

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 fetchAndStoreFailureModel = (id) =>
    fetchAndStoreObject("failureModel", id, call(getFailureModel, id));

export const fetchAndStoreAllCPUs = () =>
    fetchAndStoreObjects("cpu", call(getAllCPUs));

export const fetchAndStoreCPU = (id) =>
    fetchAndStoreObject("cpu", id, call(getCPU, id));

export const fetchAndStoreAllGPUs = () =>
    fetchAndStoreObjects("gpu", call(getAllGPUs));

export const fetchAndStoreGPU = (id) =>
    fetchAndStoreObject("gpu", id, call(getGPU, id));

export const fetchAndStoreAllMemories = () =>
    fetchAndStoreObjects("memory", call(getAllMemories));

export const fetchAndStoreMemory = (id) =>
    fetchAndStoreObject("memory", id, call(getMemory, id));

export const fetchAndStoreAllStorages = () =>
    fetchAndStoreObjects("storage", call(getAllStorages));

export const fetchAndStoreStorage = (id) =>
    fetchAndStoreObject("storage", id, call(getStorage, id));

export const fetchAndStoreMachinesOfTile = (tileId) =>
    fetchAndStoreObjects("machine", call(getMachinesOfRackByTile, tileId));

export const fetchAndStoreRackOnTile = (id, tileId) =>
    fetchAndStoreObject("rack", id, call(getRackByTile, tileId));

export const fetchAndStoreCoolingItem = (id) =>
    fetchAndStoreObject("coolingItem", id, call(getCoolingItem, id));

export const fetchAndStorePSU = (id) =>
    fetchAndStoreObject("psu", id, call(getPSU, id));

export const fetchAndStoreTilesOfRoom = (roomId) =>
    fetchAndStoreObjects("tile", call(getTilesOfRoom, roomId));

export const fetchAndStoreRoomsOfDatacenter = (datacenterId) =>
    fetchAndStoreObjects("room", call(getRoomsOfDatacenter, datacenterId));

export const fetchAndStoreDatacenter = (id) =>
    fetchAndStoreObject("datacenter", id, call(getDatacenter, id));

export const fetchAndStoreSection = (id) =>
    fetchAndStoreObject("section", id, call(getSection, id));

export const fetchAndStoreSectionsOfPath = (pathId) =>
    fetchAndStoreObjects("section", call(getSectionsOfPath, pathId));

export const fetchAndStorePath = (id) =>
    fetchAndStoreObject("path", id, call(getPath, id));

export const fetchAndStorePathsOfSimulation = (simulationId) =>
    fetchAndStoreObjects("path", call(getPathsOfSimulation, simulationId));