@@ -9,6 +9,7 | |||
|
9 | 9 | #include <Data/DataProviderParameters.h> |
|
10 | 10 | #include <Data/IDataProvider.h> |
|
11 | 11 | #include <Data/IDataSeries.h> |
|
12 | #include <Data/VariableRequest.h> | |
|
12 | 13 | #include <Time/TimeController.h> |
|
13 | 14 | |
|
14 | 15 | #include <QMutex> |
@@ -16,6 +17,7 | |||
|
16 | 17 | #include <QUuid> |
|
17 | 18 | #include <QtCore/QItemSelectionModel> |
|
18 | 19 | |
|
20 | #include <deque> | |
|
19 | 21 | #include <set> |
|
20 | 22 | #include <unordered_map> |
|
21 | 23 | |
@@ -97,7 +99,8 struct VariableController::VariableControllerPrivate { | |||
|
97 | 99 | } |
|
98 | 100 | |
|
99 | 101 | |
|
100 |
void processRequest(std::shared_ptr<Variable> var, const SqpRange &rangeRequested |
|
|
102 | void processRequest(std::shared_ptr<Variable> var, const SqpRange &rangeRequested, | |
|
103 | QUuid varRequestId); | |
|
101 | 104 | |
|
102 | 105 | QVector<SqpRange> provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable, |
|
103 | 106 | const SqpRange &dateTime); |
@@ -108,6 +111,11 struct VariableController::VariableControllerPrivate { | |||
|
108 | 111 | |
|
109 | 112 | void registerProvider(std::shared_ptr<IDataProvider> provider); |
|
110 | 113 | |
|
114 | void storeVariableRequest(QUuid varId, QUuid varRequestId, const VariableRequest &varRequest); | |
|
115 | QUuid acceptVariableRequest(QUuid varId, std::shared_ptr<IDataSeries> dataSeries); | |
|
116 | void updateVariableRequest(QUuid varRequestId); | |
|
117 | void cancelVariableRequest(QUuid varRequestId); | |
|
118 | ||
|
111 | 119 | QMutex m_WorkingMutex; |
|
112 | 120 | /// Variable model. The VariableController has the ownership |
|
113 | 121 | VariableModel *m_VariableModel; |
@@ -128,6 +136,10 struct VariableController::VariableControllerPrivate { | |||
|
128 | 136 | std::map<QUuid, QUuid> m_VariableIdGroupIdMap; |
|
129 | 137 | std::set<std::shared_ptr<IDataProvider> > m_ProviderSet; |
|
130 | 138 | |
|
139 | std::map<QUuid, std::map<QUuid, VariableRequest> > m_VarRequestIdToVarIdVarRequestMap; | |
|
140 | ||
|
141 | std::map<QUuid, std::deque<QUuid> > m_VarIdToVarRequestIdQueueMap; | |
|
142 | ||
|
131 | 143 | |
|
132 | 144 | VariableController *q; |
|
133 | 145 | }; |
@@ -241,7 +253,10 VariableController::createVariable(const QString &name, const QVariantHash &meta | |||
|
241 | 253 | impl->m_VariableToIdentifierMap[newVariable] = identifier; |
|
242 | 254 | |
|
243 | 255 | |
|
244 | impl->processRequest(newVariable, range); | |
|
256 | auto varRequestId = QUuid::createUuid(); | |
|
257 | qCInfo(LOG_VariableController()) << "processRequest for" << name << varRequestId; | |
|
258 | impl->processRequest(newVariable, range, varRequestId); | |
|
259 | impl->updateVariableRequest(varRequestId); | |
|
245 | 260 | |
|
246 | 261 | return newVariable; |
|
247 | 262 | } |
@@ -249,41 +264,32 VariableController::createVariable(const QString &name, const QVariantHash &meta | |||
|
249 | 264 | |
|
250 | 265 | void VariableController::onDateTimeOnSelection(const SqpRange &dateTime) |
|
251 | 266 | { |
|
252 | // TODO check synchronisation | |
|
267 | // TODO check synchronisation and Rescale | |
|
253 | 268 | qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection" |
|
254 | 269 | << QThread::currentThread()->objectName(); |
|
255 | 270 | auto selectedRows = impl->m_VariableSelectionModel->selectedRows(); |
|
271 | auto varRequestId = QUuid::createUuid(); | |
|
256 | 272 | |
|
257 | 273 | for (const auto &selectedRow : qAsConst(selectedRows)) { |
|
258 | 274 | if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) { |
|
259 | 275 | selectedVariable->setRange(dateTime); |
|
260 | impl->processRequest(selectedVariable, dateTime); | |
|
276 | impl->processRequest(selectedVariable, dateTime, varRequestId); | |
|
261 | 277 | |
|
262 | 278 | // notify that rescale operation has to be done |
|
263 | 279 | emit rangeChanged(selectedVariable, dateTime); |
|
264 | 280 | } |
|
265 | 281 | } |
|
282 | impl->updateVariableRequest(varRequestId); | |
|
266 | 283 | } |
|
267 | 284 | |
|
268 | 285 | void VariableController::onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested, |
|
269 | 286 | const SqpRange &cacheRangeRequested, |
|
270 | 287 | QVector<AcquisitionDataPacket> dataAcquired) |
|
271 | 288 | { |
|
272 | if (auto var = impl->findVariable(vIdentifier)) { | |
|
273 | var->setRange(rangeRequested); | |
|
274 | var->setCacheRange(cacheRangeRequested); | |
|
275 | qCDebug(LOG_VariableController()) << tr("1: onDataProvided") << rangeRequested; | |
|
276 | qCDebug(LOG_VariableController()) << tr("2: onDataProvided") << cacheRangeRequested; | |
|
277 | ||
|
278 | 289 |
|
|
279 | qCDebug(LOG_VariableController()) << tr("3: onDataProvided") | |
|
280 | << retrievedDataSeries->range(); | |
|
281 | var->mergeDataSeries(retrievedDataSeries); | |
|
282 | qCDebug(LOG_VariableController()) << tr("4: onDataProvided"); | |
|
283 | emit var->updated(); | |
|
284 | } | |
|
285 | else { | |
|
286 | qCCritical(LOG_VariableController()) << tr("Impossible to provide data to a null variable"); | |
|
290 | auto varRequestId = impl->acceptVariableRequest(vIdentifier, retrievedDataSeries); | |
|
291 | if (!varRequestId.isNull()) { | |
|
292 | impl->updateVariableRequest(varRequestId); | |
|
287 | 293 | } |
|
288 | 294 | } |
|
289 | 295 | |
@@ -369,9 +375,11 void VariableController::onRequestDataLoading(QVector<std::shared_ptr<Variable> | |||
|
369 | 375 | // First we check if the cache contains some of them. |
|
370 | 376 | // For the other, we ask the provider to give them. |
|
371 | 377 | |
|
378 | auto varRequestId = QUuid::createUuid(); | |
|
379 | ||
|
372 | 380 | for (const auto &var : variables) { |
|
373 |
qC |
|
|
374 | impl->processRequest(var, range); | |
|
381 | qCInfo(LOG_VariableController()) << "processRequest for" << var->name() << varRequestId; | |
|
382 | impl->processRequest(var, range, varRequestId); | |
|
375 | 383 | } |
|
376 | 384 | |
|
377 | 385 | if (synchronise) { |
@@ -409,7 +417,7 void VariableController::onRequestDataLoading(QVector<std::shared_ptr<Variable> | |||
|
409 | 417 | << var->name(); |
|
410 | 418 | auto vSyncRangeRequested |
|
411 | 419 | = computeSynchroRangeRequested(var->range(), range, oldRange); |
|
412 | impl->processRequest(var, vSyncRangeRequested); | |
|
420 | impl->processRequest(var, vSyncRangeRequested, varRequestId); | |
|
413 | 421 | } |
|
414 | 422 | else { |
|
415 | 423 | qCCritical(LOG_VariableController()) |
@@ -420,6 +428,8 void VariableController::onRequestDataLoading(QVector<std::shared_ptr<Variable> | |||
|
420 | 428 | } |
|
421 | 429 | } |
|
422 | 430 | } |
|
431 | ||
|
432 | impl->updateVariableRequest(varRequestId); | |
|
423 | 433 | } |
|
424 | 434 | |
|
425 | 435 | |
@@ -463,22 +473,37 AcquisitionZoomType VariableController::getZoomType(const SqpRange &range, const | |||
|
463 | 473 | } |
|
464 | 474 | |
|
465 | 475 | void VariableController::VariableControllerPrivate::processRequest(std::shared_ptr<Variable> var, |
|
466 |
const SqpRange &rangeRequested |
|
|
476 | const SqpRange &rangeRequested, | |
|
477 | QUuid varRequestId) | |
|
467 | 478 | { |
|
468 | 479 | |
|
469 | auto varRangesRequested | |
|
470 | = m_VariableCacheStrategy->computeCacheRange(var->range(), rangeRequested); | |
|
471 | auto notInCacheRangeList = var->provideNotInCacheRangeList(varRangesRequested.second); | |
|
472 | auto inCacheRangeList = var->provideInCacheRangeList(varRangesRequested.second); | |
|
480 | // TODO: protect at | |
|
481 | auto varRequest = VariableRequest{}; | |
|
482 | auto varId = m_VariableToIdentifierMap.at(var); | |
|
483 | ||
|
484 | ||
|
485 | auto varStrategyRangesRequested | |
|
486 | = m_VariableCacheStrategy->computeStrategyRanges(var->range(), rangeRequested); | |
|
487 | auto notInCacheRangeList = var->provideNotInCacheRangeList(varStrategyRangesRequested.second); | |
|
488 | auto inCacheRangeList = var->provideInCacheRangeList(varStrategyRangesRequested.second); | |
|
473 | 489 | |
|
474 | 490 | if (!notInCacheRangeList.empty()) { |
|
475 | auto identifier = m_VariableToIdentifierMap.at(var); | |
|
491 | varRequest.m_RangeRequested = varStrategyRangesRequested.first; | |
|
492 | varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second; | |
|
493 | // store VarRequest | |
|
494 | storeVariableRequest(varId, varRequestId, varRequest); | |
|
495 | ||
|
476 | 496 | auto varProvider = m_VariableToProviderMap.at(var); |
|
477 | 497 | if (varProvider != nullptr) { |
|
478 | m_VariableAcquisitionWorker->pushVariableRequest( | |
|
479 | identifier, varRangesRequested.first, varRangesRequested.second, | |
|
498 | auto varRequestIdCanceled = m_VariableAcquisitionWorker->pushVariableRequest( | |
|
499 | varRequestId, varId, varStrategyRangesRequested.first, | |
|
500 | varStrategyRangesRequested.second, | |
|
480 | 501 | DataProviderParameters{std::move(notInCacheRangeList), var->metadata()}, |
|
481 | 502 | varProvider); |
|
503 | ||
|
504 | if (!varRequestIdCanceled.isNull()) { | |
|
505 | cancelVariableRequest(varRequestIdCanceled); | |
|
506 | } | |
|
482 | 507 | } |
|
483 | 508 | else { |
|
484 | 509 | qCCritical(LOG_VariableController()) |
@@ -490,10 +515,13 void VariableController::VariableControllerPrivate::processRequest(std::shared_p | |||
|
490 | 515 | } |
|
491 | 516 | } |
|
492 | 517 | else { |
|
493 | var->setRange(rangeRequested); | |
|
494 |
var |
|
|
495 | var->setDataSeries(var->dataSeries()->subDataSeries(varRangesRequested.second)); | |
|
496 | emit var->updated(); | |
|
518 | ||
|
519 | varRequest.m_RangeRequested = varStrategyRangesRequested.first; | |
|
520 | varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second; | |
|
521 | // store VarRequest | |
|
522 | storeVariableRequest(varId, varRequestId, varRequest); | |
|
523 | acceptVariableRequest(varId, | |
|
524 | var->dataSeries()->subDataSeries(varStrategyRangesRequested.second)); | |
|
497 | 525 | } |
|
498 | 526 | } |
|
499 | 527 | |
@@ -550,3 +578,157 void VariableController::VariableControllerPrivate::registerProvider( | |||
|
550 | 578 | qCDebug(LOG_VariableController()) << tr("Cannot register provider, it already exists "); |
|
551 | 579 | } |
|
552 | 580 | } |
|
581 | ||
|
582 | void VariableController::VariableControllerPrivate::storeVariableRequest( | |
|
583 | QUuid varId, QUuid varRequestId, const VariableRequest &varRequest) | |
|
584 | { | |
|
585 | // First request for the variable. we can create an entry for it | |
|
586 | auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId); | |
|
587 | if (varIdToVarRequestIdQueueMapIt == m_VarIdToVarRequestIdQueueMap.cend()) { | |
|
588 | auto varRequestIdQueue = std::deque<QUuid>{}; | |
|
589 | qCDebug(LOG_VariableController()) << tr("Store REQUEST in QUEUE"); | |
|
590 | varRequestIdQueue.push_back(varRequestId); | |
|
591 | m_VarIdToVarRequestIdQueueMap.insert(std::make_pair(varId, std::move(varRequestIdQueue))); | |
|
592 | } | |
|
593 | else { | |
|
594 | qCDebug(LOG_VariableController()) << tr("Store REQUEST in EXISTING QUEUE"); | |
|
595 | auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second; | |
|
596 | varRequestIdQueue.push_back(varRequestId); | |
|
597 | } | |
|
598 | ||
|
599 | auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId); | |
|
600 | if (varRequestIdToVarIdVarRequestMapIt == m_VarRequestIdToVarIdVarRequestMap.cend()) { | |
|
601 | auto varIdToVarRequestMap = std::map<QUuid, VariableRequest>{}; | |
|
602 | varIdToVarRequestMap.insert(std::make_pair(varId, varRequest)); | |
|
603 | qCDebug(LOG_VariableController()) << tr("Store REQUESTID in MAP"); | |
|
604 | m_VarRequestIdToVarIdVarRequestMap.insert( | |
|
605 | std::make_pair(varRequestId, std::move(varIdToVarRequestMap))); | |
|
606 | } | |
|
607 | else { | |
|
608 | auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second; | |
|
609 | qCDebug(LOG_VariableController()) << tr("Store REQUESTID in EXISTING MAP"); | |
|
610 | varIdToVarRequestMap.insert(std::make_pair(varId, varRequest)); | |
|
611 | } | |
|
612 | } | |
|
613 | ||
|
614 | QUuid VariableController::VariableControllerPrivate::acceptVariableRequest( | |
|
615 | QUuid varId, std::shared_ptr<IDataSeries> dataSeries) | |
|
616 | { | |
|
617 | QUuid varRequestId; | |
|
618 | auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId); | |
|
619 | if (varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.cend()) { | |
|
620 | auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second; | |
|
621 | varRequestId = varRequestIdQueue.front(); | |
|
622 | auto varRequestIdToVarIdVarRequestMapIt | |
|
623 | = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId); | |
|
624 | if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) { | |
|
625 | auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second; | |
|
626 | auto varIdToVarRequestMapIt = varIdToVarRequestMap.find(varId); | |
|
627 | if (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) { | |
|
628 | qCDebug(LOG_VariableController()) << tr("acceptVariableRequest"); | |
|
629 | auto &varRequest = varIdToVarRequestMapIt->second; | |
|
630 | varRequest.m_DataSeries = dataSeries; | |
|
631 | varRequest.m_CanUpdate = true; | |
|
632 | } | |
|
633 | else { | |
|
634 | qCDebug(LOG_VariableController()) | |
|
635 | << tr("Impossible to acceptVariableRequest of a unknown variable id attached " | |
|
636 | "to a variableRequestId") | |
|
637 | << varRequestId << varId; | |
|
638 | } | |
|
639 | } | |
|
640 | else { | |
|
641 | qCCritical(LOG_VariableController()) | |
|
642 | << tr("Impossible to acceptVariableRequest of a unknown variableRequestId") | |
|
643 | << varRequestId; | |
|
644 | } | |
|
645 | ||
|
646 | qCDebug(LOG_VariableController()) << tr("1: erase REQUEST in QUEUE ?") | |
|
647 | << varRequestIdQueue.size(); | |
|
648 | varRequestIdQueue.pop_front(); | |
|
649 | qCDebug(LOG_VariableController()) << tr("2: erase REQUEST in QUEUE ?") | |
|
650 | << varRequestIdQueue.size(); | |
|
651 | if (varRequestIdQueue.empty()) { | |
|
652 | m_VarIdToVarRequestIdQueueMap.erase(varId); | |
|
653 | } | |
|
654 | } | |
|
655 | else { | |
|
656 | qCCritical(LOG_VariableController()) | |
|
657 | << tr("Impossible to acceptVariableRequest of a unknown variable id") << varId; | |
|
658 | } | |
|
659 | ||
|
660 | return varRequestId; | |
|
661 | } | |
|
662 | ||
|
663 | void VariableController::VariableControllerPrivate::updateVariableRequest(QUuid varRequestId) | |
|
664 | { | |
|
665 | ||
|
666 | auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId); | |
|
667 | if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) { | |
|
668 | bool processVariableUpdate = true; | |
|
669 | auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second; | |
|
670 | for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin(); | |
|
671 | (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) && processVariableUpdate; | |
|
672 | ++varIdToVarRequestMapIt) { | |
|
673 | processVariableUpdate &= varIdToVarRequestMapIt->second.m_CanUpdate; | |
|
674 | qCDebug(LOG_VariableController()) << tr("updateVariableRequest") | |
|
675 | << processVariableUpdate; | |
|
676 | } | |
|
677 | ||
|
678 | if (processVariableUpdate) { | |
|
679 | for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin(); | |
|
680 | varIdToVarRequestMapIt != varIdToVarRequestMap.cend(); ++varIdToVarRequestMapIt) { | |
|
681 | if (auto var = findVariable(varIdToVarRequestMapIt->first)) { | |
|
682 | auto &varRequest = varIdToVarRequestMapIt->second; | |
|
683 | var->setRange(varRequest.m_RangeRequested); | |
|
684 | var->setCacheRange(varRequest.m_CacheRangeRequested); | |
|
685 | qCInfo(LOG_VariableController()) << tr("1: onDataProvided") | |
|
686 | << varRequest.m_RangeRequested; | |
|
687 | qCInfo(LOG_VariableController()) << tr("2: onDataProvided") | |
|
688 | << varRequest.m_CacheRangeRequested; | |
|
689 | var->mergeDataSeries(varRequest.m_DataSeries); | |
|
690 | qCInfo(LOG_VariableController()) << tr("3: onDataProvided") | |
|
691 | << varRequest.m_DataSeries->range(); | |
|
692 | qCDebug(LOG_VariableController()) << tr("4: onDataProvided"); | |
|
693 | emit var->updated(); | |
|
694 | } | |
|
695 | else { | |
|
696 | qCCritical(LOG_VariableController()) | |
|
697 | << tr("Impossible to update data to a null variable"); | |
|
698 | } | |
|
699 | } | |
|
700 | ||
|
701 | // cleaning varRequestId | |
|
702 | qCDebug(LOG_VariableController()) << tr("0: erase REQUEST in MAP ?") | |
|
703 | << m_VarRequestIdToVarIdVarRequestMap.size(); | |
|
704 | m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId); | |
|
705 | qCDebug(LOG_VariableController()) << tr("1: erase REQUEST in MAP ?") | |
|
706 | << m_VarRequestIdToVarIdVarRequestMap.size(); | |
|
707 | } | |
|
708 | } | |
|
709 | else { | |
|
710 | qCCritical(LOG_VariableController()) | |
|
711 | << tr("Cannot updateVariableRequest for a unknow varRequestId") << varRequestId; | |
|
712 | } | |
|
713 | } | |
|
714 | ||
|
715 | void VariableController::VariableControllerPrivate::cancelVariableRequest(QUuid varRequestId) | |
|
716 | { | |
|
717 | // cleaning varRequestId | |
|
718 | m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId); | |
|
719 | ||
|
720 | for (auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.begin(); | |
|
721 | varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.end();) { | |
|
722 | auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second; | |
|
723 | varRequestIdQueue.erase( | |
|
724 | std::remove(varRequestIdQueue.begin(), varRequestIdQueue.end(), varRequestId), | |
|
725 | varRequestIdQueue.end()); | |
|
726 | if (varRequestIdQueue.empty()) { | |
|
727 | varIdToVarRequestIdQueueMapIt | |
|
728 | = m_VarIdToVarRequestIdQueueMap.erase(varIdToVarRequestIdQueueMapIt); | |
|
729 | } | |
|
730 | else { | |
|
731 | ++varIdToVarRequestIdQueueMapIt; | |
|
732 | } | |
|
733 | } | |
|
734 | } |
General Comments 0
You need to be logged in to leave comments.
Login now