|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright CERN and copyright holders of ALICE O2. This software is |
| 4 | + * distributed under the terms of the GNU General Public License v3 (GPL |
| 5 | + * Version 3), copied verbatim in the file "COPYING". |
| 6 | + * |
| 7 | + * See http://alice-o2.web.cern.ch/license for full licensing information. |
| 8 | + * |
| 9 | + * In applying this license CERN does not waive the privileges and immunities |
| 10 | + * granted to it by virtue of its status as an Intergovernmental Organization |
| 11 | + * or submit itself to any jurisdiction. |
| 12 | + */ |
| 13 | +import { Observable, RemoteData, fetchClient } from '/js/src/index.js'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Subsystem model |
| 17 | + */ |
| 18 | +class SubsystemModel extends Observable { |
| 19 | + /** |
| 20 | + * Creates a new `Subsystem Model` instance. |
| 21 | + * |
| 22 | + * @param {*} model Pass the model to access the defined functions. |
| 23 | + * @returns {undefined} |
| 24 | + */ |
| 25 | + constructor(model) { |
| 26 | + super(); |
| 27 | + this.model = model; |
| 28 | + |
| 29 | + // Overview |
| 30 | + this.clearSubsystems(); |
| 31 | + |
| 32 | + // Detail |
| 33 | + this.clearSubsystem(); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Returns the Subsystem data. |
| 38 | + * |
| 39 | + * @returns {RemoteData} The Subsystem data. |
| 40 | + */ |
| 41 | + getSubsystem() { |
| 42 | + return this.subsystem; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Returns the Logs of a subsystem. |
| 47 | + * |
| 48 | + * @returns {RemoteData} The Logs of a Subsystem. |
| 49 | + */ |
| 50 | + getLogsOfSubsystem() { |
| 51 | + return this.logsOfSubsystem; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Returns the Subsystem data. |
| 56 | + * |
| 57 | + * @returns {RemoteData} The Subsystems data. |
| 58 | + */ |
| 59 | + getSubsystems() { |
| 60 | + return this.subsystems; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Fetches all Subsystems. |
| 65 | + * |
| 66 | + * @returns {undefined} |
| 67 | + */ |
| 68 | + async fetchAllSubsystems() { |
| 69 | + if (this.getSubsystems().isSuccess()) { |
| 70 | + // We already have this panel |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + this.subsystems = RemoteData.loading(); |
| 75 | + this.notify(); |
| 76 | + |
| 77 | + const response = await fetchClient('/api/subsystems'); |
| 78 | + const result = await response.json(); |
| 79 | + |
| 80 | + if (result.data) { |
| 81 | + this.subsystems = RemoteData.success(result.data); |
| 82 | + } else { |
| 83 | + this.subsystems = RemoteData.failure(result.errors); |
| 84 | + } |
| 85 | + |
| 86 | + this.notify(); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Fetches the Subsystems data. |
| 91 | + * |
| 92 | + * @param {*} subsystemId Id of the subsystem. |
| 93 | + * @returns {undefined} |
| 94 | + */ |
| 95 | + async fetchOneSubsystem(subsystemId) { |
| 96 | + if (this.getSubsystem().isSuccess()) { |
| 97 | + // We already have this panel |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + this.subsystem = RemoteData.loading(); |
| 102 | + this.notify(); |
| 103 | + |
| 104 | + const response = await fetchClient(`/api/subsystems/${subsystemId}`); |
| 105 | + const result = await response.json(); |
| 106 | + |
| 107 | + if (result.data) { |
| 108 | + this.subsystem = RemoteData.success(result.data); |
| 109 | + } else { |
| 110 | + this.subsystem = RemoteData.failure(result.errors); |
| 111 | + } |
| 112 | + |
| 113 | + this.notify(); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Fetches the logs with provided subsystem. |
| 118 | + * |
| 119 | + * @param {*} subsystemId Id of the subsystem. |
| 120 | + * @returns {undefined} |
| 121 | + */ |
| 122 | + async fetchLogsOfSubsystem(subsystemId) { |
| 123 | + if (this.getLogsOfSubsystem().isSuccess()) { |
| 124 | + // We already have this panel |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + this.logsOfSubsystem = RemoteData.loading(); |
| 129 | + this.notify(); |
| 130 | + |
| 131 | + const response = await fetchClient(`/api/subsystems/${subsystemId}/logs`); |
| 132 | + const result = await response.json(); |
| 133 | + |
| 134 | + if (result.data) { |
| 135 | + this.logsOfSubsystem = RemoteData.success(result.data); |
| 136 | + } else { |
| 137 | + this.logsOfSubsystem = RemoteData.failure(result.errors); |
| 138 | + } |
| 139 | + |
| 140 | + this.notify(); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Sets all data related to the Subsystem to `NotAsked`. |
| 145 | + * |
| 146 | + * @returns {undefined} |
| 147 | + */ |
| 148 | + clearSubsystem() { |
| 149 | + this.subsystem = RemoteData.NotAsked(); |
| 150 | + this.logsOfSubsystem = RemoteData.NotAsked(); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Sets all data related to the Subsystem to `NotAsked`. |
| 155 | + * |
| 156 | + * @returns {undefined} |
| 157 | + */ |
| 158 | + clearSubsystems() { |
| 159 | + this.subsystems = RemoteData.NotAsked(); |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +export default SubsystemModel; |
0 commit comments