##// END OF EJS Templates
Modify variable controller request managing with introduction of current
perrinel -
r829:4e3514f47cab
parent child
Show More
@@ -1,28 +1,26
1 #ifndef SCIQLOP_VARIABLEREQUEST_H
1 #ifndef SCIQLOP_VARIABLEREQUEST_H
2 #define SCIQLOP_VARIABLEREQUEST_H
2 #define SCIQLOP_VARIABLEREQUEST_H
3
3
4 #include <QObject>
4 #include <QObject>
5
5
6 #include <QUuid>
6 #include <QUuid>
7
7
8 #include <Common/MetaTypes.h>
8 #include <Common/MetaTypes.h>
9 #include <Data/IDataSeries.h>
9 #include <Data/IDataSeries.h>
10 #include <Data/SqpRange.h>
10 #include <Data/SqpRange.h>
11
11
12 #include <memory>
12 #include <memory>
13
13
14 /**
14 /**
15 * @brief The VariableRequest struct holds the information of an acquisition request
15 * @brief The VariableRequest struct holds the information of an acquisition request
16 */
16 */
17 struct VariableRequest {
17 struct VariableRequest {
18 VariableRequest() { m_CanUpdate = false; }
18 QUuid m_VariableGroupId;
19
20 SqpRange m_RangeRequested;
19 SqpRange m_RangeRequested;
21 SqpRange m_CacheRangeRequested;
20 SqpRange m_CacheRangeRequested;
22 std::shared_ptr<IDataSeries> m_DataSeries;
21 std::shared_ptr<IDataSeries> m_DataSeries;
23 bool m_CanUpdate;
24 };
22 };
25
23
26 SCIQLOP_REGISTER_META_TYPE(VARIABLEREQUEST_REGISTRY, VariableRequest)
24 SCIQLOP_REGISTER_META_TYPE(VARIABLEREQUEST_REGISTRY, VariableRequest)
27
25
28 #endif // SCIQLOP_VARIABLEREQUEST_H
26 #endif // SCIQLOP_VARIABLEREQUEST_H
This diff has been collapsed as it changes many lines, (629 lines changed) Show them Hide them
@@ -1,958 +1,969
1 #include <Variable/Variable.h>
1 #include <Variable/Variable.h>
2 #include <Variable/VariableAcquisitionWorker.h>
2 #include <Variable/VariableAcquisitionWorker.h>
3 #include <Variable/VariableCacheStrategy.h>
3 #include <Variable/VariableCacheStrategy.h>
4 #include <Variable/VariableCacheStrategyFactory.h>
4 #include <Variable/VariableCacheStrategyFactory.h>
5 #include <Variable/VariableController.h>
5 #include <Variable/VariableController.h>
6 #include <Variable/VariableModel.h>
6 #include <Variable/VariableModel.h>
7 #include <Variable/VariableSynchronizationGroup.h>
7 #include <Variable/VariableSynchronizationGroup.h>
8
8
9 #include <Data/DataProviderParameters.h>
9 #include <Data/DataProviderParameters.h>
10 #include <Data/IDataProvider.h>
10 #include <Data/IDataProvider.h>
11 #include <Data/IDataSeries.h>
11 #include <Data/IDataSeries.h>
12 #include <Data/VariableRequest.h>
12 #include <Data/VariableRequest.h>
13 #include <Time/TimeController.h>
13 #include <Time/TimeController.h>
14
14
15 #include <QMutex>
15 #include <QMutex>
16 #include <QThread>
16 #include <QThread>
17 #include <QUuid>
17 #include <QUuid>
18 #include <QtCore/QItemSelectionModel>
18 #include <QtCore/QItemSelectionModel>
19
19
20 #include <deque>
20 #include <deque>
21 #include <set>
21 #include <set>
22 #include <unordered_map>
22 #include <unordered_map>
23
23
24 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
24 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
25
25
26 namespace {
26 namespace {
27
27
28 SqpRange computeSynchroRangeRequested(const SqpRange &varRange, const SqpRange &graphRange,
28 SqpRange computeSynchroRangeRequested(const SqpRange &varRange, const SqpRange &graphRange,
29 const SqpRange &oldGraphRange)
29 const SqpRange &oldGraphRange)
30 {
30 {
31 auto zoomType = VariableController::getZoomType(graphRange, oldGraphRange);
31 auto zoomType = VariableController::getZoomType(graphRange, oldGraphRange);
32
32
33 auto varRangeRequested = varRange;
33 auto varRangeRequested = varRange;
34 switch (zoomType) {
34 switch (zoomType) {
35 case AcquisitionZoomType::ZoomIn: {
35 case AcquisitionZoomType::ZoomIn: {
36 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
36 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
37 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
37 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
38 varRangeRequested.m_TStart += deltaLeft;
38 varRangeRequested.m_TStart += deltaLeft;
39 varRangeRequested.m_TEnd -= deltaRight;
39 varRangeRequested.m_TEnd -= deltaRight;
40 break;
40 break;
41 }
41 }
42
42
43 case AcquisitionZoomType::ZoomOut: {
43 case AcquisitionZoomType::ZoomOut: {
44 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
44 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
45 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
45 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
46 varRangeRequested.m_TStart -= deltaLeft;
46 varRangeRequested.m_TStart -= deltaLeft;
47 varRangeRequested.m_TEnd += deltaRight;
47 varRangeRequested.m_TEnd += deltaRight;
48 break;
48 break;
49 }
49 }
50 case AcquisitionZoomType::PanRight: {
50 case AcquisitionZoomType::PanRight: {
51 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
51 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
52 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
52 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
53 varRangeRequested.m_TStart += deltaLeft;
53 varRangeRequested.m_TStart += deltaLeft;
54 varRangeRequested.m_TEnd += deltaRight;
54 varRangeRequested.m_TEnd += deltaRight;
55 break;
55 break;
56 }
56 }
57 case AcquisitionZoomType::PanLeft: {
57 case AcquisitionZoomType::PanLeft: {
58 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
58 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
59 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
59 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
60 varRangeRequested.m_TStart -= deltaLeft;
60 varRangeRequested.m_TStart -= deltaLeft;
61 varRangeRequested.m_TEnd -= deltaRight;
61 varRangeRequested.m_TEnd -= deltaRight;
62 break;
62 break;
63 }
63 }
64 case AcquisitionZoomType::Unknown: {
64 case AcquisitionZoomType::Unknown: {
65 qCCritical(LOG_VariableController())
65 qCCritical(LOG_VariableController())
66 << VariableController::tr("Impossible to synchronize: zoom type unknown");
66 << VariableController::tr("Impossible to synchronize: zoom type unknown");
67 break;
67 break;
68 }
68 }
69 default:
69 default:
70 qCCritical(LOG_VariableController()) << VariableController::tr(
70 qCCritical(LOG_VariableController()) << VariableController::tr(
71 "Impossible to synchronize: zoom type not take into account");
71 "Impossible to synchronize: zoom type not take into account");
72 // No action
72 // No action
73 break;
73 break;
74 }
74 }
75
75
76 return varRangeRequested;
76 return varRangeRequested;
77 }
77 }
78 }
78 }
79
79
80 enum class VariableRequestHandlerState { OFF, RUNNING, PENDING };
81
82 struct VariableRequestHandler {
83
84 VariableRequestHandler()
85 {
86 m_CanUpdate = false;
87 m_State = VariableRequestHandlerState::OFF;
88 }
89
90 QUuid m_VarId;
91 VariableRequest m_RunningVarRequest;
92 VariableRequest m_PendingVarRequest;
93 VariableRequestHandlerState m_State;
94 bool m_CanUpdate;
95 };
96
80 struct VariableController::VariableControllerPrivate {
97 struct VariableController::VariableControllerPrivate {
81 explicit VariableControllerPrivate(VariableController *parent)
98 explicit VariableControllerPrivate(VariableController *parent)
82 : m_WorkingMutex{},
99 : m_WorkingMutex{},
83 m_VariableModel{new VariableModel{parent}},
100 m_VariableModel{new VariableModel{parent}},
84 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
101 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
85 // m_VariableCacheStrategy{std::make_unique<VariableCacheStrategy>()},
102 // m_VariableCacheStrategy{std::make_unique<VariableCacheStrategy>()},
86 m_VariableCacheStrategy{VariableCacheStrategyFactory::createCacheStrategy(
103 m_VariableCacheStrategy{VariableCacheStrategyFactory::createCacheStrategy(
87 CacheStrategy::SingleThreshold)},
104 CacheStrategy::SingleThreshold)},
88 m_VariableAcquisitionWorker{std::make_unique<VariableAcquisitionWorker>()},
105 m_VariableAcquisitionWorker{std::make_unique<VariableAcquisitionWorker>()},
89 q{parent}
106 q{parent}
90 {
107 {
91
108
92 m_VariableAcquisitionWorker->moveToThread(&m_VariableAcquisitionWorkerThread);
109 m_VariableAcquisitionWorker->moveToThread(&m_VariableAcquisitionWorkerThread);
93 m_VariableAcquisitionWorkerThread.setObjectName("VariableAcquisitionWorkerThread");
110 m_VariableAcquisitionWorkerThread.setObjectName("VariableAcquisitionWorkerThread");
94 }
111 }
95
112
96
113
97 virtual ~VariableControllerPrivate()
114 virtual ~VariableControllerPrivate()
98 {
115 {
99 qCDebug(LOG_VariableController()) << tr("VariableControllerPrivate destruction");
116 qCDebug(LOG_VariableController()) << tr("VariableControllerPrivate destruction");
100 m_VariableAcquisitionWorkerThread.quit();
117 m_VariableAcquisitionWorkerThread.quit();
101 m_VariableAcquisitionWorkerThread.wait();
118 m_VariableAcquisitionWorkerThread.wait();
102 }
119 }
103
120
104
121
105 void processRequest(std::shared_ptr<Variable> var, const SqpRange &rangeRequested,
122 void processRequest(std::shared_ptr<Variable> var, const SqpRange &rangeRequested,
106 QUuid varRequestId);
123 QUuid varRequestId);
107
124
108 std::shared_ptr<Variable> findVariable(QUuid vIdentifier);
125 std::shared_ptr<Variable> findVariable(QUuid vIdentifier);
109 std::shared_ptr<IDataSeries>
126 std::shared_ptr<IDataSeries>
110 retrieveDataSeries(const QVector<AcquisitionDataPacket> acqDataPacketVector);
127 retrieveDataSeries(const QVector<AcquisitionDataPacket> acqDataPacketVector);
111
128
112 void registerProvider(std::shared_ptr<IDataProvider> provider);
129 void registerProvider(std::shared_ptr<IDataProvider> provider);
113
130
114 void storeVariableRequest(QUuid varId, QUuid varRequestId, const VariableRequest &varRequest);
131 void storeVariableRequest(QUuid varId, QUuid varRequestId, const VariableRequest &varRequest);
115 QUuid acceptVariableRequest(QUuid varId, std::shared_ptr<IDataSeries> dataSeries);
132 QUuid acceptVariableRequest(QUuid varId, std::shared_ptr<IDataSeries> dataSeries);
133 void updateVariables(QUuid varRequestId);
116 void updateVariableRequest(QUuid varRequestId);
134 void updateVariableRequest(QUuid varRequestId);
117 void cancelVariableRequest(QUuid varRequestId);
135 void cancelVariableRequest(QUuid varRequestId);
118 void abortVariableRequest(QUuid varRequestId);
136 void executeVarRequest(std::shared_ptr<Variable> var, VariableRequest &varRequest);
119 SqpRange getLastRequestedRange(QUuid varId);
120
137
121 QMutex m_WorkingMutex;
138 QMutex m_WorkingMutex;
122 /// Variable model. The VariableController has the ownership
139 /// Variable model. The VariableController has the ownership
123 VariableModel *m_VariableModel;
140 VariableModel *m_VariableModel;
124 QItemSelectionModel *m_VariableSelectionModel;
141 QItemSelectionModel *m_VariableSelectionModel;
125
142
126
143
127 TimeController *m_TimeController{nullptr};
144 TimeController *m_TimeController{nullptr};
128 std::unique_ptr<VariableCacheStrategy> m_VariableCacheStrategy;
145 std::unique_ptr<VariableCacheStrategy> m_VariableCacheStrategy;
129 std::unique_ptr<VariableAcquisitionWorker> m_VariableAcquisitionWorker;
146 std::unique_ptr<VariableAcquisitionWorker> m_VariableAcquisitionWorker;
130 QThread m_VariableAcquisitionWorkerThread;
147 QThread m_VariableAcquisitionWorkerThread;
131
148
132 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
149 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
133 m_VariableToProviderMap;
150 m_VariableToProviderMap;
134 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap;
151 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap;
135 std::map<QUuid, std::shared_ptr<VariableSynchronizationGroup> >
152 std::map<QUuid, std::shared_ptr<VariableSynchronizationGroup> >
136 m_GroupIdToVariableSynchronizationGroupMap;
153 m_GroupIdToVariableSynchronizationGroupMap;
137 std::map<QUuid, QUuid> m_VariableIdGroupIdMap;
154 std::map<QUuid, QUuid> m_VariableIdGroupIdMap;
138 std::set<std::shared_ptr<IDataProvider> > m_ProviderSet;
155 std::set<std::shared_ptr<IDataProvider> > m_ProviderSet;
139
156
140 std::map<QUuid, std::map<QUuid, VariableRequest> > m_VarRequestIdToVarIdVarRequestMap;
157 std::map<QUuid, std::list<QUuid> > m_VarGroupIdToVarIds;
141
158 std::map<QUuid, std::unique_ptr<VariableRequestHandler> > m_VarIdToVarRequestHandler;
142 std::map<QUuid, std::deque<QUuid> > m_VarIdToVarRequestIdQueueMap;
143
159
144 VariableController *q;
160 VariableController *q;
145 };
161 };
146
162
147
163
148 VariableController::VariableController(QObject *parent)
164 VariableController::VariableController(QObject *parent)
149 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
165 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
150 {
166 {
151 qCDebug(LOG_VariableController()) << tr("VariableController construction")
167 qCDebug(LOG_VariableController()) << tr("VariableController construction")
152 << QThread::currentThread();
168 << QThread::currentThread();
153
169
154 connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this,
170 connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this,
155 &VariableController::onAbortProgressRequested);
171 &VariableController::onAbortProgressRequested);
156
172
157 connect(impl->m_VariableAcquisitionWorker.get(),
173 connect(impl->m_VariableAcquisitionWorker.get(),
158 &VariableAcquisitionWorker::variableCanceledRequested, this,
174 &VariableAcquisitionWorker::variableCanceledRequested, this,
159 &VariableController::onAbortAcquisitionRequested);
175 &VariableController::onAbortAcquisitionRequested);
160
176
161 connect(impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::dataProvided, this,
177 connect(impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::dataProvided, this,
162 &VariableController::onDataProvided);
178 &VariableController::onDataProvided);
163 connect(impl->m_VariableAcquisitionWorker.get(),
179 connect(impl->m_VariableAcquisitionWorker.get(),
164 &VariableAcquisitionWorker::variableRequestInProgress, this,
180 &VariableAcquisitionWorker::variableRequestInProgress, this,
165 &VariableController::onVariableRetrieveDataInProgress);
181 &VariableController::onVariableRetrieveDataInProgress);
166
182
167
183
168 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::started,
184 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::started,
169 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::initialize);
185 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::initialize);
170 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::finished,
186 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::finished,
171 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::finalize);
187 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::finalize);
172
188
173
189
174 impl->m_VariableAcquisitionWorkerThread.start();
190 impl->m_VariableAcquisitionWorkerThread.start();
175 }
191 }
176
192
177 VariableController::~VariableController()
193 VariableController::~VariableController()
178 {
194 {
179 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
195 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
180 << QThread::currentThread();
196 << QThread::currentThread();
181 this->waitForFinish();
197 this->waitForFinish();
182 }
198 }
183
199
184 VariableModel *VariableController::variableModel() noexcept
200 VariableModel *VariableController::variableModel() noexcept
185 {
201 {
186 return impl->m_VariableModel;
202 return impl->m_VariableModel;
187 }
203 }
188
204
189 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
205 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
190 {
206 {
191 return impl->m_VariableSelectionModel;
207 return impl->m_VariableSelectionModel;
192 }
208 }
193
209
194 void VariableController::setTimeController(TimeController *timeController) noexcept
210 void VariableController::setTimeController(TimeController *timeController) noexcept
195 {
211 {
196 impl->m_TimeController = timeController;
212 impl->m_TimeController = timeController;
197 }
213 }
198
214
199 std::shared_ptr<Variable>
215 std::shared_ptr<Variable>
200 VariableController::cloneVariable(std::shared_ptr<Variable> variable) noexcept
216 VariableController::cloneVariable(std::shared_ptr<Variable> variable) noexcept
201 {
217 {
202 if (impl->m_VariableModel->containsVariable(variable)) {
218 if (impl->m_VariableModel->containsVariable(variable)) {
203 // Clones variable
219 // Clones variable
204 auto duplicate = variable->clone();
220 auto duplicate = variable->clone();
205
221
206 // Adds clone to model
222 // Adds clone to model
207 impl->m_VariableModel->addVariable(duplicate);
223 impl->m_VariableModel->addVariable(duplicate);
208
224
209 // Generates clone identifier
225 // Generates clone identifier
210 impl->m_VariableToIdentifierMap[duplicate] = QUuid::createUuid();
226 impl->m_VariableToIdentifierMap[duplicate] = QUuid::createUuid();
211
227
212 // Registers provider
228 // Registers provider
213 auto variableProvider = impl->m_VariableToProviderMap.at(variable);
229 auto variableProvider = impl->m_VariableToProviderMap.at(variable);
214 auto duplicateProvider = variableProvider != nullptr ? variableProvider->clone() : nullptr;
230 auto duplicateProvider = variableProvider != nullptr ? variableProvider->clone() : nullptr;
215
231
216 impl->m_VariableToProviderMap[duplicate] = duplicateProvider;
232 impl->m_VariableToProviderMap[duplicate] = duplicateProvider;
217 if (duplicateProvider) {
233 if (duplicateProvider) {
218 impl->registerProvider(duplicateProvider);
234 impl->registerProvider(duplicateProvider);
219 }
235 }
220
236
221 return duplicate;
237 return duplicate;
222 }
238 }
223 else {
239 else {
224 qCCritical(LOG_VariableController())
240 qCCritical(LOG_VariableController())
225 << tr("Can't create duplicate of variable %1: variable not registered in the model")
241 << tr("Can't create duplicate of variable %1: variable not registered in the model")
226 .arg(variable->name());
242 .arg(variable->name());
227 return nullptr;
243 return nullptr;
228 }
244 }
229 }
245 }
230
246
231 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
247 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
232 {
248 {
233 if (!variable) {
249 if (!variable) {
234 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
250 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
235 return;
251 return;
236 }
252 }
237
253
238 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
254 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
239 // make some treatments before the deletion
255 // make some treatments before the deletion
240 emit variableAboutToBeDeleted(variable);
256 emit variableAboutToBeDeleted(variable);
241
257
242 // Deletes identifier
258 // Deletes identifier
243 impl->m_VariableToIdentifierMap.erase(variable);
259 impl->m_VariableToIdentifierMap.erase(variable);
244
260
245 // Deletes provider
261 // Deletes provider
246 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
262 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
247 qCDebug(LOG_VariableController())
263 qCDebug(LOG_VariableController())
248 << tr("Number of providers deleted for variable %1: %2")
264 << tr("Number of providers deleted for variable %1: %2")
249 .arg(variable->name(), QString::number(nbProvidersDeleted));
265 .arg(variable->name(), QString::number(nbProvidersDeleted));
250
266
251
267
252 // Deletes from model
268 // Deletes from model
253 impl->m_VariableModel->deleteVariable(variable);
269 impl->m_VariableModel->deleteVariable(variable);
254 }
270 }
255
271
256 void VariableController::deleteVariables(
272 void VariableController::deleteVariables(
257 const QVector<std::shared_ptr<Variable> > &variables) noexcept
273 const QVector<std::shared_ptr<Variable> > &variables) noexcept
258 {
274 {
259 for (auto variable : qAsConst(variables)) {
275 for (auto variable : qAsConst(variables)) {
260 deleteVariable(variable);
276 deleteVariable(variable);
261 }
277 }
262 }
278 }
263
279
264 std::shared_ptr<Variable>
280 std::shared_ptr<Variable>
265 VariableController::createVariable(const QString &name, const QVariantHash &metadata,
281 VariableController::createVariable(const QString &name, const QVariantHash &metadata,
266 std::shared_ptr<IDataProvider> provider) noexcept
282 std::shared_ptr<IDataProvider> provider) noexcept
267 {
283 {
268 if (!impl->m_TimeController) {
284 if (!impl->m_TimeController) {
269 qCCritical(LOG_VariableController())
285 qCCritical(LOG_VariableController())
270 << tr("Impossible to create variable: The time controller is null");
286 << tr("Impossible to create variable: The time controller is null");
271 return nullptr;
287 return nullptr;
272 }
288 }
273
289
274 auto range = impl->m_TimeController->dateTime();
290 auto range = impl->m_TimeController->dateTime();
275
291
276 if (auto newVariable = impl->m_VariableModel->createVariable(name, metadata)) {
292 if (auto newVariable = impl->m_VariableModel->createVariable(name, metadata)) {
277 auto identifier = QUuid::createUuid();
293 auto varId = QUuid::createUuid();
294
295 // Create the handler
296 auto varRequestHandler = std::make_unique<VariableRequestHandler>();
297 varRequestHandler->m_VarId = varId;
298
299 impl->m_VarIdToVarRequestHandler.insert(
300 std::make_pair(varId, std::move(varRequestHandler)));
278
301
279 // store the provider
302 // store the provider
280 impl->registerProvider(provider);
303 impl->registerProvider(provider);
281
304
282 // Associate the provider
305 // Associate the provider
283 impl->m_VariableToProviderMap[newVariable] = provider;
306 impl->m_VariableToProviderMap[newVariable] = provider;
284 impl->m_VariableToIdentifierMap[newVariable] = identifier;
307 impl->m_VariableToIdentifierMap[newVariable] = varId;
285
308
309 this->onRequestDataLoading(QVector<std::shared_ptr<Variable> >{newVariable}, range, false);
286
310
287 auto varRequestId = QUuid::createUuid();
311 // auto varRequestId = QUuid::createUuid();
288 qCInfo(LOG_VariableController()) << "createVariable: " << identifier << varRequestId;
312 // qCInfo(LOG_VariableController()) << "createVariable: " << varId << varRequestId;
289 impl->processRequest(newVariable, range, varRequestId);
313 // impl->processRequest(newVariable, range, varRequestId);
290 impl->updateVariableRequest(varRequestId);
314 // impl->updateVariableRequest(varRequestId);
291
315
292 return newVariable;
316 return newVariable;
293 }
317 }
294 }
318 }
295
319
296 void VariableController::onDateTimeOnSelection(const SqpRange &dateTime)
320 void VariableController::onDateTimeOnSelection(const SqpRange &dateTime)
297 {
321 {
298 // NOTE: Even if acquisition request is aborting, the graphe range will be changed
322 // NOTE: Even if acquisition request is aborting, the graphe range will be changed
299 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
323 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
300 << QThread::currentThread()->objectName();
324 << QThread::currentThread()->objectName();
301 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
325 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
302 auto variables = QVector<std::shared_ptr<Variable> >{};
326 auto variables = QVector<std::shared_ptr<Variable> >{};
303
327
304 for (const auto &selectedRow : qAsConst(selectedRows)) {
328 for (const auto &selectedRow : qAsConst(selectedRows)) {
305 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
329 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
306 variables << selectedVariable;
330 variables << selectedVariable;
307
331
308 // notify that rescale operation has to be done
332 // notify that rescale operation has to be done
309 emit rangeChanged(selectedVariable, dateTime);
333 emit rangeChanged(selectedVariable, dateTime);
310 }
334 }
311 }
335 }
312
336
313 if (!variables.isEmpty()) {
337 if (!variables.isEmpty()) {
314 this->onRequestDataLoading(variables, dateTime, true);
338 this->onRequestDataLoading(variables, dateTime, true);
315 }
339 }
316 }
340 }
317
341
318 void VariableController::onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
342 void VariableController::onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
319 const SqpRange &cacheRangeRequested,
343 const SqpRange &cacheRangeRequested,
320 QVector<AcquisitionDataPacket> dataAcquired)
344 QVector<AcquisitionDataPacket> dataAcquired)
321 {
345 {
346 qCInfo(LOG_VariableController()) << tr("onDataProvided") << QThread::currentThread();
322 auto retrievedDataSeries = impl->retrieveDataSeries(dataAcquired);
347 auto retrievedDataSeries = impl->retrieveDataSeries(dataAcquired);
323 auto varRequestId = impl->acceptVariableRequest(vIdentifier, retrievedDataSeries);
348 auto varRequestId = impl->acceptVariableRequest(vIdentifier, retrievedDataSeries);
324 if (!varRequestId.isNull()) {
349 if (!varRequestId.isNull()) {
325 impl->updateVariableRequest(varRequestId);
350 impl->updateVariables(varRequestId);
326 }
351 }
327 }
352 }
328
353
329 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
354 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
330 {
355 {
331 qCDebug(LOG_VariableController())
356 qCDebug(LOG_VariableController())
332 << "TORM: variableController::onVariableRetrieveDataInProgress"
357 << "TORM: variableController::onVariableRetrieveDataInProgress"
333 << QThread::currentThread()->objectName() << progress;
358 << QThread::currentThread()->objectName() << progress;
334 if (auto var = impl->findVariable(identifier)) {
359 if (auto var = impl->findVariable(identifier)) {
335 impl->m_VariableModel->setDataProgress(var, progress);
360 impl->m_VariableModel->setDataProgress(var, progress);
336 }
361 }
337 else {
362 else {
338 qCCritical(LOG_VariableController())
363 qCCritical(LOG_VariableController())
339 << tr("Impossible to notify progression of a null variable");
364 << tr("Impossible to notify progression of a null variable");
340 }
365 }
341 }
366 }
342
367
343 void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable)
368 void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable)
344 {
369 {
345 auto it = impl->m_VariableToIdentifierMap.find(variable);
370 // auto it = impl->m_VariableToIdentifierMap.find(variable);
346 if (it != impl->m_VariableToIdentifierMap.cend()) {
371 // if (it != impl->m_VariableToIdentifierMap.cend()) {
347 impl->m_VariableAcquisitionWorker->abortProgressRequested(it->second);
372 // impl->m_VariableAcquisitionWorker->abortProgressRequested(it->second);
348
373
349 QUuid varRequestId;
374 // QUuid varRequestId;
350 auto varIdToVarRequestIdQueueMapIt = impl->m_VarIdToVarRequestIdQueueMap.find(it->second);
375 // auto varIdToVarRequestIdQueueMapIt =
351 if (varIdToVarRequestIdQueueMapIt != impl->m_VarIdToVarRequestIdQueueMap.cend()) {
376 // impl->m_VarIdToVarRequestIdQueueMap.find(it->second);
352 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
377 // if (varIdToVarRequestIdQueueMapIt != impl->m_VarIdToVarRequestIdQueueMap.cend()) {
353 varRequestId = varRequestIdQueue.front();
378 // auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
354 impl->cancelVariableRequest(varRequestId);
379 // varRequestId = varRequestIdQueue.front();
355
380 // impl->cancelVariableRequest(varRequestId);
356 // Finish the progression for the request
381
357 impl->m_VariableModel->setDataProgress(variable, 0.0);
382 // // Finish the progression for the request
358 }
383 // impl->m_VariableModel->setDataProgress(variable, 0.0);
359 else {
384 // }
360 qCWarning(LOG_VariableController())
385 // else {
361 << tr("Aborting progression of inexistant variable request detected !!!")
386 // qCWarning(LOG_VariableController())
362 << QThread::currentThread()->objectName();
387 // << tr("Aborting progression of inexistant variable request detected !!!")
363 }
388 // << QThread::currentThread()->objectName();
364 }
389 // }
365 else {
390 // }
366 qCWarning(LOG_VariableController())
391 // else {
367 << tr("Aborting progression of inexistant variable detected !!!")
392 // qCWarning(LOG_VariableController())
368 << QThread::currentThread()->objectName();
393 // << tr("Aborting progression of inexistant variable detected !!!")
369 }
394 // << QThread::currentThread()->objectName();
395 // }
370 }
396 }
371
397
372 void VariableController::onAbortAcquisitionRequested(QUuid vIdentifier)
398 void VariableController::onAbortAcquisitionRequested(QUuid vIdentifier)
373 {
399 {
374 qCDebug(LOG_VariableController()) << "TORM: variableController::onAbortAcquisitionRequested"
400 qCDebug(LOG_VariableController()) << "TORM: variableController::onAbortAcquisitionRequested"
375 << QThread::currentThread()->objectName() << vIdentifier;
401 << QThread::currentThread()->objectName() << vIdentifier;
376
402
377 if (auto var = impl->findVariable(vIdentifier)) {
403 if (auto var = impl->findVariable(vIdentifier)) {
378 this->onAbortProgressRequested(var);
404 this->onAbortProgressRequested(var);
379 }
405 }
380 else {
406 else {
381 qCCritical(LOG_VariableController())
407 qCCritical(LOG_VariableController())
382 << tr("Impossible to abort Acquisition Requestof a null variable");
408 << tr("Impossible to abort Acquisition Requestof a null variable");
383 }
409 }
384 }
410 }
385
411
386 void VariableController::onAddSynchronizationGroupId(QUuid synchronizationGroupId)
412 void VariableController::onAddSynchronizationGroupId(QUuid synchronizationGroupId)
387 {
413 {
388 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronizationGroupId"
414 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronizationGroupId"
389 << QThread::currentThread()->objectName()
415 << QThread::currentThread()->objectName()
390 << synchronizationGroupId;
416 << synchronizationGroupId;
391 auto vSynchroGroup = std::make_shared<VariableSynchronizationGroup>();
417 auto vSynchroGroup = std::make_shared<VariableSynchronizationGroup>();
392 impl->m_GroupIdToVariableSynchronizationGroupMap.insert(
418 impl->m_GroupIdToVariableSynchronizationGroupMap.insert(
393 std::make_pair(synchronizationGroupId, vSynchroGroup));
419 std::make_pair(synchronizationGroupId, vSynchroGroup));
394 }
420 }
395
421
396 void VariableController::onRemoveSynchronizationGroupId(QUuid synchronizationGroupId)
422 void VariableController::onRemoveSynchronizationGroupId(QUuid synchronizationGroupId)
397 {
423 {
398 impl->m_GroupIdToVariableSynchronizationGroupMap.erase(synchronizationGroupId);
424 impl->m_GroupIdToVariableSynchronizationGroupMap.erase(synchronizationGroupId);
399 }
425 }
400
426
401 void VariableController::onAddSynchronized(std::shared_ptr<Variable> variable,
427 void VariableController::onAddSynchronized(std::shared_ptr<Variable> variable,
402 QUuid synchronizationGroupId)
428 QUuid synchronizationGroupId)
403
429
404 {
430 {
405 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronized"
431 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronized"
406 << synchronizationGroupId;
432 << synchronizationGroupId;
407 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(variable);
433 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(variable);
408 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
434 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
409 auto groupIdToVSGIt
435 auto groupIdToVSGIt
410 = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
436 = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
411 if (groupIdToVSGIt != impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
437 if (groupIdToVSGIt != impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
412 impl->m_VariableIdGroupIdMap.insert(
438 impl->m_VariableIdGroupIdMap.insert(
413 std::make_pair(varToVarIdIt->second, synchronizationGroupId));
439 std::make_pair(varToVarIdIt->second, synchronizationGroupId));
414 groupIdToVSGIt->second->addVariableId(varToVarIdIt->second);
440 groupIdToVSGIt->second->addVariableId(varToVarIdIt->second);
415 }
441 }
416 else {
442 else {
417 qCCritical(LOG_VariableController())
443 qCCritical(LOG_VariableController())
418 << tr("Impossible to synchronize a variable with an unknown sycnhronization group")
444 << tr("Impossible to synchronize a variable with an unknown sycnhronization group")
419 << variable->name();
445 << variable->name();
420 }
446 }
421 }
447 }
422 else {
448 else {
423 qCCritical(LOG_VariableController())
449 qCCritical(LOG_VariableController())
424 << tr("Impossible to synchronize a variable with no identifier") << variable->name();
450 << tr("Impossible to synchronize a variable with no identifier") << variable->name();
425 }
451 }
426 }
452 }
427
453
428 void VariableController::desynchronize(std::shared_ptr<Variable> variable,
454 void VariableController::desynchronize(std::shared_ptr<Variable> variable,
429 QUuid synchronizationGroupId)
455 QUuid synchronizationGroupId)
430 {
456 {
431 // Gets variable id
457 // Gets variable id
432 auto variableIt = impl->m_VariableToIdentifierMap.find(variable);
458 auto variableIt = impl->m_VariableToIdentifierMap.find(variable);
433 if (variableIt == impl->m_VariableToIdentifierMap.cend()) {
459 if (variableIt == impl->m_VariableToIdentifierMap.cend()) {
434 qCCritical(LOG_VariableController())
460 qCCritical(LOG_VariableController())
435 << tr("Can't desynchronize variable %1: variable identifier not found")
461 << tr("Can't desynchronize variable %1: variable identifier not found")
436 .arg(variable->name());
462 .arg(variable->name());
437 return;
463 return;
438 }
464 }
439
465
440 // Gets synchronization group
466 // Gets synchronization group
441 auto groupIt = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
467 auto groupIt = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
442 if (groupIt == impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
468 if (groupIt == impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
443 qCCritical(LOG_VariableController())
469 qCCritical(LOG_VariableController())
444 << tr("Can't desynchronize variable %1: unknown synchronization group")
470 << tr("Can't desynchronize variable %1: unknown synchronization group")
445 .arg(variable->name());
471 .arg(variable->name());
446 return;
472 return;
447 }
473 }
448
474
449 auto variableId = variableIt->second;
475 auto variableId = variableIt->second;
450
476
451 // Removes variable from synchronization group
477 // Removes variable from synchronization group
452 auto synchronizationGroup = groupIt->second;
478 auto synchronizationGroup = groupIt->second;
453 synchronizationGroup->removeVariableId(variableId);
479 synchronizationGroup->removeVariableId(variableId);
454
480
455 // Removes link between variable and synchronization group
481 // Removes link between variable and synchronization group
456 impl->m_VariableIdGroupIdMap.erase(variableId);
482 impl->m_VariableIdGroupIdMap.erase(variableId);
457 }
483 }
458
484
459 void VariableController::onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables,
485 void VariableController::onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables,
460 const SqpRange &range, bool synchronise)
486 const SqpRange &range, bool synchronise)
461 {
487 {
462 // NOTE: oldRange isn't really necessary since oldRange == variable->range().
488 // variables is assumed synchronized
463
489 // TODO: Asser variables synchronization
464 // we want to load data of the variable for the dateTime.
490 // we want to load data of the variable for the dateTime.
465 // First we check if the cache contains some of them.
491 if (variables.isEmpty()) {
466 // For the other, we ask the provider to give them.
492 return;
493 }
467
494
468 auto varRequestId = QUuid::createUuid();
495 auto varRequestId = QUuid::createUuid();
469 qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading"
496 qCCritical(LOG_VariableController()) << "VariableController::onRequestDataLoading"
470 << QThread::currentThread()->objectName() << varRequestId;
497 << QThread::currentThread()->objectName() << varRequestId
498 << range;
471
499
500 if (!synchronise) {
501 auto varIds = std::list<QUuid>{};
472 for (const auto &var : variables) {
502 for (const auto &var : variables) {
473 qCInfo(LOG_VariableController()) << "processRequest for" << var->name() << varRequestId;
503 auto vId = impl->m_VariableToIdentifierMap.at(var);
474 impl->processRequest(var, range, varRequestId);
504 varIds.push_back(vId);
475 }
505 }
476
506 impl->m_VarGroupIdToVarIds.insert(std::make_pair(varRequestId, varIds));
477 if (synchronise) {
478 // Get the group ids
479 qCDebug(LOG_VariableController())
480 << "TORM VariableController::onRequestDataLoading for synchro var ENABLE";
481 auto groupIds = std::set<QUuid>{};
482 auto groupIdToOldRangeMap = std::map<QUuid, SqpRange>{};
483 for (const auto &var : variables) {
507 for (const auto &var : variables) {
484 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(var);
508 qCInfo(LOG_VariableController()) << "processRequest for" << var->name() << varRequestId
485 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
509 << varIds.size();
486 auto vId = varToVarIdIt->second;
510 impl->processRequest(var, range, varRequestId);
487 auto varIdToGroupIdIt = impl->m_VariableIdGroupIdMap.find(vId);
488 if (varIdToGroupIdIt != impl->m_VariableIdGroupIdMap.cend()) {
489 auto gId = varIdToGroupIdIt->second;
490 groupIdToOldRangeMap.insert(std::make_pair(gId, var->range()));
491 if (groupIds.find(gId) == groupIds.cend()) {
492 qCDebug(LOG_VariableController()) << "Synchro detect group " << gId;
493 groupIds.insert(gId);
494 }
495 }
496 }
511 }
497 }
512 }
513 else {
514 auto vId = impl->m_VariableToIdentifierMap.at(variables.first());
515 qCInfo(LOG_VariableController()) << "Var in synchro group 0" << vId;
516 auto varIdToGroupIdIt = impl->m_VariableIdGroupIdMap.find(vId);
517 if (varIdToGroupIdIt != impl->m_VariableIdGroupIdMap.cend()) {
518 auto groupId = varIdToGroupIdIt->second;
498
519
499 // We assume here all group ids exist
520 auto vSynchronizationGroup
500 for (const auto &gId : groupIds) {
521 = impl->m_GroupIdToVariableSynchronizationGroupMap.at(groupId);
501 auto vSynchronizationGroup = impl->m_GroupIdToVariableSynchronizationGroupMap.at(gId);
502 auto vSyncIds = vSynchronizationGroup->getIds();
522 auto vSyncIds = vSynchronizationGroup->getIds();
503 qCDebug(LOG_VariableController()) << "Var in synchro group ";
523
524 qCInfo(LOG_VariableController()) << "Var in synchro group 1" << groupId;
525
526 auto varIds = std::list<QUuid>{};
527 for (auto vId : vSyncIds) {
528 varIds.push_back(vId);
529 }
530 impl->m_VarGroupIdToVarIds.insert(std::make_pair(varRequestId, varIds));
531
504 for (auto vId : vSyncIds) {
532 for (auto vId : vSyncIds) {
505 auto var = impl->findVariable(vId);
533 auto var = impl->findVariable(vId);
506
534
507 // Don't process already processed var
535 // Don't process already processed var
508 if (!variables.contains(var)) {
509 if (var != nullptr) {
536 if (var != nullptr) {
510 qCInfo(LOG_VariableController()) << "processRequest synchro for"
537 qCInfo(LOG_VariableController()) << "processRequest synchro for" << var->name()
511 << var->name() << varRequestId;
538 << varRequestId;
512 auto vSyncRangeRequested = computeSynchroRangeRequested(
539 auto vSyncRangeRequested
513 var->range(), range, groupIdToOldRangeMap.at(gId));
540 = variables.contains(var)
541 ? range
542 : computeSynchroRangeRequested(var->range(), range,
543 variables.first()->range());
514 qCDebug(LOG_VariableController()) << "synchro RR" << vSyncRangeRequested;
544 qCDebug(LOG_VariableController()) << "synchro RR" << vSyncRangeRequested;
515 impl->processRequest(var, vSyncRangeRequested, varRequestId);
545 impl->processRequest(var, vSyncRangeRequested, varRequestId);
516 }
546 }
517 else {
547 else {
518 qCCritical(LOG_VariableController())
548 qCCritical(LOG_VariableController())
519
549
520 << tr("Impossible to synchronize a null variable");
550 << tr("Impossible to synchronize a null variable");
521 }
551 }
522 }
552 }
523 }
553 }
524 }
554 }
525 }
526
555
527 impl->updateVariableRequest(varRequestId);
556 impl->updateVariables(varRequestId);
528 }
557 }
529
558
530
559
531 void VariableController::initialize()
560 void VariableController::initialize()
532 {
561 {
533 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
562 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
534 impl->m_WorkingMutex.lock();
563 impl->m_WorkingMutex.lock();
535 qCDebug(LOG_VariableController()) << tr("VariableController init END");
564 qCDebug(LOG_VariableController()) << tr("VariableController init END");
536 }
565 }
537
566
538 void VariableController::finalize()
567 void VariableController::finalize()
539 {
568 {
540 impl->m_WorkingMutex.unlock();
569 impl->m_WorkingMutex.unlock();
541 }
570 }
542
571
543 void VariableController::waitForFinish()
572 void VariableController::waitForFinish()
544 {
573 {
545 QMutexLocker locker{&impl->m_WorkingMutex};
574 QMutexLocker locker{&impl->m_WorkingMutex};
546 }
575 }
547
576
548 AcquisitionZoomType VariableController::getZoomType(const SqpRange &range, const SqpRange &oldRange)
577 AcquisitionZoomType VariableController::getZoomType(const SqpRange &range, const SqpRange &oldRange)
549 {
578 {
550 // t1.m_TStart <= t2.m_TStart && t2.m_TEnd <= t1.m_TEnd
579 // t1.m_TStart <= t2.m_TStart && t2.m_TEnd <= t1.m_TEnd
551 auto zoomType = AcquisitionZoomType::Unknown;
580 auto zoomType = AcquisitionZoomType::Unknown;
552 if (range.m_TStart <= oldRange.m_TStart && oldRange.m_TEnd <= range.m_TEnd) {
581 if (range.m_TStart <= oldRange.m_TStart && oldRange.m_TEnd <= range.m_TEnd) {
553 qCDebug(LOG_VariableController()) << "zoomtype: ZoomOut";
582 qCDebug(LOG_VariableController()) << "zoomtype: ZoomOut";
554 zoomType = AcquisitionZoomType::ZoomOut;
583 zoomType = AcquisitionZoomType::ZoomOut;
555 }
584 }
556 else if (range.m_TStart > oldRange.m_TStart && range.m_TEnd > oldRange.m_TEnd) {
585 else if (range.m_TStart > oldRange.m_TStart && range.m_TEnd > oldRange.m_TEnd) {
557 qCDebug(LOG_VariableController()) << "zoomtype: PanRight";
586 qCDebug(LOG_VariableController()) << "zoomtype: PanRight";
558 zoomType = AcquisitionZoomType::PanRight;
587 zoomType = AcquisitionZoomType::PanRight;
559 }
588 }
560 else if (range.m_TStart < oldRange.m_TStart && range.m_TEnd < oldRange.m_TEnd) {
589 else if (range.m_TStart < oldRange.m_TStart && range.m_TEnd < oldRange.m_TEnd) {
561 qCDebug(LOG_VariableController()) << "zoomtype: PanLeft";
590 qCDebug(LOG_VariableController()) << "zoomtype: PanLeft";
562 zoomType = AcquisitionZoomType::PanLeft;
591 zoomType = AcquisitionZoomType::PanLeft;
563 }
592 }
564 else if (range.m_TStart > oldRange.m_TStart && oldRange.m_TEnd > range.m_TEnd) {
593 else if (range.m_TStart > oldRange.m_TStart && oldRange.m_TEnd > range.m_TEnd) {
565 qCDebug(LOG_VariableController()) << "zoomtype: ZoomIn";
594 qCDebug(LOG_VariableController()) << "zoomtype: ZoomIn";
566 zoomType = AcquisitionZoomType::ZoomIn;
595 zoomType = AcquisitionZoomType::ZoomIn;
567 }
596 }
568 else {
597 else {
569 qCDebug(LOG_VariableController()) << "getZoomType: Unknown type detected";
598 qCDebug(LOG_VariableController()) << "getZoomType: Unknown type detected";
570 }
599 }
571 return zoomType;
600 return zoomType;
572 }
601 }
573
602
574 void VariableController::VariableControllerPrivate::processRequest(std::shared_ptr<Variable> var,
603 void VariableController::VariableControllerPrivate::processRequest(std::shared_ptr<Variable> var,
575 const SqpRange &rangeRequested,
604 const SqpRange &rangeRequested,
576 QUuid varRequestId)
605 QUuid varRequestId)
577 {
606 {
578 auto varRequest = VariableRequest{};
607 auto itVar = m_VariableToIdentifierMap.find(var);
608 if (itVar == m_VariableToIdentifierMap.cend()) {
609 qCCritical(LOG_VariableController())
610 << tr("Impossible to process request for unknown variable");
611 return;
612 }
613
614 auto varId = itVar->second;
579
615
580 auto it = m_VariableToIdentifierMap.find(var);
616 auto itVarHandler = m_VarIdToVarRequestHandler.find(varId);
581 if (it != m_VariableToIdentifierMap.cend()) {
617 if (itVarHandler == m_VarIdToVarRequestHandler.cend()) {
618 qCCritical(LOG_VariableController())
619 << tr("Impossible to process request for variable with unknown handler");
620 return;
621 }
582
622
583 auto varId = it->second;
623 auto oldRange = var->range();
584
624
585 auto oldRange = getLastRequestedRange(varId);
625 auto varHandler = itVarHandler->second.get();
586
626
587 // check for update oldRange to the last request range.
627 if (varHandler->m_State != VariableRequestHandlerState::OFF) {
588 if (oldRange == INVALID_RANGE) {
628 oldRange = varHandler->m_RunningVarRequest.m_RangeRequested;
589 oldRange = var->range();
590 }
629 }
591
630
631 auto varRequest = VariableRequest{};
632 varRequest.m_VariableGroupId = varRequestId;
592 auto varStrategyRangesRequested
633 auto varStrategyRangesRequested
593 = m_VariableCacheStrategy->computeRange(oldRange, rangeRequested);
634 = m_VariableCacheStrategy->computeRange(oldRange, rangeRequested);
594
595 // Use commented lines to remove the cache (run time)
596 // auto notInCacheRangeList = QVector<SqpRange>{varStrategyRangesRequested.second};
597 // auto inCacheRangeList = QVector<SqpRange>{};
598 auto notInCacheRangeList
599 = Variable::provideNotInCacheRangeList(oldRange, varStrategyRangesRequested.second);
600 auto inCacheRangeList
601 = Variable::provideInCacheRangeList(oldRange, varStrategyRangesRequested.second);
602
603 if (!notInCacheRangeList.empty()) {
604 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
635 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
605 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
636 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
606
637
607 // store VarRequest
638 switch (varHandler->m_State) {
608 storeVariableRequest(varId, varRequestId, varRequest);
639 case VariableRequestHandlerState::OFF: {
609
640 qCCritical(LOG_VariableController()) << tr("Process Request OFF")
610 auto varProvider = m_VariableToProviderMap.at(var);
641 << varRequest.m_RangeRequested
611 if (varProvider != nullptr) {
642 << varRequest.m_CacheRangeRequested;
612 auto varRequestIdCanceled = m_VariableAcquisitionWorker->pushVariableRequest(
643 varHandler->m_RunningVarRequest = varRequest;
613 varRequestId, varId, varStrategyRangesRequested.first,
644 varHandler->m_State = VariableRequestHandlerState::RUNNING;
614 varStrategyRangesRequested.second,
645 executeVarRequest(var, varRequest);
615 DataProviderParameters{std::move(notInCacheRangeList), var->metadata()},
646 break;
616 varProvider);
617
618 }
647 }
619 else {
648 case VariableRequestHandlerState::RUNNING: {
620 qCCritical(LOG_VariableController())
649 qCCritical(LOG_VariableController()) << tr("Process Request RUNNING")
621 << "Impossible to provide data with a null provider";
650 << varRequest.m_RangeRequested
651 << varRequest.m_CacheRangeRequested;
652 varHandler->m_State = VariableRequestHandlerState::PENDING;
653 varHandler->m_PendingVarRequest = varRequest;
654 break;
622 }
655 }
656 case VariableRequestHandlerState::PENDING: {
657 qCCritical(LOG_VariableController()) << tr("Process Request PENDING")
658 << varRequest.m_RangeRequested
659 << varRequest.m_CacheRangeRequested;
660 auto variableGroupIdToCancel = varHandler->m_PendingVarRequest.m_VariableGroupId;
661 varHandler->m_PendingVarRequest = varRequest;
662 cancelVariableRequest(variableGroupIdToCancel);
623
663
624 if (!inCacheRangeList.empty()) {
664 break;
625 emit q->updateVarDisplaying(var, inCacheRangeList.first());
626 }
627 }
628 else {
629 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
630 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
631 // store VarRequest
632 storeVariableRequest(varId, varRequestId, varRequest);
633 acceptVariableRequest(
634 varId, var->dataSeries()->subDataSeries(varStrategyRangesRequested.second));
635 }
665 }
666 default:
667 qCCritical(LOG_VariableController())
668 << QObject::tr("Unknown VariableRequestHandlerState");
636 }
669 }
637 }
670 }
638
671
639 std::shared_ptr<Variable>
672 std::shared_ptr<Variable>
640 VariableController::VariableControllerPrivate::findVariable(QUuid vIdentifier)
673 VariableController::VariableControllerPrivate::findVariable(QUuid vIdentifier)
641 {
674 {
642 std::shared_ptr<Variable> var;
675 std::shared_ptr<Variable> var;
643 auto findReply = [vIdentifier](const auto &entry) { return vIdentifier == entry.second; };
676 auto findReply = [vIdentifier](const auto &entry) { return vIdentifier == entry.second; };
644
677
645 auto end = m_VariableToIdentifierMap.cend();
678 auto end = m_VariableToIdentifierMap.cend();
646 auto it = std::find_if(m_VariableToIdentifierMap.cbegin(), end, findReply);
679 auto it = std::find_if(m_VariableToIdentifierMap.cbegin(), end, findReply);
647 if (it != end) {
680 if (it != end) {
648 var = it->first;
681 var = it->first;
649 }
682 }
650 else {
683 else {
651 qCCritical(LOG_VariableController())
684 qCCritical(LOG_VariableController())
652 << tr("Impossible to find the variable with the identifier: ") << vIdentifier;
685 << tr("Impossible to find the variable with the identifier: ") << vIdentifier;
653 }
686 }
654
687
655 return var;
688 return var;
656 }
689 }
657
690
658 std::shared_ptr<IDataSeries> VariableController::VariableControllerPrivate::retrieveDataSeries(
691 std::shared_ptr<IDataSeries> VariableController::VariableControllerPrivate::retrieveDataSeries(
659 const QVector<AcquisitionDataPacket> acqDataPacketVector)
692 const QVector<AcquisitionDataPacket> acqDataPacketVector)
660 {
693 {
661 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size")
694 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size")
662 << acqDataPacketVector.size();
695 << acqDataPacketVector.size();
663 std::shared_ptr<IDataSeries> dataSeries;
696 std::shared_ptr<IDataSeries> dataSeries;
664 if (!acqDataPacketVector.isEmpty()) {
697 if (!acqDataPacketVector.isEmpty()) {
665 dataSeries = acqDataPacketVector[0].m_DateSeries;
698 dataSeries = acqDataPacketVector[0].m_DateSeries;
666 for (int i = 1; i < acqDataPacketVector.size(); ++i) {
699 for (int i = 1; i < acqDataPacketVector.size(); ++i) {
667 dataSeries->merge(acqDataPacketVector[i].m_DateSeries.get());
700 dataSeries->merge(acqDataPacketVector[i].m_DateSeries.get());
668 }
701 }
669 }
702 }
670 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size END")
703 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size END")
671 << acqDataPacketVector.size();
704 << acqDataPacketVector.size();
672 return dataSeries;
705 return dataSeries;
673 }
706 }
674
707
675 void VariableController::VariableControllerPrivate::registerProvider(
708 void VariableController::VariableControllerPrivate::registerProvider(
676 std::shared_ptr<IDataProvider> provider)
709 std::shared_ptr<IDataProvider> provider)
677 {
710 {
678 if (m_ProviderSet.find(provider) == m_ProviderSet.end()) {
711 if (m_ProviderSet.find(provider) == m_ProviderSet.end()) {
679 qCDebug(LOG_VariableController()) << tr("Registering of a new provider")
712 qCDebug(LOG_VariableController()) << tr("Registering of a new provider")
680 << provider->objectName();
713 << provider->objectName();
681 m_ProviderSet.insert(provider);
714 m_ProviderSet.insert(provider);
682 connect(provider.get(), &IDataProvider::dataProvided, m_VariableAcquisitionWorker.get(),
715 connect(provider.get(), &IDataProvider::dataProvided, m_VariableAcquisitionWorker.get(),
683 &VariableAcquisitionWorker::onVariableDataAcquired);
716 &VariableAcquisitionWorker::onVariableDataAcquired);
684 connect(provider.get(), &IDataProvider::dataProvidedProgress,
717 connect(provider.get(), &IDataProvider::dataProvidedProgress,
685 m_VariableAcquisitionWorker.get(),
718 m_VariableAcquisitionWorker.get(),
686 &VariableAcquisitionWorker::onVariableRetrieveDataInProgress);
719 &VariableAcquisitionWorker::onVariableRetrieveDataInProgress);
687 connect(provider.get(), &IDataProvider::dataProvidedFailed,
720 connect(provider.get(), &IDataProvider::dataProvidedFailed,
688 m_VariableAcquisitionWorker.get(),
721 m_VariableAcquisitionWorker.get(),
689 &VariableAcquisitionWorker::onVariableAcquisitionFailed);
722 &VariableAcquisitionWorker::onVariableAcquisitionFailed);
690 }
723 }
691 else {
724 else {
692 qCDebug(LOG_VariableController()) << tr("Cannot register provider, it already exists ");
725 qCDebug(LOG_VariableController()) << tr("Cannot register provider, it already exists ");
693 }
726 }
694 }
727 }
695
728
696 void VariableController::VariableControllerPrivate::storeVariableRequest(
697 QUuid varId, QUuid varRequestId, const VariableRequest &varRequest)
698 {
699 // First request for the variable. we can create an entry for it
700 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
701 if (varIdToVarRequestIdQueueMapIt == m_VarIdToVarRequestIdQueueMap.cend()) {
702 auto varRequestIdQueue = std::deque<QUuid>{};
703 qCDebug(LOG_VariableController()) << tr("Store REQUEST in QUEUE");
704 varRequestIdQueue.push_back(varRequestId);
705 m_VarIdToVarRequestIdQueueMap.insert(std::make_pair(varId, std::move(varRequestIdQueue)));
706 }
707 else {
708 qCDebug(LOG_VariableController()) << tr("Store REQUEST in EXISTING QUEUE");
709 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
710 varRequestIdQueue.push_back(varRequestId);
711 }
712
713 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
714 if (varRequestIdToVarIdVarRequestMapIt == m_VarRequestIdToVarIdVarRequestMap.cend()) {
715 auto varIdToVarRequestMap = std::map<QUuid, VariableRequest>{};
716 varIdToVarRequestMap.insert(std::make_pair(varId, varRequest));
717 qCDebug(LOG_VariableController()) << tr("Store REQUESTID in MAP");
718 m_VarRequestIdToVarIdVarRequestMap.insert(
719 std::make_pair(varRequestId, std::move(varIdToVarRequestMap)));
720 }
721 else {
722 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
723 qCDebug(LOG_VariableController()) << tr("Store REQUESTID in EXISTING MAP");
724 varIdToVarRequestMap.insert(std::make_pair(varId, varRequest));
725 }
726 }
727
728 QUuid VariableController::VariableControllerPrivate::acceptVariableRequest(
729 QUuid VariableController::VariableControllerPrivate::acceptVariableRequest(
729 QUuid varId, std::shared_ptr<IDataSeries> dataSeries)
730 QUuid varId, std::shared_ptr<IDataSeries> dataSeries)
730 {
731 {
731 QUuid varRequestId;
732 auto itVarHandler = m_VarIdToVarRequestHandler.find(varId);
732 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
733 if (itVarHandler == m_VarIdToVarRequestHandler.cend()) {
733 if (varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.cend()) {
734 return QUuid();
734 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
735 varRequestId = varRequestIdQueue.front();
736 auto varRequestIdToVarIdVarRequestMapIt
737 = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
738 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
739 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
740 auto varIdToVarRequestMapIt = varIdToVarRequestMap.find(varId);
741 if (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) {
742 qCDebug(LOG_VariableController()) << tr("acceptVariableRequest");
743 auto &varRequest = varIdToVarRequestMapIt->second;
744 varRequest.m_DataSeries = dataSeries;
745 varRequest.m_CanUpdate = true;
746 }
747 else {
748 qCDebug(LOG_VariableController())
749 << tr("Impossible to acceptVariableRequest of a unknown variable id attached "
750 "to a variableRequestId")
751 << varRequestId << varId;
752 }
753 }
754 else {
755 qCCritical(LOG_VariableController())
756 << tr("Impossible to acceptVariableRequest of a unknown variableRequestId")
757 << varRequestId;
758 }
735 }
759
736
760 varRequestIdQueue.pop_front();
737 auto varHandler = itVarHandler->second.get();
761 if (varRequestIdQueue.empty()) {
738 if (varHandler->m_State == VariableRequestHandlerState::OFF) {
762 qCDebug(LOG_VariableController())
739 // TODO log impossible case !!!
763 << tr("TORM Erase REQUEST because it has been accepted") << varId;
764 m_VarIdToVarRequestIdQueueMap.erase(varId);
765 }
766 }
767 else {
768 qCCritical(LOG_VariableController())
769 << tr("Impossible to acceptVariableRequest of a unknown variable id") << varId;
770 }
740 }
771
741
772 return varRequestId;
742 varHandler->m_RunningVarRequest.m_DataSeries = dataSeries;
743 varHandler->m_CanUpdate = true;
744
745 // Element traitΓ©, on a dΓ©jΓ  toutes les donnΓ©es necessaires
746 auto varGroupId = varHandler->m_RunningVarRequest.m_VariableGroupId;
747 qCInfo(LOG_VariableController()) << "Variable::acceptVariableRequest" << varGroupId
748 << m_VarGroupIdToVarIds.size();
749
750 return varHandler->m_RunningVarRequest.m_VariableGroupId;
773 }
751 }
774
752
775 void VariableController::VariableControllerPrivate::updateVariableRequest(QUuid varRequestId)
753 void VariableController::VariableControllerPrivate::updateVariables(QUuid varRequestId)
776 {
754 {
755 qCInfo(LOG_VariableController()) << "VariableControllerPrivate::updateVariables"
756 << QThread::currentThread()->objectName() << varRequestId;
777
757
778 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
758 auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId);
779 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
759 if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) {
760 // TODO LOG cannot update since varGroupdId isn't here anymore
761 qCInfo(LOG_VariableController()) << "Impossible to updateVariables of unknown variables"
762 << varRequestId;
763 return;
764 }
765
766 auto &varIds = varGroupIdToVarIdsIt->second;
767 auto varIdsEnd = varIds.end();
780 bool processVariableUpdate = true;
768 bool processVariableUpdate = true;
781 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
769 qCInfo(LOG_VariableController())
782 for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin();
770 << "VariableControllerPrivate::compute procss for group of size" << varIds.size();
783 (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) && processVariableUpdate;
771 for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd) && processVariableUpdate;
784 ++varIdToVarRequestMapIt) {
772 ++varIdsIt) {
785 processVariableUpdate &= varIdToVarRequestMapIt->second.m_CanUpdate;
773 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
786 qCDebug(LOG_VariableController()) << tr("updateVariableRequest")
774 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
787 << processVariableUpdate;
775 processVariableUpdate &= itVarHandler->second->m_CanUpdate;
776 }
788 }
777 }
789
778
790 if (processVariableUpdate) {
779 if (processVariableUpdate) {
791 for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin();
780 qCInfo(LOG_VariableController()) << "Final update OK for the var request" << varIds.size();
792 varIdToVarRequestMapIt != varIdToVarRequestMap.cend(); ++varIdToVarRequestMapIt) {
781 for (auto varIdsIt = varIds.begin(); varIdsIt != varIdsEnd; ++varIdsIt) {
793 if (auto var = findVariable(varIdToVarRequestMapIt->first)) {
782 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
794 auto &varRequest = varIdToVarRequestMapIt->second;
783 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
784 if (auto var = findVariable(*varIdsIt)) {
785 auto &varRequest = itVarHandler->second->m_RunningVarRequest;
795 var->setRange(varRequest.m_RangeRequested);
786 var->setRange(varRequest.m_RangeRequested);
796 var->setCacheRange(varRequest.m_CacheRangeRequested);
787 var->setCacheRange(varRequest.m_CacheRangeRequested);
797 qCDebug(LOG_VariableController()) << tr("1: onDataProvided")
788 qCInfo(LOG_VariableController()) << tr("1: onDataProvided")
798 << varRequest.m_RangeRequested
789 << varRequest.m_RangeRequested
799 << varRequest.m_CacheRangeRequested;
790 << varRequest.m_CacheRangeRequested;
800 qCDebug(LOG_VariableController()) << tr("2: onDataProvided var points before")
791 qCInfo(LOG_VariableController()) << tr("2: onDataProvided var points before")
801 << var->nbPoints()
792 << var->nbPoints()
802 << varRequest.m_DataSeries->nbPoints();
793 << varRequest.m_DataSeries->nbPoints();
803 var->mergeDataSeries(varRequest.m_DataSeries);
794 var->mergeDataSeries(varRequest.m_DataSeries);
804 qCDebug(LOG_VariableController()) << tr("3: onDataProvided var points after")
795 qCInfo(LOG_VariableController()) << tr("3: onDataProvided var points after")
805 << var->nbPoints();
796 << var->nbPoints();
806
797
807 emit var->updated();
798 emit var->updated();
799 qCCritical(LOG_VariableController()) << tr("Update OK");
808 }
800 }
809 else {
801 else {
810 qCCritical(LOG_VariableController())
802 qCCritical(LOG_VariableController())
811 << tr("Impossible to update data to a null variable");
803 << tr("Impossible to update data to a null variable");
812 }
804 }
813 }
805 }
814 // cleaning varRequestId
815 qCDebug(LOG_VariableController()) << tr("0: erase REQUEST in MAP ?")
816 << m_VarRequestIdToVarIdVarRequestMap.size();
817 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId);
818 qCDebug(LOG_VariableController()) << tr("1: erase REQUEST in MAP ?")
819 << m_VarRequestIdToVarIdVarRequestMap.size();
820 }
821 }
806 }
822 else {
807 updateVariableRequest(varRequestId);
823 qCCritical(LOG_VariableController())
808
824 << tr("Cannot updateVariableRequest for a unknow varRequestId") << varRequestId;
809 // cleaning varRequestId
810 qCInfo(LOG_VariableController()) << tr("m_VarGroupIdToVarIds erase") << varRequestId;
811 m_VarGroupIdToVarIds.erase(varRequestId);
825 }
812 }
826 }
813 }
827
814
828 void VariableController::VariableControllerPrivate::cancelVariableRequest(QUuid varRequestId)
815
816 void VariableController::VariableControllerPrivate::updateVariableRequest(QUuid varRequestId)
829 {
817 {
830 // cleaning varRequestId
818 auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId);
831 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId);
819 if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) {
820 // TODO LOG cannot update variable request since varGroupdId isn't here anymore
821 return;
822 }
832
823
833 for (auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.begin();
824 auto &varIds = varGroupIdToVarIdsIt->second;
834 varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.end();) {
825 auto varIdsEnd = varIds.end();
835 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
826 for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd); ++varIdsIt) {
836 varRequestIdQueue.erase(
827 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
837 std::remove(varRequestIdQueue.begin(), varRequestIdQueue.end(), varRequestId),
828 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
838 varRequestIdQueue.end());
839 if (varRequestIdQueue.empty()) {
840 varIdToVarRequestIdQueueMapIt
841 = m_VarIdToVarRequestIdQueueMap.erase(varIdToVarRequestIdQueueMapIt);
842
829
843 // Recompute if there is any next request based on the removed request.
830 auto varHandler = itVarHandler->second.get();
831 varHandler->m_CanUpdate = false;
832
833
834 switch (varHandler->m_State) {
835 case VariableRequestHandlerState::OFF: {
836 qCCritical(LOG_VariableController())
837 << QObject::tr("Impossible to update a variable with handler in OFF state");
838 } break;
839 case VariableRequestHandlerState::RUNNING: {
840 varHandler->m_State = VariableRequestHandlerState::OFF;
841 varHandler->m_RunningVarRequest = VariableRequest{};
842 break;
843 }
844 case VariableRequestHandlerState::PENDING: {
845 varHandler->m_State = VariableRequestHandlerState::RUNNING;
846 varHandler->m_RunningVarRequest = varHandler->m_PendingVarRequest;
847 varHandler->m_PendingVarRequest = VariableRequest{};
848 auto var = findVariable(itVarHandler->first);
849 executeVarRequest(var, varHandler->m_RunningVarRequest);
850 break;
851 }
852 default:
853 qCCritical(LOG_VariableController())
854 << QObject::tr("Unknown VariableRequestHandlerState");
844 }
855 }
845 else {
846 ++varIdToVarRequestIdQueueMapIt;
847 }
856 }
848 }
857 }
849 }
858 }
850
859
851 void VariableController::VariableControllerPrivate::abortVariableRequest(QUuid varRequestId)
860
861 void VariableController::VariableControllerPrivate::cancelVariableRequest(QUuid varRequestId)
852 {
862 {
853 auto varRequestIdsMap = std::map<QUuid, VariableRequest>{};
863 auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId);
854 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
864 if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) {
855 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
865 // TODO LOG cannot cancel variable request since varGroupdId isn't here anymore
856 varRequestIdsMap = varRequestIdToVarIdVarRequestMapIt->second;
866 return;
857 }
867 }
858 qCCritical(LOG_VariableController())
859 << tr("abortVariableRequest : varRequestId and count of aborting") << varRequestId
860 << varRequestIdsMap.size();
861
868
862 auto nextUuidToRemove = QSet<QUuid>{};
869 auto &varIds = varGroupIdToVarIdsIt->second;
863 auto varIdEnd = varRequestIdsMap.end();
870 auto varIdsEnd = varIds.end();
864 for (auto varIdIt = varRequestIdsMap.begin(); varIdIt != varIdEnd;) {
871 for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd); ++varIdsIt) {
865 auto currentVarId = varIdIt->first;
872 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
866 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(currentVarId);
873 if (itVarHandler == m_VarIdToVarRequestHandler.cend()) {
867 if (varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.cend()) {
868
874
869 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
875 auto varHandler = itVarHandler->second.get();
870
876 varHandler->m_CanUpdate = false;
871 auto varReqIdQueueEnd = varRequestIdQueue.end();
877 varHandler->m_VarId = QUuid{};
872 for (auto varReqIdQueueIt = varRequestIdQueue.begin();
878 switch (varHandler->m_State) {
873 varReqIdQueueIt != varRequestIdQueue.end(); ++varReqIdQueueIt) {
879 case VariableRequestHandlerState::OFF: {
874 if (*varReqIdQueueIt == varRequestId) {
880 qCWarning(LOG_VariableController())
875 auto nextVarRequestIdToRm = varReqIdQueueIt;
881 << QObject::tr("Impossible to cancel a variable with no running request");
876 ++nextVarRequestIdToRm;
882 break;
883 }
884 case VariableRequestHandlerState::RUNNING: {
877
885
878 if (nextVarRequestIdToRm == varReqIdQueueEnd) {
886 if (varHandler->m_RunningVarRequest.m_VariableGroupId == varRequestId) {
879 // The varRequestId is in progress for the current var, let's aborting it.
887 auto var = findVariable(itVarHandler->first);
880 m_VariableAcquisitionWorker->abortProgressRequested(currentVarId);
888 auto varProvider = m_VariableToProviderMap.at(var);
889 if (varProvider != nullptr) {
890 m_VariableAcquisitionWorker->abortProgressRequested(
891 itVarHandler->first);
881 }
892 }
882 else {
893 varHandler->m_RunningVarRequest = VariableRequest{};
883 // There is at least one Request after
884 // let's add only new id to remove
885 if (!nextUuidToRemove.contains(*nextVarRequestIdToRm)) {
886 nextUuidToRemove << *nextVarRequestIdToRm;
887 }
894 }
895 else {
896 // TODO: log Impossible to cancel the running variable request beacause its
897 // varRequestId isn't not the canceled one
888 }
898 }
889
890 qCCritical(LOG_VariableController()) << tr("remove the request : aborting")
891 << currentVarId;
892
893 varReqIdQueueIt = varRequestIdQueue.erase(varReqIdQueueIt);
894 // break is necessary here, we don"t need to iterate on the dequeue anymore and
895 // the iteration is broken because of the erase
896 break;
899 break;
897 }
900 }
901 case VariableRequestHandlerState::PENDING: {
902 if (varHandler->m_RunningVarRequest.m_VariableGroupId == varRequestId) {
903 auto var = findVariable(itVarHandler->first);
904 auto varProvider = m_VariableToProviderMap.at(var);
905 if (varProvider != nullptr) {
906 m_VariableAcquisitionWorker->abortProgressRequested(
907 itVarHandler->first);
898 }
908 }
899
909 varHandler->m_RunningVarRequest = varHandler->m_PendingVarRequest;
900 // beacause the process can modify the map under the iteration, we need to update the
910 executeVarRequest(var, varHandler->m_RunningVarRequest);
901 // iterator only if no erase has been done
911 }
902 if (varRequestIdQueue.empty()) {
912 else if (varHandler->m_PendingVarRequest.m_VariableGroupId == varRequestId) {
903 varIdIt = varRequestIdsMap.erase(varIdIt);
913 varHandler->m_PendingVarRequest = VariableRequest{};
904 }
914 }
905 else {
915 else {
906 ++varIdIt;
916 // TODO: log Impossible to cancel the variable request beacause its
917 // varRequestId isn't not the canceled one
907 }
918 }
919 break;
908 }
920 }
921 default:
922 qCCritical(LOG_VariableController())
923 << QObject::tr("Unknown VariableRequestHandlerState");
909 }
924 }
910
911 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
912 qCCritical(LOG_VariableController()) << tr("remove the varRequestId");
913 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestIdToVarIdVarRequestMapIt);
914 }
925 }
915
916 for (auto nextVarRequestIdToRm : nextUuidToRemove) {
917 qCCritical(LOG_VariableController()) << tr("Cancel automaticaly the next request:")
918 << nextVarRequestIdToRm;
919 abortVariableRequest(nextVarRequestIdToRm);
920 }
926 }
921 }
927 }
922
928
923 SqpRange VariableController::VariableControllerPrivate::getLastRequestedRange(QUuid varId)
929 void VariableController::VariableControllerPrivate::executeVarRequest(std::shared_ptr<Variable> var,
930 VariableRequest &varRequest)
924 {
931 {
925 auto lastRangeRequested = SqpRange{INVALID_RANGE};
932 qCCritical(LOG_VariableController()) << tr("TORM: executeVarRequest");
926 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
933
927 if (varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.cend()) {
934 auto varId = m_VariableToIdentifierMap.at(var);
928 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
935
929 auto varRequestId = varRequestIdQueue.back();
936 auto varCacheRange = var->cacheRange();
930 auto varRequestIdToVarIdVarRequestMapIt
937 auto varCacheRangeRequested = varRequest.m_CacheRangeRequested;
931 = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
938 auto notInCacheRangeList
932 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
939 = Variable::provideNotInCacheRangeList(varCacheRange, varCacheRangeRequested);
933 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
940 // auto inCacheRangeList
934 auto varIdToVarRequestMapIt = varIdToVarRequestMap.find(varId);
941 // = Variable::provideInCacheRangeList(varCacheRange, varCacheRangeRequested);
935 if (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) {
942
936 auto &varRequest = varIdToVarRequestMapIt->second;
943 if (!notInCacheRangeList.empty()) {
937 lastRangeRequested = varRequest.m_RangeRequested;
944
938 }
945 auto varProvider = m_VariableToProviderMap.at(var);
939 else {
946 if (varProvider != nullptr) {
940 qCDebug(LOG_VariableController())
947 qCCritical(LOG_VariableController()) << "executeVarRequest "
941 << tr("Impossible to getLastRequestedRange of a unknown variable id attached "
948 << varRequest.m_RangeRequested
942 "to a variableRequestId")
949 << varRequest.m_CacheRangeRequested;
943 << varRequestId << varId;
950 m_VariableAcquisitionWorker->pushVariableRequest(
944 }
951 varRequest.m_VariableGroupId, varId, varRequest.m_RangeRequested,
952 varRequest.m_CacheRangeRequested,
953 DataProviderParameters{std::move(notInCacheRangeList), var->metadata()},
954 varProvider);
945 }
955 }
946 else {
956 else {
947 qCCritical(LOG_VariableController())
957 qCCritical(LOG_VariableController())
948 << tr("Impossible to getLastRequestedRange of a unknown variableRequestId")
958 << "Impossible to provide data with a null provider";
949 << varRequestId;
950 }
959 }
960
961 // if (!inCacheRangeList.empty()) {
962 // emit q->updateVarDisplaying(var, inCacheRangeList.first());
963 // }
951 }
964 }
952 else {
965 else {
953 qDebug(LOG_VariableController())
966 acceptVariableRequest(varId,
954 << tr("Impossible to getLastRequestedRange of a unknown variable id") << varId;
967 var->dataSeries()->subDataSeries(varRequest.m_CacheRangeRequested));
955 }
968 }
956
957 return lastRangeRequested;
958 }
969 }
General Comments 3
Under Review
author

Auto status change to "Under Review"

Approved
author

Merge lasted acquisition developpement on main Sciqlop branch

You need to be logged in to leave comments. Login now