##// END OF EJS Templates
Removes unused method in DataSeries
Alexandre Leroux -
r798:1d29cb0c0563
parent child
Show More
@@ -1,400 +1,391
1 1 #ifndef SCIQLOP_DATASERIES_H
2 2 #define SCIQLOP_DATASERIES_H
3 3
4 4 #include "CoreGlobal.h"
5 5
6 6 #include <Common/SortUtils.h>
7 7
8 8 #include <Data/ArrayData.h>
9 9 #include <Data/DataSeriesMergeHelper.h>
10 10 #include <Data/IDataSeries.h>
11 11
12 12 #include <QLoggingCategory>
13 13 #include <QReadLocker>
14 14 #include <QReadWriteLock>
15 15 #include <memory>
16 16
17 17 // We don't use the Qt macro since the log is used in the header file, which causes multiple log
18 18 // definitions with inheritance. Inline method is used instead
19 19 inline const QLoggingCategory &LOG_DataSeries()
20 20 {
21 21 static const QLoggingCategory category{"DataSeries"};
22 22 return category;
23 23 }
24 24
25 25 template <int Dim>
26 26 class DataSeries;
27 27
28 28 namespace dataseries_detail {
29 29
30 30 template <int Dim, bool IsConst>
31 31 class IteratorValue : public DataSeriesIteratorValue::Impl {
32 32 public:
33 33 friend class DataSeries<Dim>;
34 34
35 35 template <bool IC = IsConst, typename = std::enable_if_t<IC == false> >
36 36 explicit IteratorValue(DataSeries<Dim> &dataSeries, bool begin)
37 37 : m_XIt(begin ? dataSeries.xAxisData()->begin() : dataSeries.xAxisData()->end()),
38 38 m_ValuesIt(begin ? dataSeries.valuesData()->begin() : dataSeries.valuesData()->end())
39 39 {
40 40 }
41 41
42 42 template <bool IC = IsConst, typename = std::enable_if_t<IC == true> >
43 43 explicit IteratorValue(const DataSeries<Dim> &dataSeries, bool begin)
44 44 : m_XIt(begin ? dataSeries.xAxisData()->cbegin() : dataSeries.xAxisData()->cend()),
45 45 m_ValuesIt(begin ? dataSeries.valuesData()->cbegin()
46 46 : dataSeries.valuesData()->cend())
47 47 {
48 48 }
49 49
50 50 IteratorValue(const IteratorValue &other) = default;
51 51
52 52 std::unique_ptr<DataSeriesIteratorValue::Impl> clone() const override
53 53 {
54 54 return std::make_unique<IteratorValue<Dim, IsConst> >(*this);
55 55 }
56 56
57 57 int distance(const DataSeriesIteratorValue::Impl &other) const override try {
58 58 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
59 59 return m_XIt->distance(*otherImpl.m_XIt);
60 60 }
61 61 catch (const std::bad_cast &) {
62 62 return 0;
63 63 }
64 64
65 65 bool equals(const DataSeriesIteratorValue::Impl &other) const override try {
66 66 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
67 67 return std::tie(m_XIt, m_ValuesIt) == std::tie(otherImpl.m_XIt, otherImpl.m_ValuesIt);
68 68 }
69 69 catch (const std::bad_cast &) {
70 70 return false;
71 71 }
72 72
73 73 bool lowerThan(const DataSeriesIteratorValue::Impl &other) const override try {
74 74 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
75 75 return m_XIt->lowerThan(*otherImpl.m_XIt);
76 76 }
77 77 catch (const std::bad_cast &) {
78 78 return false;
79 79 }
80 80
81 81 std::unique_ptr<DataSeriesIteratorValue::Impl> advance(int offset) const override
82 82 {
83 83 auto result = clone();
84 84 result->next(offset);
85 85 return result;
86 86 }
87 87
88 88 void next(int offset) override
89 89 {
90 90 m_XIt->next(offset);
91 91 m_ValuesIt->next(offset);
92 92 }
93 93
94 94 void prev() override
95 95 {
96 96 --m_XIt;
97 97 --m_ValuesIt;
98 98 }
99 99
100 100 double x() const override { return m_XIt->at(0); }
101 101 double value() const override { return m_ValuesIt->at(0); }
102 102 double value(int componentIndex) const override { return m_ValuesIt->at(componentIndex); }
103 103 double minValue() const override { return m_ValuesIt->min(); }
104 104 double maxValue() const override { return m_ValuesIt->max(); }
105 105 QVector<double> values() const override { return m_ValuesIt->values(); }
106 106
107 107 void swap(DataSeriesIteratorValue::Impl &other) override
108 108 {
109 109 auto &otherImpl = dynamic_cast<IteratorValue &>(other);
110 110 m_XIt->impl()->swap(*otherImpl.m_XIt->impl());
111 111 m_ValuesIt->impl()->swap(*otherImpl.m_ValuesIt->impl());
112 112 }
113 113
114 114 private:
115 115 ArrayDataIterator m_XIt;
116 116 ArrayDataIterator m_ValuesIt;
117 117 };
118 118 } // namespace dataseries_detail
119 119
120 120 /**
121 121 * @brief The DataSeries class is the base (abstract) implementation of IDataSeries.
122 122 *
123 123 * It proposes to set a dimension for the values ​​data.
124 124 *
125 125 * A DataSeries is always sorted on its x-axis data.
126 126 *
127 127 * @tparam Dim The dimension of the values data
128 128 *
129 129 */
130 130 template <int Dim>
131 131 class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries {
132 132 friend class DataSeriesMergeHelper;
133 133
134 134 public:
135 135 /// @sa IDataSeries::xAxisData()
136 136 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
137 137 const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; }
138 138
139 139 /// @sa IDataSeries::xAxisUnit()
140 140 Unit xAxisUnit() const override { return m_XAxisUnit; }
141 141
142 142 /// @return the values dataset
143 143 std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; }
144 144 const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; }
145 145
146 146 /// @sa IDataSeries::valuesUnit()
147 147 Unit valuesUnit() const override { return m_ValuesUnit; }
148 148
149 149 int nbPoints() const override { return m_XAxisData->totalSize() + m_ValuesData->totalSize(); }
150 150
151 SqpRange range() const override
152 {
153 if (!m_XAxisData->cdata().empty()) {
154 return SqpRange{m_XAxisData->cdata().front(), m_XAxisData->cdata().back()};
155 }
156
157 return SqpRange{};
158 }
159
160 151 void clear()
161 152 {
162 153 m_XAxisData->clear();
163 154 m_ValuesData->clear();
164 155 }
165 156
166 157 bool isEmpty() const noexcept { return m_XAxisData->size() == 0; }
167 158
168 159 /// Merges into the data series an other data series
169 160 /// @remarks the data series to merge with is cleared after the operation
170 161 void merge(IDataSeries *dataSeries) override
171 162 {
172 163 dataSeries->lockWrite();
173 164 lockWrite();
174 165
175 166 if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
176 167 DataSeriesMergeHelper::merge(*other, *this);
177 168 }
178 169 else {
179 170 qCWarning(LOG_DataSeries())
180 171 << QObject::tr("Detection of a type of IDataSeries we cannot merge with !");
181 172 }
182 173 unlock();
183 174 dataSeries->unlock();
184 175 }
185 176
186 177 void purge(double min, double max) override
187 178 {
188 179 // Nothing to purge if series is empty
189 180 if (isEmpty()) {
190 181 return;
191 182 }
192 183
193 184 if (min > max) {
194 185 std::swap(min, max);
195 186 }
196 187
197 188 // Nothing to purge if series min/max are inside purge range
198 189 auto xMin = cbegin()->x();
199 190 auto xMax = (--cend())->x();
200 191 if (xMin >= min && xMax <= max) {
201 192 return;
202 193 }
203 194
204 195 auto lowerIt = std::lower_bound(
205 196 begin(), end(), min, [](const auto &it, const auto &val) { return it.x() < val; });
206 197 erase(begin(), lowerIt);
207 198 auto upperIt = std::upper_bound(
208 199 begin(), end(), max, [](const auto &val, const auto &it) { return val < it.x(); });
209 200 erase(upperIt, end());
210 201 }
211 202
212 203 // ///////// //
213 204 // Iterators //
214 205 // ///////// //
215 206
216 207 DataSeriesIterator begin() override
217 208 {
218 209 return DataSeriesIterator{DataSeriesIteratorValue{
219 210 std::make_unique<dataseries_detail::IteratorValue<Dim, false> >(*this, true)}};
220 211 }
221 212
222 213 DataSeriesIterator end() override
223 214 {
224 215 return DataSeriesIterator{DataSeriesIteratorValue{
225 216 std::make_unique<dataseries_detail::IteratorValue<Dim, false> >(*this, false)}};
226 217 }
227 218
228 219 DataSeriesIterator cbegin() const override
229 220 {
230 221 return DataSeriesIterator{DataSeriesIteratorValue{
231 222 std::make_unique<dataseries_detail::IteratorValue<Dim, true> >(*this, true)}};
232 223 }
233 224
234 225 DataSeriesIterator cend() const override
235 226 {
236 227 return DataSeriesIterator{DataSeriesIteratorValue{
237 228 std::make_unique<dataseries_detail::IteratorValue<Dim, true> >(*this, false)}};
238 229 }
239 230
240 231 void erase(DataSeriesIterator first, DataSeriesIterator last)
241 232 {
242 233 auto firstImpl
243 234 = dynamic_cast<dataseries_detail::IteratorValue<Dim, false> *>(first->impl());
244 235 auto lastImpl = dynamic_cast<dataseries_detail::IteratorValue<Dim, false> *>(last->impl());
245 236
246 237 if (firstImpl && lastImpl) {
247 238 m_XAxisData->erase(firstImpl->m_XIt, lastImpl->m_XIt);
248 239 m_ValuesData->erase(firstImpl->m_ValuesIt, lastImpl->m_ValuesIt);
249 240 }
250 241 }
251 242
252 243 void insert(DataSeriesIterator first, DataSeriesIterator last, bool prepend = false)
253 244 {
254 245 auto firstImpl = dynamic_cast<dataseries_detail::IteratorValue<Dim, true> *>(first->impl());
255 246 auto lastImpl = dynamic_cast<dataseries_detail::IteratorValue<Dim, true> *>(last->impl());
256 247
257 248 if (firstImpl && lastImpl) {
258 249 m_XAxisData->insert(firstImpl->m_XIt, lastImpl->m_XIt, prepend);
259 250 m_ValuesData->insert(firstImpl->m_ValuesIt, lastImpl->m_ValuesIt, prepend);
260 251 }
261 252 }
262 253
263 254 /// @sa IDataSeries::minXAxisData()
264 255 DataSeriesIterator minXAxisData(double minXAxisData) const override
265 256 {
266 257 return std::lower_bound(
267 258 cbegin(), cend(), minXAxisData,
268 259 [](const auto &itValue, const auto &value) { return itValue.x() < value; });
269 260 }
270 261
271 262 /// @sa IDataSeries::maxXAxisData()
272 263 DataSeriesIterator maxXAxisData(double maxXAxisData) const override
273 264 {
274 265 // Gets the first element that greater than max value
275 266 auto it = std::upper_bound(
276 267 cbegin(), cend(), maxXAxisData,
277 268 [](const auto &value, const auto &itValue) { return value < itValue.x(); });
278 269
279 270 return it == cbegin() ? cend() : --it;
280 271 }
281 272
282 273 std::pair<DataSeriesIterator, DataSeriesIterator> xAxisRange(double minXAxisData,
283 274 double maxXAxisData) const override
284 275 {
285 276 if (minXAxisData > maxXAxisData) {
286 277 std::swap(minXAxisData, maxXAxisData);
287 278 }
288 279
289 280 auto begin = cbegin();
290 281 auto end = cend();
291 282
292 283 auto lowerIt = std::lower_bound(
293 284 begin, end, minXAxisData,
294 285 [](const auto &itValue, const auto &value) { return itValue.x() < value; });
295 286 auto upperIt = std::upper_bound(
296 287 lowerIt, end, maxXAxisData,
297 288 [](const auto &value, const auto &itValue) { return value < itValue.x(); });
298 289
299 290 return std::make_pair(lowerIt, upperIt);
300 291 }
301 292
302 293 std::pair<DataSeriesIterator, DataSeriesIterator>
303 294 valuesBounds(double minXAxisData, double maxXAxisData) const override
304 295 {
305 296 // Places iterators to the correct x-axis range
306 297 auto xAxisRangeIts = xAxisRange(minXAxisData, maxXAxisData);
307 298
308 299 // Returns end iterators if the range is empty
309 300 if (xAxisRangeIts.first == xAxisRangeIts.second) {
310 301 return std::make_pair(cend(), cend());
311 302 }
312 303
313 304 // Gets the iterator on the min of all values data
314 305 auto minIt = std::min_element(
315 306 xAxisRangeIts.first, xAxisRangeIts.second, [](const auto &it1, const auto &it2) {
316 307 return SortUtils::minCompareWithNaN(it1.minValue(), it2.minValue());
317 308 });
318 309
319 310 // Gets the iterator on the max of all values data
320 311 auto maxIt = std::max_element(
321 312 xAxisRangeIts.first, xAxisRangeIts.second, [](const auto &it1, const auto &it2) {
322 313 return SortUtils::maxCompareWithNaN(it1.maxValue(), it2.maxValue());
323 314 });
324 315
325 316 return std::make_pair(minIt, maxIt);
326 317 }
327 318
328 319 // /////// //
329 320 // Mutexes //
330 321 // /////// //
331 322
332 323 virtual void lockRead() { m_Lock.lockForRead(); }
333 324 virtual void lockWrite() { m_Lock.lockForWrite(); }
334 325 virtual void unlock() { m_Lock.unlock(); }
335 326
336 327 protected:
337 328 /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a
338 329 /// DataSeries with no values will be created.
339 330 /// @remarks data series is automatically sorted on its x-axis data
340 331 explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit,
341 332 std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit)
342 333 : m_XAxisData{xAxisData},
343 334 m_XAxisUnit{xAxisUnit},
344 335 m_ValuesData{valuesData},
345 336 m_ValuesUnit{valuesUnit}
346 337 {
347 338 if (m_XAxisData->size() != m_ValuesData->size()) {
348 339 clear();
349 340 }
350 341
351 342 // Sorts data if it's not the case
352 343 const auto &xAxisCData = m_XAxisData->cdata();
353 344 if (!std::is_sorted(xAxisCData.cbegin(), xAxisCData.cend())) {
354 345 sort();
355 346 }
356 347 }
357 348
358 349 /// Copy ctor
359 350 explicit DataSeries(const DataSeries<Dim> &other)
360 351 : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)},
361 352 m_XAxisUnit{other.m_XAxisUnit},
362 353 m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)},
363 354 m_ValuesUnit{other.m_ValuesUnit}
364 355 {
365 356 // Since a series is ordered from its construction and is always ordered, it is not
366 357 // necessary to call the sort method here ('other' is sorted)
367 358 }
368 359
369 360 /// Assignment operator
370 361 template <int D>
371 362 DataSeries &operator=(DataSeries<D> other)
372 363 {
373 364 std::swap(m_XAxisData, other.m_XAxisData);
374 365 std::swap(m_XAxisUnit, other.m_XAxisUnit);
375 366 std::swap(m_ValuesData, other.m_ValuesData);
376 367 std::swap(m_ValuesUnit, other.m_ValuesUnit);
377 368
378 369 return *this;
379 370 }
380 371
381 372 private:
382 373 /**
383 374 * Sorts data series on its x-axis data
384 375 */
385 376 void sort() noexcept
386 377 {
387 378 auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>());
388 379 m_XAxisData = m_XAxisData->sort(permutation);
389 380 m_ValuesData = m_ValuesData->sort(permutation);
390 381 }
391 382
392 383 std::shared_ptr<ArrayData<1> > m_XAxisData;
393 384 Unit m_XAxisUnit;
394 385 std::shared_ptr<ArrayData<Dim> > m_ValuesData;
395 386 Unit m_ValuesUnit;
396 387
397 388 QReadWriteLock m_Lock;
398 389 };
399 390
400 391 #endif // SCIQLOP_DATASERIES_H
@@ -1,113 +1,111
1 1 #ifndef SCIQLOP_IDATASERIES_H
2 2 #define SCIQLOP_IDATASERIES_H
3 3
4 4 #include <Common/MetaTypes.h>
5 5 #include <Data/DataSeriesIterator.h>
6 6 #include <Data/SqpRange.h>
7 7
8 8 #include <memory>
9 9
10 10 #include <QString>
11 11
12 12 template <int Dim>
13 13 class ArrayData;
14 14
15 15 struct Unit {
16 16 explicit Unit(const QString &name = {}, bool timeUnit = false)
17 17 : m_Name{name}, m_TimeUnit{timeUnit}
18 18 {
19 19 }
20 20
21 21 inline bool operator==(const Unit &other) const
22 22 {
23 23 return std::tie(m_Name, m_TimeUnit) == std::tie(other.m_Name, other.m_TimeUnit);
24 24 }
25 25 inline bool operator!=(const Unit &other) const { return !(*this == other); }
26 26
27 27 QString m_Name; ///< Unit name
28 28 bool m_TimeUnit; ///< The unit is a unit of time (UTC)
29 29 };
30 30
31 31 /**
32 32 * @brief The IDataSeries aims to declare a data series.
33 33 *
34 34 * A data series is an entity that contains at least :
35 35 * - one dataset representing the x-axis
36 36 * - one dataset representing the values
37 37 *
38 38 * Each dataset is represented by an ArrayData, and is associated with a unit.
39 39 *
40 40 * An ArrayData can be unidimensional or two-dimensional, depending on the implementation of the
41 41 * IDataSeries. The x-axis dataset is always unidimensional.
42 42 *
43 43 * @sa ArrayData
44 44 */
45 45 class IDataSeries {
46 46 public:
47 47 virtual ~IDataSeries() noexcept = default;
48 48
49 49 /// Returns the x-axis dataset
50 50 virtual std::shared_ptr<ArrayData<1> > xAxisData() = 0;
51 51
52 52 /// Returns the x-axis dataset (as const)
53 53 virtual const std::shared_ptr<ArrayData<1> > xAxisData() const = 0;
54 54
55 55 virtual Unit xAxisUnit() const = 0;
56 56
57 57 virtual Unit valuesUnit() const = 0;
58 58
59 59 virtual void merge(IDataSeries *dataSeries) = 0;
60 60 /// Removes from data series all entries whose value on the x-axis is not between min and max
61 61 virtual void purge(double min, double max) = 0;
62 62
63 63 /// @todo Review the name and signature of this method
64 64 virtual std::shared_ptr<IDataSeries> subDataSeries(const SqpRange &range) = 0;
65 65
66 66 virtual std::unique_ptr<IDataSeries> clone() const = 0;
67 67
68 68 /// @return the total number of points contained in the data series
69 69 virtual int nbPoints() const = 0;
70 70
71 virtual SqpRange range() const = 0;
72
73 71 // ///////// //
74 72 // Iterators //
75 73 // ///////// //
76 74
77 75 virtual DataSeriesIterator cbegin() const = 0;
78 76 virtual DataSeriesIterator cend() const = 0;
79 77 virtual DataSeriesIterator begin() = 0;
80 78 virtual DataSeriesIterator end() = 0;
81 79
82 80 /// @return the iterator to the first entry of the data series whose x-axis data is greater than
83 81 /// or equal to the value passed in parameter, or the end iterator if there is no matching value
84 82 virtual DataSeriesIterator minXAxisData(double minXAxisData) const = 0;
85 83
86 84 /// @return the iterator to the last entry of the data series whose x-axis data is less than or
87 85 /// equal to the value passed in parameter, or the end iterator if there is no matching value
88 86 virtual DataSeriesIterator maxXAxisData(double maxXAxisData) const = 0;
89 87
90 88 /// @return the iterators pointing to the range of data whose x-axis values are between min and
91 89 /// max passed in parameters
92 90 virtual std::pair<DataSeriesIterator, DataSeriesIterator>
93 91 xAxisRange(double minXAxisData, double maxXAxisData) const = 0;
94 92
95 93 /// @return two iterators pointing to the data that have respectively the min and the max value
96 94 /// data of a data series' range. The search is performed for a given x-axis range.
97 95 /// @sa xAxisRange()
98 96 virtual std::pair<DataSeriesIterator, DataSeriesIterator>
99 97 valuesBounds(double minXAxisData, double maxXAxisData) const = 0;
100 98
101 99 // /////// //
102 100 // Mutexes //
103 101 // /////// //
104 102
105 103 virtual void lockRead() = 0;
106 104 virtual void lockWrite() = 0;
107 105 virtual void unlock() = 0;
108 106 };
109 107
110 108 // Required for using shared_ptr in signals/slots
111 109 SCIQLOP_REGISTER_META_TYPE(IDATASERIES_PTR_REGISTRY, std::shared_ptr<IDataSeries>)
112 110
113 111 #endif // SCIQLOP_IDATASERIES_H
@@ -1,842 +1,840
1 1 #include <Variable/Variable.h>
2 2 #include <Variable/VariableAcquisitionWorker.h>
3 3 #include <Variable/VariableCacheStrategy.h>
4 4 #include <Variable/VariableCacheStrategyFactory.h>
5 5 #include <Variable/VariableController.h>
6 6 #include <Variable/VariableModel.h>
7 7 #include <Variable/VariableSynchronizationGroup.h>
8 8
9 9 #include <Data/DataProviderParameters.h>
10 10 #include <Data/IDataProvider.h>
11 11 #include <Data/IDataSeries.h>
12 12 #include <Data/VariableRequest.h>
13 13 #include <Time/TimeController.h>
14 14
15 15 #include <QMutex>
16 16 #include <QThread>
17 17 #include <QUuid>
18 18 #include <QtCore/QItemSelectionModel>
19 19
20 20 #include <deque>
21 21 #include <set>
22 22 #include <unordered_map>
23 23
24 24 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
25 25
26 26 namespace {
27 27
28 28 SqpRange computeSynchroRangeRequested(const SqpRange &varRange, const SqpRange &graphRange,
29 29 const SqpRange &oldGraphRange)
30 30 {
31 31 auto zoomType = VariableController::getZoomType(graphRange, oldGraphRange);
32 32
33 33 auto varRangeRequested = varRange;
34 34 switch (zoomType) {
35 35 case AcquisitionZoomType::ZoomIn: {
36 36 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
37 37 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
38 38 varRangeRequested.m_TStart += deltaLeft;
39 39 varRangeRequested.m_TEnd -= deltaRight;
40 40 break;
41 41 }
42 42
43 43 case AcquisitionZoomType::ZoomOut: {
44 44 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
45 45 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
46 46 varRangeRequested.m_TStart -= deltaLeft;
47 47 varRangeRequested.m_TEnd += deltaRight;
48 48 break;
49 49 }
50 50 case AcquisitionZoomType::PanRight: {
51 51 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
52 52 varRangeRequested.m_TStart += deltaRight;
53 53 varRangeRequested.m_TEnd += deltaRight;
54 54 break;
55 55 }
56 56 case AcquisitionZoomType::PanLeft: {
57 57 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
58 58 varRangeRequested.m_TStart -= deltaLeft;
59 59 varRangeRequested.m_TEnd -= deltaLeft;
60 60 break;
61 61 }
62 62 case AcquisitionZoomType::Unknown: {
63 63 qCCritical(LOG_VariableController())
64 64 << VariableController::tr("Impossible to synchronize: zoom type unknown");
65 65 break;
66 66 }
67 67 default:
68 68 qCCritical(LOG_VariableController()) << VariableController::tr(
69 69 "Impossible to synchronize: zoom type not take into account");
70 70 // No action
71 71 break;
72 72 }
73 73
74 74 return varRangeRequested;
75 75 }
76 76 }
77 77
78 78 struct VariableController::VariableControllerPrivate {
79 79 explicit VariableControllerPrivate(VariableController *parent)
80 80 : m_WorkingMutex{},
81 81 m_VariableModel{new VariableModel{parent}},
82 82 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
83 83 // m_VariableCacheStrategy{std::make_unique<VariableCacheStrategy>()},
84 84 m_VariableCacheStrategy{VariableCacheStrategyFactory::createCacheStrategy(
85 85 CacheStrategy::SingleThreshold)},
86 86 m_VariableAcquisitionWorker{std::make_unique<VariableAcquisitionWorker>()},
87 87 q{parent}
88 88 {
89 89
90 90 m_VariableAcquisitionWorker->moveToThread(&m_VariableAcquisitionWorkerThread);
91 91 m_VariableAcquisitionWorkerThread.setObjectName("VariableAcquisitionWorkerThread");
92 92 }
93 93
94 94
95 95 virtual ~VariableControllerPrivate()
96 96 {
97 97 qCDebug(LOG_VariableController()) << tr("VariableControllerPrivate destruction");
98 98 m_VariableAcquisitionWorkerThread.quit();
99 99 m_VariableAcquisitionWorkerThread.wait();
100 100 }
101 101
102 102
103 103 void processRequest(std::shared_ptr<Variable> var, const SqpRange &rangeRequested,
104 104 QUuid varRequestId);
105 105
106 106 QVector<SqpRange> provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable,
107 107 const SqpRange &dateTime);
108 108
109 109 std::shared_ptr<Variable> findVariable(QUuid vIdentifier);
110 110 std::shared_ptr<IDataSeries>
111 111 retrieveDataSeries(const QVector<AcquisitionDataPacket> acqDataPacketVector);
112 112
113 113 void registerProvider(std::shared_ptr<IDataProvider> provider);
114 114
115 115 void storeVariableRequest(QUuid varId, QUuid varRequestId, const VariableRequest &varRequest);
116 116 QUuid acceptVariableRequest(QUuid varId, std::shared_ptr<IDataSeries> dataSeries);
117 117 void updateVariableRequest(QUuid varRequestId);
118 118 void cancelVariableRequest(QUuid varRequestId);
119 119
120 120 QMutex m_WorkingMutex;
121 121 /// Variable model. The VariableController has the ownership
122 122 VariableModel *m_VariableModel;
123 123 QItemSelectionModel *m_VariableSelectionModel;
124 124
125 125
126 126 TimeController *m_TimeController{nullptr};
127 127 std::unique_ptr<VariableCacheStrategy> m_VariableCacheStrategy;
128 128 std::unique_ptr<VariableAcquisitionWorker> m_VariableAcquisitionWorker;
129 129 QThread m_VariableAcquisitionWorkerThread;
130 130
131 131 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
132 132 m_VariableToProviderMap;
133 133 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap;
134 134 std::map<QUuid, std::shared_ptr<VariableSynchronizationGroup> >
135 135 m_GroupIdToVariableSynchronizationGroupMap;
136 136 std::map<QUuid, QUuid> m_VariableIdGroupIdMap;
137 137 std::set<std::shared_ptr<IDataProvider> > m_ProviderSet;
138 138
139 139 std::map<QUuid, std::map<QUuid, VariableRequest> > m_VarRequestIdToVarIdVarRequestMap;
140 140
141 141 std::map<QUuid, std::deque<QUuid> > m_VarIdToVarRequestIdQueueMap;
142 142
143 143
144 144 VariableController *q;
145 145 };
146 146
147 147
148 148 VariableController::VariableController(QObject *parent)
149 149 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
150 150 {
151 151 qCDebug(LOG_VariableController()) << tr("VariableController construction")
152 152 << QThread::currentThread();
153 153
154 154 connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this,
155 155 &VariableController::onAbortProgressRequested);
156 156
157 157 connect(impl->m_VariableAcquisitionWorker.get(),
158 158 &VariableAcquisitionWorker::variableCanceledRequested, this,
159 159 &VariableController::onAbortAcquisitionRequested);
160 160
161 161 connect(impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::dataProvided, this,
162 162 &VariableController::onDataProvided);
163 163 connect(impl->m_VariableAcquisitionWorker.get(),
164 164 &VariableAcquisitionWorker::variableRequestInProgress, this,
165 165 &VariableController::onVariableRetrieveDataInProgress);
166 166
167 167
168 168 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::started,
169 169 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::initialize);
170 170 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::finished,
171 171 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::finalize);
172 172
173 173
174 174 impl->m_VariableAcquisitionWorkerThread.start();
175 175 }
176 176
177 177 VariableController::~VariableController()
178 178 {
179 179 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
180 180 << QThread::currentThread();
181 181 this->waitForFinish();
182 182 }
183 183
184 184 VariableModel *VariableController::variableModel() noexcept
185 185 {
186 186 return impl->m_VariableModel;
187 187 }
188 188
189 189 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
190 190 {
191 191 return impl->m_VariableSelectionModel;
192 192 }
193 193
194 194 void VariableController::setTimeController(TimeController *timeController) noexcept
195 195 {
196 196 impl->m_TimeController = timeController;
197 197 }
198 198
199 199 std::shared_ptr<Variable>
200 200 VariableController::cloneVariable(std::shared_ptr<Variable> variable) noexcept
201 201 {
202 202 if (impl->m_VariableModel->containsVariable(variable)) {
203 203 // Clones variable
204 204 auto duplicate = variable->clone();
205 205
206 206 // Adds clone to model
207 207 impl->m_VariableModel->addVariable(duplicate);
208 208
209 209 // Generates clone identifier
210 210 impl->m_VariableToIdentifierMap[duplicate] = QUuid::createUuid();
211 211
212 212 // Registers provider
213 213 auto variableProvider = impl->m_VariableToProviderMap.at(variable);
214 214 auto duplicateProvider = variableProvider != nullptr ? variableProvider->clone() : nullptr;
215 215
216 216 impl->m_VariableToProviderMap[duplicate] = duplicateProvider;
217 217 if (duplicateProvider) {
218 218 impl->registerProvider(duplicateProvider);
219 219 }
220 220
221 221 return duplicate;
222 222 }
223 223 else {
224 224 qCCritical(LOG_VariableController())
225 225 << tr("Can't create duplicate of variable %1: variable not registered in the model")
226 226 .arg(variable->name());
227 227 return nullptr;
228 228 }
229 229 }
230 230
231 231 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
232 232 {
233 233 if (!variable) {
234 234 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
235 235 return;
236 236 }
237 237
238 238 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
239 239 // make some treatments before the deletion
240 240 emit variableAboutToBeDeleted(variable);
241 241
242 242 // Deletes identifier
243 243 impl->m_VariableToIdentifierMap.erase(variable);
244 244
245 245 // Deletes provider
246 246 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
247 247 qCDebug(LOG_VariableController())
248 248 << tr("Number of providers deleted for variable %1: %2")
249 249 .arg(variable->name(), QString::number(nbProvidersDeleted));
250 250
251 251
252 252 // Deletes from model
253 253 impl->m_VariableModel->deleteVariable(variable);
254 254 }
255 255
256 256 void VariableController::deleteVariables(
257 257 const QVector<std::shared_ptr<Variable> > &variables) noexcept
258 258 {
259 259 for (auto variable : qAsConst(variables)) {
260 260 deleteVariable(variable);
261 261 }
262 262 }
263 263
264 264 std::shared_ptr<Variable>
265 265 VariableController::createVariable(const QString &name, const QVariantHash &metadata,
266 266 std::shared_ptr<IDataProvider> provider) noexcept
267 267 {
268 268 if (!impl->m_TimeController) {
269 269 qCCritical(LOG_VariableController())
270 270 << tr("Impossible to create variable: The time controller is null");
271 271 return nullptr;
272 272 }
273 273
274 274 auto range = impl->m_TimeController->dateTime();
275 275
276 276 if (auto newVariable = impl->m_VariableModel->createVariable(name, metadata)) {
277 277 auto identifier = QUuid::createUuid();
278 278
279 279 // store the provider
280 280 impl->registerProvider(provider);
281 281
282 282 // Associate the provider
283 283 impl->m_VariableToProviderMap[newVariable] = provider;
284 284 qCInfo(LOG_VariableController()) << "createVariable: " << identifier;
285 285 impl->m_VariableToIdentifierMap[newVariable] = identifier;
286 286
287 287
288 288 auto varRequestId = QUuid::createUuid();
289 289 impl->processRequest(newVariable, range, varRequestId);
290 290 impl->updateVariableRequest(varRequestId);
291 291
292 292 return newVariable;
293 293 }
294 294 }
295 295
296 296 void VariableController::onDateTimeOnSelection(const SqpRange &dateTime)
297 297 {
298 298 // TODO check synchronisation and Rescale
299 299 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
300 300 << QThread::currentThread()->objectName();
301 301 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
302 302 auto varRequestId = QUuid::createUuid();
303 303
304 304 for (const auto &selectedRow : qAsConst(selectedRows)) {
305 305 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
306 306 selectedVariable->setRange(dateTime);
307 307 impl->processRequest(selectedVariable, dateTime, varRequestId);
308 308
309 309 // notify that rescale operation has to be done
310 310 emit rangeChanged(selectedVariable, dateTime);
311 311 }
312 312 }
313 313 impl->updateVariableRequest(varRequestId);
314 314 }
315 315
316 316 void VariableController::onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
317 317 const SqpRange &cacheRangeRequested,
318 318 QVector<AcquisitionDataPacket> dataAcquired)
319 319 {
320 320 auto retrievedDataSeries = impl->retrieveDataSeries(dataAcquired);
321 321 auto varRequestId = impl->acceptVariableRequest(vIdentifier, retrievedDataSeries);
322 322 if (!varRequestId.isNull()) {
323 323 impl->updateVariableRequest(varRequestId);
324 324 }
325 325 }
326 326
327 327 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
328 328 {
329 329 qCDebug(LOG_VariableController())
330 330 << "TORM: variableController::onVariableRetrieveDataInProgress"
331 331 << QThread::currentThread()->objectName() << progress;
332 332 if (auto var = impl->findVariable(identifier)) {
333 333 impl->m_VariableModel->setDataProgress(var, progress);
334 334 }
335 335 else {
336 336 qCCritical(LOG_VariableController())
337 337 << tr("Impossible to notify progression of a null variable");
338 338 }
339 339 }
340 340
341 341 void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable)
342 342 {
343 343 auto it = impl->m_VariableToIdentifierMap.find(variable);
344 344 if (it != impl->m_VariableToIdentifierMap.cend()) {
345 345 impl->m_VariableAcquisitionWorker->abortProgressRequested(it->second);
346 346
347 347 QUuid varRequestId;
348 348 auto varIdToVarRequestIdQueueMapIt = impl->m_VarIdToVarRequestIdQueueMap.find(it->second);
349 349 if (varIdToVarRequestIdQueueMapIt != impl->m_VarIdToVarRequestIdQueueMap.cend()) {
350 350 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
351 351 varRequestId = varRequestIdQueue.front();
352 352 impl->cancelVariableRequest(varRequestId);
353 353
354 354 // Finish the progression for the request
355 355 impl->m_VariableModel->setDataProgress(variable, 0.0);
356 356 }
357 357 else {
358 358 qCWarning(LOG_VariableController())
359 359 << tr("Aborting progression of inexistant variable request detected !!!")
360 360 << QThread::currentThread()->objectName();
361 361 }
362 362 }
363 363 else {
364 364 qCWarning(LOG_VariableController())
365 365 << tr("Aborting progression of inexistant variable detected !!!")
366 366 << QThread::currentThread()->objectName();
367 367 }
368 368 }
369 369
370 370 void VariableController::onAbortAcquisitionRequested(QUuid vIdentifier)
371 371 {
372 372 qCDebug(LOG_VariableController()) << "TORM: variableController::onAbortAcquisitionRequested"
373 373 << QThread::currentThread()->objectName() << vIdentifier;
374 374
375 375 if (auto var = impl->findVariable(vIdentifier)) {
376 376 this->onAbortProgressRequested(var);
377 377 }
378 378 else {
379 379 qCCritical(LOG_VariableController())
380 380 << tr("Impossible to abort Acquisition Requestof a null variable");
381 381 }
382 382 }
383 383
384 384 void VariableController::onAddSynchronizationGroupId(QUuid synchronizationGroupId)
385 385 {
386 386 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronizationGroupId"
387 387 << QThread::currentThread()->objectName()
388 388 << synchronizationGroupId;
389 389 auto vSynchroGroup = std::make_shared<VariableSynchronizationGroup>();
390 390 impl->m_GroupIdToVariableSynchronizationGroupMap.insert(
391 391 std::make_pair(synchronizationGroupId, vSynchroGroup));
392 392 }
393 393
394 394 void VariableController::onRemoveSynchronizationGroupId(QUuid synchronizationGroupId)
395 395 {
396 396 impl->m_GroupIdToVariableSynchronizationGroupMap.erase(synchronizationGroupId);
397 397 }
398 398
399 399 void VariableController::onAddSynchronized(std::shared_ptr<Variable> variable,
400 400 QUuid synchronizationGroupId)
401 401
402 402 {
403 403 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronized"
404 404 << synchronizationGroupId;
405 405 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(variable);
406 406 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
407 407 auto groupIdToVSGIt
408 408 = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
409 409 if (groupIdToVSGIt != impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
410 410 impl->m_VariableIdGroupIdMap.insert(
411 411 std::make_pair(varToVarIdIt->second, synchronizationGroupId));
412 412 groupIdToVSGIt->second->addVariableId(varToVarIdIt->second);
413 413 }
414 414 else {
415 415 qCCritical(LOG_VariableController())
416 416 << tr("Impossible to synchronize a variable with an unknown sycnhronization group")
417 417 << variable->name();
418 418 }
419 419 }
420 420 else {
421 421 qCCritical(LOG_VariableController())
422 422 << tr("Impossible to synchronize a variable with no identifier") << variable->name();
423 423 }
424 424 }
425 425
426 426 void VariableController::desynchronize(std::shared_ptr<Variable> variable,
427 427 QUuid synchronizationGroupId)
428 428 {
429 429 // Gets variable id
430 430 auto variableIt = impl->m_VariableToIdentifierMap.find(variable);
431 431 if (variableIt == impl->m_VariableToIdentifierMap.cend()) {
432 432 qCCritical(LOG_VariableController())
433 433 << tr("Can't desynchronize variable %1: variable identifier not found")
434 434 .arg(variable->name());
435 435 return;
436 436 }
437 437
438 438 // Gets synchronization group
439 439 auto groupIt = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
440 440 if (groupIt == impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
441 441 qCCritical(LOG_VariableController())
442 442 << tr("Can't desynchronize variable %1: unknown synchronization group")
443 443 .arg(variable->name());
444 444 return;
445 445 }
446 446
447 447 auto variableId = variableIt->second;
448 448
449 449 // Removes variable from synchronization group
450 450 auto synchronizationGroup = groupIt->second;
451 451 synchronizationGroup->removeVariableId(variableId);
452 452
453 453 // Removes link between variable and synchronization group
454 454 impl->m_VariableIdGroupIdMap.erase(variableId);
455 455 }
456 456
457 457 void VariableController::onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables,
458 458 const SqpRange &range, const SqpRange &oldRange,
459 459 bool synchronise)
460 460 {
461 461 // NOTE: oldRange isn't really necessary since oldRange == variable->range().
462 462
463 463 // we want to load data of the variable for the dateTime.
464 464 // First we check if the cache contains some of them.
465 465 // For the other, we ask the provider to give them.
466 466
467 467 auto varRequestId = QUuid::createUuid();
468 468 qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading"
469 469 << QThread::currentThread()->objectName() << varRequestId;
470 470
471 471 for (const auto &var : variables) {
472 472 qCDebug(LOG_VariableController()) << "processRequest for" << var->name() << varRequestId;
473 473 impl->processRequest(var, range, varRequestId);
474 474 }
475 475
476 476 if (synchronise) {
477 477 // Get the group ids
478 478 qCDebug(LOG_VariableController())
479 479 << "TORM VariableController::onRequestDataLoading for synchro var ENABLE";
480 480 auto groupIds = std::set<QUuid>{};
481 481 auto groupIdToOldRangeMap = std::map<QUuid, SqpRange>{};
482 482 for (const auto &var : variables) {
483 483 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(var);
484 484 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
485 485 auto vId = varToVarIdIt->second;
486 486 auto varIdToGroupIdIt = impl->m_VariableIdGroupIdMap.find(vId);
487 487 if (varIdToGroupIdIt != impl->m_VariableIdGroupIdMap.cend()) {
488 488 auto gId = varIdToGroupIdIt->second;
489 489 groupIdToOldRangeMap.insert(std::make_pair(gId, var->range()));
490 490 if (groupIds.find(gId) == groupIds.cend()) {
491 491 qCDebug(LOG_VariableController()) << "Synchro detect group " << gId;
492 492 groupIds.insert(gId);
493 493 }
494 494 }
495 495 }
496 496 }
497 497
498 498 // We assume here all group ids exist
499 499 for (const auto &gId : groupIds) {
500 500 auto vSynchronizationGroup = impl->m_GroupIdToVariableSynchronizationGroupMap.at(gId);
501 501 auto vSyncIds = vSynchronizationGroup->getIds();
502 502 qCDebug(LOG_VariableController()) << "Var in synchro group ";
503 503 for (auto vId : vSyncIds) {
504 504 auto var = impl->findVariable(vId);
505 505
506 506 // Don't process already processed var
507 507 if (!variables.contains(var)) {
508 508 if (var != nullptr) {
509 509 qCDebug(LOG_VariableController()) << "processRequest synchro for"
510 510 << var->name();
511 511 auto vSyncRangeRequested = computeSynchroRangeRequested(
512 512 var->range(), range, groupIdToOldRangeMap.at(gId));
513 513 qCDebug(LOG_VariableController()) << "synchro RR" << vSyncRangeRequested;
514 514 impl->processRequest(var, vSyncRangeRequested, varRequestId);
515 515 }
516 516 else {
517 517 qCCritical(LOG_VariableController())
518 518
519 519 << tr("Impossible to synchronize a null variable");
520 520 }
521 521 }
522 522 }
523 523 }
524 524 }
525 525
526 526 impl->updateVariableRequest(varRequestId);
527 527 }
528 528
529 529
530 530 void VariableController::initialize()
531 531 {
532 532 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
533 533 impl->m_WorkingMutex.lock();
534 534 qCDebug(LOG_VariableController()) << tr("VariableController init END");
535 535 }
536 536
537 537 void VariableController::finalize()
538 538 {
539 539 impl->m_WorkingMutex.unlock();
540 540 }
541 541
542 542 void VariableController::waitForFinish()
543 543 {
544 544 QMutexLocker locker{&impl->m_WorkingMutex};
545 545 }
546 546
547 547 AcquisitionZoomType VariableController::getZoomType(const SqpRange &range, const SqpRange &oldRange)
548 548 {
549 549 // t1.m_TStart <= t2.m_TStart && t2.m_TEnd <= t1.m_TEnd
550 550 auto zoomType = AcquisitionZoomType::Unknown;
551 551 if (range.m_TStart <= oldRange.m_TStart && oldRange.m_TEnd <= range.m_TEnd) {
552 552 zoomType = AcquisitionZoomType::ZoomOut;
553 553 }
554 554 else if (range.m_TStart > oldRange.m_TStart && range.m_TEnd > oldRange.m_TEnd) {
555 555 zoomType = AcquisitionZoomType::PanRight;
556 556 }
557 557 else if (range.m_TStart < oldRange.m_TStart && range.m_TEnd < oldRange.m_TEnd) {
558 558 zoomType = AcquisitionZoomType::PanLeft;
559 559 }
560 560 else if (range.m_TStart > oldRange.m_TStart && oldRange.m_TEnd > range.m_TEnd) {
561 561 zoomType = AcquisitionZoomType::ZoomIn;
562 562 }
563 563 else {
564 564 qCCritical(LOG_VariableController()) << "getZoomType: Unknown type detected";
565 565 }
566 566 return zoomType;
567 567 }
568 568
569 569 void VariableController::VariableControllerPrivate::processRequest(std::shared_ptr<Variable> var,
570 570 const SqpRange &rangeRequested,
571 571 QUuid varRequestId)
572 572 {
573 573
574 574 // TODO: protect at
575 575 auto varRequest = VariableRequest{};
576 576 auto varId = m_VariableToIdentifierMap.at(var);
577 577
578 578 auto varStrategyRangesRequested
579 579 = m_VariableCacheStrategy->computeRange(var->range(), rangeRequested);
580 580
581 581 auto notInCacheRangeList = QVector<SqpRange>{varStrategyRangesRequested.second};
582 582 auto inCacheRangeList = QVector<SqpRange>{};
583 583 if (m_VarIdToVarRequestIdQueueMap.find(varId) == m_VarIdToVarRequestIdQueueMap.cend()) {
584 584 notInCacheRangeList = var->provideNotInCacheRangeList(varStrategyRangesRequested.second);
585 585 inCacheRangeList = var->provideInCacheRangeList(varStrategyRangesRequested.second);
586 586 }
587 587
588 588 if (!notInCacheRangeList.empty()) {
589 589 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
590 590 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
591 591
592 592 // store VarRequest
593 593 storeVariableRequest(varId, varRequestId, varRequest);
594 594
595 595 auto varProvider = m_VariableToProviderMap.at(var);
596 596 if (varProvider != nullptr) {
597 597 auto varRequestIdCanceled = m_VariableAcquisitionWorker->pushVariableRequest(
598 598 varRequestId, varId, varStrategyRangesRequested.first,
599 599 varStrategyRangesRequested.second,
600 600 DataProviderParameters{std::move(notInCacheRangeList), var->metadata()},
601 601 varProvider);
602 602
603 603 if (!varRequestIdCanceled.isNull()) {
604 604 qCDebug(LOG_VariableAcquisitionWorker()) << tr("vsarRequestIdCanceled: ")
605 605 << varRequestIdCanceled;
606 606 cancelVariableRequest(varRequestIdCanceled);
607 607 }
608 608 }
609 609 else {
610 610 qCCritical(LOG_VariableController())
611 611 << "Impossible to provide data with a null provider";
612 612 }
613 613
614 614 if (!inCacheRangeList.empty()) {
615 615 emit q->updateVarDisplaying(var, inCacheRangeList.first());
616 616 }
617 617 }
618 618 else {
619 619 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
620 620 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
621 621 // store VarRequest
622 622 storeVariableRequest(varId, varRequestId, varRequest);
623 623 acceptVariableRequest(varId,
624 624 var->dataSeries()->subDataSeries(varStrategyRangesRequested.second));
625 625 }
626 626 }
627 627
628 628 std::shared_ptr<Variable>
629 629 VariableController::VariableControllerPrivate::findVariable(QUuid vIdentifier)
630 630 {
631 631 std::shared_ptr<Variable> var;
632 632 auto findReply = [vIdentifier](const auto &entry) { return vIdentifier == entry.second; };
633 633
634 634 auto end = m_VariableToIdentifierMap.cend();
635 635 auto it = std::find_if(m_VariableToIdentifierMap.cbegin(), end, findReply);
636 636 if (it != end) {
637 637 var = it->first;
638 638 }
639 639 else {
640 640 qCCritical(LOG_VariableController())
641 641 << tr("Impossible to find the variable with the identifier: ") << vIdentifier;
642 642 }
643 643
644 644 return var;
645 645 }
646 646
647 647 std::shared_ptr<IDataSeries> VariableController::VariableControllerPrivate::retrieveDataSeries(
648 648 const QVector<AcquisitionDataPacket> acqDataPacketVector)
649 649 {
650 650 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size")
651 651 << acqDataPacketVector.size();
652 652 std::shared_ptr<IDataSeries> dataSeries;
653 653 if (!acqDataPacketVector.isEmpty()) {
654 654 dataSeries = acqDataPacketVector[0].m_DateSeries;
655 655 for (int i = 1; i < acqDataPacketVector.size(); ++i) {
656 656 dataSeries->merge(acqDataPacketVector[i].m_DateSeries.get());
657 657 }
658 658 }
659 659 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size END")
660 660 << acqDataPacketVector.size();
661 661 return dataSeries;
662 662 }
663 663
664 664 void VariableController::VariableControllerPrivate::registerProvider(
665 665 std::shared_ptr<IDataProvider> provider)
666 666 {
667 667 if (m_ProviderSet.find(provider) == m_ProviderSet.end()) {
668 668 qCDebug(LOG_VariableController()) << tr("Registering of a new provider")
669 669 << provider->objectName();
670 670 m_ProviderSet.insert(provider);
671 671 connect(provider.get(), &IDataProvider::dataProvided, m_VariableAcquisitionWorker.get(),
672 672 &VariableAcquisitionWorker::onVariableDataAcquired);
673 673 connect(provider.get(), &IDataProvider::dataProvidedProgress,
674 674 m_VariableAcquisitionWorker.get(),
675 675 &VariableAcquisitionWorker::onVariableRetrieveDataInProgress);
676 676 connect(provider.get(), &IDataProvider::dataProvidedFailed,
677 677 m_VariableAcquisitionWorker.get(),
678 678 &VariableAcquisitionWorker::onVariableAcquisitionFailed);
679 679 }
680 680 else {
681 681 qCDebug(LOG_VariableController()) << tr("Cannot register provider, it already exists ");
682 682 }
683 683 }
684 684
685 685 void VariableController::VariableControllerPrivate::storeVariableRequest(
686 686 QUuid varId, QUuid varRequestId, const VariableRequest &varRequest)
687 687 {
688 688 // First request for the variable. we can create an entry for it
689 689 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
690 690 if (varIdToVarRequestIdQueueMapIt == m_VarIdToVarRequestIdQueueMap.cend()) {
691 691 auto varRequestIdQueue = std::deque<QUuid>{};
692 692 qCDebug(LOG_VariableController()) << tr("Store REQUEST in QUEUE");
693 693 varRequestIdQueue.push_back(varRequestId);
694 694 m_VarIdToVarRequestIdQueueMap.insert(std::make_pair(varId, std::move(varRequestIdQueue)));
695 695 }
696 696 else {
697 697 qCDebug(LOG_VariableController()) << tr("Store REQUEST in EXISTING QUEUE");
698 698 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
699 699 varRequestIdQueue.push_back(varRequestId);
700 700 }
701 701
702 702 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
703 703 if (varRequestIdToVarIdVarRequestMapIt == m_VarRequestIdToVarIdVarRequestMap.cend()) {
704 704 auto varIdToVarRequestMap = std::map<QUuid, VariableRequest>{};
705 705 varIdToVarRequestMap.insert(std::make_pair(varId, varRequest));
706 706 qCDebug(LOG_VariableController()) << tr("Store REQUESTID in MAP");
707 707 m_VarRequestIdToVarIdVarRequestMap.insert(
708 708 std::make_pair(varRequestId, std::move(varIdToVarRequestMap)));
709 709 }
710 710 else {
711 711 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
712 712 qCDebug(LOG_VariableController()) << tr("Store REQUESTID in EXISTING MAP");
713 713 varIdToVarRequestMap.insert(std::make_pair(varId, varRequest));
714 714 }
715 715 }
716 716
717 717 QUuid VariableController::VariableControllerPrivate::acceptVariableRequest(
718 718 QUuid varId, std::shared_ptr<IDataSeries> dataSeries)
719 719 {
720 720 QUuid varRequestId;
721 721 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
722 722 if (varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.cend()) {
723 723 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
724 724 varRequestId = varRequestIdQueue.front();
725 725 auto varRequestIdToVarIdVarRequestMapIt
726 726 = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
727 727 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
728 728 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
729 729 auto varIdToVarRequestMapIt = varIdToVarRequestMap.find(varId);
730 730 if (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) {
731 731 qCDebug(LOG_VariableController()) << tr("acceptVariableRequest");
732 732 auto &varRequest = varIdToVarRequestMapIt->second;
733 733 varRequest.m_DataSeries = dataSeries;
734 734 varRequest.m_CanUpdate = true;
735 735 }
736 736 else {
737 737 qCDebug(LOG_VariableController())
738 738 << tr("Impossible to acceptVariableRequest of a unknown variable id attached "
739 739 "to a variableRequestId")
740 740 << varRequestId << varId;
741 741 }
742 742 }
743 743 else {
744 744 qCCritical(LOG_VariableController())
745 745 << tr("Impossible to acceptVariableRequest of a unknown variableRequestId")
746 746 << varRequestId;
747 747 }
748 748
749 749 varRequestIdQueue.pop_front();
750 750 if (varRequestIdQueue.empty()) {
751 751 qCDebug(LOG_VariableController())
752 752 << tr("TORM Erase REQUEST because it has been accepted") << varId;
753 753 m_VarIdToVarRequestIdQueueMap.erase(varId);
754 754 }
755 755 }
756 756 else {
757 757 qCCritical(LOG_VariableController())
758 758 << tr("Impossible to acceptVariableRequest of a unknown variable id") << varId;
759 759 }
760 760
761 761 return varRequestId;
762 762 }
763 763
764 764 void VariableController::VariableControllerPrivate::updateVariableRequest(QUuid varRequestId)
765 765 {
766 766
767 767 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
768 768 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
769 769 bool processVariableUpdate = true;
770 770 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
771 771 for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin();
772 772 (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) && processVariableUpdate;
773 773 ++varIdToVarRequestMapIt) {
774 774 processVariableUpdate &= varIdToVarRequestMapIt->second.m_CanUpdate;
775 775 qCDebug(LOG_VariableController()) << tr("updateVariableRequest")
776 776 << processVariableUpdate;
777 777 }
778 778
779 779 if (processVariableUpdate) {
780 780 for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin();
781 781 varIdToVarRequestMapIt != varIdToVarRequestMap.cend(); ++varIdToVarRequestMapIt) {
782 782 if (auto var = findVariable(varIdToVarRequestMapIt->first)) {
783 783 auto &varRequest = varIdToVarRequestMapIt->second;
784 784 var->setRange(varRequest.m_RangeRequested);
785 785 var->setCacheRange(varRequest.m_CacheRangeRequested);
786 786 qCDebug(LOG_VariableController()) << tr("1: onDataProvided")
787 787 << varRequest.m_RangeRequested;
788 788 qCDebug(LOG_VariableController()) << tr("2: onDataProvided")
789 789 << varRequest.m_CacheRangeRequested;
790 790 var->mergeDataSeries(varRequest.m_DataSeries);
791 qCDebug(LOG_VariableController()) << tr("3: onDataProvided")
792 << varRequest.m_DataSeries->range();
793 qCDebug(LOG_VariableController()) << tr("4: onDataProvided");
791 qCDebug(LOG_VariableController()) << tr("3: onDataProvided");
794 792
795 793 /// @todo MPL: confirm
796 794 // Variable update is notified only if there is no pending request for it
797 795 // if
798 796 // (m_VarIdToVarRequestIdQueueMap.count(varIdToVarRequestMapIt->first)
799 797 // == 0) {
800 798 emit var->updated();
801 799 // }
802 800 }
803 801 else {
804 802 qCCritical(LOG_VariableController())
805 803 << tr("Impossible to update data to a null variable");
806 804 }
807 805 }
808 806
809 807 // cleaning varRequestId
810 808 qCDebug(LOG_VariableController()) << tr("0: erase REQUEST in MAP ?")
811 809 << m_VarRequestIdToVarIdVarRequestMap.size();
812 810 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId);
813 811 qCDebug(LOG_VariableController()) << tr("1: erase REQUEST in MAP ?")
814 812 << m_VarRequestIdToVarIdVarRequestMap.size();
815 813 }
816 814 }
817 815 else {
818 816 qCCritical(LOG_VariableController())
819 817 << tr("Cannot updateVariableRequest for a unknow varRequestId") << varRequestId;
820 818 }
821 819 }
822 820
823 821 void VariableController::VariableControllerPrivate::cancelVariableRequest(QUuid varRequestId)
824 822 {
825 823 // cleaning varRequestId
826 824 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId);
827 825
828 826 for (auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.begin();
829 827 varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.end();) {
830 828 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
831 829 varRequestIdQueue.erase(
832 830 std::remove(varRequestIdQueue.begin(), varRequestIdQueue.end(), varRequestId),
833 831 varRequestIdQueue.end());
834 832 if (varRequestIdQueue.empty()) {
835 833 varIdToVarRequestIdQueueMapIt
836 834 = m_VarIdToVarRequestIdQueueMap.erase(varIdToVarRequestIdQueueMapIt);
837 835 }
838 836 else {
839 837 ++varIdToVarRequestIdQueueMapIt;
840 838 }
841 839 }
842 840 }
General Comments 0
You need to be logged in to leave comments. Login now