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

Auto status change to "Under Review"

Approved
author

Status change > Approved

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