##// END OF EJS Templates
Modify variable controller request managing with introduction of current
perrinel -
r825:4e3514f47cab
parent child
Show More
@@ -15,12 +15,10
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)
This diff has been collapsed as it changes many lines, (689 lines changed) Show them Hide them
@@ -77,6 +77,23 SqpRange computeSynchroRangeRequested(const SqpRange &varRange, const SqpRange &
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{},
@@ -113,10 +130,10 struct VariableController::VariableControllerPrivate {
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
@@ -137,9 +154,8 struct VariableController::VariableControllerPrivate {
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 };
@@ -274,20 +290,28 VariableController::createVariable(const QString &name, const QVariantHash &meta
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 }
@@ -319,10 +343,11 void VariableController::onDataProvided(QUuid vIdentifier, const SqpRange &range
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
@@ -342,31 +367,32 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, doub
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)
@@ -459,72 +485,75 void VariableController::desynchronize(std::shared_ptr<Variable> variable,
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
472 for (const auto &var : variables) {
500 if (!synchronise) {
473 qCInfo(LOG_VariableController()) << "processRequest for" << var->name() << varRequestId;
501 auto varIds = std::list<QUuid>{};
474 impl->processRequest(var, range, varRequestId);
502 for (const auto &var : variables) {
503 auto vId = impl->m_VariableToIdentifierMap.at(var);
504 varIds.push_back(vId);
505 }
506 impl->m_VarGroupIdToVarIds.insert(std::make_pair(varRequestId, varIds));
507 for (const auto &var : variables) {
508 qCInfo(LOG_VariableController()) << "processRequest for" << var->name() << varRequestId
509 << varIds.size();
510 impl->processRequest(var, range, varRequestId);
511 }
475 }
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;
519
520 auto vSynchronizationGroup
521 = impl->m_GroupIdToVariableSynchronizationGroupMap.at(groupId);
522 auto vSyncIds = vSynchronizationGroup->getIds();
476
523
477 if (synchronise) {
524 qCInfo(LOG_VariableController()) << "Var in synchro group 1" << groupId;
478 // Get the group ids
525
479 qCDebug(LOG_VariableController())
526 auto varIds = std::list<QUuid>{};
480 << "TORM VariableController::onRequestDataLoading for synchro var ENABLE";
527 for (auto vId : vSyncIds) {
481 auto groupIds = std::set<QUuid>{};
528 varIds.push_back(vId);
482 auto groupIdToOldRangeMap = std::map<QUuid, SqpRange>{};
483 for (const auto &var : variables) {
484 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(var);
485 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
486 auto vId = varToVarIdIt->second;
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 }
529 }
497 }
530 impl->m_VarGroupIdToVarIds.insert(std::make_pair(varRequestId, varIds));
498
531
499 // We assume here all group ids exist
500 for (const auto &gId : groupIds) {
501 auto vSynchronizationGroup = impl->m_GroupIdToVariableSynchronizationGroupMap.at(gId);
502 auto vSyncIds = vSynchronizationGroup->getIds();
503 qCDebug(LOG_VariableController()) << "Var in synchro group ";
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)) {
536 if (var != nullptr) {
509 if (var != nullptr) {
537 qCInfo(LOG_VariableController()) << "processRequest synchro for" << var->name()
510 qCInfo(LOG_VariableController()) << "processRequest synchro for"
538 << varRequestId;
511 << var->name() << varRequestId;
539 auto vSyncRangeRequested
512 auto vSyncRangeRequested = computeSynchroRangeRequested(
540 = variables.contains(var)
513 var->range(), range, groupIdToOldRangeMap.at(gId));
541 ? range
514 qCDebug(LOG_VariableController()) << "synchro RR" << vSyncRangeRequested;
542 : computeSynchroRangeRequested(var->range(), range,
515 impl->processRequest(var, vSyncRangeRequested, varRequestId);
543 variables.first()->range());
516 }
544 qCDebug(LOG_VariableController()) << "synchro RR" << vSyncRangeRequested;
517 else {
545 impl->processRequest(var, vSyncRangeRequested, varRequestId);
518 qCCritical(LOG_VariableController())
546 }
547 else {
548 qCCritical(LOG_VariableController())
519
549
520 << tr("Impossible to synchronize a null variable");
550 << tr("Impossible to synchronize a null variable");
521 }
522 }
551 }
523 }
552 }
524 }
553 }
525 }
554 }
526
555
527 impl->updateVariableRequest(varRequestId);
556 impl->updateVariables(varRequestId);
528 }
557 }
529
558
530
559
@@ -575,64 +604,68 void VariableController::VariableControllerPrivate::processRequest(std::shared_p
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);
579
608 if (itVar == m_VariableToIdentifierMap.cend()) {
580 auto it = m_VariableToIdentifierMap.find(var);
609 qCCritical(LOG_VariableController())
581 if (it != m_VariableToIdentifierMap.cend()) {
610 << tr("Impossible to process request for unknown variable");
582
611 return;
583 auto varId = it->second;
612 }
584
585 auto oldRange = getLastRequestedRange(varId);
586
587 // check for update oldRange to the last request range.
588 if (oldRange == INVALID_RANGE) {
589 oldRange = var->range();
590 }
591
592 auto varStrategyRangesRequested
593 = m_VariableCacheStrategy->computeRange(oldRange, rangeRequested);
594
613
595 // Use commented lines to remove the cache (run time)
614 auto varId = itVar->second;
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
615
603 if (!notInCacheRangeList.empty()) {
616 auto itVarHandler = m_VarIdToVarRequestHandler.find(varId);
604 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
617 if (itVarHandler == m_VarIdToVarRequestHandler.cend()) {
605 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
618 qCCritical(LOG_VariableController())
619 << tr("Impossible to process request for variable with unknown handler");
620 return;
621 }
606
622
607 // store VarRequest
623 auto oldRange = var->range();
608 storeVariableRequest(varId, varRequestId, varRequest);
609
624
610 auto varProvider = m_VariableToProviderMap.at(var);
625 auto varHandler = itVarHandler->second.get();
611 if (varProvider != nullptr) {
612 auto varRequestIdCanceled = m_VariableAcquisitionWorker->pushVariableRequest(
613 varRequestId, varId, varStrategyRangesRequested.first,
614 varStrategyRangesRequested.second,
615 DataProviderParameters{std::move(notInCacheRangeList), var->metadata()},
616 varProvider);
617
626
618 }
627 if (varHandler->m_State != VariableRequestHandlerState::OFF) {
619 else {
628 oldRange = varHandler->m_RunningVarRequest.m_RangeRequested;
620 qCCritical(LOG_VariableController())
629 }
621 << "Impossible to provide data with a null provider";
622 }
623
630
624 if (!inCacheRangeList.empty()) {
631 auto varRequest = VariableRequest{};
625 emit q->updateVarDisplaying(var, inCacheRangeList.first());
632 varRequest.m_VariableGroupId = varRequestId;
626 }
633 auto varStrategyRangesRequested
634 = m_VariableCacheStrategy->computeRange(oldRange, rangeRequested);
635 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
636 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
637
638 switch (varHandler->m_State) {
639 case VariableRequestHandlerState::OFF: {
640 qCCritical(LOG_VariableController()) << tr("Process Request OFF")
641 << varRequest.m_RangeRequested
642 << varRequest.m_CacheRangeRequested;
643 varHandler->m_RunningVarRequest = varRequest;
644 varHandler->m_State = VariableRequestHandlerState::RUNNING;
645 executeVarRequest(var, varRequest);
646 break;
627 }
647 }
628 else {
648 case VariableRequestHandlerState::RUNNING: {
629 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
649 qCCritical(LOG_VariableController()) << tr("Process Request RUNNING")
630 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
650 << varRequest.m_RangeRequested
631 // store VarRequest
651 << varRequest.m_CacheRangeRequested;
632 storeVariableRequest(varId, varRequestId, varRequest);
652 varHandler->m_State = VariableRequestHandlerState::PENDING;
633 acceptVariableRequest(
653 varHandler->m_PendingVarRequest = varRequest;
634 varId, var->dataSeries()->subDataSeries(varStrategyRangesRequested.second));
654 break;
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);
663
664 break;
635 }
665 }
666 default:
667 qCCritical(LOG_VariableController())
668 << QObject::tr("Unknown VariableRequestHandlerState");
636 }
669 }
637 }
670 }
638
671
@@ -693,266 +726,244 void VariableController::VariableControllerPrivate::registerProvider(
693 }
726 }
694 }
727 }
695
728
696 void VariableController::VariableControllerPrivate::storeVariableRequest(
729 QUuid VariableController::VariableControllerPrivate::acceptVariableRequest(
697 QUuid varId, QUuid varRequestId, const VariableRequest &varRequest)
730 QUuid varId, std::shared_ptr<IDataSeries> dataSeries)
698 {
731 {
699 // First request for the variable. we can create an entry for it
732 auto itVarHandler = m_VarIdToVarRequestHandler.find(varId);
700 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
733 if (itVarHandler == m_VarIdToVarRequestHandler.cend()) {
701 if (varIdToVarRequestIdQueueMapIt == m_VarIdToVarRequestIdQueueMap.cend()) {
734 return QUuid();
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 }
735 }
712
736
713 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
737 auto varHandler = itVarHandler->second.get();
714 if (varRequestIdToVarIdVarRequestMapIt == m_VarRequestIdToVarIdVarRequestMap.cend()) {
738 if (varHandler->m_State == VariableRequestHandlerState::OFF) {
715 auto varIdToVarRequestMap = std::map<QUuid, VariableRequest>{};
739 // TODO log impossible case !!!
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 }
740 }
726 }
727
741
728 QUuid VariableController::VariableControllerPrivate::acceptVariableRequest(
742 varHandler->m_RunningVarRequest.m_DataSeries = dataSeries;
729 QUuid varId, std::shared_ptr<IDataSeries> dataSeries)
743 varHandler->m_CanUpdate = true;
730 {
731 QUuid varRequestId;
732 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
733 if (varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.cend()) {
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 }
759
744
760 varRequestIdQueue.pop_front();
745 // Element traitΓ©, on a dΓ©jΓ  toutes les donnΓ©es necessaires
761 if (varRequestIdQueue.empty()) {
746 auto varGroupId = varHandler->m_RunningVarRequest.m_VariableGroupId;
762 qCDebug(LOG_VariableController())
747 qCInfo(LOG_VariableController()) << "Variable::acceptVariableRequest" << varGroupId
763 << tr("TORM Erase REQUEST because it has been accepted") << varId;
748 << m_VarGroupIdToVarIds.size();
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 }
771
749
772 return varRequestId;
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;
757
758 auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId);
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 }
777
765
778 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
766 auto &varIds = varGroupIdToVarIdsIt->second;
779 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
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;
788 }
776 }
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 }
806 }
821 }
807 updateVariableRequest(varRequestId);
822 else {
808
823 qCCritical(LOG_VariableController())
809 // cleaning varRequestId
824 << tr("Cannot updateVariableRequest for a unknow varRequestId") << 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();
844 }
831 varHandler->m_CanUpdate = false;
845 else {
832
846 ++varIdToVarRequestIdQueueMapIt;
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");
855 }
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
862 auto nextUuidToRemove = QSet<QUuid>{};
863 auto varIdEnd = varRequestIdsMap.end();
864 for (auto varIdIt = varRequestIdsMap.begin(); varIdIt != varIdEnd;) {
865 auto currentVarId = varIdIt->first;
866 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(currentVarId);
867 if (varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.cend()) {
868
869 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
870
868
871 auto varReqIdQueueEnd = varRequestIdQueue.end();
869 auto &varIds = varGroupIdToVarIdsIt->second;
872 for (auto varReqIdQueueIt = varRequestIdQueue.begin();
870 auto varIdsEnd = varIds.end();
873 varReqIdQueueIt != varRequestIdQueue.end(); ++varReqIdQueueIt) {
871 for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd); ++varIdsIt) {
874 if (*varReqIdQueueIt == varRequestId) {
872 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
875 auto nextVarRequestIdToRm = varReqIdQueueIt;
873 if (itVarHandler == m_VarIdToVarRequestHandler.cend()) {
876 ++nextVarRequestIdToRm;
877
874
878 if (nextVarRequestIdToRm == varReqIdQueueEnd) {
875 auto varHandler = itVarHandler->second.get();
879 // The varRequestId is in progress for the current var, let's aborting it.
876 varHandler->m_CanUpdate = false;
880 m_VariableAcquisitionWorker->abortProgressRequested(currentVarId);
877 varHandler->m_VarId = QUuid{};
878 switch (varHandler->m_State) {
879 case VariableRequestHandlerState::OFF: {
880 qCWarning(LOG_VariableController())
881 << QObject::tr("Impossible to cancel a variable with no running request");
882 break;
883 }
884 case VariableRequestHandlerState::RUNNING: {
885
886 if (varHandler->m_RunningVarRequest.m_VariableGroupId == varRequestId) {
887 auto var = findVariable(itVarHandler->first);
888 auto varProvider = m_VariableToProviderMap.at(var);
889 if (varProvider != nullptr) {
890 m_VariableAcquisitionWorker->abortProgressRequested(
891 itVarHandler->first);
892 }
893 varHandler->m_RunningVarRequest = VariableRequest{};
881 }
894 }
882 else {
895 else {
883 // There is at least one Request after
896 // TODO: log Impossible to cancel the running variable request beacause its
884 // let's add only new id to remove
897 // varRequestId isn't not the canceled one
885 if (!nextUuidToRemove.contains(*nextVarRequestIdToRm)) {
898 }
886 nextUuidToRemove << *nextVarRequestIdToRm;
899 break;
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);
887 }
908 }
909 varHandler->m_RunningVarRequest = varHandler->m_PendingVarRequest;
910 executeVarRequest(var, varHandler->m_RunningVarRequest);
911 }
912 else if (varHandler->m_PendingVarRequest.m_VariableGroupId == varRequestId) {
913 varHandler->m_PendingVarRequest = VariableRequest{};
914 }
915 else {
916 // TODO: log Impossible to cancel the variable request beacause its
917 // varRequestId isn't not the canceled one
888 }
918 }
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;
919 break;
897 }
920 }
898 }
921 default:
899
922 qCCritical(LOG_VariableController())
900 // beacause the process can modify the map under the iteration, we need to update the
923 << QObject::tr("Unknown VariableRequestHandlerState");
901 // iterator only if no erase has been done
902 if (varRequestIdQueue.empty()) {
903 varIdIt = varRequestIdsMap.erase(varIdIt);
904 }
905 else {
906 ++varIdIt;
907 }
924 }
908 }
925 }
909 }
926 }
910
911 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
912 qCCritical(LOG_VariableController()) << tr("remove the varRequestId");
913 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestIdToVarIdVarRequestMapIt);
914 }
915
916 for (auto nextVarRequestIdToRm : nextUuidToRemove) {
917 qCCritical(LOG_VariableController()) << tr("Cancel automaticaly the next request:")
918 << nextVarRequestIdToRm;
919 abortVariableRequest(nextVarRequestIdToRm);
920 }
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 0
You need to be logged in to leave comments. Login now