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