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

Auto status change to "Under Review"

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