summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-ui/src/redux/sagas/portfolios.js
blob: c32fcdc01167b8f8d5fc794d8ea544ea96e9c5c4 (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
import { call, put, select, delay, getContext } from 'redux-saga/effects'
import { addToStore } from '../actions/objects'
import { addPortfolio, deletePortfolio, getPortfolio, updatePortfolio } from '../../api/portfolios'
import { fetchProject } from '../../api/projects'
import { fetchAndStoreAllSchedulers, fetchAndStoreAllTraces } from './objects'
import { fetchAndStoreAllTopologiesOfProject } from './topology'
import { getScenario } from '../../api/scenarios'

export function* onOpenPortfolioSucceeded(action) {
    try {
        const auth = yield getContext('auth')
        const queryClient = yield getContext('queryClient')
        const project = yield call(() =>
            queryClient.fetchQuery(`projects/${action.projectId}`, () => fetchProject(auth, action.projectId))
        )
        yield fetchAndStoreAllTopologiesOfProject(action.projectId)
        yield fetchPortfoliosOfProject(project)
        yield fetchAndStoreAllSchedulers()
        yield fetchAndStoreAllTraces()

        yield watchForPortfolioResults(action.portfolioId)
    } catch (error) {
        console.error(error)
    }
}

export function* watchForPortfolioResults(portfolioId) {
    try {
        let unfinishedScenarios = yield getCurrentUnfinishedScenarios(portfolioId)

        while (unfinishedScenarios.length > 0) {
            yield delay(3000)
            yield fetchPortfolioWithScenarios(portfolioId)
            unfinishedScenarios = yield getCurrentUnfinishedScenarios(portfolioId)
        }
    } catch (error) {
        console.error(error)
    }
}

export function* getCurrentUnfinishedScenarios(portfolioId) {
    try {
        if (!portfolioId) {
            return []
        }

        const scenarioIds = yield select((state) => state.objects.portfolio[portfolioId].scenarioIds)
        const scenarioObjects = yield select((state) => state.objects.scenario)
        const scenarios = scenarioIds.map((s) => scenarioObjects[s])
        return scenarios.filter((s) => !s || s.simulation.state === 'QUEUED' || s.simulation.state === 'RUNNING')
    } catch (error) {
        console.error(error)
    }
}

export function* fetchPortfoliosOfProject(project) {
    try {
        yield fetchAndStoreAllSchedulers()
        yield fetchAndStoreAllTraces()

        for (const i in project.portfolioIds) {
            yield fetchPortfolioWithScenarios(project.portfolioIds[i])
        }
    } catch (error) {
        console.error(error)
    }
}

export function* fetchPortfolioWithScenarios(portfolioId) {
    try {
        const auth = yield getContext('auth')
        const portfolio = yield call(getPortfolio, auth, portfolioId)
        yield put(addToStore('portfolio', portfolio))

        for (let i in portfolio.scenarioIds) {
            const scenario = yield call(getScenario, auth, portfolio.scenarioIds[i])
            yield put(addToStore('scenario', scenario))
        }
        return portfolio
    } catch (error) {
        console.error(error)
    }
}

export function* onAddPortfolio(action) {
    try {
        const { projectId } = action
        const auth = yield getContext('auth')
        const portfolio = yield call(
            addPortfolio,
            auth,
            projectId,
            Object.assign({}, action.portfolio, {
                projectId: projectId,
                scenarioIds: [],
            })
        )
        yield put(addToStore('portfolio', portfolio))
    } catch (error) {
        console.error(error)
    }
}

export function* onUpdatePortfolio(action) {
    try {
        const auth = yield getContext('auth')
        const portfolio = yield call(updatePortfolio, auth, action.portfolio._id, action.portfolio)
        yield put(addToStore('portfolio', portfolio))
    } catch (error) {
        console.error(error)
    }
}

export function* onDeletePortfolio(action) {
    try {
        const auth = yield getContext('auth')
        yield call(deletePortfolio, auth, action.id)
    } catch (error) {
        console.error(error)
    }
}