##// END OF EJS Templates
Update worker for SqpRange compatibility
perrinel -
r530:7c5044f34aa3
parent child
Show More
@@ -1,36 +1,36
1 1 #ifndef SCIQLOP_VARIABLEACQUISITIONWORKER_H
2 2 #define SCIQLOP_VARIABLEACQUISITIONWORKER_H
3 3
4 4 #include "CoreGlobal.h"
5 5
6 6 #include <Data/DataProviderParameters.h>
7 7 #include <QLoggingCategory>
8 8 #include <QObject>
9 9 #include <QUuid>
10 10
11 #include <Data/SqpDateTime.h>
11 #include <Data/SqpRange.h>
12 12
13 13 #include <QLoggingCategory>
14 14
15 15 #include <Common/spimpl.h>
16 16
17 17 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableAcquisitionWorker)
18 18
19 19 class Variable;
20 20 class IDataProvider;
21 21
22 22 /// This class aims to handle all acquisition request
23 23 class SCIQLOP_CORE_EXPORT VariableAcquisitionWorker : public QObject {
24 24 Q_OBJECT
25 25 public:
26 26 explicit VariableAcquisitionWorker(QObject *parent = 0);
27 27
28 28 void pushVariableRequest(QUuid vIdentifier, SqpRange rangeRequest, SqpRange cacheRangeRequested,
29 29 DataProviderParameters parameters, IDataProvider *provider);
30 30
31 31 private:
32 32 class VariableAcquisitionWorkerPrivate;
33 33 spimpl::unique_impl_ptr<VariableAcquisitionWorkerPrivate> impl;
34 34 };
35 35
36 36 #endif // SCIQLOP_VARIABLEACQUISITIONWORKER_H
@@ -1,26 +1,26
1 1 #include "Variable/VariableAcquisitionWorker.h"
2 2
3 3 #include "Variable/Variable.h"
4 4 #include <unordered_map>
5 5
6 6 #include <QThread>
7 7 Q_LOGGING_CATEGORY(LOG_VariableAcquisitionWorker, "VariableAcquisitionWorker")
8 8
9 9 struct VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate {
10 10
11 std::unordered_map<std::shared_ptr<Variable>, QVector<SqpDateTime> >
11 std::unordered_map<std::shared_ptr<Variable>, QVector<SqpRange> >
12 12 m_VariableToSqpDateTimeListMap;
13 13 };
14 14
15 15
16 16 VariableAcquisitionWorker::VariableAcquisitionWorker(QObject *parent)
17 17 : QObject{parent}, impl{spimpl::make_unique_impl<VariableAcquisitionWorkerPrivate>()}
18 18 {
19 19 }
20 20
21 21 void VariableAcquisitionWorker::pushVariableRequest(QUuid vIdentifier, SqpRange rangeRequest,
22 22 SqpRange cacheRangeRequested,
23 23 DataProviderParameters parameters,
24 24 IDataProvider *provider)
25 25 {
26 26 }
@@ -1,276 +1,276
1 1 #include <Variable/Variable.h>
2 2 #include <Variable/VariableCacheController.h>
3 3 #include <Variable/VariableController.h>
4 4 #include <Variable/VariableModel.h>
5 5
6 6 #include <Data/DataProviderParameters.h>
7 7 #include <Data/IDataProvider.h>
8 8 #include <Data/IDataSeries.h>
9 9 #include <Time/TimeController.h>
10 10
11 11 #include <QMutex>
12 12 #include <QThread>
13 13 #include <QUuid>
14 14 #include <QtCore/QItemSelectionModel>
15 15
16 16 #include <unordered_map>
17 17
18 18 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
19 19
20 20 struct VariableController::VariableControllerPrivate {
21 21 explicit VariableControllerPrivate(VariableController *parent)
22 22 : m_WorkingMutex{},
23 23 m_VariableModel{new VariableModel{parent}},
24 24 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
25 25 m_VariableCacheController{std::make_unique<VariableCacheController>()}
26 26 {
27 27 }
28 28
29 QVector<SqpDateTime> provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable,
30 const SqpDateTime &dateTime);
29 QVector<SqpRange> provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable,
30 const SqpRange &dateTime);
31 31
32 32 QMutex m_WorkingMutex;
33 33 /// Variable model. The VariableController has the ownership
34 34 VariableModel *m_VariableModel;
35 35 QItemSelectionModel *m_VariableSelectionModel;
36 36
37 37
38 38 TimeController *m_TimeController{nullptr};
39 39 std::unique_ptr<VariableCacheController> m_VariableCacheController;
40 40
41 41 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
42 42 m_VariableToProviderMap;
43 43 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap;
44 44 };
45 45
46 46 VariableController::VariableController(QObject *parent)
47 47 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
48 48 {
49 49 qCDebug(LOG_VariableController()) << tr("VariableController construction")
50 50 << QThread::currentThread();
51 51
52 52 connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this,
53 53 &VariableController::onAbortProgressRequested);
54 54 }
55 55
56 56 VariableController::~VariableController()
57 57 {
58 58 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
59 59 << QThread::currentThread();
60 60 this->waitForFinish();
61 61 }
62 62
63 63 VariableModel *VariableController::variableModel() noexcept
64 64 {
65 65 return impl->m_VariableModel;
66 66 }
67 67
68 68 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
69 69 {
70 70 return impl->m_VariableSelectionModel;
71 71 }
72 72
73 73 void VariableController::setTimeController(TimeController *timeController) noexcept
74 74 {
75 75 impl->m_TimeController = timeController;
76 76 }
77 77
78 78 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
79 79 {
80 80 if (!variable) {
81 81 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
82 82 return;
83 83 }
84 84
85 85 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
86 86 // make some treatments before the deletion
87 87 emit variableAboutToBeDeleted(variable);
88 88
89 89 // Deletes identifier
90 90 impl->m_VariableToIdentifierMap.erase(variable);
91 91
92 92 // Deletes provider
93 93 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
94 94 qCDebug(LOG_VariableController())
95 95 << tr("Number of providers deleted for variable %1: %2")
96 96 .arg(variable->name(), QString::number(nbProvidersDeleted));
97 97
98 98 // Clears cache
99 99 impl->m_VariableCacheController->clear(variable);
100 100
101 101 // Deletes from model
102 102 impl->m_VariableModel->deleteVariable(variable);
103 103 }
104 104
105 105 void VariableController::deleteVariables(
106 106 const QVector<std::shared_ptr<Variable> > &variables) noexcept
107 107 {
108 108 for (auto variable : qAsConst(variables)) {
109 109 deleteVariable(variable);
110 110 }
111 111 }
112 112
113 113 void VariableController::abortProgress(std::shared_ptr<Variable> variable)
114 114 {
115 115 }
116 116
117 117 void VariableController::createVariable(const QString &name, const QVariantHash &metadata,
118 118 std::shared_ptr<IDataProvider> provider) noexcept
119 119 {
120 120
121 121 if (!impl->m_TimeController) {
122 122 qCCritical(LOG_VariableController())
123 123 << tr("Impossible to create variable: The time controller is null");
124 124 return;
125 125 }
126 126
127 127 auto dateTime = impl->m_TimeController->dateTime();
128 128
129 129 if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime, metadata)) {
130 130 auto identifier = QUuid::createUuid();
131 131
132 132 // store the provider
133 133 impl->m_VariableToProviderMap[newVariable] = provider;
134 134 impl->m_VariableToIdentifierMap[newVariable] = identifier;
135 135
136 136 auto addDateTimeAcquired = [ this, varW = std::weak_ptr<Variable>{newVariable} ](
137 137 QUuid identifier, auto dataSeriesAcquired, auto dateTimeToPutInCache)
138 138 {
139 139 if (auto variable = varW.lock()) {
140 140 auto varIdentifier = impl->m_VariableToIdentifierMap.at(variable);
141 141 if (varIdentifier == identifier) {
142 142 impl->m_VariableCacheController->addDateTime(variable, dateTimeToPutInCache);
143 143 variable->setDataSeries(dataSeriesAcquired);
144 144 emit variable->updated();
145 145 }
146 146 }
147 147 };
148 148
149 149 connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired);
150 150 connect(provider.get(), &IDataProvider::dataProvidedProgress, this,
151 151 &VariableController::onVariableRetrieveDataInProgress);
152 152 this->onRequestDataLoading(newVariable, dateTime);
153 153 }
154 154 }
155 155
156 156 void VariableController::onDateTimeOnSelection(const SqpRange &dateTime)
157 157 {
158 158 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
159 159 << QThread::currentThread()->objectName();
160 160 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
161 161
162 162 for (const auto &selectedRow : qAsConst(selectedRows)) {
163 163 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
164 164 selectedVariable->setDateTime(dateTime);
165 165 this->onRequestDataLoading(selectedVariable, dateTime);
166 166
167 167 // notify that rescale operation has to be done
168 168 emit rangeChanged(selectedVariable, dateTime);
169 169 }
170 170 }
171 171 }
172 172
173 173 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
174 174 {
175 175 auto findReply = [identifier](const auto &entry) { return identifier == entry.second; };
176 176
177 177 auto end = impl->m_VariableToIdentifierMap.cend();
178 178 auto it = std::find_if(impl->m_VariableToIdentifierMap.cbegin(), end, findReply);
179 179 if (it != end) {
180 180 impl->m_VariableModel->setDataProgress(it->first, progress);
181 181 }
182 182 }
183 183
184 184 void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable)
185 185 {
186 186 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAbortProgressRequested"
187 187 << QThread::currentThread()->objectName();
188 188
189 189 auto it = impl->m_VariableToIdentifierMap.find(variable);
190 190 if (it != impl->m_VariableToIdentifierMap.cend()) {
191 191 impl->m_VariableToProviderMap.at(variable)->requestDataAborting(it->second);
192 192 }
193 193 else {
194 194 qCWarning(LOG_VariableController())
195 195 << tr("Aborting progression of inexistant variable detected !!!")
196 196 << QThread::currentThread()->objectName();
197 197 }
198 198 }
199 199
200 200
201 201 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable,
202 202 const SqpRange &dateTime)
203 203 {
204 204 qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading"
205 205 << QThread::currentThread()->objectName();
206 206 // we want to load data of the variable for the dateTime.
207 207 // First we check if the cache contains some of them.
208 208 // For the other, we ask the provider to give them.
209 209 if (variable) {
210 210
211 211 auto dateTimeListNotInCache
212 212 = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime);
213 213
214 214 if (!dateTimeListNotInCache.empty()) {
215 215 // Ask the provider for each data on the dateTimeListNotInCache
216 216 auto identifier = impl->m_VariableToIdentifierMap.at(variable);
217 217 impl->m_VariableToProviderMap.at(variable)->requestDataLoading(
218 218 identifier,
219 219 DataProviderParameters{std::move(dateTimeListNotInCache), variable->metadata()});
220 220 }
221 221 else {
222 222 emit variable->updated();
223 223 }
224 224 }
225 225 else {
226 226 qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null");
227 227 }
228 228 }
229 229
230 230
231 231 void VariableController::initialize()
232 232 {
233 233 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
234 234 impl->m_WorkingMutex.lock();
235 235 qCDebug(LOG_VariableController()) << tr("VariableController init END");
236 236 }
237 237
238 238 void VariableController::finalize()
239 239 {
240 240 impl->m_WorkingMutex.unlock();
241 241 }
242 242
243 243 void VariableController::waitForFinish()
244 244 {
245 245 QMutexLocker locker{&impl->m_WorkingMutex};
246 246 }
247 247
248 248
249 QVector<SqpDateTime> VariableController::VariableControllerPrivate::provideNotInCacheDateTimeList(
250 std::shared_ptr<Variable> variable, const SqpDateTime &dateTime)
249 QVector<SqpRange> VariableController::VariableControllerPrivate::provideNotInCacheDateTimeList(
250 std::shared_ptr<Variable> variable, const SqpRange &dateTime)
251 251 {
252 auto notInCache = QVector<SqpDateTime>{};
252 auto notInCache = QVector<SqpRange>{};
253 253
254 254 if (!variable->contains(dateTime)) {
255 255 auto vDateTime = variable->dateTime();
256 256 if (dateTime.m_TEnd <= vDateTime.m_TStart || dateTime.m_TStart >= vDateTime.m_TEnd) {
257 257 notInCache << dateTime;
258 258 }
259 259 else if (dateTime.m_TStart < vDateTime.m_TStart && dateTime.m_TEnd <= vDateTime.m_TEnd) {
260 notInCache << SqpDateTime{dateTime.m_TStart, vDateTime.m_TStart};
260 notInCache << SqpRange{dateTime.m_TStart, vDateTime.m_TStart};
261 261 }
262 262 else if (dateTime.m_TStart < vDateTime.m_TStart && dateTime.m_TEnd > vDateTime.m_TEnd) {
263 notInCache << SqpDateTime{dateTime.m_TStart, vDateTime.m_TStart}
264 << SqpDateTime{vDateTime.m_TEnd, dateTime.m_TStart};
263 notInCache << SqpRange{dateTime.m_TStart, vDateTime.m_TStart}
264 << SqpRange{vDateTime.m_TEnd, dateTime.m_TStart};
265 265 }
266 266 else if (dateTime.m_TStart < vDateTime.m_TEnd) {
267 notInCache << SqpDateTime{vDateTime.m_TEnd, dateTime.m_TStart};
267 notInCache << SqpRange{vDateTime.m_TEnd, dateTime.m_TStart};
268 268 }
269 269 else {
270 270 qCCritical(LOG_VariableController()) << tr("Detection of unknown case.")
271 271 << QThread::currentThread();
272 272 }
273 273 }
274 274
275 275 return notInCache;
276 276 }
General Comments 0
You need to be logged in to leave comments. Login now