##// END OF EJS Templates
Blocks UI update of a variable while it has pending requests
Alexandre Leroux -
r649:a482710ef34e
parent child
Show More
@@ -1,738 +1,743
1 1 #include <Variable/Variable.h>
2 2 #include <Variable/VariableAcquisitionWorker.h>
3 3 #include <Variable/VariableCacheStrategy.h>
4 4 #include <Variable/VariableController.h>
5 5 #include <Variable/VariableModel.h>
6 6 #include <Variable/VariableSynchronizationGroup.h>
7 7
8 8 #include <Data/DataProviderParameters.h>
9 9 #include <Data/IDataProvider.h>
10 10 #include <Data/IDataSeries.h>
11 11 #include <Data/VariableRequest.h>
12 12 #include <Time/TimeController.h>
13 13
14 14 #include <QMutex>
15 15 #include <QThread>
16 16 #include <QUuid>
17 17 #include <QtCore/QItemSelectionModel>
18 18
19 19 #include <deque>
20 20 #include <set>
21 21 #include <unordered_map>
22 22
23 23 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
24 24
25 25 namespace {
26 26
27 27 SqpRange computeSynchroRangeRequested(const SqpRange &varRange, const SqpRange &graphRange,
28 28 const SqpRange &oldGraphRange)
29 29 {
30 30 auto zoomType = VariableController::getZoomType(graphRange, oldGraphRange);
31 31
32 32 auto varRangeRequested = varRange;
33 33 switch (zoomType) {
34 34 case AcquisitionZoomType::ZoomIn: {
35 35 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
36 36 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
37 37 varRangeRequested.m_TStart += deltaLeft;
38 38 varRangeRequested.m_TEnd -= deltaRight;
39 39 break;
40 40 }
41 41
42 42 case AcquisitionZoomType::ZoomOut: {
43 43 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
44 44 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
45 45 varRangeRequested.m_TStart -= deltaLeft;
46 46 varRangeRequested.m_TEnd += deltaRight;
47 47 break;
48 48 }
49 49 case AcquisitionZoomType::PanRight: {
50 50 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
51 51 varRangeRequested.m_TStart += deltaRight;
52 52 varRangeRequested.m_TEnd += deltaRight;
53 53 break;
54 54 }
55 55 case AcquisitionZoomType::PanLeft: {
56 56 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
57 57 varRangeRequested.m_TStart -= deltaLeft;
58 58 varRangeRequested.m_TEnd -= deltaLeft;
59 59 break;
60 60 }
61 61 case AcquisitionZoomType::Unknown: {
62 62 qCCritical(LOG_VariableController())
63 63 << VariableController::tr("Impossible to synchronize: zoom type unknown");
64 64 break;
65 65 }
66 66 default:
67 67 qCCritical(LOG_VariableController()) << VariableController::tr(
68 68 "Impossible to synchronize: zoom type not take into account");
69 69 // No action
70 70 break;
71 71 }
72 72
73 73 return varRangeRequested;
74 74 }
75 75 }
76 76
77 77 struct VariableController::VariableControllerPrivate {
78 78 explicit VariableControllerPrivate(VariableController *parent)
79 79 : m_WorkingMutex{},
80 80 m_VariableModel{new VariableModel{parent}},
81 81 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
82 82 m_VariableCacheStrategy{std::make_unique<VariableCacheStrategy>()},
83 83 m_VariableAcquisitionWorker{std::make_unique<VariableAcquisitionWorker>()},
84 84 q{parent}
85 85 {
86 86
87 87 m_VariableAcquisitionWorker->moveToThread(&m_VariableAcquisitionWorkerThread);
88 88 m_VariableAcquisitionWorkerThread.setObjectName("VariableAcquisitionWorkerThread");
89 89 }
90 90
91 91
92 92 virtual ~VariableControllerPrivate()
93 93 {
94 94 qCDebug(LOG_VariableController()) << tr("VariableControllerPrivate destruction");
95 95 m_VariableAcquisitionWorkerThread.quit();
96 96 m_VariableAcquisitionWorkerThread.wait();
97 97 }
98 98
99 99
100 100 void processRequest(std::shared_ptr<Variable> var, const SqpRange &rangeRequested,
101 101 QUuid varRequestId);
102 102
103 103 QVector<SqpRange> provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable,
104 104 const SqpRange &dateTime);
105 105
106 106 std::shared_ptr<Variable> findVariable(QUuid vIdentifier);
107 107 std::shared_ptr<IDataSeries>
108 108 retrieveDataSeries(const QVector<AcquisitionDataPacket> acqDataPacketVector);
109 109
110 110 void registerProvider(std::shared_ptr<IDataProvider> provider);
111 111
112 112 void storeVariableRequest(QUuid varId, QUuid varRequestId, const VariableRequest &varRequest);
113 113 QUuid acceptVariableRequest(QUuid varId, std::shared_ptr<IDataSeries> dataSeries);
114 114 void updateVariableRequest(QUuid varRequestId);
115 115 void cancelVariableRequest(QUuid varRequestId);
116 116
117 117 QMutex m_WorkingMutex;
118 118 /// Variable model. The VariableController has the ownership
119 119 VariableModel *m_VariableModel;
120 120 QItemSelectionModel *m_VariableSelectionModel;
121 121
122 122
123 123 TimeController *m_TimeController{nullptr};
124 124 std::unique_ptr<VariableCacheStrategy> m_VariableCacheStrategy;
125 125 std::unique_ptr<VariableAcquisitionWorker> m_VariableAcquisitionWorker;
126 126 QThread m_VariableAcquisitionWorkerThread;
127 127
128 128 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
129 129 m_VariableToProviderMap;
130 130 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap;
131 131 std::map<QUuid, std::shared_ptr<VariableSynchronizationGroup> >
132 132 m_GroupIdToVariableSynchronizationGroupMap;
133 133 std::map<QUuid, QUuid> m_VariableIdGroupIdMap;
134 134 std::set<std::shared_ptr<IDataProvider> > m_ProviderSet;
135 135
136 136 std::map<QUuid, std::map<QUuid, VariableRequest> > m_VarRequestIdToVarIdVarRequestMap;
137 137
138 138 std::map<QUuid, std::deque<QUuid> > m_VarIdToVarRequestIdQueueMap;
139 139
140 140
141 141 VariableController *q;
142 142 };
143 143
144 144
145 145 VariableController::VariableController(QObject *parent)
146 146 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
147 147 {
148 148 qCDebug(LOG_VariableController()) << tr("VariableController construction")
149 149 << QThread::currentThread();
150 150
151 151 connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this,
152 152 &VariableController::onAbortProgressRequested);
153 153
154 154 connect(impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::dataProvided, this,
155 155 &VariableController::onDataProvided);
156 156 connect(impl->m_VariableAcquisitionWorker.get(),
157 157 &VariableAcquisitionWorker::variableRequestInProgress, this,
158 158 &VariableController::onVariableRetrieveDataInProgress);
159 159
160 160 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::started,
161 161 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::initialize);
162 162 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::finished,
163 163 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::finalize);
164 164
165 165
166 166 impl->m_VariableAcquisitionWorkerThread.start();
167 167 }
168 168
169 169 VariableController::~VariableController()
170 170 {
171 171 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
172 172 << QThread::currentThread();
173 173 this->waitForFinish();
174 174 }
175 175
176 176 VariableModel *VariableController::variableModel() noexcept
177 177 {
178 178 return impl->m_VariableModel;
179 179 }
180 180
181 181 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
182 182 {
183 183 return impl->m_VariableSelectionModel;
184 184 }
185 185
186 186 void VariableController::setTimeController(TimeController *timeController) noexcept
187 187 {
188 188 impl->m_TimeController = timeController;
189 189 }
190 190
191 191 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
192 192 {
193 193 if (!variable) {
194 194 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
195 195 return;
196 196 }
197 197
198 198 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
199 199 // make some treatments before the deletion
200 200 emit variableAboutToBeDeleted(variable);
201 201
202 202 // Deletes identifier
203 203 impl->m_VariableToIdentifierMap.erase(variable);
204 204
205 205 // Deletes provider
206 206 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
207 207 qCDebug(LOG_VariableController())
208 208 << tr("Number of providers deleted for variable %1: %2")
209 209 .arg(variable->name(), QString::number(nbProvidersDeleted));
210 210
211 211
212 212 // Deletes from model
213 213 impl->m_VariableModel->deleteVariable(variable);
214 214 }
215 215
216 216 void VariableController::deleteVariables(
217 217 const QVector<std::shared_ptr<Variable> > &variables) noexcept
218 218 {
219 219 for (auto variable : qAsConst(variables)) {
220 220 deleteVariable(variable);
221 221 }
222 222 }
223 223
224 224 void VariableController::abortProgress(std::shared_ptr<Variable> variable)
225 225 {
226 226 }
227 227
228 228 std::shared_ptr<Variable>
229 229 VariableController::createVariable(const QString &name, const QVariantHash &metadata,
230 230 std::shared_ptr<IDataProvider> provider) noexcept
231 231 {
232 232 if (!impl->m_TimeController) {
233 233 qCCritical(LOG_VariableController())
234 234 << tr("Impossible to create variable: The time controller is null");
235 235 return nullptr;
236 236 }
237 237
238 238 auto range = impl->m_TimeController->dateTime();
239 239
240 240 if (auto newVariable = impl->m_VariableModel->createVariable(name, range, metadata)) {
241 241 auto identifier = QUuid::createUuid();
242 242
243 243 // store the provider
244 244 impl->registerProvider(provider);
245 245
246 246 // Associate the provider
247 247 impl->m_VariableToProviderMap[newVariable] = provider;
248 248 impl->m_VariableToIdentifierMap[newVariable] = identifier;
249 249
250 250
251 251 auto varRequestId = QUuid::createUuid();
252 252 qCInfo(LOG_VariableController()) << "processRequest for" << name << varRequestId;
253 253 impl->processRequest(newVariable, range, varRequestId);
254 254 impl->updateVariableRequest(varRequestId);
255 255
256 256 return newVariable;
257 257 }
258 258 }
259 259
260 260 void VariableController::onDateTimeOnSelection(const SqpRange &dateTime)
261 261 {
262 262 // TODO check synchronisation and Rescale
263 263 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
264 264 << QThread::currentThread()->objectName();
265 265 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
266 266 auto varRequestId = QUuid::createUuid();
267 267
268 268 for (const auto &selectedRow : qAsConst(selectedRows)) {
269 269 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
270 270 selectedVariable->setRange(dateTime);
271 271 impl->processRequest(selectedVariable, dateTime, varRequestId);
272 272
273 273 // notify that rescale operation has to be done
274 274 emit rangeChanged(selectedVariable, dateTime);
275 275 }
276 276 }
277 277 impl->updateVariableRequest(varRequestId);
278 278 }
279 279
280 280 void VariableController::onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
281 281 const SqpRange &cacheRangeRequested,
282 282 QVector<AcquisitionDataPacket> dataAcquired)
283 283 {
284 284 auto retrievedDataSeries = impl->retrieveDataSeries(dataAcquired);
285 285 auto varRequestId = impl->acceptVariableRequest(vIdentifier, retrievedDataSeries);
286 286 if (!varRequestId.isNull()) {
287 287 impl->updateVariableRequest(varRequestId);
288 288 }
289 289 }
290 290
291 291 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
292 292 {
293 293 if (auto var = impl->findVariable(identifier)) {
294 294 impl->m_VariableModel->setDataProgress(var, progress);
295 295 }
296 296 else {
297 297 qCCritical(LOG_VariableController())
298 298 << tr("Impossible to notify progression of a null variable");
299 299 }
300 300 }
301 301
302 302 void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable)
303 303 {
304 304 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAbortProgressRequested"
305 305 << QThread::currentThread()->objectName();
306 306
307 307 auto it = impl->m_VariableToIdentifierMap.find(variable);
308 308 if (it != impl->m_VariableToIdentifierMap.cend()) {
309 309 impl->m_VariableToProviderMap.at(variable)->requestDataAborting(it->second);
310 310 }
311 311 else {
312 312 qCWarning(LOG_VariableController())
313 313 << tr("Aborting progression of inexistant variable detected !!!")
314 314 << QThread::currentThread()->objectName();
315 315 }
316 316 }
317 317
318 318 void VariableController::onAddSynchronizationGroupId(QUuid synchronizationGroupId)
319 319 {
320 320 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronizationGroupId"
321 321 << QThread::currentThread()->objectName()
322 322 << synchronizationGroupId;
323 323 auto vSynchroGroup = std::make_shared<VariableSynchronizationGroup>();
324 324 impl->m_GroupIdToVariableSynchronizationGroupMap.insert(
325 325 std::make_pair(synchronizationGroupId, vSynchroGroup));
326 326 }
327 327
328 328 void VariableController::onRemoveSynchronizationGroupId(QUuid synchronizationGroupId)
329 329 {
330 330 impl->m_GroupIdToVariableSynchronizationGroupMap.erase(synchronizationGroupId);
331 331 }
332 332
333 333 void VariableController::onAddSynchronized(std::shared_ptr<Variable> variable,
334 334 QUuid synchronizationGroupId)
335 335
336 336 {
337 337 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronized"
338 338 << synchronizationGroupId;
339 339 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(variable);
340 340 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
341 341 auto groupIdToVSGIt
342 342 = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
343 343 if (groupIdToVSGIt != impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
344 344 impl->m_VariableIdGroupIdMap.insert(
345 345 std::make_pair(varToVarIdIt->second, synchronizationGroupId));
346 346 groupIdToVSGIt->second->addVariableId(varToVarIdIt->second);
347 347 }
348 348 else {
349 349 qCCritical(LOG_VariableController())
350 350 << tr("Impossible to synchronize a variable with an unknown sycnhronization group")
351 351 << variable->name();
352 352 }
353 353 }
354 354 else {
355 355 qCCritical(LOG_VariableController())
356 356 << tr("Impossible to synchronize a variable with no identifier") << variable->name();
357 357 }
358 358 }
359 359
360 360
361 361 void VariableController::onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables,
362 362 const SqpRange &range, const SqpRange &oldRange,
363 363 bool synchronise)
364 364 {
365 365 // NOTE: oldRange isn't really necessary since oldRange == variable->range().
366 366
367 367 // we want to load data of the variable for the dateTime.
368 368 // First we check if the cache contains some of them.
369 369 // For the other, we ask the provider to give them.
370 370
371 371 auto varRequestId = QUuid::createUuid();
372 372 qCInfo(LOG_VariableController()) << "VariableController::onRequestDataLoading"
373 373 << QThread::currentThread()->objectName() << varRequestId;
374 374
375 375 for (const auto &var : variables) {
376 376 qCDebug(LOG_VariableController()) << "processRequest for" << var->name() << varRequestId;
377 377 impl->processRequest(var, range, varRequestId);
378 378 }
379 379
380 380 if (synchronise) {
381 381 // Get the group ids
382 382 qCDebug(LOG_VariableController())
383 383 << "TORM VariableController::onRequestDataLoading for synchro var ENABLE";
384 384 auto groupIds = std::set<QUuid>{};
385 385 auto groupIdToOldRangeMap = std::map<QUuid, SqpRange>{};
386 386 for (const auto &var : variables) {
387 387 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(var);
388 388 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
389 389 auto vId = varToVarIdIt->second;
390 390 auto varIdToGroupIdIt = impl->m_VariableIdGroupIdMap.find(vId);
391 391 if (varIdToGroupIdIt != impl->m_VariableIdGroupIdMap.cend()) {
392 392 auto gId = varIdToGroupIdIt->second;
393 393 groupIdToOldRangeMap.insert(std::make_pair(gId, var->range()));
394 394 if (groupIds.find(gId) == groupIds.cend()) {
395 395 qCDebug(LOG_VariableController()) << "Synchro detect group " << gId;
396 396 groupIds.insert(gId);
397 397 }
398 398 }
399 399 }
400 400 }
401 401
402 402 // We assume here all group ids exist
403 403 for (const auto &gId : groupIds) {
404 404 auto vSynchronizationGroup = impl->m_GroupIdToVariableSynchronizationGroupMap.at(gId);
405 405 auto vSyncIds = vSynchronizationGroup->getIds();
406 406 qCDebug(LOG_VariableController()) << "Var in synchro group ";
407 407 for (auto vId : vSyncIds) {
408 408 auto var = impl->findVariable(vId);
409 409
410 410 // Don't process already processed var
411 411 if (!variables.contains(var)) {
412 412 if (var != nullptr) {
413 413 qCDebug(LOG_VariableController()) << "processRequest synchro for"
414 414 << var->name();
415 415 auto vSyncRangeRequested = computeSynchroRangeRequested(
416 416 var->range(), range, groupIdToOldRangeMap.at(gId));
417 417 qCDebug(LOG_VariableController()) << "synchro RR" << vSyncRangeRequested;
418 418 impl->processRequest(var, vSyncRangeRequested, varRequestId);
419 419 }
420 420 else {
421 421 qCCritical(LOG_VariableController())
422 422
423 423 << tr("Impossible to synchronize a null variable");
424 424 }
425 425 }
426 426 }
427 427 }
428 428 }
429 429
430 430 impl->updateVariableRequest(varRequestId);
431 431 }
432 432
433 433
434 434 void VariableController::initialize()
435 435 {
436 436 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
437 437 impl->m_WorkingMutex.lock();
438 438 qCDebug(LOG_VariableController()) << tr("VariableController init END");
439 439 }
440 440
441 441 void VariableController::finalize()
442 442 {
443 443 impl->m_WorkingMutex.unlock();
444 444 }
445 445
446 446 void VariableController::waitForFinish()
447 447 {
448 448 QMutexLocker locker{&impl->m_WorkingMutex};
449 449 }
450 450
451 451 AcquisitionZoomType VariableController::getZoomType(const SqpRange &range, const SqpRange &oldRange)
452 452 {
453 453 // t1.m_TStart <= t2.m_TStart && t2.m_TEnd <= t1.m_TEnd
454 454 auto zoomType = AcquisitionZoomType::Unknown;
455 455 if (range.m_TStart <= oldRange.m_TStart && oldRange.m_TEnd <= range.m_TEnd) {
456 456 zoomType = AcquisitionZoomType::ZoomOut;
457 457 }
458 458 else if (range.m_TStart > oldRange.m_TStart && range.m_TEnd > oldRange.m_TEnd) {
459 459 zoomType = AcquisitionZoomType::PanRight;
460 460 }
461 461 else if (range.m_TStart < oldRange.m_TStart && range.m_TEnd < oldRange.m_TEnd) {
462 462 zoomType = AcquisitionZoomType::PanLeft;
463 463 }
464 464 else if (range.m_TStart > oldRange.m_TStart && oldRange.m_TEnd > range.m_TEnd) {
465 465 zoomType = AcquisitionZoomType::ZoomIn;
466 466 }
467 467 else {
468 468 qCCritical(LOG_VariableController()) << "getZoomType: Unknown type detected";
469 469 }
470 470 return zoomType;
471 471 }
472 472
473 473 void VariableController::VariableControllerPrivate::processRequest(std::shared_ptr<Variable> var,
474 474 const SqpRange &rangeRequested,
475 475 QUuid varRequestId)
476 476 {
477 477
478 478 // TODO: protect at
479 479 auto varRequest = VariableRequest{};
480 480 auto varId = m_VariableToIdentifierMap.at(var);
481 481
482 482 auto varStrategyRangesRequested
483 483 = m_VariableCacheStrategy->computeStrategyRanges(var->range(), rangeRequested);
484 484 auto notInCacheRangeList = var->provideNotInCacheRangeList(varStrategyRangesRequested.second);
485 485 auto inCacheRangeList = var->provideInCacheRangeList(varStrategyRangesRequested.second);
486 486
487 487 if (!notInCacheRangeList.empty()) {
488 488 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
489 489 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
490 490 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM processRequest RR ") << rangeRequested;
491 491 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM processRequest R ")
492 492 << varStrategyRangesRequested.first;
493 493 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM processRequest CR ")
494 494 << varStrategyRangesRequested.second;
495 495 // store VarRequest
496 496 storeVariableRequest(varId, varRequestId, varRequest);
497 497
498 498 auto varProvider = m_VariableToProviderMap.at(var);
499 499 if (varProvider != nullptr) {
500 500 auto varRequestIdCanceled = m_VariableAcquisitionWorker->pushVariableRequest(
501 501 varRequestId, varId, varStrategyRangesRequested.first,
502 502 varStrategyRangesRequested.second,
503 503 DataProviderParameters{std::move(notInCacheRangeList), var->metadata()},
504 504 varProvider);
505 505
506 506 if (!varRequestIdCanceled.isNull()) {
507 507 qCInfo(LOG_VariableAcquisitionWorker()) << tr("varRequestIdCanceled: ")
508 508 << varRequestIdCanceled;
509 509 cancelVariableRequest(varRequestIdCanceled);
510 510 }
511 511 }
512 512 else {
513 513 qCCritical(LOG_VariableController())
514 514 << "Impossible to provide data with a null provider";
515 515 }
516 516
517 517 if (!inCacheRangeList.empty()) {
518 518 emit q->updateVarDisplaying(var, inCacheRangeList.first());
519 519 }
520 520 }
521 521 else {
522 522
523 523 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
524 524 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
525 525 // store VarRequest
526 526 storeVariableRequest(varId, varRequestId, varRequest);
527 527 acceptVariableRequest(varId,
528 528 var->dataSeries()->subDataSeries(varStrategyRangesRequested.second));
529 529 }
530 530 }
531 531
532 532 std::shared_ptr<Variable>
533 533 VariableController::VariableControllerPrivate::findVariable(QUuid vIdentifier)
534 534 {
535 535 std::shared_ptr<Variable> var;
536 536 auto findReply = [vIdentifier](const auto &entry) { return vIdentifier == entry.second; };
537 537
538 538 auto end = m_VariableToIdentifierMap.cend();
539 539 auto it = std::find_if(m_VariableToIdentifierMap.cbegin(), end, findReply);
540 540 if (it != end) {
541 541 var = it->first;
542 542 }
543 543 else {
544 544 qCCritical(LOG_VariableController())
545 545 << tr("Impossible to find the variable with the identifier: ") << vIdentifier;
546 546 }
547 547
548 548 return var;
549 549 }
550 550
551 551 std::shared_ptr<IDataSeries> VariableController::VariableControllerPrivate::retrieveDataSeries(
552 552 const QVector<AcquisitionDataPacket> acqDataPacketVector)
553 553 {
554 554 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size")
555 555 << acqDataPacketVector.size();
556 556 std::shared_ptr<IDataSeries> dataSeries;
557 557 if (!acqDataPacketVector.isEmpty()) {
558 558 dataSeries = acqDataPacketVector[0].m_DateSeries;
559 559 for (int i = 1; i < acqDataPacketVector.size(); ++i) {
560 560 dataSeries->merge(acqDataPacketVector[i].m_DateSeries.get());
561 561 }
562 562 }
563 563 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size END")
564 564 << acqDataPacketVector.size();
565 565 return dataSeries;
566 566 }
567 567
568 568 void VariableController::VariableControllerPrivate::registerProvider(
569 569 std::shared_ptr<IDataProvider> provider)
570 570 {
571 571 if (m_ProviderSet.find(provider) == m_ProviderSet.end()) {
572 572 qCDebug(LOG_VariableController()) << tr("Registering of a new provider")
573 573 << provider->objectName();
574 574 m_ProviderSet.insert(provider);
575 575 connect(provider.get(), &IDataProvider::dataProvided, m_VariableAcquisitionWorker.get(),
576 576 &VariableAcquisitionWorker::onVariableDataAcquired);
577 577 connect(provider.get(), &IDataProvider::dataProvidedProgress,
578 578 m_VariableAcquisitionWorker.get(),
579 579 &VariableAcquisitionWorker::onVariableRetrieveDataInProgress);
580 580 }
581 581 else {
582 582 qCDebug(LOG_VariableController()) << tr("Cannot register provider, it already exists ");
583 583 }
584 584 }
585 585
586 586 void VariableController::VariableControllerPrivate::storeVariableRequest(
587 587 QUuid varId, QUuid varRequestId, const VariableRequest &varRequest)
588 588 {
589 589 // First request for the variable. we can create an entry for it
590 590 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
591 591 if (varIdToVarRequestIdQueueMapIt == m_VarIdToVarRequestIdQueueMap.cend()) {
592 592 auto varRequestIdQueue = std::deque<QUuid>{};
593 593 qCDebug(LOG_VariableController()) << tr("Store REQUEST in QUEUE");
594 594 varRequestIdQueue.push_back(varRequestId);
595 595 m_VarIdToVarRequestIdQueueMap.insert(std::make_pair(varId, std::move(varRequestIdQueue)));
596 596 }
597 597 else {
598 598 qCDebug(LOG_VariableController()) << tr("Store REQUEST in EXISTING QUEUE");
599 599 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
600 600 varRequestIdQueue.push_back(varRequestId);
601 601 }
602 602
603 603 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
604 604 if (varRequestIdToVarIdVarRequestMapIt == m_VarRequestIdToVarIdVarRequestMap.cend()) {
605 605 auto varIdToVarRequestMap = std::map<QUuid, VariableRequest>{};
606 606 varIdToVarRequestMap.insert(std::make_pair(varId, varRequest));
607 607 qCDebug(LOG_VariableController()) << tr("Store REQUESTID in MAP");
608 608 m_VarRequestIdToVarIdVarRequestMap.insert(
609 609 std::make_pair(varRequestId, std::move(varIdToVarRequestMap)));
610 610 }
611 611 else {
612 612 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
613 613 qCDebug(LOG_VariableController()) << tr("Store REQUESTID in EXISTING MAP");
614 614 varIdToVarRequestMap.insert(std::make_pair(varId, varRequest));
615 615 }
616 616 }
617 617
618 618 QUuid VariableController::VariableControllerPrivate::acceptVariableRequest(
619 619 QUuid varId, std::shared_ptr<IDataSeries> dataSeries)
620 620 {
621 621 QUuid varRequestId;
622 622 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
623 623 if (varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.cend()) {
624 624 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
625 625 varRequestId = varRequestIdQueue.front();
626 626 auto varRequestIdToVarIdVarRequestMapIt
627 627 = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
628 628 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
629 629 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
630 630 auto varIdToVarRequestMapIt = varIdToVarRequestMap.find(varId);
631 631 if (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) {
632 632 qCDebug(LOG_VariableController()) << tr("acceptVariableRequest");
633 633 auto &varRequest = varIdToVarRequestMapIt->second;
634 634 varRequest.m_DataSeries = dataSeries;
635 635 varRequest.m_CanUpdate = true;
636 636 }
637 637 else {
638 638 qCDebug(LOG_VariableController())
639 639 << tr("Impossible to acceptVariableRequest of a unknown variable id attached "
640 640 "to a variableRequestId")
641 641 << varRequestId << varId;
642 642 }
643 643 }
644 644 else {
645 645 qCCritical(LOG_VariableController())
646 646 << tr("Impossible to acceptVariableRequest of a unknown variableRequestId")
647 647 << varRequestId;
648 648 }
649 649
650 650 qCDebug(LOG_VariableController()) << tr("1: erase REQUEST in QUEUE ?")
651 651 << varRequestIdQueue.size();
652 652 varRequestIdQueue.pop_front();
653 653 qCDebug(LOG_VariableController()) << tr("2: erase REQUEST in QUEUE ?")
654 654 << varRequestIdQueue.size();
655 655 if (varRequestIdQueue.empty()) {
656 656 m_VarIdToVarRequestIdQueueMap.erase(varId);
657 657 }
658 658 }
659 659 else {
660 660 qCCritical(LOG_VariableController())
661 661 << tr("Impossible to acceptVariableRequest of a unknown variable id") << varId;
662 662 }
663 663
664 664 return varRequestId;
665 665 }
666 666
667 667 void VariableController::VariableControllerPrivate::updateVariableRequest(QUuid varRequestId)
668 668 {
669 669
670 670 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
671 671 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
672 672 bool processVariableUpdate = true;
673 673 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
674 674 for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin();
675 675 (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) && processVariableUpdate;
676 676 ++varIdToVarRequestMapIt) {
677 677 processVariableUpdate &= varIdToVarRequestMapIt->second.m_CanUpdate;
678 678 qCDebug(LOG_VariableController()) << tr("updateVariableRequest")
679 679 << processVariableUpdate;
680 680 }
681 681
682 682 if (processVariableUpdate) {
683 683 for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin();
684 684 varIdToVarRequestMapIt != varIdToVarRequestMap.cend(); ++varIdToVarRequestMapIt) {
685 685 if (auto var = findVariable(varIdToVarRequestMapIt->first)) {
686 686 auto &varRequest = varIdToVarRequestMapIt->second;
687 687 var->setRange(varRequest.m_RangeRequested);
688 688 var->setCacheRange(varRequest.m_CacheRangeRequested);
689 689 qCDebug(LOG_VariableController()) << tr("1: onDataProvided")
690 690 << varRequest.m_RangeRequested;
691 691 qCDebug(LOG_VariableController()) << tr("2: onDataProvided")
692 692 << varRequest.m_CacheRangeRequested;
693 693 var->mergeDataSeries(varRequest.m_DataSeries);
694 694 qCDebug(LOG_VariableController()) << tr("3: onDataProvided")
695 695 << varRequest.m_DataSeries->range();
696 696 qCDebug(LOG_VariableController()) << tr("4: onDataProvided");
697 emit var->updated();
697
698 /// @todo MPL: confirm
699 // Variable update is notified only if there is no pending request for it
700 if (m_VarIdToVarRequestIdQueueMap.count(varIdToVarRequestMapIt->first) == 0) {
701 emit var->updated();
702 }
698 703 }
699 704 else {
700 705 qCCritical(LOG_VariableController())
701 706 << tr("Impossible to update data to a null variable");
702 707 }
703 708 }
704 709
705 710 // cleaning varRequestId
706 711 qCDebug(LOG_VariableController()) << tr("0: erase REQUEST in MAP ?")
707 712 << m_VarRequestIdToVarIdVarRequestMap.size();
708 713 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId);
709 714 qCDebug(LOG_VariableController()) << tr("1: erase REQUEST in MAP ?")
710 715 << m_VarRequestIdToVarIdVarRequestMap.size();
711 716 }
712 717 }
713 718 else {
714 719 qCCritical(LOG_VariableController())
715 720 << tr("Cannot updateVariableRequest for a unknow varRequestId") << varRequestId;
716 721 }
717 722 }
718 723
719 724 void VariableController::VariableControllerPrivate::cancelVariableRequest(QUuid varRequestId)
720 725 {
721 726 // cleaning varRequestId
722 727 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId);
723 728
724 729 for (auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.begin();
725 730 varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.end();) {
726 731 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
727 732 varRequestIdQueue.erase(
728 733 std::remove(varRequestIdQueue.begin(), varRequestIdQueue.end(), varRequestId),
729 734 varRequestIdQueue.end());
730 735 if (varRequestIdQueue.empty()) {
731 736 varIdToVarRequestIdQueueMapIt
732 737 = m_VarIdToVarRequestIdQueueMap.erase(varIdToVarRequestIdQueueMapIt);
733 738 }
734 739 else {
735 740 ++varIdToVarRequestIdQueueMapIt;
736 741 }
737 742 }
738 743 }
General Comments 0
You need to be logged in to leave comments. Login now