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