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