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