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